Skip to content

Commit

Permalink
refactor and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danicc097 committed Oct 21, 2022
1 parent f627fee commit 8a9c86e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
3 changes: 3 additions & 0 deletions openapi3/example_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,15 @@ components:
properties:
username:
type: string
default: default
writeOnly: true # only sent in a request
password:
type: string
default: default
writeOnly: true # only sent in a request
user_id:
format: int64
default: 1
type: integer
readOnly: true # only returned in a response
type: object
Expand Down
10 changes: 5 additions & 5 deletions openapi3/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -1458,11 +1458,11 @@ func (schema *Schema) visitJSONObject(settings *schemaValidationSettings, value
sort.Strings(properties)
for _, propName := range properties {
propSchema := schema.Properties[propName]
reqRO := settings.asreq && propSchema.Value.ReadOnly
repWO := settings.asrep && propSchema.Value.WriteOnly

if value[propName] == nil {
if dlft := propSchema.Value.Default; dlft != nil &&
!(settings.asreq && propSchema.Value.ReadOnly) &&
!(settings.asrep && propSchema.Value.WriteOnly) {
if dlft := propSchema.Value.Default; dlft != nil && !reqRO && !repWO {
value[propName] = dlft
if f := settings.defaultsSet; f != nil {
settings.onceSettingDefaults.Do(f)
Expand All @@ -1471,9 +1471,9 @@ func (schema *Schema) visitJSONObject(settings *schemaValidationSettings, value
}

if value[propName] != nil {
if settings.asreq && propSchema.Value.ReadOnly {
if reqRO {
me = append(me, fmt.Errorf("readOnly property %q in request", propName))
} else if settings.asrep && propSchema.Value.WriteOnly {
} else if repWO {
me = append(me, fmt.Errorf("writeOnly property %q in response", propName))
}
}
Expand Down

0 comments on commit 8a9c86e

Please sign in to comment.