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

Fix OpenAPI 3.0 output of nullable references #2152

Merged
merged 1 commit into from
Jan 15, 2025
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 @@ -191,7 +191,7 @@ private void populateSchemaObject30(Schema schema, O node) {
}

// Detect {$ref=....,nullable=true} and convert to anyOf[{$ref=...}, {type=null}]
if (schema.getRef() != null && schema.getType() == null && SchemaSupport.getNullable(schema) == Boolean.TRUE) {
if (schema.getRef() != null && SchemaSupport.getNullable(schema) == Boolean.TRUE) {
List<Schema> newAnyOfSchemas = new ArrayList<>();
newAnyOfSchemas.add(OASFactory.createSchema().ref(schema.getRef()));
newAnyOfSchemas.add(SchemaSupport.nullSchema());
Expand All @@ -201,7 +201,11 @@ private void populateSchemaObject30(Schema schema, O node) {
schema.addAllOf(OASFactory.createSchema().anyOf(newAnyOfSchemas));
}
schema.setRef(null);
SchemaSupport.setNullable(schema, null);
if (schema.getType() == null) {
SchemaSupport.setNullable(schema, null);
} else {
SchemaSupport.setNullable(schema, Boolean.TRUE);
}
}

// Detect {enum=[null]} and convert to {type=null}
Expand Down Expand Up @@ -389,7 +393,7 @@ private ReplacementFields compute30ReplacementFields(Schema schema31) {
}

// If we have anyOf = [{type=null}, {$ref=...}], remove it and set nullable and allOf = [{$ref=...}]
if (result.anyOf != null && result.anyOf.size() == 2 && result.ref == null && result.type == null) {
if (result.anyOf != null && result.anyOf.size() == 2 && result.ref == null) {
Optional<Schema> typeNullSchema = result.anyOf.stream().filter(s -> isSoloTypeNull(s)).findFirst();
Optional<Schema> refSchema = result.anyOf.stream().filter(s -> isSoloRef(s)).findFirst();
if (typeNullSchema.isPresent() && refSchema.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,29 @@ static class Turtle extends Reptile {
String shellPattern;
Speed speed;
}

@Test
void testNullableFieldReference() throws Exception {
Index index = indexOf(FieldTarget.class, FieldReferrer.class);
SmallRyeOpenAPI result1 = SmallRyeOpenAPI.builder()
.withConfig(config(Collections.emptyMap()))
.withIndex(index)
.defaultRequiredProperties(false)
.build();
printToConsole(result1.model());
assertJsonEquals("components.schemas.schemaproperty-nullable.json", result1.model());
}

@org.eclipse.microprofile.openapi.annotations.media.Schema
public static class FieldTarget {
}

@org.eclipse.microprofile.openapi.annotations.media.Schema
public static class FieldReferrer {
@org.eclipse.microprofile.openapi.annotations.media.Schema(nullable = true)
private FieldTarget a;
@org.eclipse.microprofile.openapi.annotations.media.Schema(nullable = true, description = "b")
private FieldTarget b;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@
}
]
},
"NullRefWithAnnotations": {
"description": "Description is an annotation. Scanner generates type as well here.",
"type": ["object", "null"],
"anyOf": [
{
"$ref": "#/components/schemas/mySchema"
},
{
"type": "null"
}
]
},
"NullRefWithAnyOf": {
"allOf": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@
}
]
},
"NullRefWithAnnotations": {
"description": "Description is an annotation. Scanner generates type as well here.",
"type": "object",
"allOf": [
{
"$ref": "#/components/schemas/mySchema"
}
],
"nullable": true
},
"NullRefWithAnyOf": {
"allOf": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"openapi" : "3.1.0",
"components" : {
"schemas" : {
"FieldReferrer" : {
"type" : "object",
"properties" : {
"a" : {
"anyOf" : [ {
"$ref" : "#/components/schemas/FieldTarget"
}, {
"type" : "null"
} ]
},
"b" : {
"description" : "b",
"type" : "object",
"anyOf" : [ {
"$ref" : "#/components/schemas/FieldTarget"
}, {
"type" : "null"
} ]
}
}
},
"FieldTarget" : {
"type" : "object"
}
}
}
}
Loading