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

Orthogonal defaulting and new pruning algorithm #81

Merged
merged 5 commits into from
Apr 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
67 changes: 0 additions & 67 deletions defaulter_test.go

This file was deleted.

105 changes: 105 additions & 0 deletions fixtures/pruning/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
{
"properties": {
"foo": {
"type": "integer"
},
"bar": {
"type": "integer"
},
"nested": {
"type": "object",
"properties": {
"inner": {
"type": "object",
"properties": {
"foo": {
"type": "integer"
},
"bar": {
"type": "integer"
}
}
}
}
},
"all": {
"allOf": [
{
"type": "object",
"properties": {
"foo": {
"type": "integer"
}
}
},
{
"type": "object",
"properties": {
"bar": {
"type": "integer"
}
}
}
]
},
"any": {
"anyOf": [
{
"type": "object",
"properties": {
"foo": {
"type": "integer"
}
}
},
{
"type": "object",
"properties": {
"bar": {
"type": "integer"
}
}
}
]
},
"one": {
"oneOf": [
{
"type": "object",
"properties": {
"foo": {
"type": "integer"
}
},
"required": ["foo"]
},
{
"type": "object",
"properties": {
"bar": {
"type": "integer"
}
}
}
]
},
"not": {
"not": {
"type": "object",
"properties": {
"foo": {
"type": "integer"
}
}
}
},
"array": {
"items": {
"properties": {
"foo": {}
}
}
}
},
"required": ["foo", "bar", "nested", "all", "any", "one"]
}
2 changes: 1 addition & 1 deletion helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (h *paramHelper) safeExpandedParamsFor(path, method, operationID string, re
resolvedParams = append(resolvedParams, *resolvedParam)
}
}
// remove params with invalid expansion from slice
// remove params with invalid expansion from Slice
operation.Parameters = resolvedParams

for _, ppr := range s.analyzer.SafeParamsFor(method, path,
Expand Down
19 changes: 9 additions & 10 deletions object_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func (o *objectValidator) Validate(data interface{}) *Result {

o.precheck(res, val)

// check validity of field names
if o.AdditionalProperties != nil && !o.AdditionalProperties.Allows {
// Case: additionalProperties: false
for k := range val {
Expand Down Expand Up @@ -175,7 +176,8 @@ func (o *objectValidator) Validate(data interface{}) *Result {
// Cases: properties which are not regular properties and have not been matched by the PatternProperties validator
if o.AdditionalProperties != nil && o.AdditionalProperties.Schema != nil {
// AdditionalProperties as Schema
res.Merge(NewSchemaValidator(o.AdditionalProperties.Schema, o.Root, o.Path+"."+key, o.KnownFormats).Validate(value))
r := NewSchemaValidator(o.AdditionalProperties.Schema, o.Root, o.Path+"."+key, o.KnownFormats).Validate(value)
res.mergeForField(data.(map[string]interface{}), key, r)
} else if regularProperty && !(matched || succeededOnce) {
// TODO: this is dead code since regularProperty=false here
res.AddErrors(errors.FailedAllPatternProperties(o.Path, o.In, key))
Expand All @@ -189,7 +191,8 @@ func (o *objectValidator) Validate(data interface{}) *Result {

// Property types:
// - regular Property
for pName, pSchema := range o.Properties {
for pName := range o.Properties {
pSchema := o.Properties[pName] // one instance per iteration
rName := pName
if o.Path != "" {
rName = o.Path + "." + pName
Expand All @@ -198,17 +201,12 @@ func (o *objectValidator) Validate(data interface{}) *Result {
// Recursively validates each property against its schema
if v, ok := val[pName]; ok {
r := NewSchemaValidator(&pSchema, o.Root, rName, o.KnownFormats).Validate(v)
res.Merge(r)
res.mergeForField(data.(map[string]interface{}), pName, r)
} else if pSchema.Default != nil {
// If a default value is defined, creates the property from defaults
// NOTE: JSON schema does not enforce default values to be valid against schema. Swagger does.
createdFromDefaults[pName] = true
pName := pName // shadow
// TODO: should validate the default first and ignore the value if invalid
def := pSchema.Default
res.Defaulters = append(res.Defaulters, DefaulterFunc(func() {
val[pName] = def
}))
res.addPropertySchemata(data.(map[string]interface{}), pName, &pSchema)
}
}

Expand All @@ -230,7 +228,8 @@ func (o *objectValidator) Validate(data interface{}) *Result {
if !regularProperty && (matched /*|| succeededOnce*/) {
for _, pName := range patterns {
if v, ok := o.PatternProperties[pName]; ok {
res.Merge(NewSchemaValidator(&v, o.Root, o.Path+"."+key, o.KnownFormats).Validate(value))
r := NewSchemaValidator(&v, o.Root, o.Path+"."+key, o.KnownFormats).Validate(value)
res.mergeForField(data.(map[string]interface{}), key, r)
}
}
}
Expand Down
30 changes: 17 additions & 13 deletions defaulter.go → post/defaulter.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015 go-swagger maintainers
// Copyright 2018 go-swagger maintainers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -12,18 +12,22 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package validate
package post

// Defaulter defines an interface to define values from default values
// provided in a schema.
type Defaulter interface {
Apply()
}

// DefaulterFunc is a function to be called to apply default values to an object.
type DefaulterFunc func()
import (
"github.com/go-openapi/validate"
)

// Apply runs the defaulter function, thus applying default values to an object.
func (f DefaulterFunc) Apply() {
f()
// ApplyDefaults applies defaults to data.
func ApplyDefaults(r *validate.Result) {
fieldSchemata := r.FieldSchemata()
for key, schemata := range fieldSchemata {
LookForDefaultingScheme:
for _, s := range schemata {
if s.Default != nil {
key.Object()[key.Field()] = s.Default
break LookForDefaultingScheme
}
}
}
}
Loading