Skip to content

Commit fcf1a8a

Browse files
committed
fix: use array item schema ref when available instead of inline schema
Signed-off-by: Michael Edgar <[email protected]>
1 parent a2ada46 commit fcf1a8a

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

core/src/main/java/io/smallrye/openapi/runtime/io/schema/SchemaFactory.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ public static Schema includeTypeSchema(AnnotationScannerContext context, Schema
340340
if (schema.getType() != null && schema.getType().contains(Schema.SchemaType.ARRAY) && implSchema != null) {
341341
// If the @Schema annotation indicates an array type, then use the Schema
342342
// generated from the implementation Class as the "items" for the array.
343-
schema.setItems(implSchema);
343+
schema.setItems(lookupRef(context, type, implSchema));
344344
} else if (implSchema != null) {
345345
// If there is an impl class - merge the @Schema properties *onto* the schema
346346
// generated from the Class so that the annotation properties override the class
@@ -727,7 +727,21 @@ public static Schema schemaRegistration(final AnnotationScannerContext context,
727727

728728
if (allowRegistration(context, schemaRegistry, type, schema)) {
729729
schema = schemaRegistry.register(type, context.getJsonViews(), schema);
730-
} else if (schemaRegistry != null && schemaRegistry.hasRef(type, context.getJsonViews())) {
730+
} else {
731+
schema = lookupRef(context, type, schema);
732+
}
733+
734+
return schema;
735+
}
736+
737+
/**
738+
* If the given type is found in the registry and has a reference, replace the
739+
* given schema with a schema reference. Otherwise, simply return the schema unaltered.
740+
*/
741+
static Schema lookupRef(AnnotationScannerContext context, Type type, Schema schema) {
742+
SchemaRegistry schemaRegistry = context.getSchemaRegistry();
743+
744+
if (schemaRegistry != null && schemaRegistry.hasRef(type, context.getJsonViews())) {
731745
schema = schemaRegistry.lookupRef(type, context.getJsonViews());
732746
}
733747

core/src/test/java/io/smallrye/openapi/runtime/scanner/StandaloneSchemaScanTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,4 +968,25 @@ class ChildClass extends ParentClass implements FieldInterface {
968968
void testParentClassFieldSchemaAnnotation() throws IOException, JSONException {
969969
assertJsonEquals("components.schemas.annotated-parent-field.json", ParentClassFieldSchemaAnnotationTestClasses.CLASSES);
970970
}
971+
972+
@Test
973+
void testArrayItemsUseAvailableReference() throws IOException, JSONException {
974+
class ImmutableList<T> extends java.util.ArrayList<T> {
975+
private static final long serialVersionUID = 1L;
976+
}
977+
978+
@Schema(name = "Greeting", description = "A greeting message")
979+
class Greeting {
980+
@Schema(description = "The message to be displayed")
981+
String message;
982+
}
983+
984+
@Schema(name = "Response")
985+
class Response {
986+
@Schema(type = SchemaType.ARRAY, implementation = Greeting.class, description = "An array of greetings")
987+
ImmutableList<Greeting> greetings;
988+
}
989+
990+
assertJsonEquals("components.schemas.array-items-reference.json", ImmutableList.class, Greeting.class, Response.class);
991+
}
971992
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"openapi" : "3.1.0",
3+
"components" : {
4+
"schemas" : {
5+
"Greeting" : {
6+
"description" : "A greeting message",
7+
"type" : "object",
8+
"properties" : {
9+
"message" : {
10+
"type" : "string",
11+
"description" : "The message to be displayed"
12+
}
13+
}
14+
},
15+
"Response" : {
16+
"type" : "object",
17+
"properties" : {
18+
"greetings" : {
19+
"description" : "An array of greetings",
20+
"type" : "array",
21+
"items" : {
22+
"$ref" : "#/components/schemas/Greeting"
23+
}
24+
}
25+
}
26+
}
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)