Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump smithy to 1.49.0 #1485

Merged
merged 27 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# 0.18.16

* Makes the json decoding of tagged-unions lenient for AWS
* Add support for converting smithy4s services and schemas to smithy models
* Add `smithy4s.meta#only` annotation allowing to filter operations in
* Add `smithy4s.meta#only` annotation allowing to filter operations in
services, intended to reduce the amount of code generated from AWS specs

# 0.18.15
Expand Down
5 changes: 4 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,10 @@ lazy val core = projectMatrix
.map(f => (f, f.relativeTo(base)))
// this excludes modules/core/src/generated/PartiallyAppliedStruct.scala
.collect { case (f, Some(relF)) => f -> relF.getPath() }
}
},
scalacOptions ++= Seq(
"-Wconf:msg=value noInlineDocumentSupport in class ProtocolDefinition is deprecated:silent"
)
)
.jvmPlatform(allJvmScalaVersions, jvmDimSettings)
.jsPlatform(allJsScalaVersions, jsDimSettings)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ private[aws] object AwsJsonCodecs {
.withFlexibleCollectionsSupport(true)
.withHintMask(hintMask)
.withMaxArity(Int.MaxValue)
.withLenientTaggedUnionDecoding
)

private[aws] val jsonDecoders = jsonPayloadCodecs.decoders
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ private[aws] object AwsRestJsonCodecs {
.withFlexibleCollectionsSupport(true)
.withHintMask(hintMask)
.withMaxArity(Int.MaxValue)
.withLenientTaggedUnionDecoding
)

def nullToEmptyObject(blob: Blob): Blob =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,12 @@ object AwsComplianceSuite extends ProtocolComplianceSuite {
"QueryIdempotencyTokenAutoFill",

// TODO https://github.com/disneystreaming/smithy4s/issues/1424
"AwsJson10DeserializeIgnoreType",
"AwsJson10ClientPopulatesDefaultValuesInInput",
"AwsJson10ClientPopulatesDefaultsValuesWhenMissingInResponse",
"AwsJson10ClientPopulatesNestedDefaultValuesWhenMissing",
"AwsJson10ClientPopulatesNestedDefaultsWhenMissingInResponseBody",
"AwsJson10ClientErrorCorrectsWhenServerFailsToSerializeRequiredValues",
"AwsJson11DeserializeIgnoreType",
"RestJsonHttpPayloadWithUnsetUnion",
"RestJsonDeserializeIgnoreType",
"RestXmlHttpPayloadWithUnsetUnion"
)
(complianceTest: ComplianceTest[IO]) =>
Expand Down
6 changes: 6 additions & 0 deletions modules/json/src/smithy4s/json/JsoniterCodecCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ trait JsoniterCodecCompiler extends CachedSchemaCompiler[JsonCodec] {
*/
def withHintMask(hintMask: HintMask): JsoniterCodecCompiler

/**
* Enables lenient decoding of tagged unions, where unset alternatives are encoded as null
* values in the json payload. Also ignores unrecognised union keys.
*/
def withLenientTaggedUnionDecoding: JsoniterCodecCompiler

}

object JsoniterCodecCompiler {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ private[smithy4s] case class JsoniterCodecCompilerImpl(
flexibleCollectionsSupport: Boolean,
infinitySupport: Boolean,
preserveMapOrder: Boolean,
hintMask: Option[HintMask]
hintMask: Option[HintMask],
lenientTaggedUnionDecoding: Boolean
) extends CachedSchemaCompiler.Impl[JCodec]
with JsoniterCodecCompiler {

Expand Down Expand Up @@ -55,13 +56,17 @@ private[smithy4s] case class JsoniterCodecCompilerImpl(
): JsoniterCodecCompiler =
copy(preserveMapOrder = preserveMapOrder)

def withLenientTaggedUnionDecoding: JsoniterCodecCompiler =
copy(lenientTaggedUnionDecoding = true)

def fromSchema[A](schema: Schema[A], cache: Cache): JCodec[A] = {
val visitor = new SchemaVisitorJCodec(
maxArity,
explicitDefaultsEncoding,
infinitySupport,
flexibleCollectionsSupport,
preserveMapOrder,
lenientTaggedUnionDecoding,
cache
)
val amendedSchema =
Expand All @@ -82,6 +87,7 @@ private[smithy4s] object JsoniterCodecCompilerImpl {
infinitySupport = false,
flexibleCollectionsSupport = false,
preserveMapOrder = false,
lenientTaggedUnionDecoding = false,
hintMask = Some(JsoniterCodecCompiler.defaultHintMask)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ private[smithy4s] class SchemaVisitorJCodec(
infinitySupport: Boolean,
flexibleCollectionsSupport: Boolean,
preserveMapOrder: Boolean,
lenientTaggedUnionDecoding: Boolean,
val cache: CompilationCache[JCodec]
) extends SchemaVisitor.Cached[JCodec] { self =>
private val emptyMetadata: MMap[String, Any] = MMap.empty
Expand Down Expand Up @@ -1001,6 +1002,90 @@ private[smithy4s] class SchemaVisitorJCodec(
out.encodeError("Cannot use coproducts as keys")
}

private def lenientTaggedUnion[U](
alternatives: Vector[Alt[U, _]]
)(dispatch: Alt.Dispatcher[U]): JCodec[U] =
new JCodec[U] {
val expecting: String = "tagged-union"

override def canBeKey: Boolean = false

def jsonLabel[A](alt: Alt[U, A]): String =
alt.hints.get(JsonName) match {
case None => alt.label
case Some(x) => x.value
}

private[this] val handlerMap =
new util.HashMap[String, (Cursor, JsonReader) => U] {
def handler[A](alt: Alt[U, A]) = {
val codec = apply(alt.schema)
(cursor: Cursor, reader: JsonReader) =>
alt.inject(cursor.decode(codec, reader))
}

alternatives.foreach(alt => put(jsonLabel(alt), handler(alt)))
}

def decodeValue(cursor: Cursor, in: JsonReader): U = {
var result: U = null.asInstanceOf[U]
if (in.isNextToken('{')) {
// In this case, metadata and payload are mixed together
// and values field values must be sought from either.
if (!in.isNextToken('}')) {
in.rollbackToken()
while ({
val key = in.readKeyAsString()
val handler = handlerMap.get(key)
if (handler eq null) in.skip()
else if (in.isNextToken('n')) {
in.readNullOrError((), "expected null")
} else {
in.rollbackToken()
if (result != null) {
in.decodeError("Expected a single non-null value")
} else {
result = handler(cursor, in)
}
}
in.isNextToken(',')
}) ()
if (!in.isCurrentToken('}')) in.objectEndOrCommaError()
}
if (result != null) {
result
} else {
in.decodeError("Expected a single non-null value")
}
} else in.decodeError("Expected JSON object")
}
val precompiler = new smithy4s.schema.Alt.Precompiler[Writer] {
def apply[A](label: String, instance: Schema[A]): Writer[A] = {
val jsonLabel =
instance.hints.get(JsonName).map(_.value).getOrElse(label)
val jcodecA = instance.compile(self)
a =>
out => {
out.writeObjectStart()
out.writeKey(jsonLabel)
jcodecA.encodeValue(a, out)
out.writeObjectEnd()
}
}
}
val writer = dispatch.compile(precompiler)

def encodeValue(u: U, out: JsonWriter): Unit = {
writer(u)(out)
}

def decodeKey(in: JsonReader): U =
in.decodeError("Cannot use coproducts as keys")

def encodeKey(u: U, out: JsonWriter): Unit =
out.encodeError("Cannot use coproducts as keys")
}

private def untaggedUnion[U](
alternatives: Vector[Alt[U, _]]
)(dispatch: Alt.Dispatcher[U]): JCodec[U] = new JCodec[U] {
Expand Down Expand Up @@ -1139,7 +1224,9 @@ private[smithy4s] class SchemaVisitorJCodec(
): JCodec[U] = hints match {
case Untagged.hint(_) => untaggedUnion(alternatives)(dispatch)
case Discriminated.hint(d) => discriminatedUnion(alternatives, d)(dispatch)
case _ => taggedUnion(alternatives)(dispatch)
case _ =>
if (lenientTaggedUnionDecoding) lenientTaggedUnion(alternatives)(dispatch)
else taggedUnion(alternatives)(dispatch)
}

override def enumeration[E](
Expand Down
24 changes: 24 additions & 0 deletions modules/json/test/src/smithy4s/json/SchemaVisitorJCodecTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import smithy4s.schema.Schema._

import scala.collection.immutable.ListMap
import scala.util.Try
import smithy4s.json.internals.JsoniterCodecCompilerImpl
import smithy4s.schema.Schema

class SchemaVisitorJCodecTests() extends FunSuite {

Expand Down Expand Up @@ -317,6 +319,28 @@ class SchemaVisitorJCodecTests() extends FunSuite {
}
}

test("Lenient union") {
val json = """|{
| "right" : "foo",
| "left" : null
|}
|""".stripMargin

val json2 = """|{
| "right": null,
| "left" : 1
|}
|""".stripMargin
val schema = Schema.either(Schema.int, Schema.string)

implicit val codec: JsonCodec[Either[Int, String]] =
JsoniterCodecCompilerImpl.defaultJsoniterCodecCompiler.withLenientTaggedUnionDecoding
.fromSchema(schema)

expect.same(readFromString[Either[Int, String]](json), Right("foo"))
expect.same(readFromString[Either[Int, String]](json2), Left(1))
}

test("Untagged union are encoded / decoded") {
val oneJ = """ {"three":"three_value"}"""
val twoJ = """ {"four":4}"""
Expand Down
2 changes: 1 addition & 1 deletion project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ object Dependencies {

val Smithy = new {
val org = "software.amazon.smithy"
val smithyVersion = "1.45.0"
val smithyVersion = "1.47.0"
val model = org % "smithy-model" % smithyVersion
val testTraits = org % "smithy-protocol-test-traits" % smithyVersion
val build = org % "smithy-build" % smithyVersion
Expand Down
4 changes: 2 additions & 2 deletions smithy-build.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"imports": ["./sampleSpecs", "./modules/protocol/resources"],
"maven": {
"dependencies": [
"software.amazon.smithy:smithy-protocol-test-traits:1.45.0",
"com.disneystreaming.alloy:alloy-core:0.3.2"
"software.amazon.smithy:smithy-protocol-test-traits:1.47.0",
"com.disneystreaming.alloy:alloy-core:0.3.6"
]
}
}
Loading