@@ -642,22 +642,29 @@ private Schema normalizeAllOfWithProperties(Schema schema, Set<Schema> visitedSc
642
642
}
643
643
644
644
private Schema normalizeOneOf (Schema schema , Set <Schema > visitedSchemas ) {
645
- for (int i = 0 ; i < schema .getOneOf ().size (); i ++) {
646
- // normalize oneOf sub schemas one by one
647
- Object item = schema .getOneOf ().get (i );
645
+ // simplify first as the schema may no longer be a oneOf after processing the rule below
646
+ schema = processSimplifyOneOf (schema );
648
647
649
- if (item == null ) {
650
- continue ;
651
- }
652
- if (!(item instanceof Schema )) {
653
- throw new RuntimeException ("Error! oneOf schema is not of the type Schema: " + item );
654
- }
648
+ // if it's still a oneOf, loop through the sub-schemas
649
+ if (schema .getOneOf () != null ) {
650
+ for (int i = 0 ; i < schema .getOneOf ().size (); i ++) {
651
+ // normalize oneOf sub schemas one by one
652
+ Object item = schema .getOneOf ().get (i );
655
653
656
- // update sub-schema with the updated schema
657
- schema .getOneOf ().set (i , normalizeSchema ((Schema ) item , visitedSchemas ));
654
+ if (item == null ) {
655
+ continue ;
656
+ }
657
+ if (!(item instanceof Schema )) {
658
+ throw new RuntimeException ("Error! oneOf schema is not of the type Schema: " + item );
659
+ }
660
+
661
+ // update sub-schema with the updated schema
662
+ schema .getOneOf ().set (i , normalizeSchema ((Schema ) item , visitedSchemas ));
663
+ }
664
+ } else {
665
+ // normalize it as it's no longer an oneOf
666
+ schema = normalizeSchema (schema , visitedSchemas );
658
667
}
659
- // process rules here
660
- schema = processSimplifyOneOf (schema );
661
668
662
669
return schema ;
663
670
}
@@ -683,7 +690,7 @@ private Schema normalizeAnyOf(Schema schema, Set<Schema> visitedSchemas) {
683
690
schema = processSimplifyAnyOf (schema );
684
691
685
692
// last rule to process as the schema may become String schema (not "anyOf") after the completion
686
- return processSimplifyAnyOfStringAndEnumString (schema );
693
+ return normalizeSchema ( processSimplifyAnyOfStringAndEnumString (schema ), visitedSchemas );
687
694
}
688
695
689
696
private Schema normalizeComplexComposedSchema (Schema schema , Set <Schema > visitedSchemas ) {
@@ -694,7 +701,7 @@ private Schema normalizeComplexComposedSchema(Schema schema, Set<Schema> visited
694
701
695
702
processRemoveAnyOfOneOfAndKeepPropertiesOnly (schema );
696
703
697
- return schema ;
704
+ return normalizeSchema ( schema , visitedSchemas ) ;
698
705
}
699
706
700
707
// ===================== a list of rules =====================
@@ -893,21 +900,40 @@ private Schema processSimplifyAnyOfStringAndEnumString(Schema schema) {
893
900
}
894
901
895
902
/**
896
- * Check if the schema is of type 'null'
903
+ * Check if the schema is of type 'null' or schema itself is pointing to null
897
904
* <p>
898
905
* Return true if the schema's type is 'null' or not specified
899
906
*
900
907
* @param schema Schema
908
+ * @param openAPI OpenAPI
909
+ *
910
+ * @return true if schema is null type
901
911
*/
902
- public boolean isNullTypeSchema (Schema schema ) {
912
+ public boolean isNullTypeSchema (OpenAPI openAPI , Schema schema ) {
903
913
if (schema == null ) {
904
914
return true ;
905
915
}
906
916
917
+ // dereference the schema
918
+ schema = ModelUtils .getReferencedSchema (openAPI , schema );
919
+
920
+ // allOf/anyOf/oneOf
907
921
if (ModelUtils .hasAllOf (schema ) || ModelUtils .hasOneOf (schema ) || ModelUtils .hasAnyOf (schema )) {
908
922
return false ;
909
923
}
910
924
925
+ // schema with properties
926
+ if (schema .getProperties () != null ) {
927
+ return false ;
928
+ }
929
+
930
+ // convert referenced enum of null only to `nullable:true`
931
+ if (schema .getEnum () != null && schema .getEnum ().size () == 1 ) {
932
+ if ("null" .equals (String .valueOf (schema .getEnum ().get (0 )))) {
933
+ return true ;
934
+ }
935
+ }
936
+
911
937
if (schema .getTypes () != null && !schema .getTypes ().isEmpty ()) {
912
938
// 3.1 spec
913
939
if (schema .getTypes ().size () == 1 ) { // 1 type only
@@ -933,14 +959,6 @@ public boolean isNullTypeSchema(Schema schema) {
933
959
}
934
960
}
935
961
936
- // convert referenced enum of null only to `nullable:true`
937
- Schema referencedSchema = ModelUtils .getReferencedSchema (openAPI , schema );
938
- if (referencedSchema .getEnum () != null && referencedSchema .getEnum ().size () == 1 ) {
939
- if ("null" .equals (String .valueOf (referencedSchema .getEnum ().get (0 )))) {
940
- return true ;
941
- }
942
- }
943
-
944
962
return false ;
945
963
}
946
964
@@ -986,7 +1004,7 @@ private Schema processSimplifyOneOf(Schema schema) {
986
1004
}
987
1005
}
988
1006
989
- if (oneOfSchemas .removeIf (oneOf -> isNullTypeSchema (oneOf ))) {
1007
+ if (oneOfSchemas .removeIf (oneOf -> isNullTypeSchema (openAPI , oneOf ))) {
990
1008
schema .setNullable (true );
991
1009
992
1010
// if only one element left, simplify to just the element (schema)
@@ -997,6 +1015,11 @@ private Schema processSimplifyOneOf(Schema schema) {
997
1015
return (Schema ) oneOfSchemas .get (0 );
998
1016
}
999
1017
}
1018
+
1019
+ if (ModelUtils .isIntegerSchema (schema ) || ModelUtils .isNumberSchema (schema ) || ModelUtils .isStringSchema (schema )) {
1020
+ // TODO convert oneOf const to enum
1021
+ schema .setOneOf (null );
1022
+ }
1000
1023
}
1001
1024
1002
1025
return schema ;
@@ -1117,7 +1140,7 @@ private Schema processSimplifyAnyOf(Schema schema) {
1117
1140
}
1118
1141
}
1119
1142
1120
- if (anyOfSchemas .removeIf (anyOf -> isNullTypeSchema (anyOf ))) {
1143
+ if (anyOfSchemas .removeIf (anyOf -> isNullTypeSchema (openAPI , anyOf ))) {
1121
1144
schema .setNullable (true );
1122
1145
}
1123
1146
0 commit comments