fix: add test coverage

This commit is contained in:
marclave
2023-05-22 10:36:22 -07:00
committed by Dave Shanley
parent bdc6038722
commit 2777dcc4ad
10 changed files with 589 additions and 236 deletions

View File

@@ -5,12 +5,13 @@ package base
import (
"fmt"
"strings"
"testing"
lowmodel "github.com/pb33f/libopenapi/datamodel/low"
lowbase "github.com/pb33f/libopenapi/datamodel/low/base"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"strings"
"testing"
)
func TestNewTag(t *testing.T) {
@@ -46,8 +47,18 @@ x-hack: code`
}
func ExampleNewTag() {
func TestTag_RenderInline(t *testing.T) {
tag := &Tag{
Name: "cake",
}
tri, _ := tag.RenderInline()
assert.Equal(t, "name: cake", strings.TrimSpace(string(tri)))
}
func ExampleNewTag() {
// create an example schema object
// this can be either JSON or YAML.
yml := `

View File

@@ -4,55 +4,146 @@
package v3
import (
"github.com/pb33f/libopenapi/datamodel"
"github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/datamodel/low/v3"
"github.com/pb33f/libopenapi/index"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"io/ioutil"
"strings"
"testing"
"io/ioutil"
"strings"
"testing"
"github.com/pb33f/libopenapi/datamodel"
"github.com/pb33f/libopenapi/datamodel/low"
v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
"github.com/pb33f/libopenapi/index"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
)
func TestMediaType_MarshalYAML(t *testing.T) {
// load the petstore spec
data, _ := ioutil.ReadFile("../../../test_specs/petstorev3.json")
info, _ := datamodel.ExtractSpecInfo(data)
var err []error
lowDoc, err = v3.CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{})
if err != nil {
panic("broken something")
}
func TestMediaType_MarshalYAMLInline(t *testing.T) {
// load the petstore spec
data, _ := ioutil.ReadFile("../../../test_specs/petstorev3.json")
info, _ := datamodel.ExtractSpecInfo(data)
var err []error
lowDoc, err = v3.CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{})
if err != nil {
panic("broken something")
}
// create a new document and extract a media type object from it.
d := NewDocument(lowDoc)
mt := d.Paths.PathItems["/pet"].Put.RequestBody.Content["application/json"]
// create a new document and extract a media type object from it.
d := NewDocument(lowDoc)
mt := d.Paths.PathItems["/pet"].Put.RequestBody.Content["application/json"]
// render out the media type
yml, _ := mt.Render()
// render out the media type
yml, _ := mt.Render()
// the rendered output should be a ref to the media type.
op := `schema:
// the rendered output should be a ref to the media type.
op := `schema:
$ref: '#/components/schemas/Pet'`
assert.Equal(t, op, strings.TrimSpace(string(yml)))
assert.Equal(t, op, strings.TrimSpace(string(yml)))
// modify the media type to have an example
mt.Example = "testing a nice mutation"
// modify the media type to have an example
mt.Example = "testing a nice mutation"
op = `schema:
op = `schema:
required:
- name
- photoUrls
type: object
properties:
id:
type: integer
format: int64
example: 10
name:
type: string
example: doggie
category:
type: object
properties:
id:
type: integer
format: int64
example: 1
name:
type: string
example: Dogs
xml:
name: category
photoUrls:
type: array
xml:
wrapped: true
items:
type: string
xml:
name: photoUrl
tags:
type: array
xml:
wrapped: true
items:
type: object
properties:
id:
type: integer
format: int64
name:
type: string
xml:
name: tag
status:
type: string
description: pet status in the store
enum:
- available
- pending
- sold
xml:
name: pet
example: testing a nice mutation`
yml, _ = mt.RenderInline()
assert.Equal(t, op, strings.TrimSpace(string(yml)))
}
func TestMediaType_MarshalYAML(t *testing.T) {
// load the petstore spec
data, _ := ioutil.ReadFile("../../../test_specs/petstorev3.json")
info, _ := datamodel.ExtractSpecInfo(data)
var err []error
lowDoc, err = v3.CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{})
if err != nil {
panic("broken something")
}
// create a new document and extract a media type object from it.
d := NewDocument(lowDoc)
mt := d.Paths.PathItems["/pet"].Put.RequestBody.Content["application/json"]
// render out the media type
yml, _ := mt.Render()
// the rendered output should be a ref to the media type.
op := `schema:
$ref: '#/components/schemas/Pet'`
assert.Equal(t, op, strings.TrimSpace(string(yml)))
// modify the media type to have an example
mt.Example = "testing a nice mutation"
op = `schema:
$ref: '#/components/schemas/Pet'
example: testing a nice mutation`
yml, _ = mt.Render()
yml, _ = mt.Render()
assert.Equal(t, op, strings.TrimSpace(string(yml)))
assert.Equal(t, op, strings.TrimSpace(string(yml)))
}
func TestMediaType_Examples(t *testing.T) {
yml := `examples:
yml := `examples:
pbjBurger:
summary: A horrible, nutty, sticky mess.
value:
@@ -64,17 +155,16 @@ func TestMediaType_Examples(t *testing.T) {
name: Chocolate Cake Burger
numPatties: 5`
var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
idx := index.NewSpecIndexWithConfig(&idxNode, index.CreateOpenAPIIndexConfig())
var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
idx := index.NewSpecIndexWithConfig(&idxNode, index.CreateOpenAPIIndexConfig())
var n v3.MediaType
_ = low.BuildModel(idxNode.Content[0], &n)
_ = n.Build(idxNode.Content[0], idx)
var n v3.MediaType
_ = low.BuildModel(idxNode.Content[0], &n)
_ = n.Build(idxNode.Content[0], idx)
r := NewMediaType(&n)
r := NewMediaType(&n)
rend, _ := r.Render()
assert.Len(t, rend, 290)
rend, _ := r.Render()
assert.Len(t, rend, 290)
}

View File

@@ -4,10 +4,11 @@
package v3
import (
"github.com/pb33f/libopenapi/datamodel/high/base"
"strings"
"testing"
"github.com/pb33f/libopenapi/datamodel/high/base"
"github.com/pb33f/libopenapi/datamodel/low"
v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
"github.com/pb33f/libopenapi/index"
@@ -89,4 +90,42 @@ requestBody:
assert.Equal(t, desired, strings.TrimSpace(string(rend)))
}
}
func TestOperation_MarshalYAMLInline(t *testing.T) {
op := &Operation{
Tags: []string{"test"},
Summary: "nice",
Description: "rice",
ExternalDocs: &base.ExternalDoc{
Description: "spice",
},
OperationId: "slice",
Parameters: []*Parameter{
&Parameter{
Name: "mice",
},
},
RequestBody: &RequestBody{
Description: "dice",
},
}
rend, _ := op.RenderInline()
desired := `tags:
- test
summary: nice
description: rice
externalDocs:
description: spice
operationId: slice
parameters:
- name: mice
requestBody:
description: dice`
assert.Equal(t, desired, strings.TrimSpace(string(rend)))
}

View File

@@ -4,31 +4,32 @@
package v3
import (
"github.com/pb33f/libopenapi/datamodel/high/base"
"github.com/stretchr/testify/assert"
"strings"
"testing"
"strings"
"testing"
"github.com/pb33f/libopenapi/datamodel/high/base"
"github.com/stretchr/testify/assert"
)
func TestParameter_MarshalYAML(t *testing.T) {
explode := true
param := Parameter{
Name: "chicken",
In: "nuggets",
Description: "beefy",
Deprecated: true,
Style: "simple",
Explode: &explode,
AllowReserved: true,
Example: "example",
Examples: map[string]*base.Example{"example": {Value: "example"}},
Extensions: map[string]interface{}{"x-burgers": "why not?"},
}
explode := true
param := Parameter{
Name: "chicken",
In: "nuggets",
Description: "beefy",
Deprecated: true,
Style: "simple",
Explode: &explode,
AllowReserved: true,
Example: "example",
Examples: map[string]*base.Example{"example": {Value: "example"}},
Extensions: map[string]interface{}{"x-burgers": "why not?"},
}
rend, _ := param.Render()
rend, _ := param.Render()
desired := `name: chicken
desired := `name: chicken
in: nuggets
description: beefy
deprecated: true
@@ -41,90 +42,124 @@ examples:
value: example
x-burgers: why not?`
assert.Equal(t, desired, strings.TrimSpace(string(rend)))
assert.Equal(t, desired, strings.TrimSpace(string(rend)))
}
func TestParameter_MarshalYAMLInline(t *testing.T) {
explode := true
param := Parameter{
Name: "chicken",
In: "nuggets",
Description: "beefy",
Deprecated: true,
Style: "simple",
Explode: &explode,
AllowReserved: true,
Example: "example",
Examples: map[string]*base.Example{"example": {Value: "example"}},
Extensions: map[string]interface{}{"x-burgers": "why not?"},
}
rend, _ := param.RenderInline()
desired := `name: chicken
in: nuggets
description: beefy
deprecated: true
style: simple
explode: true
allowReserved: true
example: example
examples:
example:
value: example
x-burgers: why not?`
assert.Equal(t, desired, strings.TrimSpace(string(rend)))
}
func TestParameter_IsExploded(t *testing.T) {
explode := true
param := Parameter{
Explode: &explode,
}
explode := true
param := Parameter{
Explode: &explode,
}
assert.True(t, param.IsExploded())
assert.True(t, param.IsExploded())
explode = false
param = Parameter{
Explode: &explode,
}
explode = false
param = Parameter{
Explode: &explode,
}
assert.False(t, param.IsExploded())
assert.False(t, param.IsExploded())
param = Parameter{}
param = Parameter{}
assert.False(t, param.IsExploded())
assert.False(t, param.IsExploded())
}
func TestParameter_IsDefaultFormEncoding(t *testing.T) {
param := Parameter{}
assert.True(t, param.IsDefaultFormEncoding())
param := Parameter{}
assert.True(t, param.IsDefaultFormEncoding())
param = Parameter{Style: "form"}
assert.True(t, param.IsDefaultFormEncoding())
param = Parameter{Style: "form"}
assert.True(t, param.IsDefaultFormEncoding())
explode := false
param = Parameter{
Explode: &explode,
}
assert.False(t, param.IsDefaultFormEncoding())
explode := false
param = Parameter{
Explode: &explode,
}
assert.False(t, param.IsDefaultFormEncoding())
explode = true
param = Parameter{
Explode: &explode,
}
assert.True(t, param.IsDefaultFormEncoding())
explode = true
param = Parameter{
Explode: &explode,
}
assert.True(t, param.IsDefaultFormEncoding())
param = Parameter{
Explode: &explode,
Style: "simple",
}
assert.False(t, param.IsDefaultFormEncoding())
param = Parameter{
Explode: &explode,
Style: "simple",
}
assert.False(t, param.IsDefaultFormEncoding())
}
func TestParameter_IsDefaultHeaderEncoding(t *testing.T) {
param := Parameter{}
assert.True(t, param.IsDefaultHeaderEncoding())
param := Parameter{}
assert.True(t, param.IsDefaultHeaderEncoding())
param = Parameter{Style: "simple"}
assert.True(t, param.IsDefaultHeaderEncoding())
param = Parameter{Style: "simple"}
assert.True(t, param.IsDefaultHeaderEncoding())
explode := false
param = Parameter{
Explode: &explode,
Style: "simple",
}
assert.True(t, param.IsDefaultHeaderEncoding())
explode := false
param = Parameter{
Explode: &explode,
Style: "simple",
}
assert.True(t, param.IsDefaultHeaderEncoding())
explode = true
param = Parameter{
Explode: &explode,
Style: "simple",
}
assert.False(t, param.IsDefaultHeaderEncoding())
explode = true
param = Parameter{
Explode: &explode,
Style: "simple",
}
assert.False(t, param.IsDefaultHeaderEncoding())
explode = false
param = Parameter{
Explode: &explode,
Style: "form",
}
assert.False(t, param.IsDefaultHeaderEncoding())
explode = false
param = Parameter{
Explode: &explode,
Style: "form",
}
assert.False(t, param.IsDefaultHeaderEncoding())
}
func TestParameter_IsDefaultPathEncoding(t *testing.T) {
param := Parameter{}
assert.True(t, param.IsDefaultPathEncoding())
param := Parameter{}
assert.True(t, param.IsDefaultPathEncoding())
}

View File

@@ -4,13 +4,14 @@
package v3
import (
"github.com/pb33f/libopenapi/datamodel/low"
v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
"github.com/pb33f/libopenapi/index"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"strings"
"testing"
"strings"
"testing"
"github.com/pb33f/libopenapi/datamodel/low"
v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
"github.com/pb33f/libopenapi/index"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
)
// this test exists because the sample contract doesn't contain a
@@ -18,26 +19,26 @@ import (
// with hard coded line and column numbers in them, changing the spec above the bottom will
// create pointless test changes. So here is a standalone test. you know... for science.
func TestPathItem(t *testing.T) {
yml := `servers:
yml := `servers:
- description: so many options for things in places.`
var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
idx := index.NewSpecIndex(&idxNode)
var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
idx := index.NewSpecIndex(&idxNode)
var n v3.PathItem
_ = low.BuildModel(&idxNode, &n)
_ = n.Build(idxNode.Content[0], idx)
var n v3.PathItem
_ = low.BuildModel(&idxNode, &n)
_ = n.Build(idxNode.Content[0], idx)
r := NewPathItem(&n)
r := NewPathItem(&n)
assert.Len(t, r.Servers, 1)
assert.Equal(t, "so many options for things in places.", r.Servers[0].Description)
assert.Equal(t, 1, r.GoLow().Servers.KeyNode.Line)
assert.Len(t, r.Servers, 1)
assert.Equal(t, "so many options for things in places.", r.Servers[0].Description)
assert.Equal(t, 1, r.GoLow().Servers.KeyNode.Line)
}
func TestPathItem_GetOperations(t *testing.T) {
yml := `get:
yml := `get:
description: get
put:
description: put
@@ -55,46 +56,46 @@ trace:
description: trace
`
var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
idx := index.NewSpecIndex(&idxNode)
var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
idx := index.NewSpecIndex(&idxNode)
var n v3.PathItem
_ = low.BuildModel(&idxNode, &n)
_ = n.Build(idxNode.Content[0], idx)
var n v3.PathItem
_ = low.BuildModel(&idxNode, &n)
_ = n.Build(idxNode.Content[0], idx)
r := NewPathItem(&n)
r := NewPathItem(&n)
assert.Len(t, r.GetOperations(), 8)
assert.Len(t, r.GetOperations(), 8)
}
func TestPathItem_MarshalYAML(t *testing.T) {
pi := &PathItem{
Description: "a path item",
Summary: "It's a test, don't worry about it, Jim",
Servers: []*Server{
{
Description: "a server",
},
},
Parameters: []*Parameter{
{
Name: "I am a query parameter",
In: "query",
},
},
Get: &Operation{
Description: "a get operation",
},
Post: &Operation{
Description: "a post operation",
},
}
pi := &PathItem{
Description: "a path item",
Summary: "It's a test, don't worry about it, Jim",
Servers: []*Server{
{
Description: "a server",
},
},
Parameters: []*Parameter{
{
Name: "I am a query parameter",
In: "query",
},
},
Get: &Operation{
Description: "a get operation",
},
Post: &Operation{
Description: "a post operation",
},
}
rend, _ := pi.Render()
rend, _ := pi.Render()
desired := `description: a path item
desired := `description: a path item
summary: It's a test, don't worry about it, Jim
get:
description: a get operation
@@ -106,5 +107,46 @@ parameters:
- name: I am a query parameter
in: query`
assert.Equal(t, desired, strings.TrimSpace(string(rend)))
assert.Equal(t, desired, strings.TrimSpace(string(rend)))
}
func TestPathItem_MarshalYAMLInline(t *testing.T) {
pi := &PathItem{
Description: "a path item",
Summary: "It's a test, don't worry about it, Jim",
Servers: []*Server{
{
Description: "a server",
},
},
Parameters: []*Parameter{
{
Name: "I am a query parameter",
In: "query",
},
},
Get: &Operation{
Description: "a get operation",
},
Post: &Operation{
Description: "a post operation",
},
}
rend, _ := pi.RenderInline()
desired := `description: a path item
summary: It's a test, don't worry about it, Jim
get:
description: a get operation
post:
description: a post operation
servers:
- description: a server
parameters:
- name: I am a query parameter
in: query`
assert.Equal(t, desired, strings.TrimSpace(string(rend)))
}

View File

@@ -4,18 +4,19 @@
package v3
import (
"github.com/pb33f/libopenapi/datamodel/low"
v3low "github.com/pb33f/libopenapi/datamodel/low/v3"
"github.com/pb33f/libopenapi/index"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"strings"
"testing"
"strings"
"testing"
"github.com/pb33f/libopenapi/datamodel/low"
v3low "github.com/pb33f/libopenapi/datamodel/low/v3"
"github.com/pb33f/libopenapi/index"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
)
func TestPaths_MarshalYAML(t *testing.T) {
yml := `/foo/bar/bizzle:
yml := `/foo/bar/bizzle:
get:
description: get a bizzle
/jim/jam/jizzle:
@@ -25,31 +26,31 @@ func TestPaths_MarshalYAML(t *testing.T) {
get:
description: get a beer now.`
var rootNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &rootNode)
var rootNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &rootNode)
var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
idx := index.NewSpecIndexWithConfig(&idxNode, index.CreateOpenAPIIndexConfig())
var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
idx := index.NewSpecIndexWithConfig(&idxNode, index.CreateOpenAPIIndexConfig())
var n v3low.Paths
err := low.BuildModel(&idxNode, &n)
assert.NoError(t, err)
var n v3low.Paths
err := low.BuildModel(&idxNode, &n)
assert.NoError(t, err)
err = n.Build(idxNode.Content[0], idx)
assert.NoError(t, err)
err = n.Build(idxNode.Content[0], idx)
assert.NoError(t, err)
high := NewPaths(&n)
assert.NotNil(t, high)
high := NewPaths(&n)
assert.NotNil(t, high)
rend, _ := high.Render()
assert.Equal(t, yml, strings.TrimSpace(string(rend)))
rend, _ := high.Render()
assert.Equal(t, yml, strings.TrimSpace(string(rend)))
// mutate
deprecated := true
high.PathItems["/beer"].Get.Deprecated = &deprecated
// mutate
deprecated := true
high.PathItems["/beer"].Get.Deprecated = &deprecated
yml = `/foo/bar/bizzle:
yml = `/foo/bar/bizzle:
get:
description: get a bizzle
/jim/jam/jizzle:
@@ -60,7 +61,59 @@ func TestPaths_MarshalYAML(t *testing.T) {
description: get a beer now.
deprecated: true`
rend, _ = high.Render()
assert.Equal(t, yml, strings.TrimSpace(string(rend)))
rend, _ = high.Render()
assert.Equal(t, yml, strings.TrimSpace(string(rend)))
}
func TestPaths_MarshalYAMLInline(t *testing.T) {
yml := `/foo/bar/bizzle:
get:
description: get a bizzle
/jim/jam/jizzle:
post:
description: post a jizzle
/beer:
get:
description: get a beer now.`
var rootNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &rootNode)
var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
idx := index.NewSpecIndexWithConfig(&idxNode, index.CreateOpenAPIIndexConfig())
var n v3low.Paths
err := low.BuildModel(&idxNode, &n)
assert.NoError(t, err)
err = n.Build(idxNode.Content[0], idx)
assert.NoError(t, err)
high := NewPaths(&n)
assert.NotNil(t, high)
rend, _ := high.Render()
assert.Equal(t, yml, strings.TrimSpace(string(rend)))
// mutate
deprecated := true
high.PathItems["/beer"].Get.Deprecated = &deprecated
yml = `/foo/bar/bizzle:
get:
description: get a bizzle
/jim/jam/jizzle:
post:
description: post a jizzle
/beer:
get:
description: get a beer now.
deprecated: true`
rend, _ = high.RenderInline()
assert.Equal(t, yml, strings.TrimSpace(string(rend)))
}

View File

@@ -4,61 +4,80 @@
package v3
import (
"github.com/stretchr/testify/assert"
"strings"
"testing"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRequestBody_MarshalYAML(t *testing.T) {
rb := true
req := &RequestBody{
Description: "beer",
Required: &rb,
Extensions: map[string]interface{}{"x-high-gravity": "why not?"},
}
rb := true
req := &RequestBody{
Description: "beer",
Required: &rb,
Extensions: map[string]interface{}{"x-high-gravity": "why not?"},
}
rend, _ := req.Render()
rend, _ := req.Render()
desired := `description: beer
desired := `description: beer
required: true
x-high-gravity: why not?`
assert.Equal(t, desired, strings.TrimSpace(string(rend)))
assert.Equal(t, desired, strings.TrimSpace(string(rend)))
}
func TestRequestBody_MarshalYAMLInline(t *testing.T) {
rb := true
req := &RequestBody{
Description: "beer",
Required: &rb,
Extensions: map[string]interface{}{"x-high-gravity": "why not?"},
}
rend, _ := req.RenderInline()
desired := `description: beer
required: true
x-high-gravity: why not?`
assert.Equal(t, desired, strings.TrimSpace(string(rend)))
}
func TestRequestBody_MarshalNoRequired(t *testing.T) {
rb := false
req := &RequestBody{
Description: "beer",
Required: &rb,
Extensions: map[string]interface{}{"x-high-gravity": "why not?"},
}
rb := false
req := &RequestBody{
Description: "beer",
Required: &rb,
Extensions: map[string]interface{}{"x-high-gravity": "why not?"},
}
rend, _ := req.Render()
rend, _ := req.Render()
desired := `description: beer
desired := `description: beer
required: false
x-high-gravity: why not?`
assert.Equal(t, desired, strings.TrimSpace(string(rend)))
assert.Equal(t, desired, strings.TrimSpace(string(rend)))
}
func TestRequestBody_MarshalRequiredNil(t *testing.T) {
req := &RequestBody{
Description: "beer",
Extensions: map[string]interface{}{"x-high-gravity": "why not?"},
}
rend, _ := req.Render()
req := &RequestBody{
Description: "beer",
Extensions: map[string]interface{}{"x-high-gravity": "why not?"},
}
desired := `description: beer
rend, _ := req.Render()
desired := `description: beer
x-high-gravity: why not?`
assert.Equal(t, desired, strings.TrimSpace(string(rend)))
assert.Equal(t, desired, strings.TrimSpace(string(rend)))
}

View File

@@ -4,13 +4,14 @@
package v3
import (
"strings"
"testing"
"github.com/pb33f/libopenapi/datamodel/low"
v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
"github.com/pb33f/libopenapi/index"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"strings"
"testing"
)
// this test exists because the sample contract doesn't contain a
@@ -75,4 +76,32 @@ links:
rend, _ := r.Render()
assert.Equal(t, yml, strings.TrimSpace(string(rend)))
}
}
func TestResponse_MarshalYAMLInline(t *testing.T) {
yml := `description: this is a response
headers:
someHeader:
description: a header
content:
something/thing:
example: cake
links:
someLink:
description: a link!`
var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
idx := index.NewSpecIndexWithConfig(&idxNode, index.CreateOpenAPIIndexConfig())
var n v3.Response
_ = low.BuildModel(idxNode.Content[0], &n)
_ = n.Build(idxNode.Content[0], idx)
r := NewResponse(&n)
rend, _ := r.RenderInline()
assert.Equal(t, yml, strings.TrimSpace(string(rend)))
}

View File

@@ -4,13 +4,14 @@
package v3
import (
"strings"
"testing"
"github.com/pb33f/libopenapi/datamodel/low"
v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
"github.com/pb33f/libopenapi/index"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"strings"
"testing"
)
// this test exists because the sample contract doesn't contain a
@@ -67,3 +68,33 @@ func TestResponses_MarshalYAML(t *testing.T) {
assert.Equal(t, yml, strings.TrimSpace(string(rend)))
}
func TestResponses_MarshalYAMLInline(t *testing.T) {
yml := `"201":
description: this is a response
content:
something/thing:
example: cake
"404":
description: this is a 404
content:
something/thing:
example: why do you need an example?
"200":
description: OK! not bad.`
var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
idx := index.NewSpecIndexWithConfig(&idxNode, index.CreateOpenAPIIndexConfig())
var n v3.Responses
_ = low.BuildModel(idxNode.Content[0], &n)
_ = n.Build(idxNode.Content[0], idx)
r := NewResponses(&n)
rend, _ := r.RenderInline()
assert.Equal(t, yml, strings.TrimSpace(string(rend)))
}

4
go.sum
View File

@@ -58,6 +58,8 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/vmware-labs/yaml-jsonpath v0.3.2 h1:/5QKeCBGdsInyDCyVNLbXyilb61MXGi9NP674f9Hobk=
github.com/vmware-labs/yaml-jsonpath v0.3.2/go.mod h1:U6whw1z03QyqgWdgXxvVnQ90zN1BWz5V+51Ewf8k+rQ=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
@@ -78,6 +80,8 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.2.0 h1:PUR+T4wwASmuSTYdKjYHI5TD22Wy5ogLU5qZCOLxBrI=
golang.org/x/sync v0.2.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=