swagger with 100% coverage

so far, so good!
This commit is contained in:
Dave Shanley
2022-09-11 12:12:27 -04:00
parent 3d281f44df
commit 165b835f3e
11 changed files with 332 additions and 122 deletions

View File

@@ -84,7 +84,11 @@ func NewHeader(header *low.Header) *Header {
h.UniqueItems = header.UniqueItems.IsEmpty() h.UniqueItems = header.UniqueItems.IsEmpty()
} }
if !header.Enum.IsEmpty() { if !header.Enum.IsEmpty() {
h.Enum = header.Enum.Value var enums []string
for e := range header.Enum.Value {
enums = append(enums, header.Enum.Value[e].Value)
}
h.Enum = enums
} }
if !header.MultipleOf.IsEmpty() { if !header.MultipleOf.IsEmpty() {
h.MultipleOf = header.MultipleOf.Value h.MultipleOf = header.MultipleOf.Value

View File

@@ -33,7 +33,7 @@ func NewItems(items *low.Items) *Items {
i.Type = items.Type.Value i.Type = items.Type.Value
} }
if !items.Format.IsEmpty() { if !items.Format.IsEmpty() {
i.Format = items.Type.Value i.Format = items.Format.Value
} }
if !items.Items.IsEmpty() { if !items.Items.IsEmpty() {
i.Items = NewItems(items.Items.Value) i.Items = NewItems(items.Items.Value)
@@ -53,7 +53,7 @@ func NewItems(items *low.Items) *Items {
if !items.Minimum.IsEmpty() { if !items.Minimum.IsEmpty() {
i.Minimum = items.Minimum.Value i.Minimum = items.Minimum.Value
} }
if !items.ExclusiveMinimum.Value { if !items.ExclusiveMinimum.IsEmpty() {
i.ExclusiveMinimum = items.ExclusiveMinimum.Value i.ExclusiveMinimum = items.ExclusiveMinimum.Value
} }
if !items.MaxLength.IsEmpty() { if !items.MaxLength.IsEmpty() {
@@ -72,10 +72,14 @@ func NewItems(items *low.Items) *Items {
i.MaxItems = items.MaxItems.Value i.MaxItems = items.MaxItems.Value
} }
if !items.UniqueItems.IsEmpty() { if !items.UniqueItems.IsEmpty() {
i.UniqueItems = items.UniqueItems.IsEmpty() i.UniqueItems = items.UniqueItems.Value
} }
if !items.Enum.IsEmpty() { if !items.Enum.IsEmpty() {
i.Enum = items.Enum.Value var enums []string
for e := range items.Enum.Value {
enums = append(enums, items.Enum.Value[e].Value)
}
i.Enum = enums
} }
if !items.MultipleOf.IsEmpty() { if !items.MultipleOf.IsEmpty() {
i.MultipleOf = items.MultipleOf.Value i.MultipleOf = items.MultipleOf.Value

View File

@@ -51,7 +51,7 @@ func NewParameter(parameter *low.Parameter) *Parameter {
p.Type = parameter.Type.Value p.Type = parameter.Type.Value
} }
if !parameter.Format.IsEmpty() { if !parameter.Format.IsEmpty() {
p.Format = parameter.Type.Value p.Format = parameter.Format.Value
} }
if !parameter.Description.IsEmpty() { if !parameter.Description.IsEmpty() {
p.Description = parameter.Description.Value p.Description = parameter.Description.Value
@@ -83,7 +83,7 @@ func NewParameter(parameter *low.Parameter) *Parameter {
if !parameter.Minimum.IsEmpty() { if !parameter.Minimum.IsEmpty() {
p.Minimum = parameter.Minimum.Value p.Minimum = parameter.Minimum.Value
} }
if !parameter.ExclusiveMinimum.Value { if !parameter.ExclusiveMinimum.IsEmpty() {
p.ExclusiveMinimum = parameter.ExclusiveMinimum.Value p.ExclusiveMinimum = parameter.ExclusiveMinimum.Value
} }
if !parameter.MaxLength.IsEmpty() { if !parameter.MaxLength.IsEmpty() {
@@ -102,10 +102,14 @@ func NewParameter(parameter *low.Parameter) *Parameter {
p.MaxItems = parameter.MaxItems.Value p.MaxItems = parameter.MaxItems.Value
} }
if !parameter.UniqueItems.IsEmpty() { if !parameter.UniqueItems.IsEmpty() {
p.UniqueItems = parameter.UniqueItems.IsEmpty() p.UniqueItems = parameter.UniqueItems.Value
} }
if !parameter.Enum.IsEmpty() { if !parameter.Enum.IsEmpty() {
p.Enum = parameter.Enum.Value var enums []string
for e := range parameter.Enum.Value {
enums = append(enums, parameter.Enum.Value[e].Value)
}
p.Enum = enums
} }
if !parameter.MultipleOf.IsEmpty() { if !parameter.MultipleOf.IsEmpty() {
p.MultipleOf = parameter.MultipleOf.Value p.MultipleOf = parameter.MultipleOf.Value

View File

@@ -40,6 +40,7 @@ func NewPaths(paths *low.Paths) *Paths {
pathItems[res.key] = res.result pathItems[res.key] = res.result
} }
} }
p.PathItems = pathItems
} }
return p return p
} }

View File

@@ -33,6 +33,7 @@ func NewResponse(response *low.Response) *Response {
for k := range response.Headers.Value { for k := range response.Headers.Value {
headers[k.Value] = NewHeader(response.Headers.Value[k].Value) headers[k.Value] = NewHeader(response.Headers.Value[k].Value)
} }
r.Headers = headers
} }
if !response.Examples.IsEmpty() { if !response.Examples.IsEmpty() {
r.Examples = NewExamples(response.Examples.Value) r.Examples = NewExamples(response.Examples.Value)

View File

@@ -46,6 +46,7 @@ func NewResponses(responses *low.Responses) *Responses {
resp[res.key] = res.result resp[res.key] = res.result
} }
} }
r.Codes = resp
} }
return r return r
} }

View File

@@ -4,162 +4,277 @@
package v2 package v2
import ( import (
"github.com/pb33f/libopenapi/datamodel" "github.com/pb33f/libopenapi/datamodel"
v2 "github.com/pb33f/libopenapi/datamodel/low/2.0" v2 "github.com/pb33f/libopenapi/datamodel/low/2.0"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"io/ioutil" "io/ioutil"
"testing" "testing"
) )
var doc *v2.Swagger var doc *v2.Swagger
func initTest() { func initTest() {
data, _ := ioutil.ReadFile("../../../test_specs/petstorev2-complete.yaml") data, _ := ioutil.ReadFile("../../../test_specs/petstorev2-complete.yaml")
info, _ := datamodel.ExtractSpecInfo(data) info, _ := datamodel.ExtractSpecInfo(data)
var err []error var err []error
doc, err = v2.CreateDocument(info) doc, err = v2.CreateDocument(info)
if err != nil { if err != nil {
panic("broken something") panic("broken something")
} }
} }
func TestNewSwaggerDocument(t *testing.T) { func TestNewSwaggerDocument(t *testing.T) {
initTest() initTest()
h := NewSwaggerDocument(doc) h := NewSwaggerDocument(doc)
assert.NotNil(t, h) assert.NotNil(t, h)
} }
func BenchmarkNewDocument(b *testing.B) { func BenchmarkNewDocument(b *testing.B) {
initTest() initTest()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
_ = NewSwaggerDocument(doc) _ = NewSwaggerDocument(doc)
} }
} }
func TestNewSwaggerDocument_Base(t *testing.T) { func TestNewSwaggerDocument_Base(t *testing.T) {
initTest() initTest()
highDoc := NewSwaggerDocument(doc) highDoc := NewSwaggerDocument(doc)
assert.Equal(t, "2.0", highDoc.Swagger) assert.Equal(t, "2.0", highDoc.Swagger)
assert.True(t, highDoc.Extensions["x-pet"].(bool)) assert.True(t, highDoc.Extensions["x-pet"].(bool))
assert.Equal(t, "petstore.swagger.io", highDoc.Host) assert.Equal(t, "petstore.swagger.io", highDoc.Host)
assert.Equal(t, "/v2", highDoc.BasePath) assert.Equal(t, "/v2", highDoc.BasePath)
assert.Len(t, highDoc.Schemes, 2) assert.Len(t, highDoc.Schemes, 2)
assert.Equal(t, "https", highDoc.Schemes[0]) assert.Equal(t, "https", highDoc.Schemes[0])
assert.Len(t, highDoc.Consumes, 2) assert.Len(t, highDoc.Consumes, 2)
assert.Equal(t, "application/json", highDoc.Consumes[0]) assert.Equal(t, "application/json", highDoc.Consumes[0])
assert.Len(t, highDoc.Produces, 1) assert.Len(t, highDoc.Produces, 1)
assert.Equal(t, "application/json", highDoc.Produces[0]) assert.Equal(t, "application/json", highDoc.Produces[0])
wentLow := highDoc.GoLow() wentLow := highDoc.GoLow()
assert.Equal(t, 16, wentLow.Host.ValueNode.Line) assert.Equal(t, 16, wentLow.Host.ValueNode.Line)
assert.Equal(t, 7, wentLow.Host.ValueNode.Column) assert.Equal(t, 7, wentLow.Host.ValueNode.Column)
} }
func TestNewSwaggerDocument_Info(t *testing.T) { func TestNewSwaggerDocument_Info(t *testing.T) {
initTest() initTest()
highDoc := NewSwaggerDocument(doc) highDoc := NewSwaggerDocument(doc)
assert.Equal(t, "1.0.6", highDoc.Info.Version) assert.Equal(t, "1.0.6", highDoc.Info.Version)
assert.NotEmpty(t, highDoc.Info.Description) assert.NotEmpty(t, highDoc.Info.Description)
assert.Equal(t, "Swagger Petstore", highDoc.Info.Title) assert.Equal(t, "Swagger Petstore", highDoc.Info.Title)
assert.Equal(t, "Apache 2.0", highDoc.Info.License.Name) assert.Equal(t, "Apache 2.0", highDoc.Info.License.Name)
assert.Equal(t, "http://www.apache.org/licenses/LICENSE-2.0.html", highDoc.Info.License.URL) assert.Equal(t, "http://www.apache.org/licenses/LICENSE-2.0.html", highDoc.Info.License.URL)
assert.Equal(t, "apiteam@swagger.io", highDoc.Info.Contact.Email) assert.Equal(t, "apiteam@swagger.io", highDoc.Info.Contact.Email)
wentLow := highDoc.Info.Contact.GoLow() wentLow := highDoc.Info.Contact.GoLow()
assert.Equal(t, 12, wentLow.Email.ValueNode.Line) assert.Equal(t, 12, wentLow.Email.ValueNode.Line)
assert.Equal(t, 12, wentLow.Email.ValueNode.Column) assert.Equal(t, 12, wentLow.Email.ValueNode.Column)
wentLowLic := highDoc.Info.License.GoLow() wentLowLic := highDoc.Info.License.GoLow()
assert.Equal(t, 14, wentLowLic.Name.ValueNode.Line) assert.Equal(t, 14, wentLowLic.Name.ValueNode.Line)
assert.Equal(t, 11, wentLowLic.Name.ValueNode.Column) assert.Equal(t, 11, wentLowLic.Name.ValueNode.Column)
} }
func TestNewSwaggerDocument_Parameters(t *testing.T) { func TestNewSwaggerDocument_Parameters(t *testing.T) {
initTest() initTest()
highDoc := NewSwaggerDocument(doc) highDoc := NewSwaggerDocument(doc)
params := highDoc.Parameters params := highDoc.Parameters
assert.Len(t, params.Definitions, 1) assert.Len(t, params.Definitions, 1)
assert.Equal(t, "query", params.Definitions["simpleParam"].In) assert.Equal(t, "query", params.Definitions["simpleParam"].In)
assert.Equal(t, "simple", params.Definitions["simpleParam"].Name) assert.Equal(t, "simple", params.Definitions["simpleParam"].Name)
assert.Equal(t, "string", params.Definitions["simpleParam"].Type) assert.Equal(t, "string", params.Definitions["simpleParam"].Type)
assert.Equal(t, "nuggets", params.Definitions["simpleParam"].Extensions["x-chicken"]) assert.Equal(t, "nuggets", params.Definitions["simpleParam"].Extensions["x-chicken"])
wentLow := params.GoLow() wentLow := params.GoLow()
assert.Equal(t, 20, wentLow.FindParameter("simpleParam").ValueNode.Line) assert.Equal(t, 20, wentLow.FindParameter("simpleParam").ValueNode.Line)
assert.Equal(t, 5, wentLow.FindParameter("simpleParam").ValueNode.Column) assert.Equal(t, 5, wentLow.FindParameter("simpleParam").ValueNode.Column)
wentLower := params.Definitions["simpleParam"].GoLow() wentLower := params.Definitions["simpleParam"].GoLow()
assert.Equal(t, 21, wentLower.Name.ValueNode.Line) assert.Equal(t, 21, wentLower.Name.ValueNode.Line)
assert.Equal(t, 11, wentLower.Name.ValueNode.Column) assert.Equal(t, 11, wentLower.Name.ValueNode.Column)
} }
func TestNewSwaggerDocument_Security(t *testing.T) { func TestNewSwaggerDocument_Security(t *testing.T) {
initTest() initTest()
highDoc := NewSwaggerDocument(doc) highDoc := NewSwaggerDocument(doc)
assert.Len(t, highDoc.Security, 1) assert.Len(t, highDoc.Security, 1)
assert.Len(t, highDoc.Security[0].Requirements["global_auth"], 2) assert.Len(t, highDoc.Security[0].Requirements["global_auth"], 2)
wentLow := highDoc.Security[0].GoLow() wentLow := highDoc.Security[0].GoLow()
assert.Equal(t, 25, wentLow.Values.ValueNode.Line) assert.Equal(t, 25, wentLow.Values.ValueNode.Line)
assert.Equal(t, 5, wentLow.Values.ValueNode.Column) assert.Equal(t, 5, wentLow.Values.ValueNode.Column)
} }
func TestNewSwaggerDocument_Definitions_Security(t *testing.T) { func TestNewSwaggerDocument_Definitions_Security(t *testing.T) {
initTest() initTest()
highDoc := NewSwaggerDocument(doc) highDoc := NewSwaggerDocument(doc)
assert.Len(t, highDoc.SecurityDefinitions.Definitions, 3) assert.Len(t, highDoc.SecurityDefinitions.Definitions, 3)
assert.Equal(t, "oauth2", highDoc.SecurityDefinitions.Definitions["petstore_auth"].Type) assert.Equal(t, "oauth2", highDoc.SecurityDefinitions.Definitions["petstore_auth"].Type)
assert.Equal(t, "https://petstore.swagger.io/oauth/authorize", assert.Equal(t, "https://petstore.swagger.io/oauth/authorize",
highDoc.SecurityDefinitions.Definitions["petstore_auth"].AuthorizationUrl) highDoc.SecurityDefinitions.Definitions["petstore_auth"].AuthorizationUrl)
assert.Equal(t, "implicit", highDoc.SecurityDefinitions.Definitions["petstore_auth"].Flow) assert.Equal(t, "implicit", highDoc.SecurityDefinitions.Definitions["petstore_auth"].Flow)
assert.Len(t, highDoc.SecurityDefinitions.Definitions["petstore_auth"].Scopes.Values, 2) assert.Len(t, highDoc.SecurityDefinitions.Definitions["petstore_auth"].Scopes.Values, 2)
goLow := highDoc.SecurityDefinitions.GoLow() goLow := highDoc.SecurityDefinitions.GoLow()
assert.Equal(t, 637, goLow.FindSecurityDefinition("petstore_auth").ValueNode.Line) assert.Equal(t, 661, goLow.FindSecurityDefinition("petstore_auth").ValueNode.Line)
assert.Equal(t, 5, goLow.FindSecurityDefinition("petstore_auth").ValueNode.Column) assert.Equal(t, 5, goLow.FindSecurityDefinition("petstore_auth").ValueNode.Column)
goLower := highDoc.SecurityDefinitions.Definitions["petstore_auth"].GoLow() goLower := highDoc.SecurityDefinitions.Definitions["petstore_auth"].GoLow()
assert.Equal(t, 640, goLower.Scopes.KeyNode.Line) assert.Equal(t, 664, goLower.Scopes.KeyNode.Line)
assert.Equal(t, 5, goLower.Scopes.KeyNode.Column) assert.Equal(t, 5, goLower.Scopes.KeyNode.Column)
goLowest := highDoc.SecurityDefinitions.Definitions["petstore_auth"].Scopes.GoLow() goLowest := highDoc.SecurityDefinitions.Definitions["petstore_auth"].Scopes.GoLow()
assert.Equal(t, 641, goLowest.FindScope("read:pets").ValueNode.Line) assert.Equal(t, 665, goLowest.FindScope("read:pets").ValueNode.Line)
assert.Equal(t, 18, goLowest.FindScope("read:pets").ValueNode.Column) assert.Equal(t, 18, goLowest.FindScope("read:pets").ValueNode.Column)
} }
func TestNewSwaggerDocument_Definitions_Responses(t *testing.T) { func TestNewSwaggerDocument_Definitions_Responses(t *testing.T) {
initTest() initTest()
highDoc := NewSwaggerDocument(doc) highDoc := NewSwaggerDocument(doc)
assert.Len(t, highDoc.Responses.Definitions, 2) assert.Len(t, highDoc.Responses.Definitions, 2)
defs := highDoc.Responses.Definitions defs := highDoc.Responses.Definitions
assert.Equal(t, "morning", defs["200"].Extensions["x-coffee"]) assert.Equal(t, "morning", defs["200"].Extensions["x-coffee"])
assert.Equal(t, "OK", defs["200"].Description) assert.Equal(t, "OK", defs["200"].Description)
assert.Equal(t, "a generic API response object", assert.Equal(t, "a generic API response object",
defs["200"].Schema.Schema().Description) defs["200"].Schema.Schema().Description)
assert.Len(t, defs["200"].Examples.Values, 3) assert.Len(t, defs["200"].Examples.Values, 3)
exp := defs["200"].Examples.Values["application/json"] exp := defs["200"].Examples.Values["application/json"]
assert.Len(t, exp.(map[string]interface{}), 2) assert.Len(t, exp.(map[string]interface{}), 2)
assert.Equal(t, "two", exp.(map[string]interface{})["one"]) assert.Equal(t, "two", exp.(map[string]interface{})["one"])
exp = defs["200"].Examples.Values["text/xml"] exp = defs["200"].Examples.Values["text/xml"]
assert.Len(t, exp.([]interface{}), 3) assert.Len(t, exp.([]interface{}), 3)
assert.Equal(t, "two", exp.([]interface{})[1]) assert.Equal(t, "two", exp.([]interface{})[1])
exp = defs["200"].Examples.Values["text/plain"] exp = defs["200"].Examples.Values["text/plain"]
assert.Equal(t, "something else.", exp) assert.Equal(t, "something else.", exp)
expWentLow := defs["200"].Examples.GoLow() expWentLow := defs["200"].Examples.GoLow()
assert.Equal(t, 678, expWentLow.FindExample("application/json").ValueNode.Line) assert.Equal(t, 702, expWentLow.FindExample("application/json").ValueNode.Line)
assert.Equal(t, 9, expWentLow.FindExample("application/json").ValueNode.Column) assert.Equal(t, 9, expWentLow.FindExample("application/json").ValueNode.Column)
wentLow := highDoc.Responses.GoLow() wentLow := highDoc.Responses.GoLow()
assert.Equal(t, 645, wentLow.FindResponse("200").ValueNode.Line) assert.Equal(t, 669, wentLow.FindResponse("200").ValueNode.Line)
y := defs["500"].Headers["someHeader"]
assert.Len(t, y.Enum, 2)
x := y.Items
assert.Equal(t, "something", x.Format)
assert.Equal(t, "array", x.Type)
assert.Equal(t, "csv", x.CollectionFormat)
assert.Equal(t, "cake", x.Default)
assert.Equal(t, 10, x.Maximum)
assert.Equal(t, 1, x.Minimum)
assert.True(t, x.ExclusiveMaximum)
assert.True(t, x.ExclusiveMinimum)
assert.Equal(t, 5, x.MaxLength)
assert.Equal(t, 1, x.MinLength)
assert.Equal(t, "hi!", x.Pattern)
assert.Equal(t, 1, x.MinItems)
assert.True(t, x.UniqueItems)
assert.Len(t, x.Enum, 2)
wentQuiteLow := y.GoLow()
assert.Equal(t, 717, wentQuiteLow.Type.KeyNode.Line)
wentLowest := x.GoLow()
assert.Equal(t, 733, wentLowest.UniqueItems.KeyNode.Line)
}
func TestNewSwaggerDocument_Definitions(t *testing.T) {
initTest()
highDoc := NewSwaggerDocument(doc)
assert.Len(t, highDoc.Definitions.Definitions, 6)
wentLow := highDoc.Definitions.GoLow()
assert.Equal(t, 836, wentLow.FindSchema("User").ValueNode.Line)
}
func TestNewSwaggerDocument_Paths(t *testing.T) {
initTest()
highDoc := NewSwaggerDocument(doc)
assert.Len(t, highDoc.Paths.PathItems, 15)
upload := highDoc.Paths.PathItems["/pet/{petId}/uploadImage"]
assert.Equal(t, "man", upload.Extensions["x-potato"])
assert.Nil(t, upload.Get)
assert.Nil(t, upload.Put)
assert.Nil(t, upload.Patch)
assert.Nil(t, upload.Delete)
assert.Nil(t, upload.Head)
assert.Nil(t, upload.Options)
assert.Equal(t, "pet", upload.Post.Tags[0])
assert.Equal(t, "uploads an image", upload.Post.Summary)
assert.NotEmpty(t, upload.Post.Description)
assert.Equal(t, "uploadFile", upload.Post.OperationId)
assert.Equal(t, "multipart/form-data", upload.Post.Consumes[0])
assert.Equal(t, "application/json", upload.Post.Produces[0])
assert.Len(t, upload.Parameters, 3)
assert.Equal(t, "petId", upload.Parameters[0].Name)
assert.Equal(t, "path", upload.Parameters[0].In)
assert.Equal(t, "ID of pet to update", upload.Parameters[0].Description)
assert.True(t, upload.Parameters[0].Required)
assert.Equal(t, "integer", upload.Parameters[0].Type)
assert.Equal(t, "int64", upload.Parameters[0].Format)
assert.True(t, upload.Parameters[0].ExclusiveMaximum)
assert.True(t, upload.Parameters[0].ExclusiveMinimum)
assert.Equal(t, 2, upload.Parameters[0].MaxLength)
assert.Equal(t, 1, upload.Parameters[0].MinLength)
assert.Equal(t, 1, upload.Parameters[0].Minimum)
assert.Equal(t, 5, upload.Parameters[0].Maximum)
assert.Equal(t, "hi!", upload.Parameters[0].Pattern)
assert.Equal(t, 1, upload.Parameters[0].MinItems)
assert.Equal(t, 20, upload.Parameters[0].MaxItems)
assert.True(t, upload.Parameters[0].UniqueItems)
assert.Len(t, upload.Parameters[0].Enum, 2)
assert.Equal(t, "hello", upload.Parameters[0].Enum[0])
def := upload.Parameters[0].Default.(map[string]interface{})
assert.Equal(t, "here", def["something"])
assert.Equal(t, "https://pb33f.io", upload.Post.ExternalDocs.URL)
assert.Len(t, upload.Post.Schemes, 2)
wentLow := highDoc.Paths.GoLow()
assert.Equal(t, 52,
wentLow.FindPath("/pet/{petId}/uploadImage").ValueNode.Line)
assert.Equal(t, 5,
wentLow.FindPath("/pet/{petId}/uploadImage").ValueNode.Column)
wentLower := upload.GoLow()
assert.Equal(t, 52, wentLower.FindExtension("x-potato").ValueNode.Line)
assert.Equal(t, 15, wentLower.FindExtension("x-potato").ValueNode.Column)
wentLowest := upload.Post.GoLow()
assert.Equal(t, 55, wentLowest.Tags.KeyNode.Line)
}
func TestNewSwaggerDocument_Responses(t *testing.T) {
initTest()
highDoc := NewSwaggerDocument(doc)
upload := highDoc.Paths.PathItems["/pet/{petId}/uploadImage"].Post
assert.Len(t, upload.Responses.Codes, 1)
OK := upload.Responses.Codes["200"]
assert.Equal(t, "successful operation", OK.Description)
assert.Equal(t, "a generic API response object", OK.Schema.Schema().Description)
wentLow := upload.Responses.GoLow()
assert.Equal(t, 106, wentLow.FindResponseByCode("200").ValueNode.Line)
wentLower := OK.GoLow()
assert.Equal(t, 107, wentLower.Schema.KeyNode.Line)
assert.Equal(t, 11, wentLower.Schema.KeyNode.Column)
} }

View File

@@ -31,7 +31,7 @@ type Header struct {
MaxItems low.NodeReference[int] MaxItems low.NodeReference[int]
MinItems low.NodeReference[int] MinItems low.NodeReference[int]
UniqueItems low.NodeReference[bool] UniqueItems low.NodeReference[bool]
Enum low.NodeReference[[]string] Enum low.NodeReference[[]low.ValueReference[string]]
MultipleOf low.NodeReference[int] MultipleOf low.NodeReference[int]
Extensions map[low.KeyReference[string]]low.ValueReference[any] Extensions map[low.KeyReference[string]]low.ValueReference[any]
} }
@@ -74,7 +74,7 @@ func (h *Header) Build(root *yaml.Node, idx *index.SpecIndex) error {
} }
return nil return nil
} }
h.Default.Value = low.NodeReference[any]{ h.Default = low.NodeReference[any]{
Value: n, Value: n,
KeyNode: ln, KeyNode: ln,
ValueNode: vn, ValueNode: vn,

View File

@@ -31,7 +31,7 @@ type Items struct {
MaxItems low.NodeReference[int] MaxItems low.NodeReference[int]
MinItems low.NodeReference[int] MinItems low.NodeReference[int]
UniqueItems low.NodeReference[bool] UniqueItems low.NodeReference[bool]
Enum low.NodeReference[[]string] Enum low.NodeReference[[]low.ValueReference[string]]
MultipleOf low.NodeReference[int] MultipleOf low.NodeReference[int]
} }

View File

@@ -7,6 +7,7 @@ import (
"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/utils"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
@@ -36,7 +37,7 @@ type Parameter struct {
MaxItems low.NodeReference[int] MaxItems low.NodeReference[int]
MinItems low.NodeReference[int] MinItems low.NodeReference[int]
UniqueItems low.NodeReference[bool] UniqueItems low.NodeReference[bool]
Enum low.NodeReference[[]string] Enum low.NodeReference[[]low.ValueReference[string]]
MultipleOf low.NodeReference[int] MultipleOf low.NodeReference[int]
Extensions map[low.KeyReference[string]]low.ValueReference[any] Extensions map[low.KeyReference[string]]low.ValueReference[any]
} }
@@ -59,5 +60,37 @@ func (p *Parameter) Build(root *yaml.Node, idx *index.SpecIndex) error {
return iErr return iErr
} }
p.Items = items p.Items = items
_, ln, vn := utils.FindKeyNodeFull(DefaultLabel, root.Content)
if vn != nil {
var n map[string]interface{}
err := vn.Decode(&n)
if err != nil {
var k []interface{}
err = vn.Decode(&k)
if err != nil {
var j interface{}
_ = vn.Decode(&j)
p.Default = low.NodeReference[any]{
Value: j,
KeyNode: ln,
ValueNode: vn,
}
return nil
}
p.Default = low.NodeReference[any]{
Value: k,
KeyNode: ln,
ValueNode: vn,
}
return nil
}
p.Default = low.NodeReference[any]{
Value: n,
KeyNode: ln,
ValueNode: vn,
}
return nil
}
return nil return nil
} }

View File

@@ -51,11 +51,14 @@ paths:
"/pet/{petId}/uploadImage": "/pet/{petId}/uploadImage":
x-potato: man x-potato: man
post: post:
schemes: [ http, https]
tags: tags:
- pet - pet
summary: uploads an image summary: uploads an image
description: '' description: also, uploads an image - but this time with more details.
operationId: uploadFile operationId: uploadFile
externalDocs:
url: https://pb33f.io
consumes: consumes:
- multipart/form-data - multipart/form-data
produces: produces:
@@ -67,16 +70,37 @@ paths:
required: true required: true
type: integer type: integer
format: int64 format: int64
exclusiveMaximum: true
exclusiveMinimum: true
maxLength: 2
minLength: 1
minimum: 1
maximum: 5
pattern: hi!
minItems: 1
maxItems: 20
uniqueItems: true
enum:
- hello
- there
default:
something: here
multipleOf: 2
- name: additionalMetadata - name: additionalMetadata
in: formData in: formData
description: Additional data to pass to server description: Additional data to pass to server
required: false required: false
type: string type: string
allowEmptyValue: true
default: something
- name: file - name: file
in: formData in: formData
description: file to upload description: file to upload
required: false required: false
type: file type: file
default:
- one
- two
responses: responses:
'200': '200':
description: successful operation description: successful operation
@@ -688,6 +712,29 @@ responses:
description: Error description: Error
schema: schema:
$ref: '#/definitions/ApiResponse' $ref: '#/definitions/ApiResponse'
headers:
someHeader:
type: array
enum: [one, two]
items:
format: something
type: array
collectionFormat: csv
default: cake
maximum: 10
exclusiveMaximum: true
minimum: 1
exclusiveMinimum: true
maxLength: 5
minLength: 1
pattern: hi!
minItems: 1
maxItems: 10
uniqueItems: true
enum:
- one
- two
multipleOf: 1
definitions: definitions:
ApiResponse: ApiResponse:
description: a generic API response object description: a generic API response object