Skip to content

Commit 8772729

Browse files
committed
refactor: simplify schema name retrieval in validation messages
Signed-off-by: Zhiwei Liang <zhiwei.liang@zliang.me>
1 parent 05dd7fb commit 8772729

File tree

1 file changed

+7
-15
lines changed

1 file changed

+7
-15
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/validations/oas/OpenApiSchemaValidations.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,9 @@ private static ValidationRule.Result checkNullType(SchemaWrapper schemaWrapper)
9696
// OAS spec is 3.0.x
9797
if (ModelUtils.isNullType(schema)) {
9898
result = new ValidationRule.Fail();
99-
String name = schema.getName();
100-
if (name == null) {
101-
name = schema.getTitle();
102-
}
10399
result.setDetails(String.format(Locale.ROOT,
104100
"Schema '%s' uses a 'null' type, which is specified in OAS 3.1 and above, but OAS document is version %s",
105-
name, schemaWrapper.getOpenAPI().getOpenapi()));
101+
nameOf(schema), schemaWrapper.getOpenAPI().getOpenapi()));
106102
return result;
107103
}
108104
}
@@ -126,20 +122,20 @@ private static ValidationRule.Result checkNullableAttribute(SchemaWrapper schema
126122
if (version.atLeast("3.1")) {
127123
if (ModelUtils.isNullable(schema)) {
128124
result = new ValidationRule.Fail();
129-
String name = schema.getName();
130-
if (name == null) {
131-
name = schema.getTitle();
132-
}
133125
result.setDetails(String.format(Locale.ROOT,
134126
"OAS document is version '%s'. Schema '%s' uses 'nullable' attribute, which has been deprecated in OAS 3.1.",
135-
schemaWrapper.getOpenAPI().getOpenapi(), name));
127+
schemaWrapper.getOpenAPI().getOpenapi(), nameOf(schema)));
136128
return result;
137129
}
138130
}
139131
}
140132
return result;
141133
}
142134

135+
private static String nameOf(Schema schema) {
136+
return schema.getName() != null ? schema.getName() : schema.getTitle();
137+
}
138+
143139
// The set of valid OAS values for the 'type' attribute.
144140
private static Set<String> validTypes = new HashSet<String>(
145141
Arrays.asList("null", "boolean", "object", "array", "number", "string", "integer"));
@@ -157,13 +153,9 @@ private static ValidationRule.Result checkInvalidType(SchemaWrapper schemaWrappe
157153
ValidationRule.Result result = ValidationRule.Pass.empty();
158154
if (schema.getType() != null && !validTypes.contains(schema.getType())) {
159155
result = new ValidationRule.Fail();
160-
String name = schema.getName();
161-
if (name == null) {
162-
name = schema.getTitle();
163-
}
164156
result.setDetails(String.format(Locale.ROOT,
165157
"Schema '%s' uses the '%s' type, which is not a valid type.",
166-
name, schema.getType()));
158+
nameOf(schema), schema.getType()));
167159
return result;
168160
}
169161
return result;

0 commit comments

Comments
 (0)