Skip to content

Commit 5227b3d

Browse files
authored
fix: preserve parent description when simplifying nullable anyOf/oneOf (#23361)
1 parent b502f9d commit 5227b3d

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2300,6 +2300,10 @@ public static Schema simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema(OpenAPI openA
23002300
// if only one element left, simplify to just the element (schema)
23012301
if (subSchemas.size() == 1) {
23022302
Schema<?> subSchema = subSchemas.get(0);
2303+
// Preserve parent-level docs when nullable anyOf/oneOf collapses to a single child schema.
2304+
if (subSchema.getDescription() == null && schema.getDescription() != null) {
2305+
subSchema.setDescription(schema.getDescription());
2306+
}
23032307
if (Boolean.TRUE.equals(schema.getNullable())) { // retain nullable setting
23042308
subSchema.setNullable(true);
23052309
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/ModelUtilsTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.testng.annotations.Test;
2828

2929
import java.math.BigDecimal;
30+
import java.util.ArrayList;
3031
import java.util.Arrays;
3132
import java.util.HashMap;
3233
import java.util.List;
@@ -582,6 +583,38 @@ public void simplifyAnyOfWithOnlyOneNonNullSubSchemaKeepsReadOnlyWriteOnlyAttrib
582583
assertEquals(schema.get$ref(), "#/components/schemas/IntegerRef");
583584
}
584585

586+
@Test
587+
public void simplifyOneOfAnyOfWithOnlyOneNonNullSubSchemaKeepsParentDescription() {
588+
OpenAPI openAPI = new OpenAPI();
589+
590+
Schema anyOfParent = new Schema().description("Access token");
591+
anyOfParent.setAnyOf(new ArrayList<>(Arrays.asList(
592+
new StringSchema(),
593+
new Schema<>().type("null")
594+
)));
595+
Schema anyOfSchema = ModelUtils.simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema(openAPI, anyOfParent, anyOfParent.getAnyOf());
596+
assertEquals(anyOfSchema.getDescription(), "Access token");
597+
598+
Schema oneOfParent = new Schema().description("Expires at");
599+
oneOfParent.setOneOf(new ArrayList<>(Arrays.asList(
600+
new IntegerSchema(),
601+
new Schema<>().type("null")
602+
)));
603+
Schema oneOfSchema = ModelUtils.simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema(openAPI, oneOfParent, oneOfParent.getOneOf());
604+
assertEquals(oneOfSchema.getDescription(), "Expires at");
605+
606+
Schema anyOfParentWithChildDescription = new Schema().description("Parent description");
607+
anyOfParentWithChildDescription.setAnyOf(new ArrayList<>(Arrays.asList(
608+
new StringSchema().description("Child description"),
609+
new Schema<>().type("null")
610+
)));
611+
Schema anyOfSchemaWithChildDescription = ModelUtils.simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema(
612+
openAPI,
613+
anyOfParentWithChildDescription,
614+
anyOfParentWithChildDescription.getAnyOf());
615+
assertEquals(anyOfSchemaWithChildDescription.getDescription(), "Child description");
616+
}
617+
585618
@Test
586619
public void isNullTypeSchemaTest() {
587620
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/null_schema_test.yaml");

0 commit comments

Comments
 (0)