Skip to content

Commit

Permalink
Register full schema of array items for known types (#1574)
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Edgar <michael@xlate.io>
  • Loading branch information
MikeEdgar authored Sep 24, 2023
1 parent 899b3ce commit d3d36b9
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
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"
}
}
}
}

0 comments on commit d3d36b9

Please sign in to comment.