Add e2e tests

This commit is contained in:
Manuel de Brito Fontes 2017-10-17 19:50:27 -03:00
parent 99a355f25d
commit 601fb7dacf
1163 changed files with 289217 additions and 14195 deletions

View file

@ -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) {

82
vendor/github.com/go-openapi/spec/refmodifier.go generated vendored Normal file
View file

@ -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
}
}

335
vendor/github.com/go-openapi/spec/refmodifier_test.go generated vendored Normal file
View file

@ -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))
}