diff options
Diffstat (limited to 'vendor/github.com/go-openapi/spec')
-rw-r--r-- | vendor/github.com/go-openapi/spec/expander.go | 24 | ||||
-rw-r--r-- | vendor/github.com/go-openapi/spec/items.go | 11 | ||||
-rw-r--r-- | vendor/github.com/go-openapi/spec/refmodifier.go | 82 | ||||
-rw-r--r-- | vendor/github.com/go-openapi/spec/refmodifier_test.go | 335 | ||||
-rw-r--r-- | vendor/github.com/go-openapi/spec/swagger.go | 2 |
5 files changed, 444 insertions, 10 deletions
diff --git a/vendor/github.com/go-openapi/spec/expander.go b/vendor/github.com/go-openapi/spec/expander.go index b4429a21c..7af80691f 100644 --- a/vendor/github.com/go-openapi/spec/expander.go +++ b/vendor/github.com/go-openapi/spec/expander.go @@ -352,14 +352,12 @@ func normalizeFileRef(ref *Ref, relativeBase string) *Ref { } func (r *schemaLoader) resolveRef(currentRef, ref *Ref, node, target interface{}) error { - tgt := reflect.ValueOf(target) if tgt.Kind() != reflect.Ptr { return fmt.Errorf("resolve ref: target needs to be a pointer") } oldRef := currentRef - if currentRef != nil { debugLog("resolve ref current %s new %s", currentRef.String(), ref.String()) nextRef := nextRef(node, ref, currentRef.GetPointer()) @@ -467,8 +465,6 @@ func (r *schemaLoader) resolveRef(currentRef, ref *Ref, node, target interface{} return err } - r.currentRef = currentRef - return nil } @@ -645,14 +641,18 @@ func expandSchema(target Schema, parentRefs []string, resolver *schemaLoader) (* return resolver.root.(*Schema), nil } - // t is the new expanded schema var t *Schema - + var basePath string + b, _ := json.Marshal(target) + debugLog("Target is: %s", string(b)) for target.Ref.String() != "" { if swag.ContainsStringsCI(parentRefs, target.Ref.String()) { return &target, nil } - + basePath = target.Ref.RemoteURI() + debugLog("\n\n\n\n\nbasePath: %s", basePath) + b, _ := json.Marshal(target) + debugLog("calling Resolve with target: %s", string(b)) if err := resolver.Resolve(&target.Ref, &t); shouldStopOnError(err, resolver.options) { return &target, err } @@ -666,7 +666,13 @@ func expandSchema(target Schema, parentRefs []string, resolver *schemaLoader) (* target = *t } } - + if target.Ref.String() == "" { + b, _ := json.Marshal(target) + debugLog("before: %s", string(b)) + modifyRefs(&target, basePath) + b, _ = json.Marshal(target) + debugLog("after: %s", string(b)) + } t, err := expandItems(target, parentRefs, resolver) if shouldStopOnError(err, resolver.options) { return &target, err @@ -675,6 +681,8 @@ func expandSchema(target Schema, parentRefs []string, resolver *schemaLoader) (* target = *t } + resolver.reset() + for i := range target.AllOf { t, err := expandSchema(target.AllOf[i], parentRefs, resolver) if shouldStopOnError(err, resolver.options) { diff --git a/vendor/github.com/go-openapi/spec/items.go b/vendor/github.com/go-openapi/spec/items.go index 46944fb69..07ac88e66 100644 --- a/vendor/github.com/go-openapi/spec/items.go +++ b/vendor/github.com/go-openapi/spec/items.go @@ -178,9 +178,14 @@ func (i *Items) UnmarshalJSON(data []byte) error { if err := json.Unmarshal(data, &simpleSchema); err != nil { return err } + var vendorExtensible VendorExtensible + if err := json.Unmarshal(data, &vendorExtensible); err != nil { + return err + } i.Refable = ref i.CommonValidations = validations i.SimpleSchema = simpleSchema + i.VendorExtensible = vendorExtensible return nil } @@ -198,7 +203,11 @@ func (i Items) MarshalJSON() ([]byte, error) { if err != nil { return nil, err } - return swag.ConcatJSON(b3, b1, b2), nil + b4, err := json.Marshal(i.VendorExtensible) + if err != nil { + return nil, err + } + return swag.ConcatJSON(b4, b3, b1, b2), nil } // JSONLookup look up a value by the json property name diff --git a/vendor/github.com/go-openapi/spec/refmodifier.go b/vendor/github.com/go-openapi/spec/refmodifier.go new file mode 100644 index 000000000..8482608ea --- /dev/null +++ b/vendor/github.com/go-openapi/spec/refmodifier.go @@ -0,0 +1,82 @@ +// Copyright 2017 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. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package spec + +import ( + "fmt" +) + +func modifyItemsRefs(target *Schema, basePath string) { + if target.Items != nil { + if target.Items.Schema != nil { + modifyRefs(target.Items.Schema, basePath) + } + for i := range target.Items.Schemas { + s := target.Items.Schemas[i] + modifyRefs(&s, basePath) + target.Items.Schemas[i] = s + } + } +} + +func modifyRefs(target *Schema, basePath string) { + if target.Ref.String() != "" { + if target.Ref.RemoteURI() == basePath { + return + } + newURL := fmt.Sprintf("%s%s", basePath, target.Ref.String()) + target.Ref, _ = NewRef(newURL) + } + + modifyItemsRefs(target, basePath) + for i := range target.AllOf { + modifyRefs(&target.AllOf[i], basePath) + } + for i := range target.AnyOf { + modifyRefs(&target.AnyOf[i], basePath) + } + for i := range target.OneOf { + modifyRefs(&target.OneOf[i], basePath) + } + if target.Not != nil { + modifyRefs(target.Not, basePath) + } + for k := range target.Properties { + s := target.Properties[k] + modifyRefs(&s, basePath) + target.Properties[k] = s + } + if target.AdditionalProperties != nil && target.AdditionalProperties.Schema != nil { + modifyRefs(target.AdditionalProperties.Schema, basePath) + } + for k := range target.PatternProperties { + s := target.PatternProperties[k] + modifyRefs(&s, basePath) + target.PatternProperties[k] = s + } + for k := range target.Dependencies { + if target.Dependencies[k].Schema != nil { + modifyRefs(target.Dependencies[k].Schema, basePath) + } + } + if target.AdditionalItems != nil && target.AdditionalItems.Schema != nil { + modifyRefs(target.AdditionalItems.Schema, basePath) + } + for k := range target.Definitions { + s := target.Definitions[k] + modifyRefs(&s, basePath) + target.Definitions[k] = s + } +} diff --git a/vendor/github.com/go-openapi/spec/refmodifier_test.go b/vendor/github.com/go-openapi/spec/refmodifier_test.go new file mode 100644 index 000000000..c456cb43f --- /dev/null +++ b/vendor/github.com/go-openapi/spec/refmodifier_test.go @@ -0,0 +1,335 @@ +// Copyright 2017 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. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package spec + +import ( + "encoding/json" + "testing" + + "github.com/stretchr/testify/assert" +) + +var testJsonSchema = `{ + "id": "http://json-schema.org/draft-04/schema#", + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Core schema meta-schema", + "definitions": { + "schemaArray": { + "type": "array", + "minItems": 1, + "items": { "$ref": "#" } + }, + "positiveInteger": { + "type": "integer", + "minimum": 0 + }, + "positiveIntegerDefault0": { + "allOf": [ { "$ref": "#/definitions/positiveInteger" }, { "default": 0 } ] + }, + "simpleTypes": { + "enum": [ "array", "boolean", "integer", "null", "number", "object", "string" ] + }, + "stringArray": { + "type": "array", + "items": { "type": "string" }, + "minItems": 1, + "uniqueItems": true + } + }, + "type": "object", + "properties": { + "id": { + "type": "string", + "format": "uri" + }, + "$schema": { + "type": "string", + "format": "uri" + }, + "title": { + "type": "string" + }, + "description": { + "type": "string" + }, + "default": {}, + "multipleOf": { + "type": "number", + "minimum": 0, + "exclusiveMinimum": true + }, + "maximum": { + "type": "number" + }, + "exclusiveMaximum": { + "type": "boolean", + "default": false + }, + "minimum": { + "type": "number" + }, + "exclusiveMinimum": { + "type": "boolean", + "default": false + }, + "maxLength": { "$ref": "#/definitions/positiveInteger" }, + "minLength": { "$ref": "#/definitions/positiveIntegerDefault0" }, + "pattern": { + "type": "string", + "format": "regex" + }, + "additionalItems": { + "anyOf": [ + { "type": "boolean" }, + { "$ref": "#" } + ], + "default": {} + }, + "items": { + "anyOf": [ + { "$ref": "#" }, + { "$ref": "#/definitions/schemaArray" } + ], + "default": {} + }, + "maxItems": { "$ref": "#/definitions/positiveInteger" }, + "minItems": { "$ref": "#/definitions/positiveIntegerDefault0" }, + "uniqueItems": { + "type": "boolean", + "default": false + }, + "maxProperties": { "$ref": "#/definitions/positiveInteger" }, + "minProperties": { "$ref": "#/definitions/positiveIntegerDefault0" }, + "required": { "$ref": "#/definitions/stringArray" }, + "additionalProperties": { + "anyOf": [ + { "type": "boolean" }, + { "$ref": "#" } + ], + "default": {} + }, + "definitions": { + "type": "object", + "additionalProperties": { "$ref": "#" }, + "default": {} + }, + "properties": { + "type": "object", + "additionalProperties": { "$ref": "#" }, + "default": {} + }, + "patternProperties": { + "type": "object", + "additionalProperties": { "$ref": "#" }, + "default": {} + }, + "dependencies": { + "type": "object", + "additionalProperties": { + "anyOf": [ + { "$ref": "#" }, + { "$ref": "#/definitions/stringArray" } + ] + } + }, + "enum": { + "type": "array", + "minItems": 1, + "uniqueItems": true + }, + "type": { + "anyOf": [ + { "$ref": "#/definitions/simpleTypes" }, + { + "type": "array", + "items": { "$ref": "#/definitions/simpleTypes" }, + "minItems": 1, + "uniqueItems": true + } + ] + }, + "allOf": { "$ref": "#/definitions/schemaArray" }, + "anyOf": { "$ref": "#/definitions/schemaArray" }, + "oneOf": { "$ref": "#/definitions/schemaArray" }, + "not": { "$ref": "#" } + }, + "dependencies": { + "exclusiveMaximum": [ "maximum" ], + "exclusiveMinimum": [ "minimum" ] + }, + "default": {} +} +` + +var modifiedTestJsonSchema = `{ + "id": "http://json-schema.org/draft-04/schema#", + "$schema": "http://json-schema.org/draft-04/schema", + "description": "Core schema meta-schema", + "definitions": { + "schemaArray": { + "type": "array", + "minItems": 1, + "items": { "$ref": "" } + }, + "positiveInteger": { + "type": "integer", + "minimum": 0 + }, + "positiveIntegerDefault0": { + "allOf": [ { "$ref": "http://json-schema.org/draft-04/schema#/definitions/positiveInteger" }, { "default": 0 } ] + }, + "simpleTypes": { + "enum": [ "array", "boolean", "integer", "null", "number", "object", "string" ] + }, + "stringArray": { + "type": "array", + "items": { "type": "string" }, + "minItems": 1, + "uniqueItems": true + } + }, + "type": "object", + "properties": { + "id": { + "type": "string", + "format": "uri" + }, + "$schema": { + "type": "string", + "format": "uri" + }, + "title": { + "type": "string" + }, + "description": { + "type": "string" + }, + "default": {}, + "multipleOf": { + "type": "number", + "minimum": 0, + "exclusiveMinimum": true + }, + "maximum": { + "type": "number" + }, + "exclusiveMaximum": { + "type": "boolean", + "default": false + }, + "minimum": { + "type": "number" + }, + "exclusiveMinimum": { + "type": "boolean", + "default": false + }, + "maxLength": { "$ref": "http://json-schema.org/draft-04/schema#/definitions/positiveInteger" }, + "minLength": { "$ref": "http://json-schema.org/draft-04/schema#/definitions/positiveIntegerDefault0" }, + "pattern": { + "type": "string", + "format": "regex" + }, + "additionalItems": { + "anyOf": [ + { "type": "boolean" }, + { "$ref": "" } + ], + "default": {} + }, + "items": { + "anyOf": [ + { "$ref": "" }, + { "$ref": "http://json-schema.org/draft-04/schema#/definitions/schemaArray" } + ], + "default": {} + }, + "maxItems": { "$ref": "http://json-schema.org/draft-04/schema#/definitions/positiveInteger" }, + "minItems": { "$ref": "http://json-schema.org/draft-04/schema#/definitions/positiveIntegerDefault0" }, + "uniqueItems": { + "type": "boolean", + "default": false + }, + "maxProperties": { "$ref": "http://json-schema.org/draft-04/schema#/definitions/positiveInteger" }, + "minProperties": { "$ref": "http://json-schema.org/draft-04/schema#/definitions/positiveIntegerDefault0" }, + "required": { "$ref": "http://json-schema.org/draft-04/schema#/definitions/stringArray" }, + "additionalProperties": { + "anyOf": [ + { "type": "boolean" }, + { "$ref": "" } + ], + "default": {} + }, + "definitions": { + "type": "object", + "additionalProperties": { "$ref": "" }, + "default": {} + }, + "properties": { + "type": "object", + "additionalProperties": { "$ref": "" }, + "default": {} + }, + "patternProperties": { + "type": "object", + "additionalProperties": { "$ref": "" }, + "default": {} + }, + "dependencies": { + "type": "object", + "additionalProperties": { + "anyOf": [ + { "$ref": "" }, + { "$ref": "http://json-schema.org/draft-04/schema#/definitions/stringArray" } + ] + } + }, + "enum": { + "type": "array", + "minItems": 1, + "uniqueItems": true + }, + "type": { + "anyOf": [ + { "$ref": "http://json-schema.org/draft-04/schema#/definitions/simpleTypes" }, + { + "type": "array", + "items": { "$ref": "http://json-schema.org/draft-04/schema#/definitions/simpleTypes" }, + "minItems": 1, + "uniqueItems": true + } + ] + }, + "allOf": { "$ref": "http://json-schema.org/draft-04/schema#/definitions/schemaArray" }, + "anyOf": { "$ref": "http://json-schema.org/draft-04/schema#/definitions/schemaArray" }, + "oneOf": { "$ref": "http://json-schema.org/draft-04/schema#/definitions/schemaArray" }, + "not": { "$ref": "" } + }, + "dependencies": { + "exclusiveMaximum": [ "maximum" ], + "exclusiveMinimum": [ "minimum" ] + }, + "default": {} +} +` + +func TestRefModifier(t *testing.T) { + sch := Schema{} + assert.NoError(t, json.Unmarshal([]byte(testJsonSchema), &sch)) + modifyRefs(&sch, "http://json-schema.org/draft-04/schema") + b, err := sch.MarshalJSON() + assert.NoError(t, err) + assert.JSONEq(t, modifiedTestJsonSchema, string(b)) +} diff --git a/vendor/github.com/go-openapi/spec/swagger.go b/vendor/github.com/go-openapi/spec/swagger.go index 393a31677..23780c78a 100644 --- a/vendor/github.com/go-openapi/spec/swagger.go +++ b/vendor/github.com/go-openapi/spec/swagger.go @@ -156,7 +156,7 @@ func (s SchemaOrStringArray) MarshalJSON() ([]byte, error) { if s.Schema != nil { return json.Marshal(s.Schema) } - return nil, nil + return []byte("null"), nil } // UnmarshalJSON converts this schema object or array from a JSON structure |