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

Fixes AWS AwsStandardTypesTransformer bug #1129

Merged
merged 1 commit into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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