diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/validations/oas/OpenApiSchemaValidations.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/validations/oas/OpenApiSchemaValidations.java index 1c2a68ef4e09..86a5d5399c1a 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/validations/oas/OpenApiSchemaValidations.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/validations/oas/OpenApiSchemaValidations.java @@ -96,13 +96,9 @@ private static ValidationRule.Result checkNullType(SchemaWrapper schemaWrapper) // OAS spec is 3.0.x if (ModelUtils.isNullType(schema)) { result = new ValidationRule.Fail(); - String name = schema.getName(); - if (name == null) { - name = schema.getTitle(); - } result.setDetails(String.format(Locale.ROOT, "Schema '%s' uses a 'null' type, which is specified in OAS 3.1 and above, but OAS document is version %s", - name, schemaWrapper.getOpenAPI().getOpenapi())); + nameOf(schema), schemaWrapper.getOpenAPI().getOpenapi())); return result; } } @@ -126,13 +122,9 @@ private static ValidationRule.Result checkNullableAttribute(SchemaWrapper schema if (version.atLeast("3.1")) { if (ModelUtils.isNullable(schema)) { result = new ValidationRule.Fail(); - String name = schema.getName(); - if (name == null) { - name = schema.getTitle(); - } result.setDetails(String.format(Locale.ROOT, "OAS document is version '%s'. Schema '%s' uses 'nullable' attribute, which has been deprecated in OAS 3.1.", - schemaWrapper.getOpenAPI().getOpenapi(), name)); + schemaWrapper.getOpenAPI().getOpenapi(), nameOf(schema))); return result; } } @@ -140,6 +132,10 @@ private static ValidationRule.Result checkNullableAttribute(SchemaWrapper schema return result; } + private static String nameOf(Schema schema) { + return schema.getName() != null ? schema.getName() : schema.getTitle(); + } + // The set of valid OAS values for the 'type' attribute. private static Set validTypes = new HashSet( Arrays.asList("null", "boolean", "object", "array", "number", "string", "integer")); @@ -157,13 +153,9 @@ private static ValidationRule.Result checkInvalidType(SchemaWrapper schemaWrappe ValidationRule.Result result = ValidationRule.Pass.empty(); if (schema.getType() != null && !validTypes.contains(schema.getType())) { result = new ValidationRule.Fail(); - String name = schema.getName(); - if (name == null) { - name = schema.getTitle(); - } result.setDetails(String.format(Locale.ROOT, "Schema '%s' uses the '%s' type, which is not a valid type.", - name, schema.getType())); + nameOf(schema), schema.getType())); return result; } return result;