Skip to content
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 @@ -2268,6 +2268,10 @@ public static Schema simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema(OpenAPI openA
// if only one element left, simplify to just the element (schema)
if (subSchemas.size() == 1) {
Schema<?> subSchema = subSchemas.get(0);
// Preserve parent-level docs when nullable anyOf/oneOf collapses to a single child schema.
if (subSchema.getDescription() == null && schema.getDescription() != null) {
subSchema.setDescription(schema.getDescription());
}
if (Boolean.TRUE.equals(schema.getNullable())) { // retain nullable setting
subSchema.setNullable(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.testng.annotations.Test;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -582,6 +583,38 @@ public void simplifyAnyOfWithOnlyOneNonNullSubSchemaKeepsReadOnlyWriteOnlyAttrib
assertEquals(schema.get$ref(), "#/components/schemas/IntegerRef");
}

@Test
public void simplifyOneOfAnyOfWithOnlyOneNonNullSubSchemaKeepsParentDescription() {
OpenAPI openAPI = new OpenAPI();

Schema anyOfParent = new Schema().description("Access token");
anyOfParent.setAnyOf(new ArrayList<>(Arrays.asList(
new StringSchema(),
new Schema<>().type("null")
)));
Schema anyOfSchema = ModelUtils.simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema(openAPI, anyOfParent, anyOfParent.getAnyOf());
assertEquals(anyOfSchema.getDescription(), "Access token");

Schema oneOfParent = new Schema().description("Expires at");
oneOfParent.setOneOf(new ArrayList<>(Arrays.asList(
new IntegerSchema(),
new Schema<>().type("null")
)));
Schema oneOfSchema = ModelUtils.simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema(openAPI, oneOfParent, oneOfParent.getOneOf());
assertEquals(oneOfSchema.getDescription(), "Expires at");

Schema anyOfParentWithChildDescription = new Schema().description("Parent description");
anyOfParentWithChildDescription.setAnyOf(new ArrayList<>(Arrays.asList(
new StringSchema().description("Child description"),
new Schema<>().type("null")
)));
Schema anyOfSchemaWithChildDescription = ModelUtils.simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema(
openAPI,
anyOfParentWithChildDescription,
anyOfParentWithChildDescription.getAnyOf());
assertEquals(anyOfSchemaWithChildDescription.getDescription(), "Child description");
}

@Test
public void isNullTypeSchemaTest() {
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/null_schema_test.yaml");
Expand Down
Loading