Skip to content

Commit f2e0555

Browse files
authored
Refactor ModelUtils methods without logic changes (#15030)
* Refactor: ModelUtils: Harmonize isIntegerSchema with isStringSchema Make code isIntegerSchema look similar to isStringSchema and remove if-clause in favor to bool-OR '||'. * Refactor: ModelUtils: Simplify isMapSchema Factor out if sequence and use "return A || B || C;" scheme instead. * Refactor: ModelUtils: Simplify isUnsignedIntegerSchema Factor out 'if (x) {return true;} else {return false;}' and use 'return x;' instead. * Refactor: ModelUtils: Simplify isUnsignedLongSchema Factor out 'if (x) {return true;} else {return false;}' and use 'return x;' instead. * Refactor: ModelUtils: Simplify isTypeObjectSchema Factor out 'if (x) {return true;} return false;' and use 'return x;' instead. * Refactor: ModelUtils: Simplify isComposedSchema Factor out 'if (x) {return true;} return false;' and use 'return x;' instead. * Refactor: ModelUtils: Simplify isBooleanSchema Factor out 'if (x) {return true;} return y;' and use 'return x || y;' instead. * Refactor: ModelUtils: Simplify isNumberSchema Factor out 'if (x) {return true;} return y;' and use 'return x || y;' instead. * Refactor: ModelUtils: Simplify isDateSchema Factor out 'if (x) {return true;} return y;' and use 'return x || y;' instead. * Refactor: ModelUtils: Simplify isDateTimeSchema Factor out 'if (x) {return true;} return y;' and use 'return x || y;' instead. * Refactor: ModelUtils: Simplify isPasswordSchema Factor out 'if (x) {return true;} return y;' and use 'return x || y;' instead. * Refactor: ModelUtils: Simplify isByteArraySchema Factor out 'if (x) {return true;} return y;' and use 'return x || y;' instead. * Refactor: ModelUtils: Simplify isBinarySchema Factor out 'if (x) {return true;} return y;' and use 'return x || y;' instead. * Refactor: ModelUtils: Simplify isFileSchema Factor out 'if (x) {return true;} return y;' and use 'return x || y;' instead. * Refactor: ModelUtils: Simplify isUUIDSchema Factor out 'if (x) {return true;} return y;' and use 'return x || y;' instead. * Refactor: ModelUtils: Simplify isEmailSchema Factor out 'if (x) {return true;} return y;' and use 'return x || y;' instead. * Refactor: ModelUtils: Simplify isObjectSchema Factor out 'if (x) {return true;} if (y) {return true;} return z;' and use 'return x || y || z;' instead. * Refactor: ModelUtils: Simplify isModel Factor out 'if (x) {return false;} if (y) {return true;} return z;' and use 'return !x && (y || z);' instead. * Refactor: ModelUtils: Simplify isModelWithPropertiesOnly Factor out 'if (x) {return false;} if (y) {return true;} return false;' and use 'return !x && y;' instead. * Refactor: ModelUtils: Simplify getApiResponse Factor out 'if (x) {return null;} if (y) {return z;} return null;' and use 'if (!x && y) {return z;} return null;' instead. * Refactor: ModelUtils: Simplify getParameter Factor out 'if (x) {return null;} if (y) {return z;} return null;' and use 'if (!x && y) {return z;} return null;' instead. * Refactor: ModelUtils: Simplify getCallback Factor out 'if (x) {return null;} if (y) {return z;} return null;' and use 'if (!x && y) {return z;} return null;' instead. * Refactor: ModelUtils: Simplify getHeader Factor out 'if (x) {return null;} if (y) {return z;} return null;' and use 'if (!x && y) {return z;} return null;' instead. * Refactor: ModelUtils: Simplify isExtensionParent Factor out 'if (x) {return false;} else {y}' and use 'if (x) {return false;} y' instead. * Refactor: ModelUtils: Simplify isComplexComposedSchema Factor out 'if (x) {return true;} return false;' and use 'return x;' instead.
1 parent e925336 commit f2e0555

1 file changed

Lines changed: 74 additions & 158 deletions

File tree

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

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

Lines changed: 74 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -412,10 +412,7 @@ public static String getSimpleRef(String ref) {
412412
* @return true if the specified schema is an Object schema.
413413
*/
414414
public static boolean isTypeObjectSchema(Schema schema) {
415-
if (SchemaTypeUtil.OBJECT_TYPE.equals(schema.getType())) {
416-
return true;
417-
}
418-
return false;
415+
return SchemaTypeUtil.OBJECT_TYPE.equals(schema.getType());
419416
}
420417

421418
/**
@@ -441,17 +438,11 @@ public static boolean isTypeObjectSchema(Schema schema) {
441438
* @return true if the specified schema is an Object schema.
442439
*/
443440
public static boolean isObjectSchema(Schema schema) {
444-
if (schema instanceof ObjectSchema) {
445-
return true;
446-
}
447-
448-
// must not be a map
449-
if (SchemaTypeUtil.OBJECT_TYPE.equals(schema.getType()) && !(schema instanceof MapSchema)) {
450-
return true;
451-
}
452-
453-
// must have at least one property
454-
return schema.getType() == null && schema.getProperties() != null && !schema.getProperties().isEmpty();
441+
return (schema instanceof ObjectSchema) ||
442+
// must not be a map
443+
(SchemaTypeUtil.OBJECT_TYPE.equals(schema.getType()) && !(schema instanceof MapSchema)) ||
444+
// must have at least one property
445+
(schema.getType() == null && schema.getProperties() != null && !schema.getProperties().isEmpty());
455446
}
456447

457448
/**
@@ -462,10 +453,7 @@ public static boolean isObjectSchema(Schema schema) {
462453
* @return true if the specified schema is a Composed schema.
463454
*/
464455
public static boolean isComposedSchema(Schema schema) {
465-
if (schema instanceof ComposedSchema) {
466-
return true;
467-
}
468-
return false;
456+
return schema instanceof ComposedSchema;
469457
}
470458

471459
/**
@@ -498,11 +486,7 @@ public static boolean isComplexComposedSchema(Schema schema) {
498486
count++;
499487
}
500488

501-
if (count > 1) {
502-
return true;
503-
}
504-
505-
return false;
489+
return count > 1;
506490
}
507491

508492
/**
@@ -540,19 +524,9 @@ public static boolean isComplexComposedSchema(Schema schema) {
540524
* @return true if the specified schema is a Map schema.
541525
*/
542526
public static boolean isMapSchema(Schema schema) {
543-
if (schema instanceof MapSchema) {
544-
return true;
545-
}
546-
547-
if (schema == null) {
548-
return false;
549-
}
550-
551-
if (schema.getAdditionalProperties() instanceof Schema) {
552-
return true;
553-
}
554-
555-
return schema.getAdditionalProperties() instanceof Boolean && (Boolean) schema.getAdditionalProperties();
527+
return (schema instanceof MapSchema) ||
528+
(schema.getAdditionalProperties() instanceof Schema) ||
529+
(schema.getAdditionalProperties() instanceof Boolean && (Boolean) schema.getAdditionalProperties());
556530
}
557531

558532
/**
@@ -574,10 +548,7 @@ public static boolean isStringSchema(Schema schema) {
574548
}
575549

576550
public static boolean isIntegerSchema(Schema schema) {
577-
if (schema instanceof IntegerSchema) {
578-
return true;
579-
}
580-
return SchemaTypeUtil.INTEGER_TYPE.equals(schema.getType());
551+
return schema instanceof IntegerSchema || SchemaTypeUtil.INTEGER_TYPE.equals(schema.getType());
581552
}
582553

583554
public static boolean isShortSchema(Schema schema) {
@@ -587,12 +558,9 @@ public static boolean isShortSchema(Schema schema) {
587558
}
588559

589560
public static boolean isUnsignedIntegerSchema(Schema schema) {
590-
if (SchemaTypeUtil.INTEGER_TYPE.equals(schema.getType()) && // type: integer
561+
return SchemaTypeUtil.INTEGER_TYPE.equals(schema.getType()) && // type: integer
591562
("int32".equals(schema.getFormat()) || schema.getFormat() == null) && // format: int32
592-
(schema.getExtensions() != null && (Boolean) schema.getExtensions().getOrDefault("x-unsigned", Boolean.FALSE))) { // x-unsigned: true
593-
return true;
594-
}
595-
return false;
563+
(schema.getExtensions() != null && (Boolean) schema.getExtensions().getOrDefault("x-unsigned", Boolean.FALSE));
596564
}
597565

598566
public static boolean isLongSchema(Schema schema) {
@@ -602,26 +570,17 @@ public static boolean isLongSchema(Schema schema) {
602570
}
603571

604572
public static boolean isUnsignedLongSchema(Schema schema) {
605-
if (SchemaTypeUtil.INTEGER_TYPE.equals(schema.getType()) && // type: integer
573+
return SchemaTypeUtil.INTEGER_TYPE.equals(schema.getType()) && // type: integer
606574
"int64".equals(schema.getFormat()) && // format: int64
607-
(schema.getExtensions() != null && (Boolean) schema.getExtensions().getOrDefault("x-unsigned", Boolean.FALSE))) { // x-unsigned: true
608-
return true;
609-
}
610-
return false;
575+
(schema.getExtensions() != null && (Boolean) schema.getExtensions().getOrDefault("x-unsigned", Boolean.FALSE));
611576
}
612577

613578
public static boolean isBooleanSchema(Schema schema) {
614-
if (schema instanceof BooleanSchema) {
615-
return true;
616-
}
617-
return SchemaTypeUtil.BOOLEAN_TYPE.equals(schema.getType());
579+
return schema instanceof BooleanSchema || SchemaTypeUtil.BOOLEAN_TYPE.equals(schema.getType());
618580
}
619581

620582
public static boolean isNumberSchema(Schema schema) {
621-
if (schema instanceof NumberSchema) {
622-
return true;
623-
}
624-
return SchemaTypeUtil.NUMBER_TYPE.equals(schema.getType());
583+
return schema instanceof NumberSchema || SchemaTypeUtil.NUMBER_TYPE.equals(schema.getType());
625584
}
626585

627586
public static boolean isFloatSchema(Schema schema) {
@@ -637,66 +596,51 @@ public static boolean isDoubleSchema(Schema schema) {
637596
}
638597

639598
public static boolean isDateSchema(Schema schema) {
640-
if (schema instanceof DateSchema) {
641-
return true;
642-
}
643-
644-
// format: date
645-
return SchemaTypeUtil.STRING_TYPE.equals(schema.getType())
646-
&& SchemaTypeUtil.DATE_FORMAT.equals(schema.getFormat());
599+
return (schema instanceof DateSchema) ||
600+
// format: date
601+
(SchemaTypeUtil.STRING_TYPE.equals(schema.getType())
602+
&& SchemaTypeUtil.DATE_FORMAT.equals(schema.getFormat()));
647603
}
648604

649605
public static boolean isDateTimeSchema(Schema schema) {
650-
if (schema instanceof DateTimeSchema) {
651-
return true;
652-
}
653-
// format: date-time
654-
return SchemaTypeUtil.STRING_TYPE.equals(schema.getType())
655-
&& SchemaTypeUtil.DATE_TIME_FORMAT.equals(schema.getFormat());
606+
return (schema instanceof DateTimeSchema) ||
607+
// format: date-time
608+
(SchemaTypeUtil.STRING_TYPE.equals(schema.getType())
609+
&& SchemaTypeUtil.DATE_TIME_FORMAT.equals(schema.getFormat()));
656610
}
657611

658612
public static boolean isPasswordSchema(Schema schema) {
659-
if (schema instanceof PasswordSchema) {
660-
return true;
661-
}
662-
// double
663-
return SchemaTypeUtil.STRING_TYPE.equals(schema.getType())
664-
&& SchemaTypeUtil.PASSWORD_FORMAT.equals(schema.getFormat());
613+
return (schema instanceof PasswordSchema) ||
614+
// double
615+
(SchemaTypeUtil.STRING_TYPE.equals(schema.getType())
616+
&& SchemaTypeUtil.PASSWORD_FORMAT.equals(schema.getFormat()));
665617
}
666618

667619
public static boolean isByteArraySchema(Schema schema) {
668-
if (schema instanceof ByteArraySchema) {
669-
return true;
670-
}
671-
// format: byte
672-
return SchemaTypeUtil.STRING_TYPE.equals(schema.getType())
673-
&& SchemaTypeUtil.BYTE_FORMAT.equals(schema.getFormat());
620+
return (schema instanceof ByteArraySchema) ||
621+
// format: byte
622+
(SchemaTypeUtil.STRING_TYPE.equals(schema.getType())
623+
&& SchemaTypeUtil.BYTE_FORMAT.equals(schema.getFormat()));
674624
}
675625

676626
public static boolean isBinarySchema(Schema schema) {
677-
if (schema instanceof BinarySchema) {
678-
return true;
679-
}
680-
// format: binary
681-
return SchemaTypeUtil.STRING_TYPE.equals(schema.getType())
682-
&& SchemaTypeUtil.BINARY_FORMAT.equals(schema.getFormat());
627+
return (schema instanceof BinarySchema) ||
628+
// format: binary
629+
(SchemaTypeUtil.STRING_TYPE.equals(schema.getType())
630+
&& SchemaTypeUtil.BINARY_FORMAT.equals(schema.getFormat()));
683631
}
684632

685633
public static boolean isFileSchema(Schema schema) {
686-
if (schema instanceof FileSchema) {
687-
return true;
688-
}
689-
// file type in oas2 mapped to binary in oas3
690-
return isBinarySchema(schema);
634+
return (schema instanceof FileSchema) ||
635+
// file type in oas2 mapped to binary in oas3
636+
isBinarySchema(schema);
691637
}
692638

693639
public static boolean isUUIDSchema(Schema schema) {
694-
if (schema instanceof UUIDSchema) {
695-
return true;
696-
}
697-
// format: uuid
698-
return SchemaTypeUtil.STRING_TYPE.equals(schema.getType())
699-
&& SchemaTypeUtil.UUID_FORMAT.equals(schema.getFormat());
640+
return (schema instanceof UUIDSchema) ||
641+
// format: uuid
642+
(SchemaTypeUtil.STRING_TYPE.equals(schema.getType())
643+
&& SchemaTypeUtil.UUID_FORMAT.equals(schema.getFormat()));
700644
}
701645

702646
public static boolean isURISchema(Schema schema) {
@@ -706,12 +650,10 @@ public static boolean isURISchema(Schema schema) {
706650
}
707651

708652
public static boolean isEmailSchema(Schema schema) {
709-
if (schema instanceof EmailSchema) {
710-
return true;
711-
}
712-
// format: email
713-
return SchemaTypeUtil.STRING_TYPE.equals(schema.getType())
714-
&& SchemaTypeUtil.EMAIL_FORMAT.equals(schema.getFormat());
653+
return (schema instanceof EmailSchema) ||
654+
// format: email
655+
(SchemaTypeUtil.STRING_TYPE.equals(schema.getType())
656+
&& SchemaTypeUtil.EMAIL_FORMAT.equals(schema.getFormat()));
715657
}
716658

717659
public static boolean isDecimalSchema(Schema schema) {
@@ -727,17 +669,11 @@ public static boolean isDecimalSchema(Schema schema) {
727669
* @return true if it's a model with at least one properties
728670
*/
729671
public static boolean isModel(Schema schema) {
730-
if (schema == null) {
731-
return false;
732-
}
733-
734-
// has properties
735-
if (null != schema.getProperties() && !schema.getProperties().isEmpty()) {
736-
return true;
737-
}
738-
739-
// composed schema is a model, consider very simple ObjectSchema a model
740-
return schema instanceof ComposedSchema || schema instanceof ObjectSchema;
672+
return (schema != null) &&
673+
// has properties
674+
((null != schema.getProperties() && !schema.getProperties().isEmpty())
675+
// composed schema is a model, consider very simple ObjectSchema a model
676+
|| (schema instanceof ComposedSchema || schema instanceof ObjectSchema));
741677
}
742678

743679
/**
@@ -747,16 +683,12 @@ public static boolean isModel(Schema schema) {
747683
* @return true if it's a model with at least one properties
748684
*/
749685
public static boolean isModelWithPropertiesOnly(Schema schema) {
750-
if (schema == null) {
751-
return false;
752-
}
753-
754-
if (null != schema.getProperties() && !schema.getProperties().isEmpty() && // has properties
755-
(schema.getAdditionalProperties() == null || // no additionalProperties is set
756-
(schema.getAdditionalProperties() instanceof Boolean && !(Boolean) schema.getAdditionalProperties()))) {
757-
return true;
758-
}
759-
return false;
686+
return (schema != null) &&
687+
// has properties
688+
(null != schema.getProperties() && !schema.getProperties().isEmpty()) &&
689+
// no additionalProperties is set
690+
(schema.getAdditionalProperties() == null ||
691+
(schema.getAdditionalProperties() instanceof Boolean && !(Boolean) schema.getAdditionalProperties()));
760692
}
761693

762694
public static boolean hasValidation(Schema sc) {
@@ -967,11 +899,7 @@ public static ApiResponse getReferencedApiResponse(OpenAPI openAPI, ApiResponse
967899
}
968900

969901
public static ApiResponse getApiResponse(OpenAPI openAPI, String name) {
970-
if (name == null) {
971-
return null;
972-
}
973-
974-
if (openAPI != null && openAPI.getComponents() != null && openAPI.getComponents().getResponses() != null) {
902+
if (name != null && openAPI != null && openAPI.getComponents() != null && openAPI.getComponents().getResponses() != null) {
975903
return openAPI.getComponents().getResponses().get(name);
976904
}
977905
return null;
@@ -996,11 +924,7 @@ public static Parameter getReferencedParameter(OpenAPI openAPI, Parameter parame
996924
}
997925

998926
public static Parameter getParameter(OpenAPI openAPI, String name) {
999-
if (name == null) {
1000-
return null;
1001-
}
1002-
1003-
if (openAPI != null && openAPI.getComponents() != null && openAPI.getComponents().getParameters() != null) {
927+
if (name != null && openAPI != null && openAPI.getComponents() != null && openAPI.getComponents().getParameters() != null) {
1004928
return openAPI.getComponents().getParameters().get(name);
1005929
}
1006930
return null;
@@ -1025,11 +949,7 @@ public static Callback getReferencedCallback(OpenAPI openAPI, Callback callback)
1025949
}
1026950

1027951
public static Callback getCallback(OpenAPI openAPI, String name) {
1028-
if (name == null) {
1029-
return null;
1030-
}
1031-
1032-
if (openAPI != null && openAPI.getComponents() != null && openAPI.getComponents().getCallbacks() != null) {
952+
if (name != null && openAPI != null && openAPI.getComponents() != null && openAPI.getComponents().getCallbacks() != null) {
1033953
return openAPI.getComponents().getCallbacks().get(name);
1034954
}
1035955
return null;
@@ -1336,11 +1256,7 @@ public static Header getReferencedHeader(OpenAPI openAPI, Header header) {
13361256
}
13371257

13381258
public static Header getHeader(OpenAPI openAPI, String name) {
1339-
if (name == null) {
1340-
return null;
1341-
}
1342-
1343-
if (openAPI != null && openAPI.getComponents() != null && openAPI.getComponents().getHeaders() != null) {
1259+
if (name != null && openAPI != null && openAPI.getComponents() != null && openAPI.getComponents().getHeaders() != null) {
13441260
return openAPI.getComponents().getHeaders().get(name);
13451261
}
13461262
return null;
@@ -1538,17 +1454,17 @@ private static boolean hasOrInheritsDiscriminator(Schema schema, Map<String, Sch
15381454
public static boolean isExtensionParent(Schema schema) {
15391455
if (schema.getExtensions() == null) {
15401456
return false;
1457+
}
1458+
1459+
Object xParent = schema.getExtensions().get("x-parent");
1460+
if (xParent == null) {
1461+
return false;
1462+
} else if (xParent instanceof Boolean) {
1463+
return (Boolean) xParent;
1464+
} else if (xParent instanceof String) {
1465+
return StringUtils.isNotEmpty((String) xParent);
15411466
} else {
1542-
Object xParent = schema.getExtensions().get("x-parent");
1543-
if (xParent == null) {
1544-
return false;
1545-
} else if (xParent instanceof Boolean) {
1546-
return (Boolean) xParent;
1547-
} else if (xParent instanceof String) {
1548-
return StringUtils.isNotEmpty((String) xParent);
1549-
} else {
1550-
return false;
1551-
}
1467+
return false;
15521468
}
15531469
}
15541470

0 commit comments

Comments
 (0)