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

Remove service renames after flattenNamespaces #2109

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 @@ -28,8 +28,8 @@
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.transform.ModelTransformer;
import software.amazon.smithy.utils.FunctionalUtils;
import software.amazon.smithy.utils.ListUtils;
import software.amazon.smithy.utils.Pair;

/**
Expand Down Expand Up @@ -120,9 +120,23 @@ protected Model transformWithConfig(TransformContext context, Config config) {
throw new SmithyBuildException(
"'namespace' and 'service' properties must be set on flattenNamespace transformer.");
}

Model model = context.getModel();
if (!model.getShape(config.getService()).isPresent()) {
throw new SmithyBuildException("Configured service, " + config.getService()
+ ", not found in model when performing flattenNamespaces transform.");
}

Map<ShapeId, ShapeId> shapesToRename = getRenamedShapes(config, model);
return ModelTransformer.create().renameShapes(model, shapesToRename);
model = context.getTransformer().renameShapes(model, shapesToRename);

// Remove service renames since they've now been applied, so consumers don't fail service shape validation
ShapeId updatedServiceId = shapesToRename.get(config.getService());
ServiceShape updatedService = model.expectShape(updatedServiceId, ServiceShape.class)
.toBuilder()
.clearRename()
.build();
return context.getTransformer().replaceShapes(model, ListUtils.of(updatedService));
}

@Override
Expand All @@ -131,11 +145,6 @@ public String getName() {
}

private Map<ShapeId, ShapeId> getRenamedShapes(Config config, Model model) {
if (!model.getShape(config.getService()).isPresent()) {
throw new SmithyBuildException("Configured service, " + config.getService()
+ ", not found in model when performing flattenNamespaces transform.");
}

Map<ShapeId, ShapeId> shapesToRename = getRenamedShapesConnectedToService(config, model);
Set<ShapeId> taggedShapesToInclude = getTaggedShapesToInclude(config.getIncludeTagged(), model);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
package software.amazon.smithy.build.transforms;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.anEmptyMap;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.not;

import java.nio.file.Paths;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand All @@ -31,7 +33,11 @@
import software.amazon.smithy.model.node.ExpectationNotMetException;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.node.ObjectNode;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.validation.ValidatedResult;
import software.amazon.smithy.model.validation.ValidationEvent;
import software.amazon.smithy.utils.FunctionalUtils;

public class FlattenNamespacesTest {
Expand Down Expand Up @@ -143,6 +149,52 @@ public void supportsRenamesOnService() throws Exception {
"ns.bar#Widget", "foo.example#Widget")));
}

@Test
public void removesServiceRenames() {
Model model = Model.assembler()
.addImport(getClass().getResource("flatten-namespaces-with-renames.json"))
.assemble()
.unwrap();
TransformContext context = TransformContext.builder()
.model(model)
.settings(Node.objectNode()
.withMember("namespace", "ns.qux")
.withMember("service", "ns.foo#MyService"))
.build();

Model result = new FlattenNamespaces().transform(context);
ShapeId transformedServiceId = ShapeId.from("ns.qux#MyService");

assertThat(result.getShape(transformedServiceId), not(Optional.empty()));
assertThat(result.expectShape(transformedServiceId, ServiceShape.class).getRename(), anEmptyMap());
}

@Test
public void serviceShapeIsValidAfterTransform() {
Model model = Model.assembler()
.addImport(getClass().getResource("flatten-namespaces-with-renames.json"))
.assemble()
.unwrap();
TransformContext context = TransformContext.builder()
.model(model)
.settings(Node.objectNode()
.withMember("namespace", "ns.qux")
.withMember("service", "ns.foo#MyService"))
.build();

Model result = new FlattenNamespaces().transform(context);
ValidatedResult<Model> validatedResult = Model.assembler()
.addModel(result)
.assemble();
List<ShapeId> validationEventShapeIds = validatedResult.getValidationEvents().stream()
.map(ValidationEvent::getShapeId)
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toList());

assertThat(validationEventShapeIds, not(containsInAnyOrder(ShapeId.from("ns.qux#MyService"))));
}

@Test
public void throwsWhenServiceIsNotConfigured() {
Model model = Model.assembler()
Expand Down