|
1 | 1 | package org.openapitools.codegen.validations.oas; |
2 | 2 |
|
| 3 | +import io.swagger.v3.oas.models.OpenAPI; |
3 | 4 | import io.swagger.v3.oas.models.media.*; |
| 5 | +import org.openapitools.codegen.TestUtils; |
4 | 6 | import org.openapitools.codegen.validation.Invalid; |
5 | 7 | import org.openapitools.codegen.validation.ValidationResult; |
6 | 8 | import org.testng.Assert; |
@@ -67,6 +69,51 @@ public void testOneOfWithSiblingPropertiesDisabledRule(Schema schema, boolean ma |
67 | 69 | Assert.assertEquals(warnings.size(), 0, "Expected rule to be disabled."); |
68 | 70 | } |
69 | 71 |
|
| 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 | + |
70 | 117 | @DataProvider(name = "apacheNginxRecommendationExpectations") |
71 | 118 | public Object[][] apacheNginxRecommendationExpectations() { |
72 | 119 | return new Object[][]{ |
|
0 commit comments