Skip to content

Commit

Permalink
Merge pull request #1129 from disneystreaming/fix-aws-transformation-…
Browse files Browse the repository at this point in the history
…again

Fixes AWS AwsStandardTypesTransformer bug
  • Loading branch information
daddykotex authored Jul 28, 2023
2 parents adc23f6 + 0f4486d commit 727dd80
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,20 @@ class AwsStandardTypesTransformer extends ProjectionTransformer {
.map[Boolean](canReplace)
.orElse(false)
}
.map[Shape](shape => {
.map[Shape] { shape =>
val target = model.expectShape(shape.getTarget)
val replacementShape = replaceWith(shape.getTarget)
// Member shapes can only carry the `@default` trait IF
// their container is a structures
val canCarryDefault =
model.expectShape(shape.getContainer()).isStructureShape()

shape
.toBuilder()
.target(replacementShape)
.copyDefaultTrait(target)
.when(canCarryDefault)(_.copyDefaultTrait(target))
.build()
})
}
.orElse(shape)
}

Expand Down Expand Up @@ -127,6 +131,11 @@ object AwsStandardTypesTransformer {
private[transformers] final implicit class MemberShapeBuilderOps(
val builder: MemberShape.Builder
) extends AnyVal {
def when(condition: Boolean)(
mutation: MemberShape.Builder => MemberShape.Builder
): MemberShape.Builder =
if (condition)(mutation(builder)) else builder

def copyDefaultTrait(shape: Shape): MemberShape.Builder = {
val defaultTraitOpt = toOption(shape.getTrait(classOf[DefaultTrait]))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,62 @@ final class AwsStandardTypesTransformerSpec extends munit.FunSuite {
)
}

test("Doesn't keep default traits on Lists") {
val kinesisNamespace =
"""|$version: "2"
|
|namespace com.amazonaws.kinesis
|
|@default(5)
|integer Integer
|""".stripMargin

val testNamespace =
"""
|$version: "2"
|
|namespace test
|
|list TestList {
| member: com.amazonaws.kinesis#Integer
|}
|""".stripMargin

val rawModel = loadModel(kinesisNamespace, testNamespace)

val transformer = new AwsStandardTypesTransformer()
val transformerContext =
TransformContext
.builder()
.model(rawModel)
.build()

val transformedModel = transformer.transform(transformerContext)

val listCode =
generateScalaCode(transformedModel)("test.TestList")

assertEquals(
listCode,
"""package test
|
|import smithy4s.Hints
|import smithy4s.Newtype
|import smithy4s.Schema
|import smithy4s.ShapeId
|import smithy4s.schema.Schema.bijection
|import smithy4s.schema.Schema.int
|import smithy4s.schema.Schema.list
|
|object TestList extends Newtype[List[Int]] {
| val id: ShapeId = ShapeId("test", "TestList")
| val hints: Hints = Hints.empty
| val underlyingSchema: Schema[List[Int]] = list(int).withId(id).addHints(hints)
| implicit val schema: Schema[TestList] = bijection(underlyingSchema, asBijection)
|}""".stripMargin
)
}

test("Removes AWS new types after flattening") {
val kinesisNamespace =
"""|namespace com.amazonaws.kinesis
Expand Down

0 comments on commit 727dd80

Please sign in to comment.