@@ -963,36 +963,75 @@ protected Schema normalizeAllOfWithProperties(Schema schema, Set<Schema> visited
963963 return schema ;
964964 }
965965
966+ protected Schema simplifyNullableOneOf (Schema schema ) {
967+
968+ if (schema .getOneOf () == null || schema .getOneOf ().size () != 2 ) {
969+ // Not the pattern we are looking for, so do nothing.
970+ return schema ;
971+ }
972+
973+ List <Schema > subSchemas = new ArrayList <>(schema .getOneOf ());
974+ Schema nullSchema = null ;
975+ Schema mainSchema = null ;
976+
977+ // Identify the null schema and the main schema
978+ if (ModelUtils .isNullTypeSchema (openAPI , subSchemas .get (0 ))) {
979+ nullSchema = subSchemas .get (0 );
980+ mainSchema = subSchemas .get (1 );
981+ } else if (ModelUtils .isNullTypeSchema (openAPI , subSchemas .get (1 ))) {
982+ nullSchema = subSchemas .get (1 );
983+ mainSchema = subSchemas .get (0 );
984+ } else {
985+ // Pattern does not contain a null type, so do nothing.
986+ return schema ;
987+ }
988+
989+ // Clone the main schema to avoid modifying the original component
990+ Schema result = ModelUtils .cloneSchema (mainSchema );
991+
992+ // Set nullable to true on the result
993+ result .setNullable (true );
994+
995+ // Copy metadata from the original container schema to the result
996+ ModelUtils .copyMetadata (schema , result );
997+
998+ LOGGER .debug ("Simplified nullable oneOf schema." );
999+ return result ;
1000+ }
1001+
9661002 protected Schema normalizeOneOf (Schema schema , Set <Schema > visitedSchemas ) {
9671003 // Remove duplicate oneOf entries
9681004 ModelUtils .deduplicateOneOfSchema (schema );
9691005
970- schema = processSimplifyOneOfEnum (schema );
971-
972- // simplify first as the schema may no longer be a oneOf after processing the rule below
973- schema = processSimplifyOneOf (schema );
1006+ // Try to simplify oneOf with a nullable type first
1007+ schema = simplifyNullableOneOf (schema );
9741008
975- // if it's still a oneOf, loop through the sub-schemas
1009+ // if it's still a oneOf after simplification, continue with other rules
9761010 if (schema .getOneOf () != null ) {
977- for (int i = 0 ; i < schema .getOneOf ().size (); i ++) {
978- // normalize oneOf sub schemas one by one
979- Object item = schema .getOneOf ().get (i );
980-
1011+ schema = processSimplifyOneOfEnum (schema );
1012+ // simplify first as the schema may no longer be a oneOf after processing the rule below
1013+ schema = processSimplifyOneOf (schema );
1014+
1015+ if (schema .getOneOf () != null ) {
1016+ for (int i = 0 ; i < schema .getOneOf ().size (); i ++) {
1017+ // normalize oneOf sub schemas one by one
1018+ Object item = schema .getOneOf ().get (i );
1019+ }
9811020 if (item == null ) {
982- continue ;
1021+ continue ;
9831022 }
9841023 if (!(item instanceof Schema )) {
985- throw new RuntimeException ("Error! oneOf schema is not of the type Schema: " + item );
986- }
987-
988- // update sub-schema with the updated schema
1024+ throw new RuntimeException ("Error! oneOf schema is not of the type Schema: " + item );
9891025 schema .getOneOf ().set (i , normalizeSchema ((Schema ) item , visitedSchemas ));
9901026 }
9911027 } else {
992- // normalize it as it's no longer an oneOf
1028+ // normalize it as it's no longer a oneOf
9931029 schema = normalizeSchema (schema , visitedSchemas );
9941030 }
995-
1031+ } else {
1032+ // It was simplified and is no longer a oneOf, so normalize it as a simple schema
1033+ return normalizeSchema (schema , visitedSchemas );
1034+ }
9961035 return schema ;
9971036 }
9981037
0 commit comments