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 ( import (
"fmt" "fmt"
"strings"
"testing"
lowmodel "github.com/pb33f/libopenapi/datamodel/low" lowmodel "github.com/pb33f/libopenapi/datamodel/low"
lowbase "github.com/pb33f/libopenapi/datamodel/low/base" lowbase "github.com/pb33f/libopenapi/datamodel/low/base"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"strings"
"testing"
) )
func TestNewTag(t *testing.T) { 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 // create an example schema object
// this can be either JSON or YAML. // this can be either JSON or YAML.
yml := ` yml := `

View File

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

View File

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

View File

@@ -4,13 +4,14 @@
package v3 package v3
import ( import (
"github.com/pb33f/libopenapi/datamodel/low" "strings"
v3 "github.com/pb33f/libopenapi/datamodel/low/v3" "testing"
"github.com/pb33f/libopenapi/index"
"github.com/stretchr/testify/assert" "github.com/pb33f/libopenapi/datamodel/low"
"gopkg.in/yaml.v3" v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
"strings" "github.com/pb33f/libopenapi/index"
"testing" "github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
) )
// this test exists because the sample contract doesn't contain a // 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 // 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. // create pointless test changes. So here is a standalone test. you know... for science.
func TestPathItem(t *testing.T) { func TestPathItem(t *testing.T) {
yml := `servers: yml := `servers:
- description: so many options for things in places.` - description: so many options for things in places.`
var idxNode yaml.Node var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode) _ = yaml.Unmarshal([]byte(yml), &idxNode)
idx := index.NewSpecIndex(&idxNode) idx := index.NewSpecIndex(&idxNode)
var n v3.PathItem var n v3.PathItem
_ = low.BuildModel(&idxNode, &n) _ = low.BuildModel(&idxNode, &n)
_ = n.Build(idxNode.Content[0], idx) _ = n.Build(idxNode.Content[0], idx)
r := NewPathItem(&n) r := NewPathItem(&n)
assert.Len(t, r.Servers, 1) assert.Len(t, r.Servers, 1)
assert.Equal(t, "so many options for things in places.", r.Servers[0].Description) assert.Equal(t, "so many options for things in places.", r.Servers[0].Description)
assert.Equal(t, 1, r.GoLow().Servers.KeyNode.Line) assert.Equal(t, 1, r.GoLow().Servers.KeyNode.Line)
} }
func TestPathItem_GetOperations(t *testing.T) { func TestPathItem_GetOperations(t *testing.T) {
yml := `get: yml := `get:
description: get description: get
put: put:
description: put description: put
@@ -55,46 +56,46 @@ trace:
description: trace description: trace
` `
var idxNode yaml.Node var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode) _ = yaml.Unmarshal([]byte(yml), &idxNode)
idx := index.NewSpecIndex(&idxNode) idx := index.NewSpecIndex(&idxNode)
var n v3.PathItem var n v3.PathItem
_ = low.BuildModel(&idxNode, &n) _ = low.BuildModel(&idxNode, &n)
_ = n.Build(idxNode.Content[0], idx) _ = 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) { func TestPathItem_MarshalYAML(t *testing.T) {
pi := &PathItem{ pi := &PathItem{
Description: "a path item", Description: "a path item",
Summary: "It's a test, don't worry about it, Jim", Summary: "It's a test, don't worry about it, Jim",
Servers: []*Server{ Servers: []*Server{
{ {
Description: "a server", Description: "a server",
}, },
}, },
Parameters: []*Parameter{ Parameters: []*Parameter{
{ {
Name: "I am a query parameter", Name: "I am a query parameter",
In: "query", In: "query",
}, },
}, },
Get: &Operation{ Get: &Operation{
Description: "a get operation", Description: "a get operation",
}, },
Post: &Operation{ Post: &Operation{
Description: "a 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 summary: It's a test, don't worry about it, Jim
get: get:
description: a get operation description: a get operation
@@ -106,5 +107,46 @@ parameters:
- name: I am a query parameter - name: I am a query parameter
in: query` 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 package v3
import ( import (
"github.com/pb33f/libopenapi/datamodel/low" "strings"
v3low "github.com/pb33f/libopenapi/datamodel/low/v3" "testing"
"github.com/pb33f/libopenapi/index"
"github.com/stretchr/testify/assert" "github.com/pb33f/libopenapi/datamodel/low"
"gopkg.in/yaml.v3" v3low "github.com/pb33f/libopenapi/datamodel/low/v3"
"strings" "github.com/pb33f/libopenapi/index"
"testing" "github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
) )
func TestPaths_MarshalYAML(t *testing.T) { func TestPaths_MarshalYAML(t *testing.T) {
yml := `/foo/bar/bizzle: yml := `/foo/bar/bizzle:
get: get:
description: get a bizzle description: get a bizzle
/jim/jam/jizzle: /jim/jam/jizzle:
@@ -25,31 +26,31 @@ func TestPaths_MarshalYAML(t *testing.T) {
get: get:
description: get a beer now.` description: get a beer now.`
var rootNode yaml.Node var rootNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &rootNode) _ = yaml.Unmarshal([]byte(yml), &rootNode)
var idxNode yaml.Node var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode) _ = yaml.Unmarshal([]byte(yml), &idxNode)
idx := index.NewSpecIndexWithConfig(&idxNode, index.CreateOpenAPIIndexConfig()) idx := index.NewSpecIndexWithConfig(&idxNode, index.CreateOpenAPIIndexConfig())
var n v3low.Paths var n v3low.Paths
err := low.BuildModel(&idxNode, &n) err := low.BuildModel(&idxNode, &n)
assert.NoError(t, err) assert.NoError(t, err)
err = n.Build(idxNode.Content[0], idx) err = n.Build(idxNode.Content[0], idx)
assert.NoError(t, err) assert.NoError(t, err)
high := NewPaths(&n) high := NewPaths(&n)
assert.NotNil(t, high) assert.NotNil(t, high)
rend, _ := high.Render() rend, _ := high.Render()
assert.Equal(t, yml, strings.TrimSpace(string(rend))) assert.Equal(t, yml, strings.TrimSpace(string(rend)))
// mutate // mutate
deprecated := true deprecated := true
high.PathItems["/beer"].Get.Deprecated = &deprecated high.PathItems["/beer"].Get.Deprecated = &deprecated
yml = `/foo/bar/bizzle: yml = `/foo/bar/bizzle:
get: get:
description: get a bizzle description: get a bizzle
/jim/jam/jizzle: /jim/jam/jizzle:
@@ -60,7 +61,59 @@ func TestPaths_MarshalYAML(t *testing.T) {
description: get a beer now. description: get a beer now.
deprecated: true` deprecated: true`
rend, _ = high.Render() rend, _ = high.Render()
assert.Equal(t, yml, strings.TrimSpace(string(rend))) 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 package v3
import ( import (
"github.com/stretchr/testify/assert" "strings"
"strings" "testing"
"testing"
"github.com/stretchr/testify/assert"
) )
func TestRequestBody_MarshalYAML(t *testing.T) { func TestRequestBody_MarshalYAML(t *testing.T) {
rb := true rb := true
req := &RequestBody{ req := &RequestBody{
Description: "beer", Description: "beer",
Required: &rb, Required: &rb,
Extensions: map[string]interface{}{"x-high-gravity": "why not?"}, Extensions: map[string]interface{}{"x-high-gravity": "why not?"},
} }
rend, _ := req.Render() rend, _ := req.Render()
desired := `description: beer desired := `description: beer
required: true required: true
x-high-gravity: why not?` 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) { func TestRequestBody_MarshalNoRequired(t *testing.T) {
rb := false rb := false
req := &RequestBody{ req := &RequestBody{
Description: "beer", Description: "beer",
Required: &rb, Required: &rb,
Extensions: map[string]interface{}{"x-high-gravity": "why not?"}, Extensions: map[string]interface{}{"x-high-gravity": "why not?"},
} }
rend, _ := req.Render() rend, _ := req.Render()
desired := `description: beer desired := `description: beer
required: false required: false
x-high-gravity: why not?` 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) { func TestRequestBody_MarshalRequiredNil(t *testing.T) {
req := &RequestBody{ req := &RequestBody{
Description: "beer", Description: "beer",
Extensions: map[string]interface{}{"x-high-gravity": "why not?"}, Extensions: map[string]interface{}{"x-high-gravity": "why not?"},
} }
rend, _ := req.Render() rend, _ := req.Render()
desired := `description: beer desired := `description: beer
x-high-gravity: why not?` 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 package v3
import ( import (
"strings"
"testing"
"github.com/pb33f/libopenapi/datamodel/low" "github.com/pb33f/libopenapi/datamodel/low"
v3 "github.com/pb33f/libopenapi/datamodel/low/v3" v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
"github.com/pb33f/libopenapi/index" "github.com/pb33f/libopenapi/index"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"strings"
"testing"
) )
// this test exists because the sample contract doesn't contain a // this test exists because the sample contract doesn't contain a
@@ -76,3 +77,31 @@ links:
assert.Equal(t, yml, strings.TrimSpace(string(rend))) 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 package v3
import ( import (
"strings"
"testing"
"github.com/pb33f/libopenapi/datamodel/low" "github.com/pb33f/libopenapi/datamodel/low"
v3 "github.com/pb33f/libopenapi/datamodel/low/v3" v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
"github.com/pb33f/libopenapi/index" "github.com/pb33f/libopenapi/index"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"strings"
"testing"
) )
// this test exists because the sample contract doesn't contain a // 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))) 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.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 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= 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 h1:/5QKeCBGdsInyDCyVNLbXyilb61MXGi9NP674f9Hobk=
github.com/vmware-labs/yaml-jsonpath v0.3.2/go.mod h1:U6whw1z03QyqgWdgXxvVnQ90zN1BWz5V+51Ewf8k+rQ= 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= 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.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 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 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-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-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=