Skip to content

Commit 79df5f5

Browse files
refactor: centralize common schema checks in ModelUtils
1 parent 3971b4f commit 79df5f5

11 files changed

Lines changed: 84 additions & 69 deletions

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2515,7 +2515,7 @@ private String getPrimitiveType(Schema schema) {
25152515
// Note: the value of a free-form object cannot be an arbitrary type. Per OAS specification,
25162516
// it must be a map of string to values.
25172517
return "object";
2518-
} else if (schema.getProperties() != null && !schema.getProperties().isEmpty()) { // having property implies it's a model
2518+
} else if (ModelUtils.hasProperties(schema)) { // having property implies it's a model
25192519
return "object";
25202520
} else if (ModelUtils.isAnyType(schema)) {
25212521
return "AnyType";
@@ -2707,8 +2707,8 @@ protected void updateModelForComposedSchema(CodegenModel m, Schema schema, Map<S
27072707
List<String> allRequired = new ArrayList<>();
27082708

27092709
// if schema has properties outside of allOf/oneOf/anyOf also add them to m
2710-
if (composed.getProperties() != null && !composed.getProperties().isEmpty()) {
2711-
if (composed.getOneOf() != null && !composed.getOneOf().isEmpty()) {
2710+
if (ModelUtils.hasProperties(composed)) {
2711+
if (ModelUtils.hasOneOf(composed)) {
27122712
LOGGER.warn("'oneOf' is intended to include only the additional optional OAS extension discriminator object. " +
27132713
"For more details, see https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.9.2.1.3 and the OAS section on 'Composition and Inheritance'.");
27142714
}
@@ -3362,7 +3362,7 @@ private CodegenProperty discriminatorFound(String composedSchemaName, Schema sc,
33623362
}
33633363
}
33643364
}
3365-
if (composedSchema.getOneOf() != null && composedSchema.getOneOf().size() != 0) {
3365+
if (ModelUtils.hasOneOf(composedSchema)) {
33663366
// All oneOf definitions must contain the discriminator
33673367
CodegenProperty cp = new CodegenProperty();
33683368
for (Object oneOf : composedSchema.getOneOf()) {
@@ -3388,7 +3388,7 @@ private CodegenProperty discriminatorFound(String composedSchemaName, Schema sc,
33883388
}
33893389
return cp;
33903390
}
3391-
if (composedSchema.getAnyOf() != null && composedSchema.getAnyOf().size() != 0) {
3391+
if (ModelUtils.hasAnyOf(composedSchema)) {
33923392
// All anyOf definitions must contain the discriminator because a min of one must be selected
33933393
CodegenProperty cp = new CodegenProperty();
33943394
for (Object anyOf : composedSchema.getAnyOf()) {
@@ -3457,7 +3457,7 @@ private Discriminator recursiveGetDiscriminator(Schema sc, ArrayList<Schema> vis
34573457
}
34583458
}
34593459
}
3460-
if (composedSchema.getOneOf() != null && composedSchema.getOneOf().size() != 0) {
3460+
if (ModelUtils.hasOneOf(composedSchema)) {
34613461
// All oneOf definitions must contain the discriminator
34623462
Integer hasDiscriminatorCnt = 0;
34633463
Integer hasNullTypeCnt = 0;
@@ -3792,7 +3792,7 @@ protected void addProperties(Map<String, Schema> properties, List<String> requir
37923792
}
37933793
if (ModelUtils.isComposedSchema(schema)) {
37943794
// fix issue #16797 and #15796, constructor fail by missing parent required params
3795-
if (schema.getProperties() != null && !schema.getProperties().isEmpty()) {
3795+
if (ModelUtils.hasProperties(schema)) {
37963796
properties.putAll(schema.getProperties());
37973797
}
37983798

@@ -8599,7 +8599,7 @@ public void setRemoveEnumValuePrefix(final boolean removeEnumValuePrefix) {
85998599
* @param name name of the parent oneOf schema
86008600
*/
86018601
public void addOneOfNameExtension(Schema schema, String name) {
8602-
if (schema.getOneOf() != null && !schema.getOneOf().isEmpty()) {
8602+
if (ModelUtils.hasOneOf(schema)) {
86038603
schema.addExtension(X_ONE_OF_NAME, name);
86048604
}
86058605
}

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

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import io.swagger.v3.oas.models.parameters.RequestBody;
3131
import io.swagger.v3.oas.models.responses.ApiResponse;
3232
import io.swagger.v3.oas.models.responses.ApiResponses;
33-
import java.util.stream.Collectors;
3433
import org.apache.commons.lang3.StringUtils;
3534
import org.openapitools.codegen.utils.ModelUtils;
3635
import org.slf4j.Logger;
@@ -236,9 +235,9 @@ private boolean isModelNeeded(Schema schema, Set<Schema> visitedSchemas) {
236235
if (resolveInlineEnums && schema.getEnum() != null && schema.getEnum().size() > 0) {
237236
return true;
238237
}
239-
if (schema.getType() == null || "object".equals(schema.getType())) {
238+
if (schema.getType() == null || ModelUtils.isObjectTypeOAS30(schema)) {
240239
// object or undeclared type with properties
241-
if (schema.getProperties() != null && schema.getProperties().size() > 0) {
240+
if (ModelUtils.hasProperties(schema)) {
242241
return true;
243242
}
244243
}
@@ -264,7 +263,7 @@ private boolean isModelNeeded(Schema schema, Set<Schema> visitedSchemas) {
264263
return isModelNeeded((Schema) schema.getAllOf().get(0), visitedSchemas);
265264
}
266265

267-
if (schema.getAllOf() != null && !schema.getAllOf().isEmpty()) {
266+
if (ModelUtils.hasAllOf(schema)) {
268267
// check to ensure at least one of the allOf item is model
269268
for (Object inner : schema.getAllOf()) {
270269
if (isModelNeeded(ModelUtils.getReferencedSchema(openAPI, (Schema) inner), visitedSchemas)) {
@@ -275,10 +274,10 @@ private boolean isModelNeeded(Schema schema, Set<Schema> visitedSchemas) {
275274
return false;
276275
}
277276

278-
if (schema.getAnyOf() != null && !schema.getAnyOf().isEmpty()) {
277+
if (ModelUtils.hasAnyOf(schema)) {
279278
return true;
280279
}
281-
if (schema.getOneOf() != null && !schema.getOneOf().isEmpty()) {
280+
if (ModelUtils.hasOneOf(schema)) {
282281
return true;
283282
}
284283
}
@@ -297,9 +296,9 @@ private void gatherInlineModels(Schema schema, String modelPrefix) {
297296
if (schema.get$ref() != null) {
298297
// if ref already, no inline schemas should be present but check for
299298
// any to catch OpenAPI violations
300-
if (isModelNeeded(schema) || "object".equals(schema.getType()) ||
299+
if (isModelNeeded(schema) || ModelUtils.isObjectTypeOAS30(schema) ||
301300
schema.getProperties() != null || schema.getAdditionalProperties() != null ||
302-
ModelUtils.isComposedSchema(schema)) {
301+
ModelUtils.isComposedSchema(schema)) {
303302
LOGGER.error("Illegal schema found with $ref combined with other properties," +
304303
" no properties should be defined alongside a $ref:\n " + schema.toString());
305304
}
@@ -308,7 +307,7 @@ private void gatherInlineModels(Schema schema, String modelPrefix) {
308307
// Check object models / any type models / composed models for properties,
309308
// if the schema has a type defined that is not "object" it should not define
310309
// any properties
311-
if (schema.getType() == null || "object".equals(schema.getType())) {
310+
if (schema.getType() == null || ModelUtils.isObjectTypeOAS30(schema)) {
312311
// Check properties and recurse, each property could be its own inline model
313312
Map<String, Schema> props = schema.getProperties();
314313
if (props != null) {
@@ -640,10 +639,8 @@ private void flattenComposedChildren(String key, List<Schema> children, boolean
640639
ListIterator<Schema> listIterator = children.listIterator();
641640
while (listIterator.hasNext()) {
642641
Schema component = listIterator.next();
643-
if ((component != null) &&
644-
(component.get$ref() == null) &&
645-
((component.getProperties() != null && !component.getProperties().isEmpty()) ||
646-
(component.getEnum() != null && !component.getEnum().isEmpty()))) {
642+
boolean componentDoesNotHaveRef = component != null && component.get$ref() == null;
643+
if (componentDoesNotHaveRef && (ModelUtils.hasProperties(component) || ModelUtils.hasEnum(component))) {
647644
// If a `title` attribute is defined in the inline schema, codegen uses it to name the
648645
// inline schema. Otherwise, we'll use the default naming such as InlineObject1, etc.
649646
// We know that this is not the best way to name the model.
@@ -839,7 +836,7 @@ private void flattenProperties(OpenAPI openAPI, Map<String, Schema> properties,
839836
Schema inner = ModelUtils.getSchemaItems(property);
840837
if (ModelUtils.isObjectSchema(inner)) {
841838
Schema op = inner;
842-
if (op.getProperties() != null && op.getProperties().size() > 0) {
839+
if (ModelUtils.hasProperties(op)) {
843840
flattenProperties(openAPI, op.getProperties(), path);
844841
String modelName = resolveModelName(op.getTitle(), path + "_" + key);
845842
Schema innerModel = modelFromProperty(openAPI, op, modelName);
@@ -869,7 +866,7 @@ private void flattenProperties(OpenAPI openAPI, Map<String, Schema> properties,
869866
Schema inner = ModelUtils.getAdditionalProperties(property);
870867
if (ModelUtils.isObjectSchema(inner)) {
871868
Schema op = inner;
872-
if (op.getProperties() != null && op.getProperties().size() > 0) {
869+
if (ModelUtils.hasProperties(op)) {
873870
flattenProperties(openAPI, op.getProperties(), path);
874871
String modelName = resolveModelName(op.getTitle(), path + "_" + key);
875872
Schema innerModel = modelFromProperty(openAPI, op, modelName);

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

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -760,19 +760,19 @@ public Schema normalizeSchema(Schema schema, Set<Schema> visitedSchemas) {
760760
schema = normalizeComplexComposedSchema(schema, visitedSchemas);
761761
}
762762

763-
if (schema.getAllOf() != null && !schema.getAllOf().isEmpty()) {
763+
if (ModelUtils.hasAllOf(schema)) {
764764
return normalizeAllOf(schema, visitedSchemas);
765765
}
766766

767-
if (schema.getOneOf() != null && !schema.getOneOf().isEmpty()) {
767+
if (ModelUtils.hasOneOf(schema)) {
768768
return normalizeOneOf(schema, visitedSchemas);
769769
}
770770

771-
if (schema.getAnyOf() != null && !schema.getAnyOf().isEmpty()) {
771+
if (ModelUtils.hasAnyOf(schema)) {
772772
return normalizeAnyOf(schema, visitedSchemas);
773773
}
774774

775-
if (schema.getProperties() != null && !schema.getProperties().isEmpty()) {
775+
if (ModelUtils.hasProperties(schema)) {
776776
normalizeProperties(schema, visitedSchemas);
777777
}
778778

@@ -781,7 +781,7 @@ public Schema normalizeSchema(Schema schema, Set<Schema> visitedSchemas) {
781781
}
782782

783783
return schema;
784-
} else if (schema.getProperties() != null && !schema.getProperties().isEmpty()) {
784+
} else if (ModelUtils.hasProperties(schema)) {
785785
normalizeProperties(schema, visitedSchemas);
786786
} else if (schema.getAdditionalProperties() instanceof Schema) { // map
787787
normalizeMapSchema(schema);
@@ -1109,7 +1109,7 @@ protected Schema normalizeAnyOf(Schema schema, Set<Schema> visitedSchemas) {
11091109

11101110
protected Schema normalizeComplexComposedSchema(Schema schema, Set<Schema> visitedSchemas) {
11111111
// loop through properties, if any
1112-
if (schema.getProperties() != null && !schema.getProperties().isEmpty()) {
1112+
if (ModelUtils.hasProperties(schema)) {
11131113
normalizeProperties(schema, visitedSchemas);
11141114
}
11151115

@@ -1294,10 +1294,9 @@ protected void processRemoveAnyOfOneOfAndKeepPropertiesOnly(Schema schema) {
12941294
return;
12951295
}
12961296

1297-
if (((schema.getOneOf() != null && !schema.getOneOf().isEmpty())
1298-
|| (schema.getAnyOf() != null && !schema.getAnyOf().isEmpty())) // has anyOf or oneOf
1299-
&& (schema.getProperties() != null && !schema.getProperties().isEmpty()) // has properties
1300-
&& schema.getAllOf() == null) { // not allOf
1297+
boolean hasAnyOfOrOneOf = ModelUtils.hasOneOf(schema) || ModelUtils.hasAnyOf(schema);
1298+
boolean notAllOf = schema.getAllOf() == null;
1299+
if (hasAnyOfOrOneOf && ModelUtils.hasProperties(schema) && notAllOf) {
13011300
// clear oneOf, anyOf
13021301
schema.setOneOf(null);
13031302
schema.setAnyOf(null);
@@ -1374,9 +1373,7 @@ protected Schema processSimplifyAnyOfEnum(Schema schema) {
13741373
if (schema.getAnyOf() == null || schema.getAnyOf().isEmpty()) {
13751374
return schema;
13761375
}
1377-
if(schema.getOneOf() != null && !schema.getOneOf().isEmpty() ||
1378-
schema.getAllOf() != null && !schema.getAllOf().isEmpty() ||
1379-
schema.getNot() != null) {
1376+
if(ModelUtils.hasOneOf(schema) || ModelUtils.hasAllOf(schema) || schema.getNot() != null) {
13801377
//only convert to enum if anyOf is the only composition
13811378
return schema;
13821379
}
@@ -1399,9 +1396,7 @@ protected Schema processSimplifyOneOfEnum(Schema schema) {
13991396
if (schema.getOneOf() == null || schema.getOneOf().isEmpty()) {
14001397
return schema;
14011398
}
1402-
if(schema.getAnyOf() != null && !schema.getAnyOf().isEmpty() ||
1403-
schema.getAllOf() != null && !schema.getAllOf().isEmpty() ||
1404-
schema.getNot() != null) {
1399+
if(ModelUtils.hasAnyOf(schema) || ModelUtils.hasAllOf(schema) || schema.getNot() != null) {
14051400
//only convert to enum if oneOf is the only composition
14061401
return schema;
14071402
}
@@ -2109,7 +2104,7 @@ protected void processNormalizeOtherThanObjectWithProperties(Schema schema) {
21092104
// Check object models / any type models / composed models for properties,
21102105
// if the schema has a type defined that is not "object" it should not define
21112106
// any properties
2112-
if (schema.getType() != null && !"object".equals(schema.getType())) {
2107+
if (schema.getType() != null && !ModelUtils.isObjectTypeOAS30(schema)) {
21132108
schema.setProperties(null);
21142109
}
21152110
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1383,7 +1383,7 @@ public String toDefaultValue(CodegenProperty cp, Schema schema) {
13831383
}
13841384
return toArrayDefaultValue(cp, schema);
13851385
} else if (ModelUtils.isMapSchema(schema) && !(ModelUtils.isComposedSchema(schema))) {
1386-
if (schema.getProperties() != null && schema.getProperties().size() > 0) {
1386+
if (ModelUtils.hasProperties(schema)) {
13871387
// object is complex object with free-form additional properties
13881388
if (schema.getDefault() != null) {
13891389
return super.toDefaultValue(schema);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ public String getSchemaType(Schema p) {
406406
openAPIType = "UNKNOWN_OPENAPI_TYPE";
407407
}
408408

409-
if ((p.getAnyOf() != null && !p.getAnyOf().isEmpty()) || (p.getOneOf() != null && !p.getOneOf().isEmpty())) {
409+
if (ModelUtils.hasAnyOf(p) || ModelUtils.hasOneOf(p)) {
410410
return openAPIType;
411411
}
412412

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ private String toExampleValueRecursive(Schema schema, List<Schema> includedSchem
512512

513513
// if required and optionals
514514
List<String> reqs = new ArrayList<>();
515-
if (schema.getProperties() != null && !schema.getProperties().isEmpty()) {
515+
if (ModelUtils.hasProperties(schema)) {
516516
for (Object toAdd : schema.getProperties().keySet()) {
517517
reqs.add((String) toAdd);
518518
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ private String toExampleValueRecursive(Schema schema, List<Schema> includedSchem
483483

484484
// if required and optionals
485485
List<String> reqs = new ArrayList<>();
486-
if (schema.getProperties() != null && !schema.getProperties().isEmpty()) {
486+
if (ModelUtils.hasProperties(schema)) {
487487
for (Object toAdd : schema.getProperties().keySet()) {
488488
reqs.add((String) toAdd);
489489
}

0 commit comments

Comments
 (0)