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

Scope conflict detection more narrowly in JSONSchema conversion #978

Merged
merged 1 commit into from
Nov 12, 2021
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 @@ -189,6 +189,12 @@
"smithy.api#httpQuery": "version",
"smithy.api#required": {}
}
},
"connectionType": {
"target": "smithy.example#ConnectionType",
"traits": {
"smithy.api#httpHeader": "x-amz-connection-type"
}
}
}
},
Expand All @@ -202,6 +208,21 @@
}
}
}
},
"smithy.example#ConnectionType": {
"type": "string",
"traits": {
"smithy.api#enum": [
{
"value": "a",
"name": "A"
},
{
"value": "c",
"name": "C"
}
]
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
"nullable": true
},
"required": true
},
{
"name": "x-amz-connection-type",
"in": "header",
"schema": {
"$ref": "#/components/schemas/ConnectionType"
}
}
],
"responses": {
Expand Down Expand Up @@ -128,6 +135,13 @@
"nullable": true
},
"required": true
},
{
"name": "x-amz-connection-type",
"in": "header",
"schema": {
"$ref": "#/components/schemas/ConnectionType"
}
}
],
"responses": {
Expand Down Expand Up @@ -201,5 +215,15 @@
}
}
},
"components": { }
"components": {
"schemas": {
"ConnectionType": {
"type": "string",
"enum": [
"a",
"c"
]
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ public CfnConfig() {
// https://github.com/aws-cloudformation/cloudformation-cli/blob/master/src/rpdk/core/data/schema/provider.definition.schema.v1.json#L210
// https://github.com/aws-cloudformation/cloudformation-cli/blob/master/src/rpdk/core/data/schema/provider.definition.schema.v1.json#L166
super.setUnionStrategy(UnionStrategy.ONE_OF);

// @cfnResource's additionalSchemas property references shapes that aren't in the service closure
// so conversions must be able to reference those shapes
super.setEnableOutOfServiceReferences(true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public String toString() {
private final NodeMapper nodeMapper = new NodeMapper();
private ShapeId service;
private boolean supportNonNumericFloats = false;
private boolean enableOutOfServiceReferences = false;

public JsonSchemaConfig() {
nodeMapper.setWhenMissingSetter(NodeMapper.WhenMissing.INGORE);
Expand Down Expand Up @@ -366,4 +367,21 @@ public boolean getSupportNonNumericFloats() {
public void setSupportNonNumericFloats(boolean supportNonNumericFloats) {
this.supportNonNumericFloats = supportNonNumericFloats;
}

public boolean isEnableOutOfServiceReferences() {
return enableOutOfServiceReferences;
}

/**
* Set to true to enable references to shapes outside the service closure.
*
* Setting this to true means that all the shapes in the model must not conflict, whereas
* leaving it at the default, false, means that only the shapes connected to the configured
* service via {@link #setService(ShapeId)} must not conflict.
*
* @param enableOutOfServiceReferences true if out-of-service references should be allowed. default: false
*/
public void setEnableOutOfServiceReferences(boolean enableOutOfServiceReferences) {
this.enableOutOfServiceReferences = enableOutOfServiceReferences;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ private JsonSchemaConverter(Builder builder) {
}

LOGGER.fine("Creating JSON ref strategy");
refStrategy = RefStrategy.createDefaultStrategy(model, config, propertyNamingStrategy);
Model refModel = config.isEnableOutOfServiceReferences()
? this.model : scopeModelToService(model, config.getService());
refStrategy = RefStrategy.createDefaultStrategy(refModel, config, propertyNamingStrategy);

// Combine custom mappers with the discovered mappers and sort them.
realizedMappers = new ArrayList<>(mappers);
Expand All @@ -92,7 +94,7 @@ private JsonSchemaConverter(Builder builder) {
.map(Object::getClass)
.map(Class::getCanonicalName)
.collect(Collectors.joining(", ")));
visitor = new JsonSchemaShapeVisitor(model, this, realizedMappers);
visitor = new JsonSchemaShapeVisitor(this.model, this, realizedMappers);

// Compute the number of segments in the root definition section.
rootDefinitionPointer = config.getDefinitionPointer();
Expand Down Expand Up @@ -126,6 +128,14 @@ private static Model createUpdatedModel(
return model;
}

private static Model scopeModelToService(Model model, ShapeId serviceId) {
if (serviceId == null) {
return model;
}
Set<Shape> connected = new Walker(model).walkShapes(model.expectShape(serviceId));
return ModelTransformer.create().filterShapes(model, connected::contains);
}

private static int countSegments(String pointer) {
int totalSegments = 0;
for (int i = 0; i < pointer.length(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.not;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.math.BigDecimal;
Expand Down Expand Up @@ -113,6 +114,39 @@ public void canConvertShapesThatAreOnlyInTheClosureOfShape() {
assertThat(document2.getDefinitions().keySet(), containsInAnyOrder("#/definitions/ReferencedB"));
}

@Test
public void canConvertShapesThatHaveConflictsOutsideOfClosure() {
Model model = Model.assembler()
.addImport(getClass().getResource("multiple-closures-with-conflicting-shapes.json"))
.assemble()
.unwrap();
JsonSchemaConfig config1 = new JsonSchemaConfig();
config1.setService(ShapeId.from("com.foo#ServiceA"));
SchemaDocument document1 = JsonSchemaConverter.builder()
.model(model)
.rootShape(ShapeId.from("com.foo#ServiceA"))
.config(config1)
.build()
.convert();

JsonSchemaConfig config2 = new JsonSchemaConfig();
config2.setService(ShapeId.from("com.bar#ServiceB"));
SchemaDocument document2 = JsonSchemaConverter.builder()
.model(model)
.rootShape(ShapeId.from("com.bar#ServiceB"))
.config(config2)
.build()
.convert();

Schema string1Def = document1.getDefinitions().get("#/definitions/ConflictString");
assertNotNull(string1Def);
assertThat(string1Def.getEnumValues().get(), containsInAnyOrder("y", "z"));

Schema string2Def = document2.getDefinitions().get("#/definitions/ConflictString");
assertNotNull(string2Def);
assertThat(string2Def.getEnumValues().get(), containsInAnyOrder("a", "b"));
}

@Test
public void canFilterShapesWithCustomPredicate() {
Predicate<Shape> predicate = shape -> !shape.getId().getName().equals("Foo");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{
"smithy": "1.0",
"shapes": {
"com.foo#ServiceA": {
"type": "service",
"version": "2006-03-01",
"operations": [
{
"target": "com.foo#OperationA"
}
]
},
"com.foo#OperationA": {
"type": "operation",
"input": {
"target": "com.foo#StructureA"
}
},
"com.bar#ServiceB": {
"type": "service",
"version": "2006-03-01",
"operations": [
{
"target": "com.bar#OperationB"
}
]
},
"com.bar#OperationB": {
"type": "operation",
"input": {
"target": "com.bar#StructureB"
}
},
"com.foo#StructureA": {
"type": "structure",
"members": {
"a": {
"target": "com.foo#ConflictString"
}
}
},
"com.foo#ConflictString": {
"type": "string",
"traits": {
"smithy.api#enum": [
{
"value": "y",
"name": "Y"
},
{
"value": "z",
"name": "Z"
}
]
}
},
"com.bar#StructureB": {
"type": "structure",
"members": {
"a": {
"target": "com.bar#ConflictString"
}
}
},
"com.bar#ConflictString": {
"type": "string",
"traits": {
"smithy.api#enum": [
{
"value": "a",
"name": "A"
},
{
"value": "b",
"name": "B"
}
]
}
}
}
}