Closed kubukoz closed 3 months ago
I think this is the part responsible for the {}
in the hexdump:
Update: a trivial "fix" proves that:
diff --git a/modules/json/src/smithy4s/json/internals/JsonPayloadCodecCompilerImpl.scala b/modules/json/src/smithy4s/json/internals/JsonPayloadCodecCompilerImpl.scala
index c02450ed8..b9b6c2f9b 100644
--- a/modules/json/src/smithy4s/json/internals/JsonPayloadCodecCompilerImpl.scala
+++ b/modules/json/src/smithy4s/json/internals/JsonPayloadCodecCompilerImpl.scala
@@ -71,19 +71,22 @@ private[json] case class JsonPayloadCodecCompilerImpl(
def fromSchema[A](schema: Schema[A], cache: Cache): PayloadDecoder[A] = {
val jcodec = jsoniterCodecCompiler.fromSchema(schema, cache)
- new JsonPayloadDecoder(jcodec)
+ new JsonPayloadDecoder(jcodec, schema.getDefaultValue)
}
def fromSchema[A](schema: Schema[A]): PayloadDecoder[A] =
fromSchema(schema, createCache())
}
- private class JsonPayloadDecoder[A](jcodec: JsonCodec[A])
+ private class JsonPayloadDecoder[A](jcodec: JsonCodec[A], default: Option[A])
extends PayloadDecoder[A] {
def decode(blob: Blob): Either[PayloadError, A] = {
try {
Right {
- if (blob.isEmpty) readFromString("{}", jsoniterReaderConfig)(jcodec)
+ if (blob.isEmpty)
+ default.getOrElse(
+ readFromString("{}", jsoniterReaderConfig)(jcodec)
+ )
else
blob match {
case b: Blob.ArraySliceBlob =>
so the question remains: is this something we should support?
Why not. PR away
@kubukoz I will take a look at it. Can you assign me?
This may be controversial...
I think
@default
should be used on payloads, i.e. if the HTTP request/response body is empty, we should attempt to decode it from something likedefaultValue.orElse(emptyObject)
.It appears that we're currently only trying to decode from an empty body:
(this is from a reproduction posted below)
To reproduce:
Full repro: https://github.com/kubukoz/demos/tree/smithy4s-required-payloads