diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java index 9875ffdafa5..8b39c618816 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java @@ -951,68 +951,7 @@ public class OpenAPINormalizer { return result; } - /** - * Check if the schema is of type 'null' or schema itself is pointing to null - *
- * Return true if the schema's type is 'null' or not specified - * - * @param schema Schema - * @param openAPI OpenAPI - * - * @return true if schema is null type - */ - public boolean isNullTypeSchema(OpenAPI openAPI, Schema schema) { - if (schema == null) { - return true; - } - // dereference the schema - schema = ModelUtils.getReferencedSchema(openAPI, schema); - - // allOf/anyOf/oneOf - if (ModelUtils.hasAllOf(schema) || ModelUtils.hasOneOf(schema) || ModelUtils.hasAnyOf(schema)) { - return false; - } - - // schema with properties - if (schema.getProperties() != null) { - return false; - } - - // convert referenced enum of null only to `nullable:true` - if (schema.getEnum() != null && schema.getEnum().size() == 1) { - if ("null".equals(String.valueOf(schema.getEnum().get(0)))) { - return true; - } - } - - if (schema.getTypes() != null && !schema.getTypes().isEmpty()) { - // 3.1 spec - if (schema.getTypes().size() == 1) { // 1 type only - String type = (String) schema.getTypes().iterator().next(); - return type == null || "null".equals(type); - } else { // more than 1 type so must not be just null - return false; - } - } - - if (schema instanceof JsonSchema) { // 3.1 spec - if (Boolean.TRUE.equals(schema.getNullable())) { - return true; - } - - // for `type: null` - if (schema.getTypes() == null && schema.get$ref() == null) { - return true; - } - } else { // 3.0.x or 2.x spec - if ((schema.getType() == null || schema.getType().equals("null")) && schema.get$ref() == null) { - return true; - } - } - - return false; - } /** * If the schema is oneOf and the sub-schemas is null, set `nullable: true` @@ -1056,7 +995,7 @@ public class OpenAPINormalizer { } } - if (oneOfSchemas.removeIf(oneOf -> isNullTypeSchema(openAPI, oneOf))) { + if (oneOfSchemas.removeIf(oneOf -> ModelUtils.isNullTypeSchema(openAPI, oneOf))) { schema.setNullable(true); // if only one element left, simplify to just the element (schema) @@ -1192,7 +1131,7 @@ public class OpenAPINormalizer { } } - if (anyOfSchemas.removeIf(anyOf -> isNullTypeSchema(openAPI, anyOf))) { + if (anyOfSchemas.removeIf(anyOf -> ModelUtils.isNullTypeSchema(openAPI, anyOf))) { schema.setNullable(true); } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java index 7734adfdec3..a4ea495f755 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java @@ -2199,6 +2199,69 @@ public class ModelUtils { } } + /** + * Check if the schema is of type 'null' or schema itself is pointing to null + *
+ * Return true if the schema's type is 'null' or not specified
+ *
+ * @param schema Schema
+ * @param openAPI OpenAPI
+ *
+ * @return true if schema is null type
+ */
+ public static boolean isNullTypeSchema(OpenAPI openAPI, Schema schema) {
+ if (schema == null) {
+ return true;
+ }
+
+ // dereference the schema
+ schema = ModelUtils.getReferencedSchema(openAPI, schema);
+
+ // allOf/anyOf/oneOf
+ if (ModelUtils.hasAllOf(schema) || ModelUtils.hasOneOf(schema) || ModelUtils.hasAnyOf(schema)) {
+ return false;
+ }
+
+ // schema with properties
+ if (schema.getProperties() != null) {
+ return false;
+ }
+
+ // convert referenced enum of null only to `nullable:true`
+ if (schema.getEnum() != null && schema.getEnum().size() == 1) {
+ if ("null".equals(String.valueOf(schema.getEnum().get(0)))) {
+ return true;
+ }
+ }
+
+ if (schema.getTypes() != null && !schema.getTypes().isEmpty()) {
+ // 3.1 spec
+ if (schema.getTypes().size() == 1) { // 1 type only
+ String type = (String) schema.getTypes().iterator().next();
+ return type == null || "null".equals(type);
+ } else { // more than 1 type so must not be just null
+ return false;
+ }
+ }
+
+ if (schema instanceof JsonSchema) { // 3.1 spec
+ if (Boolean.TRUE.equals(schema.getNullable())) {
+ return true;
+ }
+
+ // for `type: null`
+ if (schema.getTypes() == null && schema.get$ref() == null) {
+ return true;
+ }
+ } else { // 3.0.x or 2.x spec
+ if ((schema.getType() == null || schema.getType().equals("null")) && schema.get$ref() == null) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
@FunctionalInterface
private interface OpenAPISchemaVisitor {
diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java
index 399076d79b3..442841c5dd6 100644
--- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java
+++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java
@@ -107,15 +107,6 @@ public class OpenAPINormalizerTest {
assertTrue(schema3.getEnum().size() > 0);
}
- @Test
- public void isNullTypeSchemaTest() {
- OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/simplifyOneOfAnyOf_test.yaml");
- Map