Skip to content

Add logic to simplify any type represented with oneof/anyof #18268

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

Merged
merged 1 commit into from
Apr 2, 2024
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 @@ -38,6 +38,8 @@ public class OpenAPINormalizer {
private Map<String, String> inputRules = new HashMap<>();
private Map<String, Boolean> rules = new HashMap<>();

private TreeSet<String> anyTypeTreeSet = new TreeSet<>();

final Logger LOGGER = LoggerFactory.getLogger(OpenAPINormalizer.class);

Set<String> ruleNames = new TreeSet<>();
Expand Down Expand Up @@ -149,6 +151,14 @@ public OpenAPINormalizer(OpenAPI openAPI, Map<String, String> inputRules) {
rules.put(SIMPLIFY_BOOLEAN_ENUM, true);

processRules(inputRules);

// represent any type in tree set
anyTypeTreeSet.add("string");
anyTypeTreeSet.add("number");
anyTypeTreeSet.add("integer");
anyTypeTreeSet.add("boolean");
anyTypeTreeSet.add("object");
anyTypeTreeSet.add("array");
}

/**
Expand Down Expand Up @@ -882,6 +892,27 @@ private Schema processSimplifyOneOf(Schema schema) {

List<Schema> oneOfSchemas = schema.getOneOf();
if (oneOfSchemas != null) {
// simplify any type with 6 sub-schemas (string, integer, etc) in oneOf
if (oneOfSchemas.size() == 6) {
TreeSet<String> ts = new TreeSet<>();
for (Schema s: oneOfSchemas) {
ts.add(s.getType());
}

if (ts.equals(anyTypeTreeSet)) {
Schema anyType = new Schema();
anyType.setDescription(schema.getDescription());
anyType.setNullable(schema.getNullable());
anyType.setExtensions(schema.getExtensions());
anyType.setTitle(schema.getTitle());
anyType.setExample(schema.getExample());
anyType.setExamples(schema.getExamples());
anyType.setDefault(schema.getDefault());
anyType.setDeprecated(schema.getDeprecated());
return anyType;
}
}

if (oneOfSchemas.removeIf(oneOf -> isNullTypeSchema(oneOf))) {
schema.setNullable(true);

Expand Down Expand Up @@ -966,6 +997,27 @@ private Schema processSimplifyAnyOf(Schema schema) {

List<Schema> anyOfSchemas = schema.getAnyOf();
if (anyOfSchemas != null) {
// simplify any type with 6 sub-schemas (string, integer, etc) in anyOf
if (anyOfSchemas.size() == 6) {
TreeSet<String> ts = new TreeSet<>();
for (Schema s: anyOfSchemas) {
ts.add(s.getType());
}

if (ts.equals(anyTypeTreeSet)) {
Schema anyType = new Schema();
anyType.setDescription(schema.getDescription());
anyType.setNullable(schema.getNullable());
anyType.setExtensions(schema.getExtensions());
anyType.setTitle(schema.getTitle());
anyType.setExample(schema.getExample());
anyType.setExamples(schema.getExamples());
anyType.setDefault(schema.getDefault());
anyType.setDeprecated(schema.getDeprecated());
return anyType;
}
}

if (anyOfSchemas.removeIf(anyOf -> isNullTypeSchema(anyOf))) {
schema.setNullable(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ public void testOpenAPINormalizerSimplifyOneOfAnyOf() {
Schema schema9 = openAPI.getComponents().getSchemas().get("AnyOfStringArrayOfString");
assertEquals(schema9.getAnyOf().size(), 2);

Schema schema11 = openAPI.getComponents().getSchemas().get("AnyOfAnyType");
assertEquals(schema11.getAnyOf().size(), 6);

Schema schema13 = openAPI.getComponents().getSchemas().get("OneOfAnyType");
assertEquals(schema13.getOneOf().size(), 6);

Map<String, String> options = new HashMap<>();
options.put("SIMPLIFY_ONEOF_ANYOF", "true");
OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, options);
Expand All @@ -192,6 +198,15 @@ public void testOpenAPINormalizerSimplifyOneOfAnyOf() {

Schema schema10 = openAPI.getComponents().getSchemas().get("AnyOfStringArrayOfString");
assertEquals(schema10.getAnyOf().size(), 2);

Schema schema12 = openAPI.getComponents().getSchemas().get("AnyOfAnyType");
assertEquals(schema12.getAnyOf(), null);
assertEquals(schema12.getType(), null);

Schema schema14 = openAPI.getComponents().getSchemas().get("OneOfAnyType");
assertEquals(schema14.getOneOf(), null);
assertEquals(schema14.getType(), null);

}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,22 @@ components:
- type: string
- type: array
items:
type: string
type: string
AnyOfAnyType:
anyOf:
- type: boolean
- type: array
items: {}
- type: object
- type: string
- type: number
- type: integer
OneOfAnyType:
oneOf:
- type: object
- type: boolean
- type: number
- type: string
- type: integer
- type: array
items: {}
Loading