-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(codegen): improve nullability of generated types (#1059)
- Loading branch information
Showing
12 changed files
with
101 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"id": "5c47545c-ec59-4078-b795-22ba5de23919", | ||
"type": "feature", | ||
"description": "**BREAKING**: Update codegen to improve nullability of generated types." | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 0 additions & 102 deletions
102
...hy-aws-kotlin-codegen/src/main/kotlin/aws/sdk/kotlin/codegen/customization/BoxServices.kt
This file was deleted.
Oops, something went wrong.
35 changes: 35 additions & 0 deletions
35
...gen/src/main/kotlin/aws/sdk/kotlin/codegen/customization/ec2/EC2MakePrimitivesOptional.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package aws.sdk.kotlin.codegen.customization.ec2 | ||
import software.amazon.smithy.kotlin.codegen.KotlinSettings | ||
import software.amazon.smithy.kotlin.codegen.integration.KotlinIntegration | ||
import software.amazon.smithy.model.Model | ||
import software.amazon.smithy.model.shapes.Shape | ||
import software.amazon.smithy.model.shapes.ShapeId | ||
import software.amazon.smithy.model.traits.ClientOptionalTrait | ||
import software.amazon.smithy.model.transform.ModelTransformer | ||
|
||
/** | ||
* EC2 incorrectly models primitive shapes as unboxed when they actually | ||
* need to be boxed for the API to work properly (e.g. sending default values). | ||
* This integration pre-processes the model to make all members optional. | ||
* | ||
* See: https://github.com/awslabs/aws-sdk-kotlin/issues/261 | ||
*/ | ||
class EC2MakePrimitivesOptional : KotlinIntegration { | ||
override val order: Byte = -127 | ||
override fun enabledForService(model: Model, settings: KotlinSettings): Boolean = | ||
settings.service == ShapeId.from("com.amazonaws.ec2#AmazonEC2") | ||
|
||
override fun preprocessModel(model: Model, settings: KotlinSettings): Model { | ||
val updates = mutableListOf<Shape>() | ||
for (struct in model.structureShapes) { | ||
for (member in struct.allMembers.values) { | ||
updates.add(member.toBuilder().addTrait(ClientOptionalTrait()).build()) | ||
} | ||
} | ||
return ModelTransformer.create().replaceShapes(model, updates) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 0 additions & 60 deletions
60
...ws-kotlin-codegen/src/test/kotlin/aws/sdk/kotlin/codegen/customization/BoxServicesTest.kt
This file was deleted.
Oops, something went wrong.
51 changes: 51 additions & 0 deletions
51
...src/test/kotlin/aws/sdk/kotlin/codegen/customization/ec2/EC2MakePrimitivesOptionalTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package aws.sdk.kotlin.codegen.customization.ec2 | ||
|
||
import org.junit.jupiter.api.Test | ||
import software.amazon.smithy.kotlin.codegen.model.expectShape | ||
import software.amazon.smithy.kotlin.codegen.model.isNullable | ||
import software.amazon.smithy.kotlin.codegen.test.newTestContext | ||
import software.amazon.smithy.kotlin.codegen.test.prependNamespaceAndService | ||
import software.amazon.smithy.kotlin.codegen.test.toSmithyModel | ||
import software.amazon.smithy.model.shapes.StructureShape | ||
import kotlin.test.assertTrue | ||
|
||
class EC2MakePrimitivesOptionalTest { | ||
@Test | ||
fun testNullability() { | ||
val model = """ | ||
operation Foo { | ||
input: FooInput | ||
} | ||
structure FooInput { | ||
@required | ||
@default(0) | ||
int: PrimitiveInteger, | ||
@default(false) | ||
bool: PrimitiveBoolean, | ||
other: Other, | ||
@default(1) | ||
defaultInt: Integer | ||
@required | ||
requiredString: String | ||
} | ||
structure Other {} | ||
""".prependNamespaceAndService(version = "2", operations = listOf("Foo")).toSmithyModel() | ||
|
||
val ctx = model.newTestContext() | ||
val transformed = EC2MakePrimitivesOptional().preprocessModel(model, ctx.generationCtx.settings) | ||
|
||
// get the synthetic input which is the one that will be transformed | ||
val struct = transformed.expectShape<StructureShape>("smithy.kotlin.synthetic.test#FooRequest") | ||
struct.members().forEach { | ||
val memberSymbol = ctx.generationCtx.symbolProvider.toSymbol(it) | ||
assertTrue(memberSymbol.isNullable) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters