diff --git a/datamodel/high/2.0/header.go b/datamodel/high/2.0/header.go index 0226b69..937beed 100644 --- a/datamodel/high/2.0/header.go +++ b/datamodel/high/2.0/header.go @@ -84,7 +84,11 @@ func NewHeader(header *low.Header) *Header { h.UniqueItems = header.UniqueItems.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() { h.MultipleOf = header.MultipleOf.Value diff --git a/datamodel/high/2.0/items.go b/datamodel/high/2.0/items.go index 33f0951..3652bec 100644 --- a/datamodel/high/2.0/items.go +++ b/datamodel/high/2.0/items.go @@ -33,7 +33,7 @@ func NewItems(items *low.Items) *Items { i.Type = items.Type.Value } if !items.Format.IsEmpty() { - i.Format = items.Type.Value + i.Format = items.Format.Value } if !items.Items.IsEmpty() { i.Items = NewItems(items.Items.Value) @@ -53,7 +53,7 @@ func NewItems(items *low.Items) *Items { if !items.Minimum.IsEmpty() { i.Minimum = items.Minimum.Value } - if !items.ExclusiveMinimum.Value { + if !items.ExclusiveMinimum.IsEmpty() { i.ExclusiveMinimum = items.ExclusiveMinimum.Value } if !items.MaxLength.IsEmpty() { @@ -72,10 +72,14 @@ func NewItems(items *low.Items) *Items { i.MaxItems = items.MaxItems.Value } if !items.UniqueItems.IsEmpty() { - i.UniqueItems = items.UniqueItems.IsEmpty() + i.UniqueItems = items.UniqueItems.Value } 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() { i.MultipleOf = items.MultipleOf.Value diff --git a/datamodel/high/2.0/parameter.go b/datamodel/high/2.0/parameter.go index 56b8421..fc28c1e 100644 --- a/datamodel/high/2.0/parameter.go +++ b/datamodel/high/2.0/parameter.go @@ -51,7 +51,7 @@ func NewParameter(parameter *low.Parameter) *Parameter { p.Type = parameter.Type.Value } if !parameter.Format.IsEmpty() { - p.Format = parameter.Type.Value + p.Format = parameter.Format.Value } if !parameter.Description.IsEmpty() { p.Description = parameter.Description.Value @@ -83,7 +83,7 @@ func NewParameter(parameter *low.Parameter) *Parameter { if !parameter.Minimum.IsEmpty() { p.Minimum = parameter.Minimum.Value } - if !parameter.ExclusiveMinimum.Value { + if !parameter.ExclusiveMinimum.IsEmpty() { p.ExclusiveMinimum = parameter.ExclusiveMinimum.Value } if !parameter.MaxLength.IsEmpty() { @@ -102,10 +102,14 @@ func NewParameter(parameter *low.Parameter) *Parameter { p.MaxItems = parameter.MaxItems.Value } if !parameter.UniqueItems.IsEmpty() { - p.UniqueItems = parameter.UniqueItems.IsEmpty() + p.UniqueItems = parameter.UniqueItems.Value } 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() { p.MultipleOf = parameter.MultipleOf.Value diff --git a/datamodel/high/2.0/paths.go b/datamodel/high/2.0/paths.go index 77e7815..a878569 100644 --- a/datamodel/high/2.0/paths.go +++ b/datamodel/high/2.0/paths.go @@ -40,6 +40,7 @@ func NewPaths(paths *low.Paths) *Paths { pathItems[res.key] = res.result } } + p.PathItems = pathItems } return p } diff --git a/datamodel/high/2.0/response.go b/datamodel/high/2.0/response.go index fe1b563..42e22aa 100644 --- a/datamodel/high/2.0/response.go +++ b/datamodel/high/2.0/response.go @@ -33,6 +33,7 @@ func NewResponse(response *low.Response) *Response { for k := range response.Headers.Value { headers[k.Value] = NewHeader(response.Headers.Value[k].Value) } + r.Headers = headers } if !response.Examples.IsEmpty() { r.Examples = NewExamples(response.Examples.Value) diff --git a/datamodel/high/2.0/responses.go b/datamodel/high/2.0/responses.go index a3f639b..7528742 100644 --- a/datamodel/high/2.0/responses.go +++ b/datamodel/high/2.0/responses.go @@ -46,6 +46,7 @@ func NewResponses(responses *low.Responses) *Responses { resp[res.key] = res.result } } + r.Codes = resp } return r } diff --git a/datamodel/high/2.0/swagger_test.go b/datamodel/high/2.0/swagger_test.go index 3fb3a5f..e1d5eee 100644 --- a/datamodel/high/2.0/swagger_test.go +++ b/datamodel/high/2.0/swagger_test.go @@ -4,162 +4,277 @@ package v2 import ( - "github.com/pb33f/libopenapi/datamodel" - v2 "github.com/pb33f/libopenapi/datamodel/low/2.0" - "github.com/stretchr/testify/assert" + "github.com/pb33f/libopenapi/datamodel" + v2 "github.com/pb33f/libopenapi/datamodel/low/2.0" + "github.com/stretchr/testify/assert" - "io/ioutil" - "testing" + "io/ioutil" + "testing" ) var doc *v2.Swagger func initTest() { - data, _ := ioutil.ReadFile("../../../test_specs/petstorev2-complete.yaml") - info, _ := datamodel.ExtractSpecInfo(data) - var err []error - doc, err = v2.CreateDocument(info) - if err != nil { - panic("broken something") - } + data, _ := ioutil.ReadFile("../../../test_specs/petstorev2-complete.yaml") + info, _ := datamodel.ExtractSpecInfo(data) + var err []error + doc, err = v2.CreateDocument(info) + if err != nil { + panic("broken something") + } } func TestNewSwaggerDocument(t *testing.T) { - initTest() - h := NewSwaggerDocument(doc) - assert.NotNil(t, h) + initTest() + h := NewSwaggerDocument(doc) + assert.NotNil(t, h) } func BenchmarkNewDocument(b *testing.B) { - initTest() - for i := 0; i < b.N; i++ { - _ = NewSwaggerDocument(doc) - } + initTest() + for i := 0; i < b.N; i++ { + _ = NewSwaggerDocument(doc) + } } func TestNewSwaggerDocument_Base(t *testing.T) { - initTest() - highDoc := NewSwaggerDocument(doc) - assert.Equal(t, "2.0", highDoc.Swagger) - assert.True(t, highDoc.Extensions["x-pet"].(bool)) - assert.Equal(t, "petstore.swagger.io", highDoc.Host) - assert.Equal(t, "/v2", highDoc.BasePath) - assert.Len(t, highDoc.Schemes, 2) - assert.Equal(t, "https", highDoc.Schemes[0]) - assert.Len(t, highDoc.Consumes, 2) - assert.Equal(t, "application/json", highDoc.Consumes[0]) - assert.Len(t, highDoc.Produces, 1) - assert.Equal(t, "application/json", highDoc.Produces[0]) + initTest() + highDoc := NewSwaggerDocument(doc) + assert.Equal(t, "2.0", highDoc.Swagger) + assert.True(t, highDoc.Extensions["x-pet"].(bool)) + assert.Equal(t, "petstore.swagger.io", highDoc.Host) + assert.Equal(t, "/v2", highDoc.BasePath) + assert.Len(t, highDoc.Schemes, 2) + assert.Equal(t, "https", highDoc.Schemes[0]) + assert.Len(t, highDoc.Consumes, 2) + assert.Equal(t, "application/json", highDoc.Consumes[0]) + assert.Len(t, highDoc.Produces, 1) + assert.Equal(t, "application/json", highDoc.Produces[0]) - wentLow := highDoc.GoLow() - assert.Equal(t, 16, wentLow.Host.ValueNode.Line) - assert.Equal(t, 7, wentLow.Host.ValueNode.Column) + wentLow := highDoc.GoLow() + assert.Equal(t, 16, wentLow.Host.ValueNode.Line) + assert.Equal(t, 7, wentLow.Host.ValueNode.Column) } func TestNewSwaggerDocument_Info(t *testing.T) { - initTest() - highDoc := NewSwaggerDocument(doc) - assert.Equal(t, "1.0.6", highDoc.Info.Version) - assert.NotEmpty(t, highDoc.Info.Description) - assert.Equal(t, "Swagger Petstore", highDoc.Info.Title) - 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, "apiteam@swagger.io", highDoc.Info.Contact.Email) + initTest() + highDoc := NewSwaggerDocument(doc) + assert.Equal(t, "1.0.6", highDoc.Info.Version) + assert.NotEmpty(t, highDoc.Info.Description) + assert.Equal(t, "Swagger Petstore", highDoc.Info.Title) + 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, "apiteam@swagger.io", highDoc.Info.Contact.Email) - wentLow := highDoc.Info.Contact.GoLow() - assert.Equal(t, 12, wentLow.Email.ValueNode.Line) - assert.Equal(t, 12, wentLow.Email.ValueNode.Column) + wentLow := highDoc.Info.Contact.GoLow() + assert.Equal(t, 12, wentLow.Email.ValueNode.Line) + assert.Equal(t, 12, wentLow.Email.ValueNode.Column) - wentLowLic := highDoc.Info.License.GoLow() - assert.Equal(t, 14, wentLowLic.Name.ValueNode.Line) - assert.Equal(t, 11, wentLowLic.Name.ValueNode.Column) + wentLowLic := highDoc.Info.License.GoLow() + assert.Equal(t, 14, wentLowLic.Name.ValueNode.Line) + assert.Equal(t, 11, wentLowLic.Name.ValueNode.Column) } func TestNewSwaggerDocument_Parameters(t *testing.T) { - initTest() - highDoc := NewSwaggerDocument(doc) - params := highDoc.Parameters - assert.Len(t, params.Definitions, 1) - assert.Equal(t, "query", params.Definitions["simpleParam"].In) - assert.Equal(t, "simple", params.Definitions["simpleParam"].Name) - assert.Equal(t, "string", params.Definitions["simpleParam"].Type) - assert.Equal(t, "nuggets", params.Definitions["simpleParam"].Extensions["x-chicken"]) + initTest() + highDoc := NewSwaggerDocument(doc) + params := highDoc.Parameters + assert.Len(t, params.Definitions, 1) + assert.Equal(t, "query", params.Definitions["simpleParam"].In) + assert.Equal(t, "simple", params.Definitions["simpleParam"].Name) + assert.Equal(t, "string", params.Definitions["simpleParam"].Type) + assert.Equal(t, "nuggets", params.Definitions["simpleParam"].Extensions["x-chicken"]) - wentLow := params.GoLow() - assert.Equal(t, 20, wentLow.FindParameter("simpleParam").ValueNode.Line) - assert.Equal(t, 5, wentLow.FindParameter("simpleParam").ValueNode.Column) + wentLow := params.GoLow() + assert.Equal(t, 20, wentLow.FindParameter("simpleParam").ValueNode.Line) + assert.Equal(t, 5, wentLow.FindParameter("simpleParam").ValueNode.Column) - wentLower := params.Definitions["simpleParam"].GoLow() - assert.Equal(t, 21, wentLower.Name.ValueNode.Line) - assert.Equal(t, 11, wentLower.Name.ValueNode.Column) + wentLower := params.Definitions["simpleParam"].GoLow() + assert.Equal(t, 21, wentLower.Name.ValueNode.Line) + assert.Equal(t, 11, wentLower.Name.ValueNode.Column) } func TestNewSwaggerDocument_Security(t *testing.T) { - initTest() - highDoc := NewSwaggerDocument(doc) - assert.Len(t, highDoc.Security, 1) - assert.Len(t, highDoc.Security[0].Requirements["global_auth"], 2) + initTest() + highDoc := NewSwaggerDocument(doc) + assert.Len(t, highDoc.Security, 1) + assert.Len(t, highDoc.Security[0].Requirements["global_auth"], 2) - wentLow := highDoc.Security[0].GoLow() - assert.Equal(t, 25, wentLow.Values.ValueNode.Line) - assert.Equal(t, 5, wentLow.Values.ValueNode.Column) + wentLow := highDoc.Security[0].GoLow() + assert.Equal(t, 25, wentLow.Values.ValueNode.Line) + assert.Equal(t, 5, wentLow.Values.ValueNode.Column) } func TestNewSwaggerDocument_Definitions_Security(t *testing.T) { - initTest() - highDoc := NewSwaggerDocument(doc) - assert.Len(t, highDoc.SecurityDefinitions.Definitions, 3) - assert.Equal(t, "oauth2", highDoc.SecurityDefinitions.Definitions["petstore_auth"].Type) - assert.Equal(t, "https://petstore.swagger.io/oauth/authorize", - highDoc.SecurityDefinitions.Definitions["petstore_auth"].AuthorizationUrl) - assert.Equal(t, "implicit", highDoc.SecurityDefinitions.Definitions["petstore_auth"].Flow) - assert.Len(t, highDoc.SecurityDefinitions.Definitions["petstore_auth"].Scopes.Values, 2) + initTest() + highDoc := NewSwaggerDocument(doc) + assert.Len(t, highDoc.SecurityDefinitions.Definitions, 3) + assert.Equal(t, "oauth2", highDoc.SecurityDefinitions.Definitions["petstore_auth"].Type) + assert.Equal(t, "https://petstore.swagger.io/oauth/authorize", + highDoc.SecurityDefinitions.Definitions["petstore_auth"].AuthorizationUrl) + assert.Equal(t, "implicit", highDoc.SecurityDefinitions.Definitions["petstore_auth"].Flow) + 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, 5, goLow.FindSecurityDefinition("petstore_auth").ValueNode.Column) + assert.Equal(t, 661, goLow.FindSecurityDefinition("petstore_auth").ValueNode.Line) + assert.Equal(t, 5, goLow.FindSecurityDefinition("petstore_auth").ValueNode.Column) - goLower := highDoc.SecurityDefinitions.Definitions["petstore_auth"].GoLow() - assert.Equal(t, 640, goLower.Scopes.KeyNode.Line) - assert.Equal(t, 5, goLower.Scopes.KeyNode.Column) + goLower := highDoc.SecurityDefinitions.Definitions["petstore_auth"].GoLow() + assert.Equal(t, 664, goLower.Scopes.KeyNode.Line) + assert.Equal(t, 5, goLower.Scopes.KeyNode.Column) - goLowest := highDoc.SecurityDefinitions.Definitions["petstore_auth"].Scopes.GoLow() - assert.Equal(t, 641, goLowest.FindScope("read:pets").ValueNode.Line) - assert.Equal(t, 18, goLowest.FindScope("read:pets").ValueNode.Column) + goLowest := highDoc.SecurityDefinitions.Definitions["petstore_auth"].Scopes.GoLow() + assert.Equal(t, 665, goLowest.FindScope("read:pets").ValueNode.Line) + assert.Equal(t, 18, goLowest.FindScope("read:pets").ValueNode.Column) } func TestNewSwaggerDocument_Definitions_Responses(t *testing.T) { - initTest() - highDoc := NewSwaggerDocument(doc) - assert.Len(t, highDoc.Responses.Definitions, 2) + initTest() + highDoc := NewSwaggerDocument(doc) + assert.Len(t, highDoc.Responses.Definitions, 2) - defs := highDoc.Responses.Definitions - assert.Equal(t, "morning", defs["200"].Extensions["x-coffee"]) - assert.Equal(t, "OK", defs["200"].Description) - assert.Equal(t, "a generic API response object", - defs["200"].Schema.Schema().Description) - assert.Len(t, defs["200"].Examples.Values, 3) + defs := highDoc.Responses.Definitions + assert.Equal(t, "morning", defs["200"].Extensions["x-coffee"]) + assert.Equal(t, "OK", defs["200"].Description) + assert.Equal(t, "a generic API response object", + defs["200"].Schema.Schema().Description) + assert.Len(t, defs["200"].Examples.Values, 3) - exp := defs["200"].Examples.Values["application/json"] - assert.Len(t, exp.(map[string]interface{}), 2) - assert.Equal(t, "two", exp.(map[string]interface{})["one"]) + exp := defs["200"].Examples.Values["application/json"] + assert.Len(t, exp.(map[string]interface{}), 2) + assert.Equal(t, "two", exp.(map[string]interface{})["one"]) - exp = defs["200"].Examples.Values["text/xml"] - assert.Len(t, exp.([]interface{}), 3) - assert.Equal(t, "two", exp.([]interface{})[1]) + exp = defs["200"].Examples.Values["text/xml"] + assert.Len(t, exp.([]interface{}), 3) + assert.Equal(t, "two", exp.([]interface{})[1]) - exp = defs["200"].Examples.Values["text/plain"] - assert.Equal(t, "something else.", exp) + exp = defs["200"].Examples.Values["text/plain"] + assert.Equal(t, "something else.", exp) - expWentLow := defs["200"].Examples.GoLow() - assert.Equal(t, 678, expWentLow.FindExample("application/json").ValueNode.Line) - assert.Equal(t, 9, expWentLow.FindExample("application/json").ValueNode.Column) + expWentLow := defs["200"].Examples.GoLow() + assert.Equal(t, 702, expWentLow.FindExample("application/json").ValueNode.Line) + assert.Equal(t, 9, expWentLow.FindExample("application/json").ValueNode.Column) - wentLow := highDoc.Responses.GoLow() - assert.Equal(t, 645, wentLow.FindResponse("200").ValueNode.Line) + wentLow := highDoc.Responses.GoLow() + 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) } diff --git a/datamodel/low/2.0/header.go b/datamodel/low/2.0/header.go index 696d66c..a0bab3a 100644 --- a/datamodel/low/2.0/header.go +++ b/datamodel/low/2.0/header.go @@ -31,7 +31,7 @@ type Header struct { MaxItems low.NodeReference[int] MinItems low.NodeReference[int] UniqueItems low.NodeReference[bool] - Enum low.NodeReference[[]string] + Enum low.NodeReference[[]low.ValueReference[string]] MultipleOf low.NodeReference[int] 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 } - h.Default.Value = low.NodeReference[any]{ + h.Default = low.NodeReference[any]{ Value: n, KeyNode: ln, ValueNode: vn, diff --git a/datamodel/low/2.0/items.go b/datamodel/low/2.0/items.go index 4a4247a..a87fd69 100644 --- a/datamodel/low/2.0/items.go +++ b/datamodel/low/2.0/items.go @@ -31,7 +31,7 @@ type Items struct { MaxItems low.NodeReference[int] MinItems low.NodeReference[int] UniqueItems low.NodeReference[bool] - Enum low.NodeReference[[]string] + Enum low.NodeReference[[]low.ValueReference[string]] MultipleOf low.NodeReference[int] } diff --git a/datamodel/low/2.0/parameter.go b/datamodel/low/2.0/parameter.go index 0628465..4e55ec3 100644 --- a/datamodel/low/2.0/parameter.go +++ b/datamodel/low/2.0/parameter.go @@ -7,6 +7,7 @@ import ( "github.com/pb33f/libopenapi/datamodel/low" "github.com/pb33f/libopenapi/datamodel/low/base" "github.com/pb33f/libopenapi/index" + "github.com/pb33f/libopenapi/utils" "gopkg.in/yaml.v3" ) @@ -36,7 +37,7 @@ type Parameter struct { MaxItems low.NodeReference[int] MinItems low.NodeReference[int] UniqueItems low.NodeReference[bool] - Enum low.NodeReference[[]string] + Enum low.NodeReference[[]low.ValueReference[string]] MultipleOf low.NodeReference[int] 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 } 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 } diff --git a/test_specs/petstorev2-complete.yaml b/test_specs/petstorev2-complete.yaml index 091d736..c017f04 100644 --- a/test_specs/petstorev2-complete.yaml +++ b/test_specs/petstorev2-complete.yaml @@ -51,11 +51,14 @@ paths: "/pet/{petId}/uploadImage": x-potato: man post: + schemes: [ http, https] tags: - pet summary: uploads an image - description: '' + description: also, uploads an image - but this time with more details. operationId: uploadFile + externalDocs: + url: https://pb33f.io consumes: - multipart/form-data produces: @@ -67,16 +70,37 @@ paths: required: true type: integer 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 in: formData description: Additional data to pass to server required: false type: string + allowEmptyValue: true + default: something - name: file in: formData description: file to upload required: false type: file + default: + - one + - two responses: '200': description: successful operation @@ -688,6 +712,29 @@ responses: description: Error schema: $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: ApiResponse: description: a generic API response object