Skip to content

Commit 1a617ea

Browse files
authored
[Core] bug - fix OAS 3.1 nullable validation when using older "nullable: true" syntax instead of type: [null, ...] (#24026)
* add (for now failing) tests * fix check nullable implementation to trigger the warning * Revert "fix check nullable implementation to trigger the warning" This reverts commit 5e8ab24. * Reapply "fix check nullable implementation to trigger the warning" This reverts commit 5507fec. * remove one test * trigger warning on any value of nullable: in open api 3.1.0 version
1 parent c21a355 commit 1a617ea

3 files changed

Lines changed: 69 additions & 1 deletion

File tree

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,14 @@ private static ValidationRule.Result checkNullableAttribute(SchemaWrapper schema
120120
if (schemaWrapper.getOpenAPI() != null) {
121121
SemVer version = new SemVer(schemaWrapper.getOpenAPI().getOpenapi());
122122
if (version.atLeast("3.1")) {
123-
if (ModelUtils.isNullable(schema)) {
123+
// ModelUtils.isNullable checks schema.getNullable(), but swagger-parser does not populate
124+
// that field when parsing OAS 3.1 documents — 'nullable' is not a valid 3.1 keyword, so
125+
// the parser stores it as a raw extension under the key "nullable" instead.
126+
// We must check both paths to catch the deprecated usage in either case,
127+
// regardless of whether the value is true or false.
128+
boolean hasNullableExtension = schema.getExtensions() != null
129+
&& schema.getExtensions().containsKey("nullable");
130+
if (ModelUtils.isNullable(schema) || schema.getNullable() != null || hasNullableExtension) {
124131
result = new ValidationRule.Fail();
125132
result.setDetails(String.format(Locale.ROOT,
126133
"OAS document is version '%s'. Schema '%s' uses 'nullable' attribute, which has been deprecated in OAS 3.1.",

modules/openapi-generator/src/test/java/org/openapitools/codegen/validations/oas/OpenApiSchemaValidationsTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.openapitools.codegen.validations.oas;
22

3+
import io.swagger.v3.oas.models.OpenAPI;
34
import io.swagger.v3.oas.models.media.*;
5+
import org.openapitools.codegen.TestUtils;
46
import org.openapitools.codegen.validation.Invalid;
57
import org.openapitools.codegen.validation.ValidationResult;
68
import org.testng.Assert;
@@ -67,6 +69,51 @@ public void testOneOfWithSiblingPropertiesDisabledRule(Schema schema, boolean ma
6769
Assert.assertEquals(warnings.size(), 0, "Expected rule to be disabled.");
6870
}
6971

72+
/**
73+
* The validation warning for 'nullable: true' in an OAS 3.1 spec must fire.
74+
* The existing checkNullableAttribute only checked ModelUtils.isNullable(schema) which relies on
75+
* schema.getNullable() — but swagger-parser does not populate that field for 3.1 specs (it stores
76+
* the value in extensions["nullable"] instead). The fix must also check extensions["nullable"].
77+
*/
78+
@Test(description = "nullable: true in OAS 3.1 spec must trigger the nullable-deprecated warning")
79+
public void testNullableAttributeInOas31_triggerWarning() {
80+
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_1/nullable-deprecated-in-oas31.yaml");
81+
Schema<?> proxyUrl = (Schema<?>) openAPI.getComponents().getSchemas().get("TestModel").getProperties().get("proxyUrl");
82+
83+
RuleConfiguration config = new RuleConfiguration();
84+
config.setEnableRecommendations(true);
85+
OpenApiSchemaValidations validator = new OpenApiSchemaValidations(config);
86+
87+
ValidationResult result = validator.validate(new SchemaWrapper(openAPI, proxyUrl));
88+
List<Invalid> nullableWarnings = result.getWarnings().stream()
89+
.filter(invalid -> "Schema uses the 'nullable' attribute.".equals(invalid.getRule().getDescription()))
90+
.collect(Collectors.toList());
91+
92+
Assert.assertEquals(nullableWarnings.size(), 1,
93+
"Expected exactly one 'nullable attribute deprecated' warning for a 3.1 spec using nullable: true");
94+
}
95+
96+
/**
97+
* The nullable-deprecated warning must NOT fire for an OAS 3.1 spec using the correct 3.1 null type syntax.
98+
*/
99+
@Test(description = "correct OAS 3.1 null type syntax (type: [string, null]) must NOT trigger the nullable-deprecated warning")
100+
public void testNullTypeInOas31_noWarning() {
101+
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_1/null-types-simple.yaml");
102+
Schema<?> stringDataOrNull = (Schema<?>) openAPI.getComponents().getSchemas().get("WithNullableType").getProperties().get("stringDataOrNull");
103+
104+
RuleConfiguration config = new RuleConfiguration();
105+
config.setEnableRecommendations(true);
106+
OpenApiSchemaValidations validator = new OpenApiSchemaValidations(config);
107+
108+
ValidationResult result = validator.validate(new SchemaWrapper(openAPI, stringDataOrNull));
109+
List<Invalid> nullableWarnings = result.getWarnings().stream()
110+
.filter(invalid -> "Schema uses the 'nullable' attribute.".equals(invalid.getRule().getDescription()))
111+
.collect(Collectors.toList());
112+
113+
Assert.assertEquals(nullableWarnings.size(), 0,
114+
"OAS 3.1 with type:[string,null] must not trigger the nullable-deprecated warning");
115+
}
116+
70117
@DataProvider(name = "apacheNginxRecommendationExpectations")
71118
public Object[][] apacheNginxRecommendationExpectations() {
72119
return new Object[][]{
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
openapi: 3.1.0
2+
info:
3+
title: Nullable deprecated in OAS 3.1 test
4+
version: 1.0.0
5+
paths: {}
6+
components:
7+
schemas:
8+
TestModel:
9+
type: object
10+
properties:
11+
# OAS 3.0 'nullable: true' used in a 3.1 spec — deprecated and should trigger a warning
12+
proxyUrl:
13+
type: string
14+
nullable: true

0 commit comments

Comments
 (0)