Skip to content

Commit 7823be3

Browse files
MikeEdgarAzquelt
andauthored
fix: skip class introspection when $ref is derived from annotation (#2266)
* fix: skip class introspection when implementation is given in annotation Signed-off-by: Michael Edgar <[email protected]> * fix: skip class introspection when `$ref` is derived from annotation Signed-off-by: Michael Edgar <[email protected]> Co-authored-by: Andrew Rouse <[email protected]> --------- Signed-off-by: Michael Edgar <[email protected]> Co-authored-by: Andrew Rouse <[email protected]>
1 parent 99acd11 commit 7823be3

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

core/src/main/java/io/smallrye/openapi/runtime/scanner/OpenApiDataObjectScanner.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,10 @@ private void depthFirstGraphSearch() {
256256
TypeUtil.mapDeprecated(context, currentClass, currentSchema::getDeprecated, currentSchema::setDeprecated);
257257
currentPathEntry.setSchema(currentSchema);
258258

259+
if (currentSchema.getRef() != null) {
260+
continue;
261+
}
262+
259263
if (!hasNonNullType(currentSchema)) {
260264
// If not schema has yet been set, consider this an "object"
261265
SchemaSupport.setType(currentSchema, Schema.SchemaType.OBJECT);
@@ -292,7 +296,7 @@ private void depthFirstGraphSearch() {
292296

293297
private void maybeRegisterSchema(Type currentType, Schema currentSchema, Schema entrySchema) {
294298
Schema ref = SchemaFactory.schemaRegistration(context, currentType, currentSchema);
295-
299+
Schema.SchemaType type = SchemaSupport.getNonNullType(currentSchema);
296300
/*
297301
* Ignore the returned ref when:
298302
*
@@ -301,7 +305,7 @@ private void maybeRegisterSchema(Type currentType, Schema currentSchema, Schema
301305
* - the target of the ref is the schema currently being processed and using the ref would
302306
* result in a self-referencing schema.
303307
*/
304-
if (ref != currentSchema && !currentSchema.getType().contains(Schema.SchemaType.OBJECT) && ref.getRef() != null) {
308+
if (ref != currentSchema && type != Schema.SchemaType.OBJECT && ref.getRef() != null) {
305309
Schema refTarget = context.getSchemaRegistry().lookupSchema(currentType, context.getJsonViews());
306310

307311
if (refTarget != entrySchema) {

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,4 +1019,41 @@ class Response {
10191019

10201020
assertJsonEquals("components.schemas.array-items-reference.json", ImmutableList.class, Greeting.class, Response.class);
10211021
}
1022+
1023+
@Test
1024+
@SuppressWarnings("unused")
1025+
void testClassSchemaWithImplNotIntrospected() throws IOException, JSONException {
1026+
class UseSchemaImplementationImpl {
1027+
private java.math.BigDecimal amount;
1028+
private String currency;
1029+
// ... constructor and getter/setter methods ....
1030+
}
1031+
1032+
@Schema(implementation = UseSchemaImplementationImpl.class)
1033+
class UseSchemaImplementationType {
1034+
private String value;
1035+
private final String internalValue = "internalValue";
1036+
private final Boolean composite = true;
1037+
1038+
public final boolean isNull() {
1039+
return value == null;
1040+
}
1041+
// ... some other methods ...
1042+
}
1043+
1044+
@Schema(ref = "#/components/schemas/1UseSchemaImplementationImpl")
1045+
class UseSchemaImplementationType2 {
1046+
private String value;
1047+
private final String internalValue = "internalValue";
1048+
private final Boolean composite = true;
1049+
1050+
public final boolean isNull() {
1051+
return value == null;
1052+
}
1053+
// ... some other methods ...
1054+
}
1055+
1056+
assertJsonEquals("components.schemas.implementation-no-introspection.json", UseSchemaImplementationImpl.class,
1057+
UseSchemaImplementationType.class, UseSchemaImplementationType2.class);
1058+
}
10221059
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"openapi" : "3.1.0",
3+
"components" : {
4+
"schemas" : {
5+
"1UseSchemaImplementationImpl" : {
6+
"type" : "object",
7+
"properties" : {
8+
"amount" : {
9+
"type" : "number"
10+
},
11+
"currency" : {
12+
"type" : "string"
13+
}
14+
}
15+
},
16+
"1UseSchemaImplementationType" : {
17+
"$ref" : "#/components/schemas/1UseSchemaImplementationImpl"
18+
},
19+
"1UseSchemaImplementationType2" : {
20+
"$ref" : "#/components/schemas/1UseSchemaImplementationImpl"
21+
}
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)