From a741ba65dea704b4bdd2ae3f0d301f03ff1bf18c Mon Sep 17 00:00:00 2001 From: David Piggott Date: Wed, 6 Mar 2024 15:24:32 +0000 Subject: [PATCH] Define JSON variant --- ...re.amazon.smithy.model.traits.TraitService | 3 +- .../smithy/unknownfieldretention.smithy | 10 +++- .../UnknownDocumentFieldRetentionTrait.java | 34 +++++++++++ ...va => UnknownJsonFieldRetentionTrait.java} | 10 ++-- .../resources/META-INF/smithy/traits.smithy | 6 +- ...umentFieldRetentionTraitProviderSpec.scala | 60 +++++++++++++++++++ ...JsonFieldRetentionTraitProviderSpec.scala} | 6 +- 7 files changed, 117 insertions(+), 12 deletions(-) create mode 100644 modules/core/src/alloy/UnknownDocumentFieldRetentionTrait.java rename modules/core/src/alloy/{UnknownFieldRetentionTrait.java => UnknownJsonFieldRetentionTrait.java} (75%) create mode 100644 modules/core/test/src/alloy/UnknownDocumentFieldRetentionTraitProviderSpec.scala rename modules/core/test/src/alloy/{UnknownFieldRetentionTraitProviderSpec.scala => UnknownJsonFieldRetentionTraitProviderSpec.scala} (88%) diff --git a/modules/core/resources/META-INF/services/software.amazon.smithy.model.traits.TraitService b/modules/core/resources/META-INF/services/software.amazon.smithy.model.traits.TraitService index 4de3feb..2ee8b10 100644 --- a/modules/core/resources/META-INF/services/software.amazon.smithy.model.traits.TraitService +++ b/modules/core/resources/META-INF/services/software.amazon.smithy.model.traits.TraitService @@ -23,6 +23,7 @@ alloy.StructurePatternTrait$Provider alloy.UrlFormFlattenedTrait$Provider alloy.UrlFormNameTrait$Provider alloy.UncheckedExamplesTrait$Provider -alloy.UnknownFieldRetentionTrait$Provider +alloy.UnknownDocumentFieldRetentionTrait$Provider +alloy.UnknownJsonFieldRetentionTrait$Provider alloy.UntaggedUnionTrait$Provider alloy.UuidFormatTrait$Provider diff --git a/modules/core/resources/META-INF/smithy/unknownfieldretention.smithy b/modules/core/resources/META-INF/smithy/unknownfieldretention.smithy index 0939658..c5ba1bf 100644 --- a/modules/core/resources/META-INF/smithy/unknownfieldretention.smithy +++ b/modules/core/resources/META-INF/smithy/unknownfieldretention.smithy @@ -3,7 +3,15 @@ $version: "2" namespace alloy /// Retain unknown fields of a containing structure in a map. +/// This variant controls the behaviour for document codecs. @trait( selector: "structure > member :test(> document)" ) -structure unknownFieldRetention {} +structure unknownDocumentFieldRetention {} + +/// Retain unknown fields of a containing structure in a map. +/// This variant controls the behaviour for JSON codecs. +@trait( + selector: "structure > member :test(> document)" +) +structure unknownJsonFieldRetention {} diff --git a/modules/core/src/alloy/UnknownDocumentFieldRetentionTrait.java b/modules/core/src/alloy/UnknownDocumentFieldRetentionTrait.java new file mode 100644 index 0000000..e6b169f --- /dev/null +++ b/modules/core/src/alloy/UnknownDocumentFieldRetentionTrait.java @@ -0,0 +1,34 @@ +/* Copyright 2022 Disney Streaming + * + * Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://disneystreaming.github.io/TOST-1.0.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package alloy; + +import software.amazon.smithy.model.node.Node; +import software.amazon.smithy.model.shapes.ShapeId; +import software.amazon.smithy.model.traits.AnnotationTrait; + +public final class UnknownDocumentFieldRetentionTrait extends AnnotationTrait { + public static ShapeId ID = ShapeId.from("alloy#unknownDocumentFieldRetention"); + + public UnknownDocumentFieldRetentionTrait() { + super(ID, Node.objectNode()); + } + + public static final class Provider extends AnnotationTrait.Provider { + public Provider() { + super(ID, (node) -> new UnknownDocumentFieldRetentionTrait()); + } + } +} diff --git a/modules/core/src/alloy/UnknownFieldRetentionTrait.java b/modules/core/src/alloy/UnknownJsonFieldRetentionTrait.java similarity index 75% rename from modules/core/src/alloy/UnknownFieldRetentionTrait.java rename to modules/core/src/alloy/UnknownJsonFieldRetentionTrait.java index 7f74335..33243db 100644 --- a/modules/core/src/alloy/UnknownFieldRetentionTrait.java +++ b/modules/core/src/alloy/UnknownJsonFieldRetentionTrait.java @@ -19,16 +19,16 @@ import software.amazon.smithy.model.shapes.ShapeId; import software.amazon.smithy.model.traits.AnnotationTrait; -public final class UnknownFieldRetentionTrait extends AnnotationTrait { - public static ShapeId ID = ShapeId.from("alloy#unknownFieldRetention"); +public final class UnknownJsonFieldRetentionTrait extends AnnotationTrait { + public static ShapeId ID = ShapeId.from("alloy#unknownJsonFieldRetention"); - public UnknownFieldRetentionTrait() { + public UnknownJsonFieldRetentionTrait() { super(ID, Node.objectNode()); } - public static final class Provider extends AnnotationTrait.Provider { + public static final class Provider extends AnnotationTrait.Provider { public Provider() { - super(ID, (node) -> new UnknownFieldRetentionTrait()); + super(ID, (node) -> new UnknownJsonFieldRetentionTrait()); } } } diff --git a/modules/core/test/resources/META-INF/smithy/traits.smithy b/modules/core/test/resources/META-INF/smithy/traits.smithy index c4e17e8..36577d5 100644 --- a/modules/core/test/resources/META-INF/smithy/traits.smithy +++ b/modules/core/test/resources/META-INF/smithy/traits.smithy @@ -11,7 +11,8 @@ use alloy#openEnum use alloy#simpleRestJson use alloy#structurePattern use alloy#uncheckedExamples -use alloy#unknownFieldRetention +use alloy#unknownDocumentFieldRetention +use alloy#unknownJsonFieldRetention use alloy#untagged use alloy#urlFormFlattened use alloy#urlFormName @@ -210,6 +211,7 @@ structure TestUrlFormName { structure TestUnknownFieldRetention { foo: String bar: String - @unknownFieldRetention + @unknownDocumentFieldRetention + @unknownJsonFieldRetention bazes: Document } diff --git a/modules/core/test/src/alloy/UnknownDocumentFieldRetentionTraitProviderSpec.scala b/modules/core/test/src/alloy/UnknownDocumentFieldRetentionTraitProviderSpec.scala new file mode 100644 index 0000000..76092d9 --- /dev/null +++ b/modules/core/test/src/alloy/UnknownDocumentFieldRetentionTraitProviderSpec.scala @@ -0,0 +1,60 @@ +/* Copyright 2022 Disney Streaming + * + * Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://disneystreaming.github.io/TOST-1.0.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package alloy + +import software.amazon.smithy.model.shapes.MemberShape +import software.amazon.smithy.model.shapes.ShapeId +import software.amazon.smithy.model.shapes.DocumentShape +import software.amazon.smithy.model.shapes.StructureShape +import software.amazon.smithy.model.Model +import java.util.Optional + +final class UnknownDocumentFieldRetentionTraitProviderSpec + extends munit.FunSuite { + + test("has trait") { + val documentShape = DocumentShape + .builder() + .id(ShapeId.fromParts("test", "MyDocument")) + .build() + val structId = ShapeId.fromParts("test", "MyStruct") + val targetId = structId.withMember("myMap") + val structShape = StructureShape + .builder() + .id(structId) + .addMember( + MemberShape + .builder() + .id(targetId) + .target(documentShape.getId) + .addTrait(new UnknownDocumentFieldRetentionTrait) + .build() + ) + .build() + + val model = + Model.assembler.disableValidation + .addShapes(structShape, documentShape) + .assemble() + .unwrap() + + val result = model + .getShape(targetId) + .map(shape => shape.hasTrait(classOf[UnknownDocumentFieldRetentionTrait])) + + assertEquals(result, Optional.of(true)) + } +} diff --git a/modules/core/test/src/alloy/UnknownFieldRetentionTraitProviderSpec.scala b/modules/core/test/src/alloy/UnknownJsonFieldRetentionTraitProviderSpec.scala similarity index 88% rename from modules/core/test/src/alloy/UnknownFieldRetentionTraitProviderSpec.scala rename to modules/core/test/src/alloy/UnknownJsonFieldRetentionTraitProviderSpec.scala index f460b70..eb6f303 100644 --- a/modules/core/test/src/alloy/UnknownFieldRetentionTraitProviderSpec.scala +++ b/modules/core/test/src/alloy/UnknownJsonFieldRetentionTraitProviderSpec.scala @@ -22,7 +22,7 @@ import software.amazon.smithy.model.shapes.StructureShape import software.amazon.smithy.model.Model import java.util.Optional -final class UnknownFieldRetentionTraitProviderSpec extends munit.FunSuite { +final class UnknownJsonFieldRetentionTraitProviderSpec extends munit.FunSuite { test("has trait") { val documentShape = DocumentShape @@ -39,7 +39,7 @@ final class UnknownFieldRetentionTraitProviderSpec extends munit.FunSuite { .builder() .id(targetId) .target(documentShape.getId) - .addTrait(new UnknownFieldRetentionTrait) + .addTrait(new UnknownJsonFieldRetentionTrait) .build() ) .build() @@ -52,7 +52,7 @@ final class UnknownFieldRetentionTraitProviderSpec extends munit.FunSuite { val result = model .getShape(targetId) - .map(shape => shape.hasTrait(classOf[UnknownFieldRetentionTrait])) + .map(shape => shape.hasTrait(classOf[UnknownJsonFieldRetentionTrait])) assertEquals(result, Optional.of(true)) }