Fixes to tests.

This commit is contained in:
Shawn Poulson
2023-09-25 09:47:50 -04:00
parent b6c05f5af0
commit c225546b04
10 changed files with 80 additions and 64 deletions

View File

@@ -56,7 +56,7 @@ func TestNewDocument_Security(t *testing.T) {
initTest() initTest()
h := NewDocument(lowDoc) h := NewDocument(lowDoc)
assert.Len(t, h.Security, 1) assert.Len(t, h.Security, 1)
assert.Len(t, h.Security[0].Requirements, 1) assert.Equal(t, 1, orderedmap.Len(h.Security[0].Requirements))
assert.Len(t, h.Security[0].Requirements.GetOrZero("OAuthScheme"), 2) assert.Len(t, h.Security[0].Requirements.GetOrZero("OAuthScheme"), 2)
} }
@@ -97,13 +97,13 @@ func TestNewDocument_Servers(t *testing.T) {
assert.Len(t, h.Servers, 2) assert.Len(t, h.Servers, 2)
assert.Equal(t, "{scheme}://api.pb33f.io", h.Servers[0].URL) assert.Equal(t, "{scheme}://api.pb33f.io", h.Servers[0].URL)
assert.Equal(t, "this is our main API server, for all fun API things.", h.Servers[0].Description) assert.Equal(t, "this is our main API server, for all fun API things.", h.Servers[0].Description)
assert.Len(t, h.Servers[0].Variables, 1) assert.Equal(t, 1, orderedmap.Len(h.Servers[0].Variables))
assert.Equal(t, "https", h.Servers[0].Variables.GetOrZero("scheme").Default) assert.Equal(t, "https", h.Servers[0].Variables.GetOrZero("scheme").Default)
assert.Len(t, h.Servers[0].Variables.GetOrZero("scheme").Enum, 2) assert.Len(t, h.Servers[0].Variables.GetOrZero("scheme").Enum, 2)
assert.Equal(t, "https://{domain}.{host}.com", h.Servers[1].URL) assert.Equal(t, "https://{domain}.{host}.com", h.Servers[1].URL)
assert.Equal(t, "this is our second API server, for all fun API things.", h.Servers[1].Description) assert.Equal(t, "this is our second API server, for all fun API things.", h.Servers[1].Description)
assert.Len(t, h.Servers[1].Variables, 2) assert.Equal(t, 2, orderedmap.Len(h.Servers[1].Variables))
assert.Equal(t, "api", h.Servers[1].Variables.GetOrZero("domain").Default) assert.Equal(t, "api", h.Servers[1].Variables.GetOrZero("domain").Default)
assert.Equal(t, "pb33f.io", h.Servers[1].Variables.GetOrZero("host").Default) assert.Equal(t, "pb33f.io", h.Servers[1].Variables.GetOrZero("host").Default)
@@ -363,7 +363,7 @@ func testBurgerShop(t *testing.T, h *Document, checkLines bool) {
) )
assert.Equal(t, 2, orderedmap.Len(okResp.Links)) assert.Equal(t, 2, orderedmap.Len(okResp.Links))
assert.Equal(t, "locateBurger", okResp.Links.GetOrZero("LocateBurger").OperationId) assert.Equal(t, "locateBurger", okResp.Links.GetOrZero("LocateBurger").OperationId)
assert.Len(t, burgersOp.Post.Security[0].Requirements, 1) assert.Equal(t, 1, orderedmap.Len(burgersOp.Post.Security[0].Requirements))
assert.Len(t, burgersOp.Post.Security[0].Requirements.GetOrZero("OAuthScheme"), 2) assert.Len(t, burgersOp.Post.Security[0].Requirements.GetOrZero("OAuthScheme"), 2)
assert.Equal(t, "read:burgers", burgersOp.Post.Security[0].Requirements.GetOrZero("OAuthScheme")[0]) assert.Equal(t, "read:burgers", burgersOp.Post.Security[0].Requirements.GetOrZero("OAuthScheme")[0])
assert.Len(t, burgersOp.Post.Servers, 1) assert.Len(t, burgersOp.Post.Servers, 1)

View File

@@ -10,6 +10,7 @@ import (
"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/pb33f/libopenapi/orderedmap"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
@@ -42,10 +43,10 @@ links:
r := NewResponse(&n) r := NewResponse(&n)
assert.Len(t, r.Headers, 1) assert.Equal(t, 1, orderedmap.Len(r.Headers))
assert.Len(t, r.Content, 1) assert.Equal(t, 1, orderedmap.Len(r.Content))
assert.Equal(t, "pizza!", r.Extensions["x-pizza-man"]) assert.Equal(t, "pizza!", r.Extensions["x-pizza-man"])
assert.Len(t, r.Links, 1) assert.Equal(t, 1, orderedmap.Len(r.Links))
assert.Equal(t, 1, r.GoLow().Description.KeyNode.Line) assert.Equal(t, 1, r.GoLow().Description.KeyNode.Line)
} }

View File

@@ -5,6 +5,7 @@ import (
"github.com/pb33f/libopenapi/datamodel/low" "github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/index" "github.com/pb33f/libopenapi/index"
"github.com/pb33f/libopenapi/orderedmap"
"github.com/pb33f/libopenapi/resolver" "github.com/pb33f/libopenapi/resolver"
"github.com/pb33f/libopenapi/utils" "github.com/pb33f/libopenapi/utils"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@@ -72,7 +73,7 @@ anyOf:
anyOfB: anyOfB:
type: string type: string
description: anyOfB description description: anyOfB description
example: 'anyOfBExp' example: 'anyOfBExp'
not: not:
type: object type: object
description: a not thing description: a not thing
@@ -84,7 +85,7 @@ not:
notB: notB:
type: string type: string
description: notB description description: notB description
example: 'notBExp' example: 'notBExp'
items: items:
type: object type: object
description: an items thing description: an items thing
@@ -134,11 +135,11 @@ properties:
attribute: true attribute: true
wrapped: false wrapped: false
x-pizza: love x-pizza: love
additionalProperties: additionalProperties:
why: yes why: yes
thatIs: true thatIs: true
additionalProperties: true additionalProperties: true
required: required:
- them - them
enum: enum:
- one - one
@@ -171,7 +172,7 @@ func Test_Schema(t *testing.T) {
assert.Equal(t, "something object", sch.Description.Value) assert.Equal(t, "something object", sch.Description.Value)
assert.True(t, sch.AdditionalProperties.Value.(bool)) assert.True(t, sch.AdditionalProperties.Value.(bool))
assert.Len(t, sch.Properties.Value, 2) assert.Equal(t, 2, orderedmap.Len(sch.Properties.Value))
v := sch.FindProperty("somethingB") v := sch.FindProperty("somethingB")
assert.Equal(t, "https://pb33f.io", v.Value.Schema().ExternalDocs.Value.URL.Value) assert.Equal(t, "https://pb33f.io", v.Value.Schema().ExternalDocs.Value.URL.Value)
@@ -204,7 +205,7 @@ func Test_Schema(t *testing.T) {
// check polymorphic values allOf // check polymorphic values allOf
f := sch.AllOf.Value[0].Value.Schema() f := sch.AllOf.Value[0].Value.Schema()
assert.Equal(t, "an allof thing", f.Description.Value) assert.Equal(t, "an allof thing", f.Description.Value)
assert.Len(t, f.Properties.Value, 2) assert.Equal(t, 2, orderedmap.Len(f.Properties.Value))
v = f.FindProperty("allOfA") v = f.FindProperty("allOfA")
assert.NotNil(t, v) assert.NotNil(t, v)
@@ -221,7 +222,7 @@ func Test_Schema(t *testing.T) {
// check polymorphic values anyOf // check polymorphic values anyOf
assert.Equal(t, "an anyOf thing", sch.AnyOf.Value[0].Value.Schema().Description.Value) assert.Equal(t, "an anyOf thing", sch.AnyOf.Value[0].Value.Schema().Description.Value)
assert.Len(t, sch.AnyOf.Value[0].Value.Schema().Properties.Value, 2) assert.Equal(t, 2, orderedmap.Len(sch.AnyOf.Value[0].Value.Schema().Properties.Value))
v = sch.AnyOf.Value[0].Value.Schema().FindProperty("anyOfA") v = sch.AnyOf.Value[0].Value.Schema().FindProperty("anyOfA")
assert.NotNil(t, v) assert.NotNil(t, v)
@@ -235,7 +236,7 @@ func Test_Schema(t *testing.T) {
// check polymorphic values oneOf // check polymorphic values oneOf
assert.Equal(t, "a oneof thing", sch.OneOf.Value[0].Value.Schema().Description.Value) assert.Equal(t, "a oneof thing", sch.OneOf.Value[0].Value.Schema().Description.Value)
assert.Len(t, sch.OneOf.Value[0].Value.Schema().Properties.Value, 2) assert.Equal(t, 2, orderedmap.Len(sch.OneOf.Value[0].Value.Schema().Properties.Value))
v = sch.OneOf.Value[0].Value.Schema().FindProperty("oneOfA") v = sch.OneOf.Value[0].Value.Schema().FindProperty("oneOfA")
assert.NotNil(t, v) assert.NotNil(t, v)
@@ -249,7 +250,7 @@ func Test_Schema(t *testing.T) {
// check values NOT // check values NOT
assert.Equal(t, "a not thing", sch.Not.Value.Schema().Description.Value) assert.Equal(t, "a not thing", sch.Not.Value.Schema().Description.Value)
assert.Len(t, sch.Not.Value.Schema().Properties.Value, 2) assert.Equal(t, 2, orderedmap.Len(sch.Not.Value.Schema().Properties.Value))
v = sch.Not.Value.Schema().FindProperty("notA") v = sch.Not.Value.Schema().FindProperty("notA")
assert.NotNil(t, v) assert.NotNil(t, v)
@@ -263,7 +264,7 @@ func Test_Schema(t *testing.T) {
// check values Items // check values Items
assert.Equal(t, "an items thing", sch.Items.Value.A.Schema().Description.Value) assert.Equal(t, "an items thing", sch.Items.Value.A.Schema().Description.Value)
assert.Len(t, sch.Items.Value.A.Schema().Properties.Value, 2) assert.Equal(t, 2, orderedmap.Len(sch.Items.Value.A.Schema().Properties.Value))
v = sch.Items.Value.A.Schema().FindProperty("itemsA") v = sch.Items.Value.A.Schema().FindProperty("itemsA")
assert.NotNil(t, v) assert.NotNil(t, v)
@@ -277,7 +278,7 @@ func Test_Schema(t *testing.T) {
// check values PrefixItems // check values PrefixItems
assert.Equal(t, "an items thing", sch.PrefixItems.Value[0].Value.Schema().Description.Value) assert.Equal(t, "an items thing", sch.PrefixItems.Value[0].Value.Schema().Description.Value)
assert.Len(t, sch.PrefixItems.Value[0].Value.Schema().Properties.Value, 2) assert.Equal(t, 2, orderedmap.Len(sch.PrefixItems.Value[0].Value.Schema().Properties.Value))
v = sch.PrefixItems.Value[0].Value.Schema().FindProperty("itemsA") v = sch.PrefixItems.Value[0].Value.Schema().FindProperty("itemsA")
assert.NotNil(t, v) assert.NotNil(t, v)
@@ -956,7 +957,7 @@ func TestExtractSchema(t *testing.T) {
assert.NoError(t, mErr) assert.NoError(t, mErr)
idx := index.NewSpecIndex(&iNode) idx := index.NewSpecIndex(&iNode)
yml = `schema: yml = `schema:
type: object type: object
properties: properties:
aValue: aValue:
@@ -974,7 +975,7 @@ func TestExtractSchema(t *testing.T) {
func TestExtractSchema_DefaultPrimitive(t *testing.T) { func TestExtractSchema_DefaultPrimitive(t *testing.T) {
yml := ` yml := `
schema: schema:
type: object type: object
default: 5` default: 5`
@@ -990,7 +991,7 @@ schema:
func TestExtractSchema_ConstPrimitive(t *testing.T) { func TestExtractSchema_ConstPrimitive(t *testing.T) {
yml := ` yml := `
schema: schema:
type: object type: object
const: 5` const: 5`
@@ -1016,7 +1017,7 @@ func TestExtractSchema_Ref(t *testing.T) {
assert.NoError(t, mErr) assert.NoError(t, mErr)
idx := index.NewSpecIndex(&iNode) idx := index.NewSpecIndex(&iNode)
yml = `schema: yml = `schema:
$ref: '#/components/schemas/Something'` $ref: '#/components/schemas/Something'`
var idxNode yaml.Node var idxNode yaml.Node
@@ -1040,7 +1041,7 @@ func TestExtractSchema_Ref_Fail(t *testing.T) {
assert.NoError(t, mErr) assert.NoError(t, mErr)
idx := index.NewSpecIndex(&iNode) idx := index.NewSpecIndex(&iNode)
yml = `schema: yml = `schema:
$ref: '#/components/schemas/Missing'` $ref: '#/components/schemas/Missing'`
var idxNode yaml.Node var idxNode yaml.Node
@@ -1061,7 +1062,7 @@ func TestExtractSchema_CheckChildPropCircular(t *testing.T) {
- nothing - nothing
Nothing: Nothing:
properties: properties:
something: something:
$ref: '#/components/schemas/Something' $ref: '#/components/schemas/Something'
required: required:
- something - something

View File

@@ -4,9 +4,11 @@
package base package base
import ( import (
"testing"
"github.com/pb33f/libopenapi/orderedmap"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"testing"
) )
func TestSecurityRequirement_Build(t *testing.T) { func TestSecurityRequirement_Build(t *testing.T) {
@@ -14,7 +16,7 @@ func TestSecurityRequirement_Build(t *testing.T) {
yml := `one: yml := `one:
- two - two
- three - three
four: four:
- five - five
- six` - six`
@@ -22,7 +24,7 @@ four:
var idxNode yaml.Node var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode) _ = yaml.Unmarshal([]byte(yml), &idxNode)
yml2 := `four: yml2 := `four:
- six - six
- five - five
one: one:
@@ -36,7 +38,7 @@ one:
_ = sr.Build(nil, idxNode.Content[0], nil) _ = sr.Build(nil, idxNode.Content[0], nil)
_ = sr2.Build(nil, idxNode2.Content[0], nil) _ = sr2.Build(nil, idxNode2.Content[0], nil)
assert.Len(t, sr.Requirements.Value, 2) assert.Equal(t, 2, orderedmap.Len(sr.Requirements.Value))
assert.Len(t, sr.GetKeys(), 2) assert.Len(t, sr.GetKeys(), 2)
assert.Len(t, sr.FindRequirement("one"), 2) assert.Len(t, sr.FindRequirement("one"), 2)
assert.Equal(t, sr.Hash(), sr2.Hash()) assert.Equal(t, sr.Hash(), sr2.Hash())

View File

@@ -4,12 +4,14 @@
package v2 package v2
import ( import (
"testing"
"github.com/pb33f/libopenapi/datamodel/low" "github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/datamodel/low/base" "github.com/pb33f/libopenapi/datamodel/low/base"
"github.com/pb33f/libopenapi/index" "github.com/pb33f/libopenapi/index"
"github.com/pb33f/libopenapi/orderedmap"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"testing"
) )
func TestOperation_Build_ExternalDocs(t *testing.T) { func TestOperation_Build_ExternalDocs(t *testing.T) {
@@ -180,7 +182,7 @@ security:
assert.Equal(t, "theMagicCastle", n.GetOperationId().Value) assert.Equal(t, "theMagicCastle", n.GetOperationId().Value)
assert.Len(t, n.GetParameters().Value, 1) assert.Len(t, n.GetParameters().Value, 1)
assert.True(t, n.GetDeprecated().Value) assert.True(t, n.GetDeprecated().Value)
assert.Len(t, n.GetResponses().Value.(*Responses).Codes, 1) assert.Equal(t, 1, orderedmap.Len(n.GetResponses().Value.(*Responses).Codes))
assert.Len(t, n.GetSecurity().Value, 1) assert.Len(t, n.GetSecurity().Value, 1)
assert.Len(t, n.GetExtensions(), 1) assert.Len(t, n.GetExtensions(), 1)
} }

View File

@@ -4,11 +4,13 @@
package v2 package v2
import ( import (
"testing"
"github.com/pb33f/libopenapi/datamodel/low" "github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/index" "github.com/pb33f/libopenapi/index"
"github.com/pb33f/libopenapi/orderedmap"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"testing"
) )
func TestSecurityScheme_Build_Borked(t *testing.T) { func TestSecurityScheme_Build_Borked(t *testing.T) {
@@ -47,7 +49,7 @@ func TestSecurityScheme_Build_Scopes(t *testing.T) {
err = n.Build(nil, idxNode.Content[0], idx) err = n.Build(nil, idxNode.Content[0], idx)
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, n.Scopes.Value.Values, 2) assert.Equal(t, 2, orderedmap.Len(n.Scopes.Value.Values))
} }

View File

@@ -5,10 +5,12 @@ package v2
import ( import (
"fmt" "fmt"
"github.com/pb33f/libopenapi/datamodel"
"github.com/stretchr/testify/assert"
"io/ioutil" "io/ioutil"
"testing" "testing"
"github.com/pb33f/libopenapi/datamodel"
"github.com/pb33f/libopenapi/orderedmap"
"github.com/stretchr/testify/assert"
) )
var doc *Swagger var doc *Swagger
@@ -55,13 +57,13 @@ func TestCreateDocument(t *testing.T) {
assert.Equal(t, "1.0.6", doc.Info.Value.Version.Value) assert.Equal(t, "1.0.6", doc.Info.Value.Version.Value)
assert.Equal(t, "petstore.swagger.io", doc.Host.Value) assert.Equal(t, "petstore.swagger.io", doc.Host.Value)
assert.Equal(t, "/v2", doc.BasePath.Value) assert.Equal(t, "/v2", doc.BasePath.Value)
assert.Len(t, doc.Parameters.Value.Definitions, 1) assert.Equal(t, 1, orderedmap.Len(doc.Parameters.Value.Definitions))
assert.Len(t, doc.Tags.Value, 3) assert.Len(t, doc.Tags.Value, 3)
assert.Len(t, doc.Schemes.Value, 2) assert.Len(t, doc.Schemes.Value, 2)
assert.Len(t, doc.Definitions.Value.Schemas, 6) assert.Equal(t, 6, orderedmap.Len(doc.Definitions.Value.Schemas))
assert.Len(t, doc.SecurityDefinitions.Value.Definitions, 3) assert.Equal(t, 3, orderedmap.Len(doc.SecurityDefinitions.Value.Definitions))
assert.Len(t, doc.Paths.Value.PathItems, 15) assert.Equal(t, 15, orderedmap.Len(doc.Paths.Value.PathItems))
assert.Len(t, doc.Responses.Value.Definitions, 2) assert.Equal(t, 2, orderedmap.Len(doc.Responses.Value.Definitions))
assert.Equal(t, "http://swagger.io", doc.ExternalDocs.Value.URL.Value) assert.Equal(t, "http://swagger.io", doc.ExternalDocs.Value.URL.Value)
assert.Equal(t, true, doc.FindExtension("x-pet").Value) assert.Equal(t, true, doc.FindExtension("x-pet").Value)
assert.Equal(t, true, doc.FindExtension("X-Pet").Value) assert.Equal(t, true, doc.FindExtension("X-Pet").Value)
@@ -101,7 +103,7 @@ func TestCreateDocument_SecurityDefinitions(t *testing.T) {
petStoreAuth := doc.SecurityDefinitions.Value.FindSecurityDefinition("petstore_auth") petStoreAuth := doc.SecurityDefinitions.Value.FindSecurityDefinition("petstore_auth")
assert.Equal(t, "oauth2", petStoreAuth.Value.Type.Value) assert.Equal(t, "oauth2", petStoreAuth.Value.Type.Value)
assert.Equal(t, "implicit", petStoreAuth.Value.Flow.Value) assert.Equal(t, "implicit", petStoreAuth.Value.Flow.Value)
assert.Len(t, petStoreAuth.Value.Scopes.Value.Values, 2) assert.Equal(t, 2, orderedmap.Len(petStoreAuth.Value.Scopes.Value.Values))
assert.Equal(t, "read your pets", petStoreAuth.Value.Scopes.Value.FindScope("read:pets").Value) assert.Equal(t, "read your pets", petStoreAuth.Value.Scopes.Value.FindScope("read:pets").Value)
} }
@@ -109,7 +111,7 @@ func TestCreateDocument_Definitions(t *testing.T) {
initTest() initTest()
apiResp := doc.Definitions.Value.FindSchema("ApiResponse").Value.Schema() apiResp := doc.Definitions.Value.FindSchema("ApiResponse").Value.Schema()
assert.NotNil(t, apiResp) assert.NotNil(t, apiResp)
assert.Len(t, apiResp.Properties.Value, 3) assert.Equal(t, 3, orderedmap.Len(apiResp.Properties.Value))
assert.Equal(t, "integer", apiResp.FindProperty("code").Value.Schema().Type.Value.A) assert.Equal(t, "integer", apiResp.FindProperty("code").Value.Schema().Type.Value.A)
pet := doc.Definitions.Value.FindSchema("Pet").Value.Schema() pet := doc.Definitions.Value.FindSchema("Pet").Value.Schema()

View File

@@ -4,11 +4,13 @@
package v3 package v3
import ( import (
"testing"
"github.com/pb33f/libopenapi/datamodel/low" "github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/index" "github.com/pb33f/libopenapi/index"
"github.com/pb33f/libopenapi/orderedmap"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"testing"
) )
func TestCallback_Build_Success(t *testing.T) { func TestCallback_Build_Success(t *testing.T) {
@@ -17,7 +19,7 @@ func TestCallback_Build_Success(t *testing.T) {
post: post:
requestBody: requestBody:
description: Callback payload description: Callback payload
content: content:
'application/json': 'application/json':
schema: schema:
type: string type: string
@@ -36,7 +38,7 @@ func TestCallback_Build_Success(t *testing.T) {
err = n.Build(nil, rootNode.Content[0], nil) err = n.Build(nil, rootNode.Content[0], nil)
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, n.Expression.Value, 1) assert.Equal(t, 1, orderedmap.Len(n.Expression.Value))
} }
@@ -102,7 +104,7 @@ func TestCallback_Build_Using_InlineRef(t *testing.T) {
err = n.Build(nil, rootNode.Content[0], idx) err = n.Build(nil, rootNode.Content[0], idx)
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, n.Expression.Value, 1) assert.Equal(t, 1, orderedmap.Len(n.Expression.Value))
exp := n.FindExpression("{$request.query.queryUrl}") exp := n.FindExpression("{$request.query.queryUrl}")
assert.NotNil(t, exp.Value) assert.NotNil(t, exp.Value)

View File

@@ -225,7 +225,7 @@ func TestCreateDocument_Info(t *testing.T) {
func TestCreateDocument_WebHooks(t *testing.T) { func TestCreateDocument_WebHooks(t *testing.T) {
initTest() initTest()
assert.Len(t, doc.Webhooks.Value, 1) assert.Equal(t, 1, orderedmap.Len(doc.Webhooks.Value))
for pair := orderedmap.First(doc.Webhooks.Value); pair != nil; pair = pair.Next() { for pair := orderedmap.First(doc.Webhooks.Value); pair != nil; pair = pair.Next() {
// a nice deep model should be available for us. // a nice deep model should be available for us.
assert.Equal(t, "Information about a new burger", assert.Equal(t, "Information about a new burger",
@@ -255,7 +255,7 @@ func TestCreateDocument_Servers(t *testing.T) {
// server 1 // server 1
assert.Equal(t, "{scheme}://api.pb33f.io", server1.URL.Value) assert.Equal(t, "{scheme}://api.pb33f.io", server1.URL.Value)
assert.NotEmpty(t, server1.Description.Value) assert.NotEmpty(t, server1.Description.Value)
assert.Len(t, server1.Variables.Value, 1) assert.Equal(t, 1, orderedmap.Len(server1.Variables.Value))
assert.Len(t, server1.FindVariable("scheme").Value.Enum, 2) assert.Len(t, server1.FindVariable("scheme").Value.Enum, 2)
assert.Equal(t, server1.FindVariable("scheme").Value.Default.Value, "https") assert.Equal(t, server1.FindVariable("scheme").Value.Default.Value, "https")
assert.NotEmpty(t, server1.FindVariable("scheme").Value.Description.Value) assert.NotEmpty(t, server1.FindVariable("scheme").Value.Description.Value)
@@ -263,7 +263,7 @@ func TestCreateDocument_Servers(t *testing.T) {
// server 2 // server 2
assert.Equal(t, "https://{domain}.{host}.com", server2.URL.Value) assert.Equal(t, "https://{domain}.{host}.com", server2.URL.Value)
assert.NotEmpty(t, server2.Description.Value) assert.NotEmpty(t, server2.Description.Value)
assert.Len(t, server2.Variables.Value, 2) assert.Equal(t, 2, orderedmap.Len(server2.Variables.Value))
assert.Equal(t, "api", server2.FindVariable("domain").Value.Default.Value) assert.Equal(t, "api", server2.FindVariable("domain").Value.Default.Value)
assert.NotEmpty(t, server2.FindVariable("domain").Value.Description.Value) assert.NotEmpty(t, server2.FindVariable("domain").Value.Description.Value)
assert.NotEmpty(t, server2.FindVariable("host").Value.Description.Value) assert.NotEmpty(t, server2.FindVariable("host").Value.Description.Value)
@@ -319,7 +319,7 @@ func TestCreateDocument_Tags(t *testing.T) {
func TestCreateDocument_Paths(t *testing.T) { func TestCreateDocument_Paths(t *testing.T) {
initTest() initTest()
assert.Len(t, doc.Paths.Value.PathItems, 5) assert.Equal(t, 5, orderedmap.Len(doc.Paths.Value.PathItems))
burgerId := doc.Paths.Value.FindPath("/burgers/{burgerId}") burgerId := doc.Paths.Value.FindPath("/burgers/{burgerId}")
assert.NotNil(t, burgerId) assert.NotNil(t, burgerId)
assert.Len(t, burgerId.Value.Get.Value.Parameters.Value, 2) assert.Len(t, burgerId.Value.Get.Value.Parameters.Value, 2)
@@ -335,7 +335,7 @@ func TestCreateDocument_Paths(t *testing.T) {
encoding := pContent.Value.FindPropertyEncoding("burgerTheme") encoding := pContent.Value.FindPropertyEncoding("burgerTheme")
assert.NotNil(t, encoding.Value) assert.NotNil(t, encoding.Value)
assert.Len(t, encoding.Value.Headers.Value, 1) assert.Equal(t, 1, orderedmap.Len(encoding.Value.Headers.Value))
header := encoding.Value.FindHeader("someHeader") header := encoding.Value.FindHeader("someHeader")
assert.NotNil(t, header.Value) assert.NotNil(t, header.Value)
@@ -357,8 +357,8 @@ func TestCreateDocument_Paths(t *testing.T) {
content := requestBody.FindContent("application/json").Value content := requestBody.FindContent("application/json").Value
assert.NotNil(t, content) assert.NotNil(t, content)
assert.Len(t, content.Schema.Value.Schema().Properties.Value, 4) assert.Equal(t, 4, orderedmap.Len(content.Schema.Value.Schema().Properties.Value))
assert.Len(t, content.GetAllExamples(), 2) assert.Equal(t, 2, orderedmap.Len(content.GetAllExamples()))
ex := content.FindExample("pbjBurger") ex := content.FindExample("pbjBurger")
assert.NotNil(t, ex.Value) assert.NotNil(t, ex.Value)
@@ -388,14 +388,14 @@ func TestCreateDocument_Paths(t *testing.T) {
// check responses // check responses
responses := burgersPost.Responses.Value responses := burgersPost.Responses.Value
assert.NotNil(t, responses) assert.NotNil(t, responses)
assert.Len(t, responses.Codes, 3) assert.Equal(t, 3, orderedmap.Len(responses.Codes))
okCode := responses.FindResponseByCode("200") okCode := responses.FindResponseByCode("200")
assert.NotNil(t, okCode.Value) assert.NotNil(t, okCode.Value)
assert.Equal(t, "A tasty burger for you to eat.", okCode.Value.Description.Value) assert.Equal(t, "A tasty burger for you to eat.", okCode.Value.Description.Value)
// check headers are populated // check headers are populated
assert.Len(t, okCode.Value.Headers.Value, 1) assert.Equal(t, 1, orderedmap.Len(okCode.Value.Headers.Value))
okheader := okCode.Value.FindHeader("UseOil") okheader := okCode.Value.FindHeader("UseOil")
assert.NotNil(t, okheader.Value) assert.NotNil(t, okheader.Value)
assert.Equal(t, "this is a header example for UseOil", okheader.Value.Description.Value) assert.Equal(t, "this is a header example for UseOil", okheader.Value.Description.Value)
@@ -421,7 +421,7 @@ func TestCreateDocument_Paths(t *testing.T) {
// check links // check links
links := okCode.Value.Links links := okCode.Value.Links
assert.NotNil(t, links.Value) assert.NotNil(t, links.Value)
assert.Len(t, links.Value, 2) assert.Equal(t, 2, orderedmap.Len(links.Value))
assert.Equal(t, "locateBurger", okCode.Value.FindLink("LocateBurger").Value.OperationId.Value) assert.Equal(t, "locateBurger", okCode.Value.FindLink("LocateBurger").Value.OperationId.Value)
locateBurger := okCode.Value.FindLink("LocateBurger").Value locateBurger := okCode.Value.FindLink("LocateBurger").Value

View File

@@ -6,9 +6,11 @@ package renderer
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
highbase "github.com/pb33f/libopenapi/datamodel/high/base"
"gopkg.in/yaml.v3"
"reflect" "reflect"
highbase "github.com/pb33f/libopenapi/datamodel/high/base"
"github.com/pb33f/libopenapi/orderedmap"
"gopkg.in/yaml.v3"
) )
const Example = "Example" const Example = "Example"
@@ -25,7 +27,7 @@ const (
// MockGenerator is used to generate mocks for high-level mockable structs or *base.Schema pointers. // MockGenerator is used to generate mocks for high-level mockable structs or *base.Schema pointers.
// The mock generator will attempt to generate a mock from a struct using the following fields: // The mock generator will attempt to generate a mock from a struct using the following fields:
// - Example: any type, this is the default example to use if no examples are present. // - Example: any type, this is the default example to use if no examples are present.
// - Examples: map[string]*base.Example, this is a map of examples keyed by name. // - Examples: orderedmap.Map[string, *base.Example], this is a map of examples keyed by name.
// - Schema: *base.SchemaProxy, this is the schema to use if no examples are present. // - Schema: *base.SchemaProxy, this is the schema to use if no examples are present.
// //
// The mock generator will attempt to generate a mock from a *base.Schema pointer. // The mock generator will attempt to generate a mock from a *base.Schema pointer.
@@ -59,7 +61,7 @@ func (mg *MockGenerator) SetPretty() {
// GenerateMock generates a mock for a given high-level mockable struct. The mockable struct must contain the following fields: // GenerateMock generates a mock for a given high-level mockable struct. The mockable struct must contain the following fields:
// Example: any type, this is the default example to use if no examples are present. // Example: any type, this is the default example to use if no examples are present.
// Examples: map[string]*base.Example, this is a map of examples keyed by name. // Examples: orderedmap.Map[string, *base.Example], this is a map of examples keyed by name.
// Schema: *base.SchemaProxy, this is the schema to use if no examples are present. // Schema: *base.SchemaProxy, this is the schema to use if no examples are present.
// The name parameter is optional, if provided, the mock generator will attempt to find an example with the given name. // The name parameter is optional, if provided, the mock generator will attempt to find an example with the given name.
// If no name is provided, the first example will be used. // If no name is provided, the first example will be used.
@@ -98,18 +100,20 @@ func (mg *MockGenerator) GenerateMock(mock any, name string) ([]byte, error) {
examplesValue := examples.Interface() examplesValue := examples.Interface()
if examplesValue != nil && !examples.IsNil() { if examplesValue != nil && !examples.IsNil() {
// cast examples to map[string]interface{} // cast examples to orderedmap.Map[string, any]
examplesMap := examplesValue.(map[string]*highbase.Example) examplesMap := examplesValue.(orderedmap.Map[string, *highbase.Example])
// if the name is not empty, try and find the example by name // if the name is not empty, try and find the example by name
for k, exp := range examplesMap { for pair := orderedmap.First(examplesMap); pair != nil; pair = pair.Next() {
k, exp := pair.Key(), pair.Value()
if k == name { if k == name {
return mg.renderMock(exp.Value), nil return mg.renderMock(exp.Value), nil
} }
} }
// if the name is empty, just return the first example // if the name is empty, just return the first example
for _, exp := range examplesMap { for pair := orderedmap.First(examplesMap); pair != nil; pair = pair.Next() {
exp := pair.Value()
return mg.renderMock(exp.Value), nil return mg.renderMock(exp.Value), nil
} }
} }