Skip to content
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 @@ -101,7 +101,7 @@ private boolean isRecursive(Set<ShapeId> visited, Shape shape) {
return true;
}

return switch (shape.getType().getCategory()) {
var result = switch (shape.getType().getCategory()) {
case SIMPLE, SERVICE -> false;
case MEMBER -> isRecursive(visited, model.expectShape(shape.asMemberShape().orElseThrow().getTarget()));
case AGGREGATE -> {
Expand All @@ -113,6 +113,10 @@ private boolean isRecursive(Set<ShapeId> visited, Shape shape) {
yield false;
}
};

// "Pop" the current node off the "stack" -- other branches are still allowed to contain this shape.
visited.remove(shape.getId());
return result;
}

private Schema createNonRecursiveSchema(Shape shape) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ enum MyEnum {
foo: String
}

structure NestedStruct {
foo: SimpleStruct
bar: SimpleStruct
}

structure ParentStruct {
foo: NestedStruct
}

union SimpleUnion {
foo: String
}
Expand Down Expand Up @@ -134,9 +143,65 @@ static List<Arguments> convertsAggregateSchemasSource() {
Arguments.of(ShapeType.LIST, "SimpleList"),
Arguments.of(ShapeType.MAP, "SimpleMap"),
Arguments.of(ShapeType.STRUCTURE, "SimpleStruct"),
Arguments.of(ShapeType.STRUCTURE, "NestedStruct"),
Arguments.of(ShapeType.STRUCTURE, "ParentStruct"),
Arguments.of(ShapeType.UNION, "SimpleUnion"),
Arguments.of(ShapeType.LIST, "RecursiveList"),
Arguments.of(ShapeType.MAP, "RecursiveMap"),
Arguments.of(ShapeType.STRUCTURE, "RecursiveStructure"));
}

@MethodSource("detectsRecursiveSchemasSource")
@ParameterizedTest
public void detectsRecursiveSchemas(String name) {
var converter = new SchemaConverter(model);
var schema = converter.getSchema(model.expectShape(ShapeId.from("smithy.example#" + name)));

// Detecting that SchemaConverter handled this schema as recursive by checking classname is
// hacky, but public API doesn't expose a cleaner way, and probably shouldn't.
assertThat(schema.getClass().getSimpleName(), is("DeferredRootSchema"));
}

static List<Arguments> detectsRecursiveSchemasSource() {
return List.of(
Arguments.of("RecursiveList"),
Arguments.of("RecursiveMap"),
Arguments.of("RecursiveStructure"));
}

@MethodSource("detectsNonRecursiveSchemasSource")
@ParameterizedTest
public void detectsNonRecursiveSchemas(String name) {
var converter = new SchemaConverter(model);
var schema = converter.getSchema(model.expectShape(ShapeId.from("smithy.example#" + name)));

// Detecting that SchemaConverter handled this schema as non-recursive by checking classname is
// hacky, but public API doesn't expose a cleaner way, and probably shouldn't.
assertThat(schema.getClass().getSimpleName(), not("DeferredRootSchema"));
}

static List<Arguments> detectsNonRecursiveSchemasSource() {
return List.of(
Arguments.of("MyDocument"),
Arguments.of("MyString"),
Arguments.of("MyBoolean"),
Arguments.of("MyTimestamp"),
Arguments.of("MyBlob"),
Arguments.of("MyByte"),
Arguments.of("MyShort"),
Arguments.of("MyInteger"),
Arguments.of("MyLong"),
Arguments.of("MyFloat"),
Arguments.of("MyDouble"),
Arguments.of("MyBigInteger"),
Arguments.of("MyBigDecimal"),
Arguments.of("MyEnum"),
Arguments.of("MyIntEnum"),
Arguments.of("SimpleList"),
Arguments.of("SimpleMap"),
Arguments.of("SimpleStruct"),
Arguments.of("NestedStruct"),
Arguments.of("ParentStruct"),
Arguments.of("SimpleUnion"));
}
}
Loading