Skip to content

Commit

Permalink
Merge pull request #781 from MikeEdgar/779_fix_field_array_component_…
Browse files Browse the repository at this point in the history
…scan

Correct array component schema scanning
  • Loading branch information
phillip-kruger committed Apr 15, 2021
2 parents 6c6d4ae + af75b13 commit 00a232a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,17 @@ private Type readArrayType(ArrayType arrayType, Schema arraySchema) {
Schema itemSchema = new SchemaImpl();
arraySchema.type(Schema.SchemaType.ARRAY);

Type componentType = typeResolver.resolve(arrayType.component());

// Only use component (excludes the special name formatting for arrays).
TypeUtil.applyTypeAttributes(arrayType.component(), itemSchema);
TypeUtil.applyTypeAttributes(componentType, itemSchema);

// If it's not a terminal type, then push for later inspection.
if (!isTerminalType(arrayType.component()) && index.containsClass(arrayType)) {
pushToStack(arrayType, itemSchema);
if (!isTerminalType(componentType) && index.containsClass(componentType)) {
pushToStack(componentType, itemSchema);
}

itemSchema = SchemaRegistry.registerReference(arrayType.component(), typeResolver, itemSchema);
itemSchema = SchemaRegistry.registerReference(componentType, typeResolver, itemSchema);

while (arrayType.dimensions() > 1) {
Schema parentArrSchema = new SchemaImpl();
Expand Down Expand Up @@ -251,7 +253,7 @@ private Schema resolveParameterizedType(Type valueType, Schema schema, Schema pr

private Type resolveTypeVariable(Schema schema, Type fieldType, boolean pushToStack) {
// Type variable (e.g. A in Foo<A>)
Type resolvedType = typeResolver.getResolvedType(fieldType);
Type resolvedType = typeResolver.resolve(fieldType);
DataObjectLogging.logger.resolvedType(fieldType, resolvedType);

if (isTerminalType(resolvedType) || !index.containsClass(resolvedType)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ private boolean isExposedByDefault() {
* @param fieldType type to resolve
* @return resolved type (if found)
*/
public Type getResolvedType(Type fieldType) {
private Type getResolvedType(Type fieldType) {
Type current = TypeUtil.resolveWildcard(fieldType);

for (Map<String, Type> map : resolutionStack) {
Expand Down Expand Up @@ -373,7 +373,7 @@ private static Type[] resolveArguments(ParameterizedType type, UnaryOperator<Typ
* @param type type to resolve
* @return resolved type (if found)
*/
public Type getResolvedType(ParameterizedType type) {
private Type getResolvedType(ParameterizedType type) {
if (type.arguments().stream().noneMatch(arg -> arg.kind() == Type.Kind.WILDCARD_TYPE)) {
return ParameterizedType.create(type.name(), resolveArguments(type, this::resolve), null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,20 +405,30 @@ static class MultivaluedMap<K, V> extends HashMap<K, List<V>> {
*/
@Test
void testNestedCustomGenericSchemas() throws IOException, JSONException {
Index index = indexOf(Foo.class, Generic1.class, Generic2.class, CustomMap.class);
Index index = indexOf(Foo.class, Generic0.class, Generic1.class, Generic2.class, CustomMap.class);
OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index);
OpenAPI result = scanner.scan();
printToConsole(result);
assertJsonEquals("components.schemas.nested-custom-generics.json", result);
}

/*
* Do not annotate with @Schema - test relies on Generic0 registration
* only as an array component of Generic2#arrayOfGeneric0.
*/
static class Generic0<T> {
T value;
}

static class Generic1<T> {
T value;
}

static class Generic2<T> {
Generic1<T> nested;
CustomMap<T, T> nestedMap;
// Do not reference Generic0 other than from this field!
Generic0<T>[] arrayOfGeneric0;
}

@Schema
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,23 @@
}
}
},
"Generic0String" : {
"type" : "object",
"properties" : {
"value" : {
"type" : "string"
}
}
},
"Generic2String": {
"type": "object",
"properties": {
"arrayOfGeneric0" : {
"type" : "array",
"items" : {
"$ref" : "#/components/schemas/Generic0String"
}
},
"nested": {
"$ref": "#/components/schemas/Generic1String"
},
Expand Down

0 comments on commit 00a232a

Please sign in to comment.