Skip to content
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

fix: setting defaults for oneOf and anyOf #690

Merged
merged 3 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions openapi3/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -933,10 +933,6 @@ func (schema *Schema) visitSetOperations(settings *schemaValidationSettings, val
matchedOneOfIdx = 0
tempValue = value
)
// make a deep copy to protect origin value from being injected default value that defined in mismatched oneOf schema
if settings.asreq || settings.asrep {
tempValue = deepcopy.Copy(value)
}
for idx, item := range v {
v := item.Value
if v == nil {
Expand All @@ -947,6 +943,11 @@ func (schema *Schema) visitSetOperations(settings *schemaValidationSettings, val
continue
}

// make a deep copy to protect origin value from being injected default value that defined in mismatched oneOf schema
if settings.asreq || settings.asrep {
tempValue = deepcopy.Copy(value)
}

if err := v.visitJSON(settings, tempValue); err != nil {
validationErrors = append(validationErrors, err)
continue
Expand Down Expand Up @@ -989,15 +990,15 @@ func (schema *Schema) visitSetOperations(settings *schemaValidationSettings, val
matchedAnyOfIdx = 0
tempValue = value
)
// make a deep copy to protect origin value from being injected default value that defined in mismatched anyOf schema
if settings.asreq || settings.asrep {
tempValue = deepcopy.Copy(value)
}
for idx, item := range v {
v := item.Value
if v == nil {
return foundUnresolvedRef(item.Ref)
}
// make a deep copy to protect origin value from being injected default value that defined in mismatched anyOf schema
if settings.asreq || settings.asrep {
tempValue = deepcopy.Copy(value)
}
if err := v.visitJSON(settings, tempValue); err == nil {
ok = true
matchedAnyOfIdx = idx
Expand Down
6 changes: 4 additions & 2 deletions openapi3filter/validate_set_default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ func TestValidateRequestBodyAndSetDefault(t *testing.T) {
"type": "string",
"default": "www.twitter.com"
}
}
},
"additionalProperties": false
},
{
"type": "object",
Expand All @@ -278,7 +279,8 @@ func TestValidateRequestBodyAndSetDefault(t *testing.T) {
"type": "string",
"default": "www.facebook.com"
}
}
},
"additionalProperties": false
}
]
},
Expand Down