Skip to content

Commit

Permalink
add couple tests around multiple file-specs (one needs fixing) + some…
Browse files Browse the repository at this point in the history
… code reuse (#236)
  • Loading branch information
fenollp authored Jul 15, 2020
1 parent 100b968 commit 6a5d509
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 14 deletions.
28 changes: 14 additions & 14 deletions openapi3/swagger_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,11 @@ func (swaggerLoader *SwaggerLoader) loadSwaggerFromDataWithPathInternal(data []b
}

func (swaggerLoader *SwaggerLoader) ResolveRefsIn(swagger *Swagger, path *url.URL) (err error) {
swaggerLoader.visited = make(map[interface{}]struct{})
if swaggerLoader.visited == nil {
swaggerLoader.visited = make(map[interface{}]struct{})
}
if swaggerLoader.visitedFiles == nil {
swaggerLoader.visitedFiles = make(map[string]struct{})
swaggerLoader.reset()
}

// Visit all components
Expand Down Expand Up @@ -232,7 +234,7 @@ func join(basePath *url.URL, relativePath *url.URL) (*url.URL, error) {
}
newPath, err := copyURL(basePath)
if err != nil {
return nil, fmt.Errorf("Can't copy path: '%s'", basePath.String())
return nil, fmt.Errorf("cannot copy path: %q", basePath.String())
}
newPath.Path = path.Join(path.Dir(newPath.Path), relativePath.Path)
return newPath, nil
Expand Down Expand Up @@ -274,9 +276,7 @@ func (swaggerLoader *SwaggerLoader) resolveComponent(swagger *Swagger, ref strin

cursor = swagger
for _, pathPart := range strings.Split(fragment[1:], "/") {

pathPart = strings.Replace(pathPart, "~1", "/", -1)
pathPart = strings.Replace(pathPart, "~0", "~", -1)
pathPart = unescapeRefString(pathPart)

if cursor, err = drillIntoSwaggerField(cursor, pathPart); err != nil {
return nil, nil, fmt.Errorf("Failed to resolve '%s' in fragment in URI: '%s': %v", ref, pathPart, err.Error())
Expand Down Expand Up @@ -369,7 +369,7 @@ func (swaggerLoader *SwaggerLoader) resolveHeaderRef(swagger *Swagger, component
if component == nil {
return errors.New("invalid header: value MUST be a JSON object")
}
if ref := component.Ref; len(ref) > 0 {
if ref := component.Ref; ref != "" {
if isSingleRefElement(ref) {
var header Header
if err := swaggerLoader.loadSingleElementFromURI(ref, path, &header); err != nil {
Expand Down Expand Up @@ -416,7 +416,7 @@ func (swaggerLoader *SwaggerLoader) resolveParameterRef(swagger *Swagger, compon
return errors.New("invalid parameter: value MUST be a JSON object")
}
ref := component.Ref
if len(ref) > 0 {
if ref != "" {
if isSingleRefElement(ref) {
var param Parameter
if err := swaggerLoader.loadSingleElementFromURI(ref, documentPath, &param); err != nil {
Expand Down Expand Up @@ -477,7 +477,7 @@ func (swaggerLoader *SwaggerLoader) resolveRequestBodyRef(swagger *Swagger, comp
if component == nil {
return errors.New("invalid requestBody: value MUST be a JSON object")
}
if ref := component.Ref; len(ref) > 0 {
if ref := component.Ref; ref != "" {
if isSingleRefElement(ref) {
var requestBody RequestBody
if err := swaggerLoader.loadSingleElementFromURI(ref, path, &requestBody); err != nil {
Expand Down Expand Up @@ -532,7 +532,7 @@ func (swaggerLoader *SwaggerLoader) resolveResponseRef(swagger *Swagger, compone
return errors.New("invalid response: value MUST be a JSON object")
}
ref := component.Ref
if len(ref) > 0 {
if ref != "" {

if isSingleRefElement(ref) {
var resp Response
Expand Down Expand Up @@ -607,7 +607,7 @@ func (swaggerLoader *SwaggerLoader) resolveSchemaRef(swagger *Swagger, component
return errors.New("invalid schema: value MUST be a JSON object")
}
ref := component.Ref
if len(ref) > 0 {
if ref != "" {
if isSingleRefElement(ref) {
var schema Schema
if err := swaggerLoader.loadSingleElementFromURI(ref, documentPath, &schema); err != nil {
Expand Down Expand Up @@ -692,7 +692,7 @@ func (swaggerLoader *SwaggerLoader) resolveSecuritySchemeRef(swagger *Swagger, c
if component == nil {
return errors.New("invalid securityScheme: value MUST be a JSON object")
}
if ref := component.Ref; len(ref) > 0 {
if ref := component.Ref; ref != "" {
if isSingleRefElement(ref) {
var scheme SecurityScheme
if err := swaggerLoader.loadSingleElementFromURI(ref, path, &scheme); err != nil {
Expand Down Expand Up @@ -729,7 +729,7 @@ func (swaggerLoader *SwaggerLoader) resolveExampleRef(swagger *Swagger, componen
if component == nil {
return errors.New("invalid example: value MUST be a JSON object")
}
if ref := component.Ref; len(ref) > 0 {
if ref := component.Ref; ref != "" {
if isSingleRefElement(ref) {
var example Example
if err := swaggerLoader.loadSingleElementFromURI(ref, path, &example); err != nil {
Expand Down Expand Up @@ -766,7 +766,7 @@ func (swaggerLoader *SwaggerLoader) resolveLinkRef(swagger *Swagger, component *
if component == nil {
return errors.New("invalid link: value MUST be a JSON object")
}
if ref := component.Ref; len(ref) > 0 {
if ref := component.Ref; ref != "" {
if isSingleRefElement(ref) {
var link Link
if err := swaggerLoader.loadSingleElementFromURI(ref, path, &link); err != nil {
Expand Down
25 changes: 25 additions & 0 deletions openapi3/swagger_loader_issue235_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package openapi3

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestIssue235OK(t *testing.T) {
loader := NewSwaggerLoader()
loader.IsExternalRefsAllowed = true
doc, err := loader.LoadSwaggerFromFile("testdata/issue235.spec0.yml")
require.NoError(t, err)
err = doc.Validate(loader.Context)
require.NoError(t, err)
}

func TestIssue235CircularDep(t *testing.T) {
t.Skip("TODO: return an error on circular dependencies between external files of a spec")
loader := NewSwaggerLoader()
loader.IsExternalRefsAllowed = true
doc, err := loader.LoadSwaggerFromFile("testdata/issue235.spec0-typo.yml")
require.Nil(t, doc)
require.Error(t, err)
}
24 changes: 24 additions & 0 deletions openapi3/testdata/issue235.spec0-typo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
openapi: 3.0.0
info:
title: 'OAI Specification in YAML'
version: 0.0.1
paths:
/test:
get:
responses:
"200":
$ref: '#/components/responses/GetTestOK'
components:
responses:
GetTestOK:
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/ObjectA'
schemas:
ObjectA:
type: object
properties:
object_b:
$ref: 'issue235.spec0-typo.yml#/components/schemas/ObjectD'
24 changes: 24 additions & 0 deletions openapi3/testdata/issue235.spec0.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
openapi: 3.0.0
info:
title: 'OAI Specification in YAML'
version: 0.0.1
paths:
/test:
get:
responses:
"200":
$ref: '#/components/responses/GetTestOK'
components:
responses:
GetTestOK:
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/ObjectA'
schemas:
ObjectA:
type: object
properties:
object_b:
$ref: 'issue235.spec1.yml#/components/schemas/ObjectD'
12 changes: 12 additions & 0 deletions openapi3/testdata/issue235.spec1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
components:
schemas:
ObjectD:
type: object
properties:
result:
$ref: '#/components/schemas/ObjectE'

ObjectE:
properties:
name:
$ref: issue235.spec2.yml#/components/schemas/ObjectX
7 changes: 7 additions & 0 deletions openapi3/testdata/issue235.spec2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
components:
schemas:
ObjectX:
type: object
properties:
name:
type: string

0 comments on commit 6a5d509

Please sign in to comment.