-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
fix(go): validate string enum exclusions #25089
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
AndreyVMarkelov
wants to merge
9
commits into
OpenAPITools:master
Choose a base branch
from
AndreyVMarkelov:andreymarkelov/fix-go-not-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.
+541
−0
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
047bc2b
fix(go): validate string enum exclusions
AndreyVMarkelov 026b1dd
chore(go): regenerate samples
AndreyVMarkelov 786b3a5
fix(go): align enum validation with JSON field matching
AndreyVMarkelov dbafad3
fix(go): validate inherited string enums
AndreyVMarkelov 9d6cec6
fix(go): preserve optional inherited JSON field tags
AndreyVMarkelov 10b5284
fix(go): reuse field tags in flattened enum decoder
AndreyVMarkelov c958155
fix(go): scope enum validation to affected oneOf models
AndreyVMarkelov c1b1b92
fix(go): address remaining enum validation review findings
AndreyVMarkelov a890b52
fix(go): normalize matched alias before required default
AndreyVMarkelov 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
| package org.openapitools.codegen.languages; | ||
|
|
||
| import com.fasterxml.jackson.databind.node.ArrayNode; | ||
| import com.fasterxml.jackson.databind.node.TextNode; | ||
| import com.google.common.collect.Iterables; | ||
| import com.samskivert.mustache.Mustache; | ||
| import io.swagger.v3.oas.models.media.Schema; | ||
|
|
@@ -600,6 +601,143 @@ public ModelsMap postProcessModels(ModelsMap objs) { | |
| return objs; | ||
| } | ||
|
|
||
| @Override | ||
| public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs) { | ||
| objs = super.postProcessAllModels(objs); | ||
| if (!generateUnmarshalJSON) { | ||
| return objs; | ||
| } | ||
|
|
||
| // An allowed enum only needs strict validation when a oneOf sibling excludes values for that property. | ||
| Map<CodegenModel, Set<String>> allowedOneOfProperties = new IdentityHashMap<>(); | ||
| for (ModelsMap models : objs.values()) { | ||
| for (ModelMap modelMap : models.getModels()) { | ||
| CodegenModel union = modelMap.getModel(); | ||
| for (String excludedMemberName : union.oneOf) { | ||
| CodegenModel excludedMember = ModelUtils.getModelByName(excludedMemberName, objs); | ||
| if (excludedMember == null) { | ||
| continue; | ||
| } | ||
| for (CodegenProperty excludedProperty : effectiveVars(excludedMember).values()) { | ||
| CodegenProperty not = excludedProperty.getComposedSchemas() == null | ||
| ? null : excludedProperty.getComposedSchemas().getNot(); | ||
| if (stringEnumComparison(not, true) == null) { | ||
| continue; | ||
| } | ||
| for (String allowedMemberName : union.oneOf) { | ||
| if (allowedMemberName.equals(excludedMemberName)) { | ||
| continue; | ||
| } | ||
| CodegenModel allowedMember = ModelUtils.getModelByName(allowedMemberName, objs); | ||
| if (allowedMember == null) { | ||
| continue; | ||
| } | ||
| CodegenProperty allowedProperty = effectiveVars(allowedMember).get(excludedProperty.baseName); | ||
| if (stringEnumComparison(allowedProperty, false) != null) { | ||
| allowedOneOfProperties.computeIfAbsent(allowedMember, ignored -> new HashSet<>()) | ||
| .add(excludedProperty.baseName); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for (ModelsMap models : objs.values()) { | ||
| for (ModelMap modelMap : models.getModels()) { | ||
| CodegenModel model = modelMap.getModel(); | ||
| if (model.isEnum || hasOneOf(model) || hasAnyOf(model)) { | ||
| continue; | ||
| } | ||
| Map<String, CodegenProperty> effectiveVars = effectiveVars(model); | ||
| Map<String, CodegenProperty> ownVars = new HashMap<>(); | ||
| for (CodegenProperty param : model.vars) { | ||
| ownVars.put(param.baseName, param); | ||
| } | ||
| List<CodegenProperty> validationVars = new ArrayList<>(); | ||
| boolean hasInheritedStringEnumValidation = false; | ||
| for (CodegenProperty param : effectiveVars.values()) { | ||
| String allowed = allowedOneOfProperties.getOrDefault(model, Collections.emptySet()).contains(param.baseName) | ||
| ? stringEnumComparison(param, false) : null; | ||
| CodegenProperty not = param.getComposedSchemas() == null ? null : param.getComposedSchemas().getNot(); | ||
| String excluded = stringEnumComparison(not, true); | ||
| if (allowed != null || excluded != null) { | ||
| hasInheritedStringEnumValidation |= !ownVars.containsKey(param.baseName); | ||
| if (ownVars.containsKey(param.baseName) && ownVars.get(param.baseName).vendorExtensions.containsKey("x-go-datatag") | ||
| && !param.vendorExtensions.containsKey("x-go-datatag")) { | ||
| param.vendorExtensions.put("x-go-datatag", ownVars.get(param.baseName).vendorExtensions.get("x-go-datatag")); | ||
| } | ||
| validationVars.add(param); | ||
| param.vendorExtensions.put("x-go-enum-property-name", TextNode.valueOf(param.baseName).toString()); | ||
| if (allowed != null) { | ||
| param.vendorExtensions.put("x-go-allowed-string-enum-comparison", allowed); | ||
| if (param.isNullable && (param.isEnumRef || ((List<?>) param.allowableValues.get("values")).contains(null))) { | ||
| param.vendorExtensions.put("x-go-allowed-string-enum-null", true); | ||
| } | ||
| } | ||
| if (excluded != null) { | ||
| param.vendorExtensions.put("x-go-excluded-string-enum-comparison", excluded); | ||
| } | ||
| } | ||
| } | ||
| if (!validationVars.isEmpty()) { | ||
| model.vendorExtensions.put("x-go-has-string-enum-validation", true); | ||
| model.vendorExtensions.put("x-go-string-enum-validation-vars", validationVars); | ||
| List<Map<String, String>> imports = models.getImports(); | ||
| if (imports.stream().noneMatch(i -> "fmt".equals(i.get("import")))) { | ||
| imports.add(createMapping("import", "fmt")); | ||
| } | ||
| if (model.hasRequired && validationVars.stream().anyMatch(param -> param.required)) { | ||
| model.vendorExtensions.put("x-go-enum-required-case-fold", true); | ||
| if (imports.stream().noneMatch(i -> "strings".equals(i.get("import")))) { | ||
| imports.add(createMapping("import", "strings")); | ||
| } | ||
| } | ||
| imports.sort(Comparator.comparing(i -> i.get("import"))); | ||
| } | ||
| if (hasInheritedStringEnumValidation && !model.isAdditionalPropertiesTrue) { | ||
| for (CodegenProperty param : effectiveVars.values()) { | ||
| param.vendorExtensions.put("x-go-flattened-json-name", TextNode.valueOf(param.baseName + (param.required ? "" : ",omitempty")).toString()); | ||
| } | ||
| model.vendorExtensions.put("x-go-inherited-string-enum-validation", true); | ||
| model.vendorExtensions.put("x-go-flattened-vars", new ArrayList<>(effectiveVars.values())); | ||
| } | ||
| } | ||
| } | ||
| return objs; | ||
| } | ||
|
|
||
| private static Map<String, CodegenProperty> effectiveVars(CodegenModel model) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add docstrings to newly-created functions |
||
| Map<String, CodegenProperty> vars = new LinkedHashMap<>(); | ||
| for (CodegenProperty param : model.parent == null ? model.vars : model.allVars) { | ||
| vars.put(param.baseName, param); | ||
| } | ||
| return vars; | ||
| } | ||
|
|
||
| private static String stringEnumComparison(CodegenProperty property, boolean excluded) { | ||
| if (property == null || !(property.isString && property.isEnum || property.isEnumRef) | ||
| || property.allowableValues == null | ||
| || !(property.allowableValues.get("values") instanceof List)) { | ||
| return null; | ||
| } | ||
| StringJoiner comparisons = new StringJoiner(excluded ? " || " : " && "); | ||
| for (Object value : (List<?>) property.allowableValues.get("values")) { | ||
| if (value == null && excluded) { | ||
| comparisons.add("value == nil"); | ||
| continue; | ||
| } | ||
| if (value == null && property.isNullable) { | ||
| continue; | ||
| } | ||
| if (!(value instanceof String)) { | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
|
||
| return null; | ||
| } | ||
| comparisons.add("value " + (excluded ? "==" : "!=") + " " + TextNode.valueOf((String) value)); | ||
| } | ||
| return comparisons.length() == 0 ? (property.isNullable && !excluded ? "true" : null) : comparisons.toString(); | ||
| } | ||
|
|
||
| /** | ||
| * Prefixes the generated {@code unknown_default_open_api} enum case with the model name when enum class prefixing | ||
| * is disabled. | ||
|
|
||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion would be nice to add more comments in the new code block (line 606 - 708)