@@ -82,6 +82,11 @@ public class OpenAPINormalizer {
8282 final String ADD_UNSIGNED_TO_INTEGER_WITH_INVALID_MAX_VALUE = "ADD_UNSIGNED_TO_INTEGER_WITH_INVALID_MAX_VALUE" ;
8383 boolean addUnsignedToIntegerWithInvalidMaxValue ;
8484
85+ // when set to true, refactor schema with allOf and properties in the same level to a schema with allOf only and
86+ // the allOf contains a new schema containing the properties in the top level
87+ final String REFACTOR_ALLOF_WITH_PROPERTIES_ONLY = "REFACTOR_ALLOF_WITH_PROPERTIES_ONLY" ;
88+ boolean refactorAllOfWithPropertiesOnly ;
89+
8590 // ============= end of rules =============
8691
8792 /**
@@ -141,6 +146,10 @@ public void parseRules(Map<String, String> rules) {
141146 if (enableAll || "true" .equalsIgnoreCase (rules .get (ADD_UNSIGNED_TO_INTEGER_WITH_INVALID_MAX_VALUE ))) {
142147 addUnsignedToIntegerWithInvalidMaxValue = true ;
143148 }
149+
150+ if (enableAll || "true" .equalsIgnoreCase (rules .get (REFACTOR_ALLOF_WITH_PROPERTIES_ONLY ))) {
151+ refactorAllOfWithPropertiesOnly = true ;
152+ }
144153 }
145154
146155 /**
@@ -346,6 +355,9 @@ public Schema normalizeSchema(Schema schema, Set<Schema> visitedSchemas) {
346355 return normalizeOneOf (schema , visitedSchemas );
347356 } else if (ModelUtils .isAnyOf (schema )) { // anyOf
348357 return normalizeAnyOf (schema , visitedSchemas );
358+ } else if (ModelUtils .isAllOfWithProperties (schema )) { // allOf with properties
359+ schema = normalizeAllOfWithProperties (schema , visitedSchemas );
360+ normalizeSchema (schema , visitedSchemas );
349361 } else if (ModelUtils .isAllOf (schema )) { // allOf
350362 return normalizeAllOf (schema , visitedSchemas );
351363 } else if (ModelUtils .isComposedSchema (schema )) { // composed schema
@@ -427,6 +439,20 @@ private Schema normalizeAllOf(Schema schema, Set<Schema> visitedSchemas) {
427439 return schema ;
428440 }
429441
442+ private Schema normalizeAllOfWithProperties (Schema schema , Set <Schema > visitedSchemas ) {
443+ for (Object item : schema .getAllOf ()) {
444+ if (!(item instanceof Schema )) {
445+ throw new RuntimeException ("Error! allOf schema is not of the type Schema: " + item );
446+ }
447+ // normalize allOf sub schemas one by one
448+ normalizeSchema ((Schema ) item , visitedSchemas );
449+ }
450+ // process rules here
451+ schema = processRefactorAllOfWithPropertiesOnly (schema );
452+
453+ return schema ;
454+ }
455+
430456 private Schema normalizeOneOf (Schema schema , Set <Schema > visitedSchemas ) {
431457 for (Object item : schema .getOneOf ()) {
432458 if (item == null ) {
@@ -759,5 +785,46 @@ private void processAddUnsignedToIntegerWithInvalidMaxValue(Schema schema) {
759785 }
760786 }
761787
788+ /*
789+ * When set to true, refactor schema with allOf and properties in the same level to a schema with allOf only and
790+ * the allOf contains a new schema containing the properties in the top level.
791+ *
792+ * @param schema Schema
793+ * @return Schema
794+ */
795+ private Schema processRefactorAllOfWithPropertiesOnly (Schema schema ) {
796+ if (!refactorAllOfWithPropertiesOnly && !enableAll ) {
797+ return schema ;
798+ }
799+
800+ ObjectSchema os = new ObjectSchema ();
801+ // set the properties, etc of the new schema to the properties of schema
802+ os .setProperties (schema .getProperties ());
803+ os .setRequired (schema .getRequired ());
804+ os .setAdditionalProperties (schema .getAdditionalProperties ());
805+ os .setNullable (schema .getNullable ());
806+ os .setDescription (schema .getDescription ());
807+ os .setDeprecated (schema .getDeprecated ());
808+ os .setExample (schema .getExample ());
809+ os .setExamples (schema .getExamples ());
810+ os .setTitle (schema .getTitle ());
811+ schema .getAllOf ().add (os ); // move new schema as a child schema of allOf
812+ // clean up by removing properties, etc
813+ schema .setProperties (null );
814+ schema .setRequired (null );
815+ schema .setAdditionalProperties (null );
816+ schema .setNullable (null );
817+ schema .setDescription (null );
818+ schema .setDeprecated (null );
819+ schema .setExample (null );
820+ schema .setExamples (null );
821+ schema .setTitle (null );
822+
823+ // at this point the schema becomes a simple allOf (no properties) with an additional schema containing
824+ // the properties
825+
826+ return schema ;
827+ }
828+
762829 // ===================== end of rules =====================
763830}
0 commit comments