Skip to content

Commit 2934f5a

Browse files
authored
add logic to simplify any type represented with oneof/anyof (#18268)
1 parent 2ce7151 commit 2934f5a

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public class OpenAPINormalizer {
3838
private Map<String, String> inputRules = new HashMap<>();
3939
private Map<String, Boolean> rules = new HashMap<>();
4040

41+
private TreeSet<String> anyTypeTreeSet = new TreeSet<>();
42+
4143
final Logger LOGGER = LoggerFactory.getLogger(OpenAPINormalizer.class);
4244

4345
Set<String> ruleNames = new TreeSet<>();
@@ -160,6 +162,14 @@ public OpenAPINormalizer(OpenAPI openAPI, Map<String, String> inputRules) {
160162
rules.put(SIMPLIFY_BOOLEAN_ENUM, true);
161163

162164
processRules(inputRules);
165+
166+
// represent any type in tree set
167+
anyTypeTreeSet.add("string");
168+
anyTypeTreeSet.add("number");
169+
anyTypeTreeSet.add("integer");
170+
anyTypeTreeSet.add("boolean");
171+
anyTypeTreeSet.add("object");
172+
anyTypeTreeSet.add("array");
163173
}
164174

165175
/**
@@ -922,6 +932,27 @@ private Schema processSimplifyOneOf(Schema schema) {
922932

923933
List<Schema> oneOfSchemas = schema.getOneOf();
924934
if (oneOfSchemas != null) {
935+
// simplify any type with 6 sub-schemas (string, integer, etc) in oneOf
936+
if (oneOfSchemas.size() == 6) {
937+
TreeSet<String> ts = new TreeSet<>();
938+
for (Schema s: oneOfSchemas) {
939+
ts.add(s.getType());
940+
}
941+
942+
if (ts.equals(anyTypeTreeSet)) {
943+
Schema anyType = new Schema();
944+
anyType.setDescription(schema.getDescription());
945+
anyType.setNullable(schema.getNullable());
946+
anyType.setExtensions(schema.getExtensions());
947+
anyType.setTitle(schema.getTitle());
948+
anyType.setExample(schema.getExample());
949+
anyType.setExamples(schema.getExamples());
950+
anyType.setDefault(schema.getDefault());
951+
anyType.setDeprecated(schema.getDeprecated());
952+
return anyType;
953+
}
954+
}
955+
925956
if (oneOfSchemas.removeIf(oneOf -> isNullTypeSchema(oneOf))) {
926957
schema.setNullable(true);
927958

@@ -1026,6 +1057,27 @@ private Schema processSimplifyAnyOf(Schema schema) {
10261057

10271058
List<Schema> anyOfSchemas = schema.getAnyOf();
10281059
if (anyOfSchemas != null) {
1060+
// simplify any type with 6 sub-schemas (string, integer, etc) in anyOf
1061+
if (anyOfSchemas.size() == 6) {
1062+
TreeSet<String> ts = new TreeSet<>();
1063+
for (Schema s: anyOfSchemas) {
1064+
ts.add(s.getType());
1065+
}
1066+
1067+
if (ts.equals(anyTypeTreeSet)) {
1068+
Schema anyType = new Schema();
1069+
anyType.setDescription(schema.getDescription());
1070+
anyType.setNullable(schema.getNullable());
1071+
anyType.setExtensions(schema.getExtensions());
1072+
anyType.setTitle(schema.getTitle());
1073+
anyType.setExample(schema.getExample());
1074+
anyType.setExamples(schema.getExamples());
1075+
anyType.setDefault(schema.getDefault());
1076+
anyType.setDeprecated(schema.getDeprecated());
1077+
return anyType;
1078+
}
1079+
}
1080+
10291081
if (anyOfSchemas.removeIf(anyOf -> isNullTypeSchema(anyOf))) {
10301082
schema.setNullable(true);
10311083
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ public void testOpenAPINormalizerSimplifyOneOfAnyOf() {
168168
Schema schema9 = openAPI.getComponents().getSchemas().get("AnyOfStringArrayOfString");
169169
assertEquals(schema9.getAnyOf().size(), 2);
170170

171+
Schema schema11 = openAPI.getComponents().getSchemas().get("AnyOfAnyType");
172+
assertEquals(schema11.getAnyOf().size(), 6);
173+
174+
Schema schema13 = openAPI.getComponents().getSchemas().get("OneOfAnyType");
175+
assertEquals(schema13.getOneOf().size(), 6);
176+
171177
Map<String, String> options = new HashMap<>();
172178
options.put("SIMPLIFY_ONEOF_ANYOF", "true");
173179
OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, options);
@@ -192,6 +198,15 @@ public void testOpenAPINormalizerSimplifyOneOfAnyOf() {
192198

193199
Schema schema10 = openAPI.getComponents().getSchemas().get("AnyOfStringArrayOfString");
194200
assertEquals(schema10.getAnyOf().size(), 2);
201+
202+
Schema schema12 = openAPI.getComponents().getSchemas().get("AnyOfAnyType");
203+
assertEquals(schema12.getAnyOf(), null);
204+
assertEquals(schema12.getType(), null);
205+
206+
Schema schema14 = openAPI.getComponents().getSchemas().get("OneOfAnyType");
207+
assertEquals(schema14.getOneOf(), null);
208+
assertEquals(schema14.getType(), null);
209+
195210
}
196211

197212
@Test

modules/openapi-generator/src/test/resources/3_0/simplifyOneOfAnyOf_test.yaml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,22 @@ components:
8888
- type: string
8989
- type: array
9090
items:
91-
type: string
91+
type: string
92+
AnyOfAnyType:
93+
anyOf:
94+
- type: boolean
95+
- type: array
96+
items: {}
97+
- type: object
98+
- type: string
99+
- type: number
100+
- type: integer
101+
OneOfAnyType:
102+
oneOf:
103+
- type: object
104+
- type: boolean
105+
- type: number
106+
- type: string
107+
- type: integer
108+
- type: array
109+
items: {}

0 commit comments

Comments
 (0)