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

Correct array component schema scanning #781

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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