-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
feat(validation): warn when schema default value is not in enum #23429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ChihweiLHBird
wants to merge
1
commit into
OpenAPITools:master
Choose a base branch
from
ChihweiLHBird:zhiwei/validate-default-not-in-enum
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
286 changes: 286 additions & 0 deletions
286
...enerator/src/test/java/org/openapitools/codegen/validations/oas/OpenApiEvaluatorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,286 @@ | ||
| package org.openapitools.codegen.validations.oas; | ||
|
|
||
| import io.swagger.v3.oas.models.Components; | ||
| import io.swagger.v3.oas.models.OpenAPI; | ||
| import io.swagger.v3.oas.models.PathItem; | ||
| import io.swagger.v3.oas.models.Paths; | ||
| import io.swagger.v3.oas.models.Operation; | ||
| import io.swagger.v3.oas.models.media.Content; | ||
| import io.swagger.v3.oas.models.media.IntegerSchema; | ||
| import io.swagger.v3.oas.models.media.MediaType; | ||
| import io.swagger.v3.oas.models.media.ObjectSchema; | ||
| import io.swagger.v3.oas.models.media.StringSchema; | ||
| import io.swagger.v3.oas.models.parameters.RequestBody; | ||
| import io.swagger.v3.oas.models.responses.ApiResponse; | ||
| import io.swagger.v3.oas.models.responses.ApiResponses; | ||
| import org.openapitools.codegen.validation.Invalid; | ||
| import org.openapitools.codegen.validation.ValidationResult; | ||
| import org.testng.Assert; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class OpenApiEvaluatorTest { | ||
|
|
||
| private static OpenAPI buildSpecWithEnumDefault(List<?> enumValues, Object defaultValue) { | ||
| OpenAPI openAPI = new OpenAPI(); | ||
| openAPI.openapi("3.0.1"); | ||
| Components components = new Components(); | ||
| ObjectSchema obj = new ObjectSchema(); | ||
| StringSchema prop = new StringSchema(); | ||
| prop.setEnum(enumValues.stream() | ||
| .filter(v -> v instanceof String) | ||
| .map(v -> (String) v) | ||
| .collect(Collectors.toList())); | ||
| prop.setDefault(defaultValue); | ||
| obj.addProperty("protocol", prop); | ||
| components.addSchemas("Config", obj); | ||
| openAPI.setComponents(components); | ||
| return openAPI; | ||
| } | ||
|
|
||
| private static List<Invalid> getDefaultNotInEnumWarnings(ValidationResult result) { | ||
| return result.getWarnings().stream() | ||
| .filter(i -> i.getMessage().contains("not in enum")) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| @Test(description = "warn when default is not in enum") | ||
| public void testDefaultNotInEnum() { | ||
| RuleConfiguration config = new RuleConfiguration(); | ||
| config.setEnableRecommendations(true); | ||
| OpenApiEvaluator evaluator = new OpenApiEvaluator(config); | ||
|
|
||
| OpenAPI openAPI = buildSpecWithEnumDefault(Arrays.asList("udp", "tcp"), "http"); | ||
| ValidationResult result = evaluator.validate(openAPI); | ||
|
|
||
| List<Invalid> warnings = getDefaultNotInEnumWarnings(result); | ||
| Assert.assertEquals(warnings.size(), 1); | ||
| Assert.assertTrue(warnings.get(0).getMessage().contains("'http'")); | ||
| Assert.assertTrue(warnings.get(0).getMessage().contains("[udp, tcp]")); | ||
| } | ||
|
|
||
| @Test(description = "no warning when default is in enum") | ||
| public void testDefaultInEnum() { | ||
| RuleConfiguration config = new RuleConfiguration(); | ||
| config.setEnableRecommendations(true); | ||
| OpenApiEvaluator evaluator = new OpenApiEvaluator(config); | ||
|
|
||
| OpenAPI openAPI = buildSpecWithEnumDefault(Arrays.asList("http", "https"), "http"); | ||
| ValidationResult result = evaluator.validate(openAPI); | ||
|
|
||
| List<Invalid> warnings = getDefaultNotInEnumWarnings(result); | ||
| Assert.assertEquals(warnings.size(), 0); | ||
| } | ||
|
|
||
| @Test(description = "no warning when rule is disabled individually") | ||
| public void testDefaultNotInEnumDisabledRule() { | ||
| RuleConfiguration config = new RuleConfiguration(); | ||
| config.setEnableRecommendations(true); | ||
| config.setEnableDefaultNotInEnumRecommendation(false); | ||
| OpenApiEvaluator evaluator = new OpenApiEvaluator(config); | ||
|
|
||
| OpenAPI openAPI = buildSpecWithEnumDefault(Arrays.asList("udp"), "http"); | ||
| ValidationResult result = evaluator.validate(openAPI); | ||
|
|
||
| List<Invalid> warnings = getDefaultNotInEnumWarnings(result); | ||
| Assert.assertEquals(warnings.size(), 0); | ||
| } | ||
|
|
||
| @Test(description = "no warning when all recommendations are disabled") | ||
| public void testDefaultNotInEnumRecommendationsOff() { | ||
| RuleConfiguration config = new RuleConfiguration(); | ||
| config.setEnableRecommendations(false); | ||
| OpenApiEvaluator evaluator = new OpenApiEvaluator(config); | ||
|
|
||
| OpenAPI openAPI = buildSpecWithEnumDefault(Arrays.asList("udp"), "http"); | ||
| ValidationResult result = evaluator.validate(openAPI); | ||
|
|
||
| List<Invalid> warnings = getDefaultNotInEnumWarnings(result); | ||
| Assert.assertEquals(warnings.size(), 0); | ||
| } | ||
|
|
||
| @Test(description = "multiple schemas with default not in enum produce separate warnings") | ||
| public void testDefaultNotInEnumMultipleOccurrences() { | ||
| RuleConfiguration config = new RuleConfiguration(); | ||
| config.setEnableRecommendations(true); | ||
| OpenApiEvaluator evaluator = new OpenApiEvaluator(config); | ||
|
|
||
| OpenAPI openAPI = new OpenAPI(); | ||
| openAPI.openapi("3.0.1"); | ||
| Components components = new Components(); | ||
|
|
||
| ObjectSchema udpConfig = new ObjectSchema(); | ||
| StringSchema proto1 = new StringSchema(); | ||
| proto1.setEnum(Arrays.asList("udp")); | ||
| proto1.setDefault("http"); | ||
| udpConfig.addProperty("protocol", proto1); | ||
|
|
||
| ObjectSchema tcpConfig = new ObjectSchema(); | ||
| StringSchema proto2 = new StringSchema(); | ||
| proto2.setEnum(Arrays.asList("tcp")); | ||
| proto2.setDefault("http"); | ||
| tcpConfig.addProperty("protocol", proto2); | ||
|
|
||
| components.addSchemas("UdpConfig", udpConfig); | ||
| components.addSchemas("TcpConfig", tcpConfig); | ||
| openAPI.setComponents(components); | ||
|
|
||
| ValidationResult result = evaluator.validate(openAPI); | ||
|
|
||
| List<Invalid> warnings = getDefaultNotInEnumWarnings(result); | ||
| // Two property schemas with distinct enum values → two unique messages | ||
| Assert.assertEquals(warnings.size(), 2); | ||
| } | ||
|
|
||
| @Test(description = "warn for integer default not in integer enum") | ||
| public void testDefaultNotInEnumInteger() { | ||
| RuleConfiguration config = new RuleConfiguration(); | ||
| config.setEnableRecommendations(true); | ||
| OpenApiEvaluator evaluator = new OpenApiEvaluator(config); | ||
|
|
||
| OpenAPI openAPI = new OpenAPI(); | ||
| openAPI.openapi("3.0.1"); | ||
| Components components = new Components(); | ||
| ObjectSchema obj = new ObjectSchema(); | ||
| IntegerSchema prop = new IntegerSchema(); | ||
| prop.setEnum(Arrays.asList(1, 2, 3)); | ||
| prop.setDefault(99); | ||
| obj.addProperty("code", prop); | ||
| components.addSchemas("Config", obj); | ||
| openAPI.setComponents(components); | ||
|
|
||
| ValidationResult result = evaluator.validate(openAPI); | ||
|
|
||
| List<Invalid> warnings = getDefaultNotInEnumWarnings(result); | ||
| Assert.assertEquals(warnings.size(), 1); | ||
| Assert.assertTrue(warnings.get(0).getMessage().contains("'99'")); | ||
| } | ||
|
|
||
| @Test(description = "no warning when schema has no enum") | ||
| public void testNoEnum() { | ||
| RuleConfiguration config = new RuleConfiguration(); | ||
| config.setEnableRecommendations(true); | ||
| OpenApiEvaluator evaluator = new OpenApiEvaluator(config); | ||
|
|
||
| OpenAPI openAPI = new OpenAPI(); | ||
| openAPI.openapi("3.0.1"); | ||
| Components components = new Components(); | ||
| ObjectSchema obj = new ObjectSchema(); | ||
| StringSchema prop = new StringSchema(); | ||
| prop.setDefault("http"); | ||
| obj.addProperty("protocol", prop); | ||
| components.addSchemas("Config", obj); | ||
| openAPI.setComponents(components); | ||
|
|
||
| ValidationResult result = evaluator.validate(openAPI); | ||
|
|
||
| List<Invalid> warnings = getDefaultNotInEnumWarnings(result); | ||
| Assert.assertEquals(warnings.size(), 0); | ||
| } | ||
|
|
||
| @Test(description = "no warning when schema has no default") | ||
| public void testNoDefault() { | ||
| RuleConfiguration config = new RuleConfiguration(); | ||
| config.setEnableRecommendations(true); | ||
| OpenApiEvaluator evaluator = new OpenApiEvaluator(config); | ||
|
|
||
| OpenAPI openAPI = new OpenAPI(); | ||
| openAPI.openapi("3.0.1"); | ||
| Components components = new Components(); | ||
| ObjectSchema obj = new ObjectSchema(); | ||
| StringSchema prop = new StringSchema(); | ||
| prop.setEnum(Arrays.asList("udp", "tcp")); | ||
| obj.addProperty("protocol", prop); | ||
| components.addSchemas("Config", obj); | ||
| openAPI.setComponents(components); | ||
|
|
||
| ValidationResult result = evaluator.validate(openAPI); | ||
|
|
||
| List<Invalid> warnings = getDefaultNotInEnumWarnings(result); | ||
| Assert.assertEquals(warnings.size(), 0); | ||
| } | ||
|
|
||
| @Test(description = "warn for default not in enum in inline request body schema") | ||
| public void testDefaultNotInEnumInlineRequestBody() { | ||
| RuleConfiguration config = new RuleConfiguration(); | ||
| config.setEnableRecommendations(true); | ||
| OpenApiEvaluator evaluator = new OpenApiEvaluator(config); | ||
|
|
||
| OpenAPI openAPI = new OpenAPI(); | ||
| openAPI.openapi("3.0.1"); | ||
|
|
||
| // Build an inline schema in a request body (not in components/schemas) | ||
| ObjectSchema bodySchema = new ObjectSchema(); | ||
| StringSchema prop = new StringSchema(); | ||
| prop.setEnum(Arrays.asList("udp", "tcp")); | ||
| prop.setDefault("http"); | ||
| bodySchema.addProperty("protocol", prop); | ||
|
|
||
| MediaType mediaType = new MediaType(); | ||
| mediaType.setSchema(bodySchema); | ||
| Content content = new Content(); | ||
| content.addMediaType("application/json", mediaType); | ||
| RequestBody requestBody = new RequestBody(); | ||
| requestBody.setContent(content); | ||
|
|
||
| Operation operation = new Operation(); | ||
| operation.setRequestBody(requestBody); | ||
| operation.setResponses(new ApiResponses()); | ||
|
|
||
| PathItem pathItem = new PathItem(); | ||
| pathItem.setPost(operation); | ||
| Paths paths = new Paths(); | ||
| paths.addPathItem("/test", pathItem); | ||
| openAPI.setPaths(paths); | ||
|
|
||
| ValidationResult result = evaluator.validate(openAPI); | ||
|
|
||
| List<Invalid> warnings = getDefaultNotInEnumWarnings(result); | ||
| Assert.assertEquals(warnings.size(), 1); | ||
| Assert.assertTrue(warnings.get(0).getMessage().contains("'http'")); | ||
| } | ||
|
|
||
| @Test(description = "warn for default not in enum in inline response schema") | ||
| public void testDefaultNotInEnumInlineResponse() { | ||
| RuleConfiguration config = new RuleConfiguration(); | ||
| config.setEnableRecommendations(true); | ||
| OpenApiEvaluator evaluator = new OpenApiEvaluator(config); | ||
|
|
||
| OpenAPI openAPI = new OpenAPI(); | ||
| openAPI.openapi("3.0.1"); | ||
|
|
||
| // Build an inline schema in a response (not in components/schemas) | ||
| ObjectSchema responseSchema = new ObjectSchema(); | ||
| StringSchema prop = new StringSchema(); | ||
| prop.setEnum(Arrays.asList("tcp")); | ||
| prop.setDefault("http"); | ||
| responseSchema.addProperty("protocol", prop); | ||
|
|
||
| MediaType mediaType = new MediaType(); | ||
| mediaType.setSchema(responseSchema); | ||
| Content content = new Content(); | ||
| content.addMediaType("application/json", mediaType); | ||
| ApiResponse apiResponse = new ApiResponse(); | ||
| apiResponse.setContent(content); | ||
| ApiResponses responses = new ApiResponses(); | ||
| responses.addApiResponse("200", apiResponse); | ||
|
|
||
| Operation operation = new Operation(); | ||
| operation.setResponses(responses); | ||
|
|
||
| PathItem pathItem = new PathItem(); | ||
| pathItem.setGet(operation); | ||
| Paths paths = new Paths(); | ||
| paths.addPathItem("/test", pathItem); | ||
| openAPI.setPaths(paths); | ||
|
|
||
| ValidationResult result = evaluator.validate(openAPI); | ||
|
|
||
| List<Invalid> warnings = getDefaultNotInEnumWarnings(result); | ||
| Assert.assertEquals(warnings.size(), 1); | ||
| Assert.assertTrue(warnings.get(0).getMessage().contains("'http'")); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.