-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add transform to mark required idempotency tokens client optional
- Loading branch information
Showing
8 changed files
with
147 additions
and
0 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
27 changes: 27 additions & 0 deletions
27
...ain/java/software/amazon/smithy/build/transforms/MarkIdempotencyTokensClientOptional.java
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,27 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.build.transforms; | ||
|
||
import software.amazon.smithy.build.ProjectionTransformer; | ||
import software.amazon.smithy.build.TransformContext; | ||
import software.amazon.smithy.model.Model; | ||
|
||
/** | ||
* {@code markIdempotencyTokensClientOptional} marks required idempotency token fields clientOptional. | ||
*/ | ||
public final class MarkIdempotencyTokensClientOptional implements ProjectionTransformer { | ||
|
||
@Override | ||
public String getName() { | ||
return "markIdempotencyTokensClientOptional"; | ||
} | ||
|
||
@Override | ||
public Model transform(TransformContext context) { | ||
Model model = context.getModel(); | ||
return context.getTransformer().markIdempotencyTokensClientOptional(model); | ||
} | ||
} |
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
25 changes: 25 additions & 0 deletions
25
.../main/java/software/amazon/smithy/model/transform/MakeIdempotencyTokenClientOptional.java
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,25 @@ | ||
package software.amazon.smithy.model.transform; | ||
|
||
|
||
import software.amazon.smithy.model.Model; | ||
import software.amazon.smithy.model.shapes.Shape; | ||
import software.amazon.smithy.model.traits.ClientOptionalTrait; | ||
import software.amazon.smithy.model.traits.IdempotencyTokenTrait; | ||
import software.amazon.smithy.model.traits.RequiredTrait; | ||
|
||
/** | ||
* Mark Idempotency token members as clientOptional so they can be injected if missing. | ||
*/ | ||
final class MakeIdempotencyTokenClientOptional { | ||
public static Model transform(Model model) { | ||
return ModelTransformer.create().mapShapes(model, shape -> { | ||
if (shape.isMemberShape() | ||
&& shape.hasTrait(RequiredTrait.class) | ||
&& shape.hasTrait(IdempotencyTokenTrait.class) | ||
&& !shape.hasTrait(ClientOptionalTrait.class)) { | ||
return Shape.shapeToBuilder(shape).addTrait(new ClientOptionalTrait()).build(); | ||
} | ||
return shape; | ||
}); | ||
} | ||
} |
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
31 changes: 31 additions & 0 deletions
31
...t/java/software/amazon/smithy/model/transform/MakeIdempotencyTokenClientOptionalTest.java
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,31 @@ | ||
package software.amazon.smithy.model.transform; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import org.junit.jupiter.api.Test; | ||
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.traits.IdempotencyTokenTrait; | ||
import software.amazon.smithy.model.traits.RequiredTrait; | ||
|
||
public class MakeIdempotencyTokenClientOptionalTest { | ||
private static final ShapeId operationInput = ShapeId.from("smithy.example#IdempotencyTokenRequiredInput"); | ||
|
||
@Test | ||
void compareTransform() { | ||
Model before = Model.assembler() | ||
.addImport(FlattenPaginationInfoTest.class.getResource("idempotency-token.smithy")) | ||
.assemble() | ||
.unwrap(); | ||
Model result = ModelTransformer.create().markIdempotencyTokensClientOptional(before); | ||
|
||
Shape input = result.expectShape(operationInput); | ||
Shape member = result.expectShape(input.getMember("token").get().getId()); | ||
|
||
assertTrue(member.hasTrait(ClientOptionalTrait.class)); | ||
assertTrue(member.hasTrait(RequiredTrait.class)); | ||
assertTrue(member.hasTrait(IdempotencyTokenTrait.class)); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...-model/src/test/resources/software/amazon/smithy/model/transform/idempotency-token.smithy
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,11 @@ | ||
$version: "2.0" | ||
|
||
namespace smithy.example | ||
|
||
operation IdempotencyTokenRequired { | ||
input := { | ||
@idempotencyToken | ||
@required | ||
token: String | ||
} | ||
} |