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 b8a9c2f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 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
12 changes: 6 additions & 6 deletions openapi3/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -1458,22 +1458,22 @@ 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)
}
}
}

if value[propName] != nil {
if settings.asreq && propSchema.Value.ReadOnly {
if v := value[propName]; v != nil {
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 b8a9c2f

Please sign in to comment.