Skip to content

Commit ee1a8e2

Browse files
authored
Merge pull request #171 from disneystreaming/http4s-empty-body
http4s make body empty when metadata is total
2 parents 496778f + cc4d6fd commit ee1a8e2

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

modules/http4s/src/smithy4s/http4s/EntityCompiler.scala

+15-7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import org.http4s._
2525
import org.http4s.headers.`Content-Type`
2626
import smithy4s.http.BodyPartial
2727
import smithy4s.http.CodecAPI
28+
import smithy4s.http.Metadata
2829

2930
trait EntityCompiler[F[_]] {
3031

@@ -67,13 +68,20 @@ object EntityCompiler {
6768
def compileEntityEncoder[A](schema: Schema[A]): EntityEncoder[F, A] = {
6869
val codecA: codecAPI.Codec[A] = codecAPI.compileCodec(schema)
6970
val mediaType = MediaType.unsafeParse(codecAPI.mediaType(codecA).value)
70-
71-
EntityEncoder
72-
.byteArrayEncoder[F]
73-
.withContentType(
74-
`Content-Type`(mediaType)
75-
)
76-
.contramap[A]((a: A) => codecAPI.writeToArray(codecA, a))
71+
val expectBody = Metadata.PartialDecoder
72+
.fromSchema(schema)
73+
.total
74+
.isEmpty // expect body if metadata decoder is not total
75+
if (expectBody) {
76+
EntityEncoder
77+
.byteArrayEncoder[F]
78+
.withContentType(
79+
`Content-Type`(mediaType)
80+
)
81+
.contramap[A]((a: A) => codecAPI.writeToArray(codecA, a))
82+
} else {
83+
EntityEncoder.emptyEncoder
84+
}
7785
}
7886

7987
def compileEntityDecoder[A](

modules/tests/src/smithy4s/tests/PizzaSpec.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,10 @@ abstract class PizzaSpec
324324
log
325325
)
326326
} yield {
327-
val (code, headers, _) = res
327+
val (code, headers, body) = res
328328

329329
expect(code == 200) &&
330+
expect(body.isEmpty()) &&
330331
expect(headers.get("x-uppercase-header") == Some(List("header-1"))) &&
331332
expect(headers.get("x-capitalized-header") == Some(List("header-2"))) &&
332333
expect(headers.get("x-lowercase-header") == Some(List("header-3"))) &&

0 commit comments

Comments
 (0)