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

Register full schema of array items for known types #1574

Merged
merged 1 commit into from
Sep 24, 2023
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 @@ -164,14 +164,17 @@ private Type readArrayType(ArrayType arrayType, Schema arraySchema) {
// Only use component (excludes the special name formatting for arrays).
TypeUtil.applyTypeAttributes(componentType, itemSchema);

// If it's not a terminal type, then push for later inspection.
if (!isTerminalType(componentType) && index.containsClass(componentType)) {
// If it's not a terminal type, then push for later inspection.
pushToStack(componentType, itemSchema);
itemSchema = context.getSchemaRegistry().registerReference(componentType, context.getJsonViews(), typeResolver,
itemSchema);
} else {
// Otherwise, allow registration since we may not encounter the array's element type again.
itemSchema = context.getSchemaRegistry().checkRegistration(componentType, context.getJsonViews(), typeResolver,
itemSchema);
}

itemSchema = context.getSchemaRegistry().registerReference(componentType, context.getJsonViews(), typeResolver,
itemSchema);

while (arrayType.dimensions() > 1) {
Schema parentArrSchema = new SchemaImpl();
parentArrSchema.setType(Schema.SchemaType.ARRAY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.IOException;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -738,4 +739,27 @@ class Bean {
printToConsole(result);
assertJsonEquals("components.schemas.iterator-stream-map-types.json", result);
}

/**
* Check that an array item that is considered "terminal" because it is a known type
* is registered in the schema registry and `#/components/schemas`.
*
* https://github.com/smallrye/smallrye-open-api/issues/1573
*/
@Test
void testZonedDateTimeArrayWrapper() throws IOException, JSONException {
@Schema(name = "ZonedDateTimeArrayWrapper")
class ZonedDateTimeArrayWrapper {
@SuppressWarnings("unused")
ZonedDateTime[] now;
}

Index index = indexOf(ZonedDateTimeArrayWrapper.class, ZonedDateTime.class);
OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index);

OpenAPI result = scanner.scan();

printToConsole(result);
assertJsonEquals("components.schemas.terminal-array-item-registration.json", result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"openapi": "3.0.3",
"components": {
"schemas": {
"ZonedDateTimeArrayWrapper": {
"type": "object",
"properties": {
"now": {
"type": "array",
"items": {
"$ref": "#/components/schemas/ZonedDateTime"
}
}
}
},
"ZonedDateTime": {
"format": "date-time",
"type": "string",
"example": "2022-03-10T12:15:50-04:00"
}
}
}
}