forked from loafle/openapi-generator-original
add logic to simplify any type represented with oneof/anyof (#18268)
This commit is contained in:
@@ -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<>();
|
||||
@@ -160,6 +162,14 @@ public class OpenAPINormalizer {
|
||||
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");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -922,6 +932,27 @@ public class OpenAPINormalizer {
|
||||
|
||||
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);
|
||||
|
||||
@@ -1026,6 +1057,27 @@ public class OpenAPINormalizer {
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -168,6 +168,12 @@ public class OpenAPINormalizerTest {
|
||||
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);
|
||||
@@ -192,6 +198,15 @@ public class OpenAPINormalizerTest {
|
||||
|
||||
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
|
||||
|
||||
@@ -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: {}
|
||||
Reference in New Issue
Block a user