Skip to content

Commit

Permalink
Support loading specifications from YAML files (#67)
Browse files Browse the repository at this point in the history
* Add macos/ JetBrains IDE git ignores

* Enable loading from YAML specifications
  • Loading branch information
wwerner authored and fenollp committed Feb 10, 2019
1 parent a4110cd commit 82a13f9
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 17 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@

# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
.glide/

# Macos file system
.DS_Store

# IntelliJ / GoLand
.idea

13 changes: 2 additions & 11 deletions openapi3/swagger_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package openapi3

import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -90,26 +89,18 @@ func (swaggerLoader *SwaggerLoader) LoadSwaggerFromFile(path string) (*Swagger,

func (swaggerLoader *SwaggerLoader) LoadSwaggerFromData(data []byte) (*Swagger, error) {
swagger := &Swagger{}
if err := json.Unmarshal(data, swagger); err != nil {
if err := yaml.Unmarshal(data, swagger); err != nil {
return nil, err
}
return swagger, swaggerLoader.ResolveRefsIn(swagger, nil)
}

func (swaggerLoader *SwaggerLoader) LoadSwaggerFromDataWithPath(data []byte, path *url.URL) (*Swagger, error) {
swagger := &Swagger{}
if err := json.Unmarshal(data, swagger); err != nil {
return nil, err
}
return swagger, swaggerLoader.ResolveRefsIn(swagger, path)
}

func (swaggerLoader *SwaggerLoader) LoadSwaggerFromYAMLData(data []byte) (*Swagger, error) {
swagger := &Swagger{}
if err := yaml.Unmarshal(data, swagger); err != nil {
return nil, err
}
return swagger, swaggerLoader.ResolveRefsIn(swagger, nil)
return swagger, swaggerLoader.ResolveRefsIn(swagger, path)
}

func (swaggerLoader *SwaggerLoader) ResolveRefsIn(swagger *Swagger, path *url.URL) (err error) {
Expand Down
28 changes: 23 additions & 5 deletions openapi3/swagger_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ paths:
`)

loader := openapi3.NewSwaggerLoader()
doc, err := loader.LoadSwaggerFromYAMLData(spec)
doc, err := loader.LoadSwaggerFromData(spec)
require.NoError(t, err)
require.Equal(t, "An API", doc.Info.Title)
require.Equal(t, 2, len(doc.Components.Schemas))
Expand Down Expand Up @@ -123,7 +123,7 @@ paths:
test:
$ref: '#/components/examples/test'`)
loader := openapi3.NewSwaggerLoader()
doc, err := loader.LoadSwaggerFromYAMLData(source)
doc, err := loader.LoadSwaggerFromData(source)
require.NoError(t, err)

err = doc.Validate(loader.Context)
Expand Down Expand Up @@ -224,7 +224,7 @@ paths:
`)

loader := openapi3.NewSwaggerLoader()
_, err := loader.LoadSwaggerFromYAMLData(spec)
_, err := loader.LoadSwaggerFromData(spec)
require.Error(t, err)
}

Expand Down Expand Up @@ -252,7 +252,7 @@ paths:
`)

loader := openapi3.NewSwaggerLoader()
swagger, err := loader.LoadSwaggerFromYAMLData(spec)
swagger, err := loader.LoadSwaggerFromData(spec)
require.NoError(t, err)

require.NotNil(t, swagger.Paths["/"].Parameters[0].Value)
Expand Down Expand Up @@ -284,7 +284,7 @@ paths:
`)

loader := openapi3.NewSwaggerLoader()
swagger, err := loader.LoadSwaggerFromYAMLData(spec)
swagger, err := loader.LoadSwaggerFromData(spec)
require.NoError(t, err)

require.NotNil(t, swagger.Paths["/"].Post.RequestBody.Value.Content.Get("application/json").Examples["test"])
Expand Down Expand Up @@ -405,3 +405,21 @@ func TestLoadFromDataWithExternalRequestResponseHeaderRemoteRef(t *testing.T) {
require.NotNil(t, swagger.Paths["/test"].Post.Responses["default"].Value.Headers["X-TEST-HEADER"].Value.Description)
require.Equal(t, "description", swagger.Paths["/test"].Post.Responses["default"].Value.Headers["X-TEST-HEADER"].Value.Description)
}

func TestLoadYamlFile(t *testing.T) {
loader := openapi3.NewSwaggerLoader()
loader.IsExternalRefsAllowed = true
swagger, err := loader.LoadSwaggerFromFile("testdata/test.openapi.yml")
require.NoError(t, err)

require.Equal(t, "OAI Specification in YAML", swagger.Info.Title)
}

func TestLoadYamlFileWithExternalSchemaRef(t *testing.T) {
loader := openapi3.NewSwaggerLoader()
loader.IsExternalRefsAllowed = true
swagger, err := loader.LoadSwaggerFromFile("testdata/testref.openapi.yml")
require.NoError(t, err)

require.NotNil(t, swagger.Components.Schemas["AnotherTestSchema"].Value.Type)
}
2 changes: 1 addition & 1 deletion openapi3/swagger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestRefsYAML(t *testing.T) {
err = loader.ResolveRefsIn(docA, nil)
require.NoError(t, err)
t.Log("Resolve refs in marshalled *openapi3.Swagger")
docB, err := loader.LoadSwaggerFromYAMLData(data)
docB, err := loader.LoadSwaggerFromData(data)
require.NoError(t, err)
require.NotEmpty(t, docB)

Expand Down
44 changes: 44 additions & 0 deletions openapi3/testdata/components.openapi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
openapi: 3.0.0
info:
title: ''
version: '1'
paths: {}
components:
schemas:
Name:
type: string
CustomTestSchema:
"$ref": "#/components/schemas/Name"
responses:
Name:
description: description
CustomTestResponse:
"$ref": "#/components/responses/Name"
parameters:
Name:
name: id
in: header
CustomTestParameter:
"$ref": "#/components/parameters/Name"
examples:
Name:
description: description
CustomTestExample:
"$ref": "#/components/examples/Name"
requestBodies:
Name:
content: {}
CustomTestRequestBody:
"$ref": "#/components/requestBodies/Name"
headers:
Name:
description: description
CustomTestHeader:
"$ref": "#/components/headers/Name"
securitySchemes:
Name:
type: cookie
description: description
CustomTestSecurityScheme:
"$ref": "#/components/securitySchemes/Name"
10 changes: 10 additions & 0 deletions openapi3/testdata/test.openapi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
openapi: 3.0.0
info:
title: 'OAI Specification in YAML'
version: 0.0.1
paths: {}
components:
schemas:
TestSchema:
type: string
10 changes: 10 additions & 0 deletions openapi3/testdata/testref.openapi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
openapi: 3.0.0
info:
title: 'OAI Specification w/ refs in YAML'
version: '1'
paths: {}
components:
schemas:
AnotherTestSchema:
"$ref": components.openapi.yml#/components/schemas/CustomTestSchema

0 comments on commit 82a13f9

Please sign in to comment.