Skip to content

Commit 0a32726

Browse files
authored
Adds parseFlattenSpec (#5526)
* Adds parseFlattenSpec, updates tests and helper functions * Adds parseSPec invocation inside parseFlattenSpec
1 parent fc9d458 commit 0a32726

40 files changed

+283
-250
lines changed

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

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,10 @@ private static void visitContent(OpenAPI openAPI, Content content, OpenAPISchema
269269

270270
/**
271271
* Invoke the specified visitor function for every schema that matches mimeType in the OpenAPI document.
272-
*
272+
*
273273
* To avoid infinite recursion, referenced schemas are visited only once. When a referenced schema is visited,
274274
* it is added to visitedSchemas.
275-
*
275+
*
276276
* @param openAPI the OpenAPI document that contains schema objects.
277277
* @param schema the root schema object to be visited.
278278
* @param mimeType the mime type. TODO: does not seem to be used in a meaningful way.
@@ -355,22 +355,22 @@ public static String getSimpleRef(String ref) {
355355

356356
/**
357357
* Return true if the specified schema is an object with a fixed number of properties.
358-
*
358+
*
359359
* A ObjectSchema differs from an MapSchema in the following way:
360360
* - An ObjectSchema is not extensible, i.e. it has a fixed number of properties.
361361
* - A MapSchema is an object that can be extended with an arbitrary set of properties.
362362
* The payload may include dynamic properties.
363-
*
363+
*
364364
* For example, an OpenAPI schema is considered an ObjectSchema in the following scenarios:
365-
*
365+
*
366366
* type: object
367367
* additionalProperties: false
368368
* properties:
369369
* name:
370370
* type: string
371371
* address:
372372
* type: string
373-
*
373+
*
374374
* @param schema the OAS schema
375375
* @return true if the specified schema is an Object schema.
376376
*/
@@ -394,7 +394,7 @@ public static boolean isObjectSchema(Schema schema) {
394394
/**
395395
* Return true if the specified schema is composed, i.e. if it uses
396396
* 'oneOf', 'anyOf' or 'allOf'.
397-
*
397+
*
398398
* @param schema the OAS schema
399399
* @return true if the specified schema is a Composed schema.
400400
*/
@@ -659,7 +659,7 @@ public static boolean isModel(Schema schema) {
659659

660660
/**
661661
* Check to see if the schema is a free form object.
662-
*
662+
*
663663
* A free form object is an object (i.e. 'type: object' in a OAS document) that:
664664
* 1) Does not define properties, and
665665
* 2) Is not a composed schema (no anyOf, oneOf, allOf), and
@@ -737,7 +737,7 @@ public static Schema getSchema(OpenAPI openAPI, String name) {
737737
* Return a Map of the schemas defined under /components/schemas in the OAS document.
738738
* The returned Map only includes the direct children of /components/schemas in the OAS document; the Map
739739
* does not include inlined schemas.
740-
*
740+
*
741741
* @param openAPI the OpenAPI document.
742742
* @return a map of schemas in the OAS document.
743743
*/
@@ -767,7 +767,7 @@ public static List<Schema> getAllSchemas(OpenAPI openAPI) {
767767
});
768768
return allSchemas;
769769
}
770-
770+
771771
/**
772772
* If a RequestBody contains a reference to an other RequestBody with '$ref', returns the referenced RequestBody if it is found or the actual RequestBody in the other cases.
773773
*
@@ -906,10 +906,10 @@ public static Schema getSchemaFromResponse(ApiResponse response) {
906906

907907
/**
908908
* Return the first Schema from a specified OAS 'content' section.
909-
*
909+
*
910910
* For example, given the following OAS, this method returns the schema
911911
* for the 'application/json' content type because it is listed first in the OAS.
912-
*
912+
*
913913
* responses:
914914
* '200':
915915
* content:
@@ -918,8 +918,8 @@ public static Schema getSchemaFromResponse(ApiResponse response) {
918918
* $ref: '#/components/schemas/XYZ'
919919
* application/xml:
920920
* ...
921-
*
922-
* @param content a 'content' section in the OAS specification.
921+
*
922+
* @param content a 'content' section in the OAS specification.
923923
* @return the Schema.
924924
*/
925925
private static Schema getSchemaFromContent(Content content) {
@@ -1110,6 +1110,14 @@ public static String getParentName(ComposedSchema composedSchema, Map<String, Sc
11101110
int nullSchemaChildrenCount = 0;
11111111
boolean hasAmbiguousParents = false;
11121112
List<String> refedWithoutDiscriminator = new ArrayList<>();
1113+
String schemaName = "";
1114+
for (String thisSchemaName : allSchemas.keySet()) {
1115+
Schema sc = allSchemas.get(thisSchemaName);
1116+
if (isComposedSchema(sc) && (ComposedSchema) sc == composedSchema) {
1117+
schemaName = thisSchemaName;
1118+
break;
1119+
}
1120+
}
11131121

11141122
if (interfaces != null && !interfaces.isEmpty()) {
11151123
for (Schema schema : interfaces) {
@@ -1126,7 +1134,10 @@ public static String getParentName(ComposedSchema composedSchema, Map<String, Sc
11261134
} else {
11271135
// not a parent since discriminator.propertyName is not set
11281136
hasAmbiguousParents = true;
1129-
refedWithoutDiscriminator.add(parentName);
1137+
boolean isNotExtractedInlineSchema = !parentName.equals(schemaName+"_allOf");
1138+
if (isNotExtractedInlineSchema) {
1139+
refedWithoutDiscriminator.add(parentName);
1140+
}
11301141
}
11311142
} else {
11321143
// not a ref, doing nothing, except counting the number of times the 'null' type
@@ -1235,19 +1246,19 @@ else if (schema instanceof ComposedSchema) {
12351246
/**
12361247
* Return true if the 'nullable' attribute is set to true in the schema, i.e. if the value
12371248
* of the property can be the null value.
1238-
*
1249+
*
12391250
* In addition, if the OAS document is 3.1 or above, isNullable returns true if the input
12401251
* schema is a 'oneOf' composed document with at most two children, and one of the children
12411252
* is the 'null' type.
1242-
*
1253+
*
12431254
* The caller is responsible for resolving schema references before invoking isNullable.
12441255
* If the input schema is a $ref and the referenced schema has 'nullable: true', this method
12451256
* returns false (because the nullable attribute is defined in the referenced schema).
1246-
*
1257+
*
12471258
* The 'nullable' attribute was introduced in OAS 3.0.
12481259
* The 'nullable' attribute is deprecated in OAS 3.1. In a OAS 3.1 document, the preferred way
12491260
* to specify nullable properties is to use the 'null' type.
1250-
*
1261+
*
12511262
* @param schema the OAS schema.
12521263
* @return true if the schema is nullable.
12531264
*/
@@ -1273,7 +1284,7 @@ public static boolean isNullable(Schema schema) {
12731284
/**
12741285
* Return true if the specified composed schema is 'oneOf', contains one or two elements,
12751286
* and at least one of the elements is the 'null' type.
1276-
*
1287+
*
12771288
* The 'null' type is supported in OAS 3.1 and above.
12781289
* In the example below, the 'OptionalOrder' can have the null value because the 'null'
12791290
* type is one of the elements under 'oneOf'.
@@ -1282,7 +1293,7 @@ public static boolean isNullable(Schema schema) {
12821293
* oneOf:
12831294
* - type: 'null'
12841295
* - $ref: '#/components/schemas/Order'
1285-
*
1296+
*
12861297
* @param schema the OAS composed schema.
12871298
* @return true if the composed schema is nullable.
12881299
*/
@@ -1296,22 +1307,22 @@ public static boolean isNullableComposedSchema(ComposedSchema schema) {
12961307
}
12971308
}
12981309
return false;
1299-
}
1310+
}
13001311

13011312
/**
13021313
* isNullType returns true if the input schema is the 'null' type.
1303-
*
1314+
*
13041315
* The 'null' type is supported in OAS 3.1 and above. It is not supported
13051316
* in OAS 2.0 and OAS 3.0.x.
1306-
*
1317+
*
13071318
* For example, the "null" type could be used to specify that a value must
13081319
* either be null or a specified type:
1309-
*
1320+
*
13101321
* OptionalOrder:
13111322
* oneOf:
13121323
* - type: 'null'
13131324
* - $ref: '#/components/schemas/Order'
1314-
*
1325+
*
13151326
* @param schema the OpenAPI schema
13161327
* @return true if the schema is the 'null' type
13171328
*/
@@ -1350,4 +1361,4 @@ public static void syncValidationProperties(Schema schema, IJsonSchemaValidation
13501361
if (maxProperties != null) target.setMaxProperties(maxProperties);
13511362
}
13521363
}
1353-
}
1364+
}

0 commit comments

Comments
 (0)