Skip to content

Commit

Permalink
fix: use default values for optional httpPayload if there is no body (#…
Browse files Browse the repository at this point in the history
…1511)

empty Json payload now will decode to the default value of the shape being decode. If no default is present, only then will it attempt to decode an empty json object
  • Loading branch information
ghostbuster91 authored Jun 19, 2024
1 parent 8cef834 commit 8e1ee57
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 6 deletions.
3 changes: 3 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ lazy val core = projectMatrix
}
.taskValue
},
scalacOptions ++= Seq(
"-Wconf:msg=value noInlineDocumentSupport in class ProtocolDefinition is deprecated:silent"
),
libraryDependencies += Dependencies.collectionsCompat.value,
Compile / packageSrc / mappings ++= {
val base = (Compile / sourceManaged).value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
],
"maven" : {
"dependencies" : [
"com.disneystreaming.alloy:alloy-core:0.3.8"
"com.disneystreaming.alloy:alloy-core:0.3.9"
],
"repositories" : [
{
Expand Down
2 changes: 1 addition & 1 deletion modules/core/src/smithy4s/http/HttpUnaryClientCodecs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ object HttpUnaryClientCodecs {

val mediaTypeWriters = new CachedSchemaCompiler.Uncached[HttpRequest.Writer[Blob, *]] {
def fromSchema[A](schema: Schema[A]): HttpRequest.Writer[Blob, A] = {
val maybeRawMediaType = HttpMediaType.fromSchema(schema).map(_.value)
val maybeRawMediaType = if (rawStringsAndBlobPayloads) HttpMediaType.fromSchema(schema).map(_.value) else None
maybeRawMediaType match {
case Some(mt) =>
new HttpRequest.Writer[Blob, A] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,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 =>
Expand Down
2 changes: 1 addition & 1 deletion project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ object Dependencies {

val Alloy = new {
val org = "com.disneystreaming.alloy"
val alloyVersion = "0.3.8"
val alloyVersion = "0.3.9"
val core = org % "alloy-core" % alloyVersion
val openapi = org %% "alloy-openapi" % alloyVersion
val protobuf = org % "alloy-protobuf" % alloyVersion
Expand Down

0 comments on commit 8e1ee57

Please sign in to comment.