From 9aa8d82fff04e2e2e840b9043134182a548c7de5 Mon Sep 17 00:00:00 2001 From: Chase Coalwell Date: Mon, 5 Apr 2021 16:10:34 -0700 Subject: [PATCH 1/2] Support service renames --- .../build/transforms/FlattenNamespaces.java | 9 +++++ .../transforms/FlattenNamespacesTest.java | 27 +++++++++++++ .../flatten-namespaces-with-renames.json | 40 +++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 smithy-build/src/test/resources/software/amazon/smithy/build/transforms/flatten-namespaces-with-renames.json diff --git a/smithy-build/src/main/java/software/amazon/smithy/build/transforms/FlattenNamespaces.java b/smithy-build/src/main/java/software/amazon/smithy/build/transforms/FlattenNamespaces.java index 3e0f87a0ee3..a481328b528 100644 --- a/smithy-build/src/main/java/software/amazon/smithy/build/transforms/FlattenNamespaces.java +++ b/smithy-build/src/main/java/software/amazon/smithy/build/transforms/FlattenNamespaces.java @@ -164,9 +164,18 @@ private Map getRenamedShapesConnectedToService(Config config, return shapeWalker.walkShapes(service).stream() .filter(FunctionalUtils.not(Prelude::isPreludeShape)) .map(shape -> Pair.of(shape.getId(), updateNamespace(shape.getId(), config.getNamespace()))) + .map(pair -> applyServiceRenames(pair, service)) .collect(Collectors.toMap(Pair::getLeft, Pair::getRight)); } + private Pair applyServiceRenames(Pair pair, ServiceShape service) { + if (!service.getRename().containsKey(pair.getLeft())) { + return pair; + } + ShapeId newId = ShapeId.fromParts(pair.getRight().getNamespace(), service.getRename().get(pair.getLeft())); + return Pair.of(pair.getLeft(), newId); + } + private Set getTaggedShapesToInclude(Set tags, Model model) { return model.shapes() .filter(FunctionalUtils.not(Prelude::isPreludeShape)) diff --git a/smithy-build/src/test/java/software/amazon/smithy/build/transforms/FlattenNamespacesTest.java b/smithy-build/src/test/java/software/amazon/smithy/build/transforms/FlattenNamespacesTest.java index 5a9fe053f5b..681b83fda6b 100644 --- a/smithy-build/src/test/java/software/amazon/smithy/build/transforms/FlattenNamespacesTest.java +++ b/smithy-build/src/test/java/software/amazon/smithy/build/transforms/FlattenNamespacesTest.java @@ -116,6 +116,33 @@ public void doesNotIncludeAdditionalTaggedShapesWhenTheyConflict() throws Except "ns.baz#MyOperationOutput$foo", "ns.qux#UnconnectedFromService"))); } + @Test + public void supportsRenamesOnService() throws Exception { + Model model = Model.assembler() + .addImport(Paths.get(getClass().getResource("flatten-namespaces-with-renames.json").toURI())) + .assemble() + .unwrap(); + ObjectNode config = Node.objectNode() + .withMember("namespace", Node.from("ns.qux")) + .withMember("service", Node.from("ns.foo#MyService")); + TransformContext context = TransformContext.builder() + .model(model) + .settings(config) + .build(); + Model result = new FlattenNamespaces().transform(context); + List ids = result.shapes() + .filter(FunctionalUtils.not(Prelude::isPreludeShape)) + .map(Shape::getId) + .map(Object::toString) + .collect(Collectors.toList()); + + assertThat(ids, containsInAnyOrder("ns.qux#MyService", "ns.qux#GetSomething", "ns.qux#GetSomethingOutput", + "ns.qux#GetSomethingOutput$widget1", "ns.qux#GetSomethingOutput$fooWidget", "ns.qux#Widget", + "ns.qux#FooWidget")); + assertThat(ids, not(containsInAnyOrder("ns.foo#MyService", "ns.foo#GetSomething", "ns.foo#GetSomethingOutput", + "ns.bar#Widget", "foo.example#Widget"))); + } + @Test public void throwsWhenServiceIsNotConfigured() { Model model = Model.assembler() diff --git a/smithy-build/src/test/resources/software/amazon/smithy/build/transforms/flatten-namespaces-with-renames.json b/smithy-build/src/test/resources/software/amazon/smithy/build/transforms/flatten-namespaces-with-renames.json new file mode 100644 index 00000000000..9687a599c26 --- /dev/null +++ b/smithy-build/src/test/resources/software/amazon/smithy/build/transforms/flatten-namespaces-with-renames.json @@ -0,0 +1,40 @@ +{ + "smithy": "1.0", + "shapes": { + "ns.foo#MyService": { + "type": "service", + "version": "2017-02-11", + "operations": [ + { + "target": "ns.foo#GetSomething" + } + ], + "rename": { + "foo.example#Widget": "FooWidget" + } + }, + "ns.foo#GetSomething": { + "type": "operation", + "output": { + "target": "ns.foo#GetSomethingOutput" + } + }, + "ns.foo#GetSomethingOutput": { + "type": "structure", + "members": { + "widget1": { + "target": "ns.bar#Widget" + }, + "fooWidget": { + "target": "foo.example#Widget" + } + } + }, + "ns.bar#Widget": { + "type": "structure" + }, + "foo.example#Widget": { + "type": "structure" + } + } +} From 089bbeab0ec122ef7e2897b8c8415b2decd06b7d Mon Sep 17 00:00:00 2001 From: Chase Coalwell Date: Tue, 6 Apr 2021 07:12:53 -0700 Subject: [PATCH 2/2] Update flattenNamespaces transform docs --- .../source/1.0/guides/building-models/build-config.rst | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/docs/source/1.0/guides/building-models/build-config.rst b/docs/source/1.0/guides/building-models/build-config.rst index 0e9b9a814b2..9ed68cd9400 100644 --- a/docs/source/1.0/guides/building-models/build-config.rst +++ b/docs/source/1.0/guides/building-models/build-config.rst @@ -894,9 +894,13 @@ namespace. Shapes not connected to a service will not be flattened. separate :ref:`service closures `. The following example will flatten the namespaces of the shapes connected to -the ``ns.bar#MyService`` service into the target namespace, ``ns.foo``. Shapes -tagged with ``baz`` or ``qux`` will also be flattened into the ``ns.foo`` -namespace, so long as they don't conflict with a shape within the :ref:`service closure `. +the ``ns.bar#MyService`` service into the target namespace, ``ns.foo``. All +shapes within :ref:`service closure ` with be flattened into +the target namespace, including shapes that have been renamed to disambiguate +them through the service shape's ``rename`` property. Shapes tagged with +``baz`` or ``qux`` will also be flattened into the ``ns.foo`` namespace, so +long as they don't conflict with a shape within the +:ref:`service closure `. .. tabs::