diff --git a/datamodel/document.go b/datamodel/document.go deleted file mode 100644 index d314f05..0000000 --- a/datamodel/document.go +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley -// SPDX-License-Identifier: MIT - -package datamodel - -// -//import ( -// v2 "github.com/pb33f/libopenapi/datamodel/high/2.0" -// v3 "github.com/pb33f/libopenapi/datamodel/high/3.0" -//) -// -//type Document struct { -// version string -// info *SpecInfo -//} -// -//func (d *Document) GetVersion() string { -// return d.version -//} -// -//func (d *Document) BuildV2Document() (*v2.Swagger, error) { -// return nil, nil -//} -// -//func (d *Document) BuildV3Document() (*v3.Document, error) { -// return nil, nil -//} -// -//func LoadDocument(specBytes []byte) (*Document, error) { -// info, err := ExtractSpecInfo(specBytes) -// if err != nil { -// return nil, err -// } -// return &Document{info: info, version: info.Version}, nil -//} diff --git a/datamodel/high/2.0/definitions.go b/datamodel/high/2.0/definitions.go new file mode 100644 index 0000000..4e73977 --- /dev/null +++ b/datamodel/high/2.0/definitions.go @@ -0,0 +1,10 @@ +// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley +// SPDX-License-Identifier: MIT + +package v2 + +type Definitions struct { +} + +type ParameterDefinitions struct { +} diff --git a/datamodel/high/2.0/examples.go b/datamodel/high/2.0/examples.go new file mode 100644 index 0000000..90446c9 --- /dev/null +++ b/datamodel/high/2.0/examples.go @@ -0,0 +1,28 @@ +// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley +// SPDX-License-Identifier: MIT + +package v2 + +import low "github.com/pb33f/libopenapi/datamodel/low/2.0" + +type Examples struct { + Values map[string]any + low *low.Examples +} + +func NewExamples(examples *low.Examples) *Examples { + e := new(Examples) + e.low = examples + if len(e.Values) > 0 { + values := make(map[string]any) + for k := range e.Values { + values[k] = e.Values[k] + } + e.Values = values + } + return e +} + +func (e *Examples) GoLow() *low.Examples { + return e.low +} diff --git a/datamodel/high/2.0/header.go b/datamodel/high/2.0/header.go new file mode 100644 index 0000000..b03eff3 --- /dev/null +++ b/datamodel/high/2.0/header.go @@ -0,0 +1,95 @@ +// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley +// SPDX-License-Identifier: MIT + +package v2 + +import ( + "github.com/pb33f/libopenapi/datamodel/high" + low "github.com/pb33f/libopenapi/datamodel/low/2.0" +) + +type Header struct { + Type string + Format string + Description string + Items *Items + CollectionFormat string + Default any + Maximum int + ExclusiveMaximum bool + Minimum int + ExclusiveMinimum bool + MaxLength int + MinLength int + Pattern string + MaxItems int + MinItems int + UniqueItems bool + Enum []string + MultipleOf int + Extensions map[string]any + low *low.Header +} + +func NewHeader(header *low.Header) *Header { + h := new(Header) + h.low = header + h.Extensions = high.ExtractExtensions(header.Extensions) + if !header.Type.IsEmpty() { + h.Type = header.Type.Value + } + if !header.Format.IsEmpty() { + h.Format = header.Type.Value + } + if !header.Description.IsEmpty() { + h.Description = header.Description.Value + } + if !header.Items.IsEmpty() { + // TODO: items + //h.Items = + } + if !header.CollectionFormat.IsEmpty() { + h.CollectionFormat = header.CollectionFormat.Value + } + if !header.Default.IsEmpty() { + h.Default = header.Default.Value + } + if !header.Maximum.IsEmpty() { + h.Maximum = header.Maximum.Value + } + if !header.ExclusiveMaximum.IsEmpty() { + h.ExclusiveMaximum = header.ExclusiveMaximum.Value + } + if !header.Minimum.IsEmpty() { + h.Minimum = header.Minimum.Value + } + if !header.ExclusiveMinimum.Value { + h.ExclusiveMinimum = header.ExclusiveMinimum.Value + } + if !header.MaxLength.IsEmpty() { + h.MaxLength = header.MaxLength.Value + } + if !header.MinLength.IsEmpty() { + h.MinLength = header.MinLength.Value + } + if !header.Pattern.IsEmpty() { + h.Pattern = header.Pattern.Value + } + if !header.MinItems.IsEmpty() { + h.MinItems = header.MinItems.Value + } + if !header.MaxItems.IsEmpty() { + h.MaxItems = header.MaxItems.Value + } + if !header.UniqueItems.IsEmpty() { + h.UniqueItems = header.UniqueItems.IsEmpty() + } + if !header.Enum.IsEmpty() { + h.Enum = header.Enum.Value + } + if !header.MultipleOf.IsEmpty() { + h.MultipleOf = header.MultipleOf.Value + } + + return h +} diff --git a/datamodel/high/2.0/items.go b/datamodel/high/2.0/items.go new file mode 100644 index 0000000..33f0951 --- /dev/null +++ b/datamodel/high/2.0/items.go @@ -0,0 +1,88 @@ +// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley +// SPDX-License-Identifier: MIT + +package v2 + +import low "github.com/pb33f/libopenapi/datamodel/low/2.0" + +type Items struct { + Type string + Format string + CollectionFormat string + Items *Items + Default any + Maximum int + ExclusiveMaximum bool + Minimum int + ExclusiveMinimum bool + MaxLength int + MinLength int + Pattern string + MaxItems int + MinItems int + UniqueItems bool + Enum []string + MultipleOf int + low *low.Items +} + +func NewItems(items *low.Items) *Items { + i := new(Items) + i.low = items + if !items.Type.IsEmpty() { + i.Type = items.Type.Value + } + if !items.Format.IsEmpty() { + i.Format = items.Type.Value + } + if !items.Items.IsEmpty() { + i.Items = NewItems(items.Items.Value) + } + if !items.CollectionFormat.IsEmpty() { + i.CollectionFormat = items.CollectionFormat.Value + } + if !items.Default.IsEmpty() { + i.Default = items.Default.Value + } + if !items.Maximum.IsEmpty() { + i.Maximum = items.Maximum.Value + } + if !items.ExclusiveMaximum.IsEmpty() { + i.ExclusiveMaximum = items.ExclusiveMaximum.Value + } + if !items.Minimum.IsEmpty() { + i.Minimum = items.Minimum.Value + } + if !items.ExclusiveMinimum.Value { + i.ExclusiveMinimum = items.ExclusiveMinimum.Value + } + if !items.MaxLength.IsEmpty() { + i.MaxLength = items.MaxLength.Value + } + if !items.MinLength.IsEmpty() { + i.MinLength = items.MinLength.Value + } + if !items.Pattern.IsEmpty() { + i.Pattern = items.Pattern.Value + } + if !items.MinItems.IsEmpty() { + i.MinItems = items.MinItems.Value + } + if !items.MaxItems.IsEmpty() { + i.MaxItems = items.MaxItems.Value + } + if !items.UniqueItems.IsEmpty() { + i.UniqueItems = items.UniqueItems.IsEmpty() + } + if !items.Enum.IsEmpty() { + i.Enum = items.Enum.Value + } + if !items.MultipleOf.IsEmpty() { + i.MultipleOf = items.MultipleOf.Value + } + return i +} + +func (i *Items) GoLow() *low.Items { + return i.low +} diff --git a/datamodel/high/2.0/paths.go b/datamodel/high/2.0/paths.go new file mode 100644 index 0000000..e874a1a --- /dev/null +++ b/datamodel/high/2.0/paths.go @@ -0,0 +1,7 @@ +// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley +// SPDX-License-Identifier: MIT + +package v2 + +type Paths struct { +} diff --git a/datamodel/high/2.0/response.go b/datamodel/high/2.0/response.go new file mode 100644 index 0000000..0443444 --- /dev/null +++ b/datamodel/high/2.0/response.go @@ -0,0 +1,38 @@ +// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley +// SPDX-License-Identifier: MIT + +package v2 + +import ( + "github.com/pb33f/libopenapi/datamodel/high" + "github.com/pb33f/libopenapi/datamodel/high/base" + low "github.com/pb33f/libopenapi/datamodel/low/2.0" +) + +type Response struct { + Description string + Schema *base.SchemaProxy + Headers map[string]*Header + Examples *Examples + Extensions map[string]any + low *low.Response +} + +func NewResponse(response *low.Response) *Response { + r := new(Response) + r.low = response + r.Extensions = high.ExtractExtensions(response.Extensions) + if !response.Schema.IsEmpty() { + r.Schema = base.NewSchemaProxy(&response.Schema) + } + if !response.Headers.IsEmpty() { + headers := make(map[string]*Header) + for k := range response.Headers.Value { + headers[k.Value] = NewHeader(response.Headers.Value[k].Value) + } + } + if !response.Examples.IsEmpty() { + r.Examples = NewExamples(response.Examples.Value) + } + return r +} diff --git a/datamodel/high/2.0/responses_definitions.go b/datamodel/high/2.0/responses_definitions.go new file mode 100644 index 0000000..80e677b --- /dev/null +++ b/datamodel/high/2.0/responses_definitions.go @@ -0,0 +1,22 @@ +// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley +// SPDX-License-Identifier: MIT + +package v2 + +import low "github.com/pb33f/libopenapi/datamodel/low/2.0" + +type ResponsesDefinitions struct { + Values map[string]*Response + low *low.ResponsesDefinitions +} + +func NewResponsesDefinitions(responsesDefinitions *low.ResponsesDefinitions) *ResponsesDefinitions { + rd := new(ResponsesDefinitions) + rd.low = responsesDefinitions + responses := make(map[string]*Response) + for k := range responsesDefinitions.Definitions { + responses[k.Value] = NewResponse(responsesDefinitions.Definitions[k].Value) + } + rd.Values = responses + return rd +} diff --git a/datamodel/high/2.0/scopes.go b/datamodel/high/2.0/scopes.go new file mode 100644 index 0000000..8a847b0 --- /dev/null +++ b/datamodel/high/2.0/scopes.go @@ -0,0 +1,28 @@ +// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley +// SPDX-License-Identifier: MIT + +package v2 + +import ( + low "github.com/pb33f/libopenapi/datamodel/low/2.0" +) + +type Scopes struct { + Values map[string]string + low *low.Scopes +} + +func NewScopes(scopes *low.Scopes) *Scopes { + s := new(Scopes) + s.low = scopes + scopeValues := make(map[string]string) + for k := range scopes.Values { + scopeValues[k.Value] = scopes.Values[k].Value + } + s.Values = scopeValues + return s +} + +func (s *Scopes) GoLow() *low.Scopes { + return s.low +} diff --git a/datamodel/high/2.0/security_definitions.go b/datamodel/high/2.0/security_definitions.go new file mode 100644 index 0000000..61342d2 --- /dev/null +++ b/datamodel/high/2.0/security_definitions.go @@ -0,0 +1,26 @@ +// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley +// SPDX-License-Identifier: MIT + +package v2 + +import low "github.com/pb33f/libopenapi/datamodel/low/2.0" + +type SecurityDefinitions struct { + Values map[string]*SecurityScheme + low *low.SecurityDefinitions +} + +func NewSecurityDefinitions(definitions *low.SecurityDefinitions) *SecurityDefinitions { + sd := new(SecurityDefinitions) + sd.low = definitions + schemes := make(map[string]*SecurityScheme) + for k := range definitions.Definitions { + schemes[k.Value] = NewSecurityScheme(definitions.Definitions[k].Value) + } + sd.Values = schemes + return sd +} + +func (sd *SecurityDefinitions) GoLow() *low.SecurityDefinitions { + return sd.low +} diff --git a/datamodel/high/2.0/security_requirement.go b/datamodel/high/2.0/security_requirement.go new file mode 100644 index 0000000..c623db1 --- /dev/null +++ b/datamodel/high/2.0/security_requirement.go @@ -0,0 +1,31 @@ +// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley +// SPDX-License-Identifier: MIT + +package v2 + +import low "github.com/pb33f/libopenapi/datamodel/low/2.0" + +type SecurityRequirement struct { + Requirements map[string][]string + low *low.SecurityRequirement +} + +func NewSecurityRequirement(req *low.SecurityRequirement) *SecurityRequirement { + r := new(SecurityRequirement) + r.low = req + var values map[string][]string + // to keep things fast, avoiding copying anything - makes it a little hard to read. + for reqK := range req.Values.Value { + var vals []string + for valK := range req.Values.Value[reqK].Value { + vals = append(vals, req.Values.Value[reqK].Value[valK].Value) + } + values[reqK.Value] = vals + } + r.Requirements = values + return r +} + +func (s *SecurityRequirement) GoLow() *low.SecurityRequirement { + return s.low +} diff --git a/datamodel/high/2.0/security_scheme.go b/datamodel/high/2.0/security_scheme.go new file mode 100644 index 0000000..9845e63 --- /dev/null +++ b/datamodel/high/2.0/security_scheme.go @@ -0,0 +1,53 @@ +// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley +// SPDX-License-Identifier: MIT + +package v2 + +import ( + "github.com/pb33f/libopenapi/datamodel/high" + low "github.com/pb33f/libopenapi/datamodel/low/2.0" +) + +type SecurityScheme struct { + Type string + Description string + Name string + In string + Flow string + AuthorizationUrl string + TokenUrl string + Scopes *Scopes + Extensions map[string]any + low *low.SecurityScheme +} + +func NewSecurityScheme(securityScheme *low.SecurityScheme) *SecurityScheme { + s := new(SecurityScheme) + s.low = securityScheme + s.Extensions = high.ExtractExtensions(securityScheme.Extensions) + if !securityScheme.Type.IsEmpty() { + s.Type = securityScheme.Type.Value + } + if !securityScheme.Description.IsEmpty() { + s.Description = securityScheme.Description.Value + } + if !securityScheme.Name.IsEmpty() { + s.Name = securityScheme.Name.Value + } + if !securityScheme.In.IsEmpty() { + s.In = securityScheme.In.Value + } + if !securityScheme.Flow.IsEmpty() { + s.Flow = securityScheme.Flow.Value + } + if !securityScheme.AuthorizationUrl.IsEmpty() { + s.AuthorizationUrl = securityScheme.AuthorizationUrl.Value + } + if !securityScheme.TokenUrl.IsEmpty() { + s.TokenUrl = securityScheme.TokenUrl.Value + } + if !securityScheme.Scopes.IsEmpty() { + s.Scopes = NewScopes(securityScheme.Scopes.Value) + } + return s +} diff --git a/datamodel/high/2.0/swagger.go b/datamodel/high/2.0/swagger.go index 0b9e87e..b10ecb8 100644 --- a/datamodel/high/2.0/swagger.go +++ b/datamodel/high/2.0/swagger.go @@ -3,5 +3,93 @@ package v2 +import ( + "github.com/pb33f/libopenapi/datamodel/high/base" + low "github.com/pb33f/libopenapi/datamodel/low/2.0" +) + type Swagger struct { + Swagger string + Info *base.Info + Host string + BasePath string + Schemes []string + Consumes []string + Produces []string + Paths *Paths + Definitions *Definitions + Parameters *ParameterDefinitions + Responses *ResponsesDefinitions + SecurityDefinitions *SecurityDefinitions + Security []*SecurityRequirement + Tags []*base.Tag + ExternalDocs *base.ExternalDoc + Extensions map[string]any + low *low.Swagger +} + +func NewSwaggerDocument(document *low.Swagger) *Swagger { + d := new(Swagger) + d.low = document + if !document.Info.IsEmpty() { + d.Info = base.NewInfo(document.Info.Value) + } + if !document.Swagger.IsEmpty() { + d.Swagger = document.Swagger.Value + } + if !document.Host.IsEmpty() { + d.Host = document.Host.Value + } + if !document.BasePath.IsEmpty() { + d.BasePath = document.BasePath.Value + } + + if !document.Schemes.IsEmpty() { + var schemes []string + for s := range document.Schemes.Value { + schemes = append(schemes, document.Schemes.Value[s].Value) + } + d.Schemes = schemes + } + if !document.Consumes.IsEmpty() { + var consumes []string + for c := range document.Consumes.Value { + consumes = append(consumes, document.Consumes.Value[c].Value) + } + d.Consumes = consumes + } + if !document.Produces.IsEmpty() { + var produces []string + for p := range document.Produces.Value { + produces = append(produces, document.Produces.Value[p].Value) + } + d.Produces = produces + } + // TODO: Paths + + if !document.SecurityDefinitions.IsEmpty() { + d.SecurityDefinitions = NewSecurityDefinitions(document.SecurityDefinitions.Value) + } + + if !document.Security.IsEmpty() { + var security []*SecurityRequirement + for s := range document.Security.Value { + security = append(security, NewSecurityRequirement(document.Security.Value[s].Value)) + } + d.Security = security + } + + if !document.Tags.IsEmpty() { + var tags []*base.Tag + for t := range document.Tags.Value { + tags = append(tags, base.NewTag(document.Tags.Value[t].Value)) + } + d.Tags = tags + } + + if !document.ExternalDocs.IsEmpty() { + d.ExternalDocs = base.NewExternalDoc(document.ExternalDocs.Value) + } + + return d } diff --git a/datamodel/high/3.0/components.go b/datamodel/high/3.0/components.go index 7a93dc5..7d0620f 100644 --- a/datamodel/high/3.0/components.go +++ b/datamodel/high/3.0/components.go @@ -5,6 +5,7 @@ package v3 import ( "github.com/pb33f/libopenapi/datamodel/high" + highbase "github.com/pb33f/libopenapi/datamodel/high/base" lowmodel "github.com/pb33f/libopenapi/datamodel/low" low "github.com/pb33f/libopenapi/datamodel/low/3.0" "github.com/pb33f/libopenapi/datamodel/low/base" @@ -22,10 +23,10 @@ const ( ) type Components struct { - Schemas map[string]*SchemaProxy + Schemas map[string]*highbase.SchemaProxy Responses map[string]*Response Parameters map[string]*Parameter - Examples map[string]*Example + Examples map[string]*highbase.Example RequestBodies map[string]*RequestBody Headers map[string]*Header SecuritySchemes map[string]*SecurityScheme @@ -45,17 +46,17 @@ func NewComponents(comp *low.Components) *Components { linkMap := make(map[string]*Link) responseMap := make(map[string]*Response) parameterMap := make(map[string]*Parameter) - exampleMap := make(map[string]*Example) + exampleMap := make(map[string]*highbase.Example) requestBodyMap := make(map[string]*RequestBody) headerMap := make(map[string]*Header) securitySchemeMap := make(map[string]*SecurityScheme) - schemas := make(map[string]*SchemaProxy) - schemaChan := make(chan componentResult[*SchemaProxy]) + schemas := make(map[string]*highbase.SchemaProxy) + schemaChan := make(chan componentResult[*highbase.SchemaProxy]) cbChan := make(chan componentResult[*Callback]) linkChan := make(chan componentResult[*Link]) responseChan := make(chan componentResult[*Response]) paramChan := make(chan componentResult[*Parameter]) - exampleChan := make(chan componentResult[*Example]) + exampleChan := make(chan componentResult[*highbase.Example]) requestBodyChan := make(chan componentResult[*RequestBody]) headerChan := make(chan componentResult[*Header]) securitySchemeChan := make(chan componentResult[*SecurityScheme]) @@ -74,7 +75,7 @@ func NewComponents(comp *low.Components) *Components { go buildComponent[*Parameter, *low.Parameter](parameters, k.Value, v.Value, paramChan, NewParameter) } for k, v := range comp.Examples.Value { - go buildComponent[*Example, *base.Example](parameters, k.Value, v.Value, exampleChan, NewExample) + go buildComponent[*highbase.Example, *base.Example](parameters, k.Value, v.Value, exampleChan, highbase.NewExample) } for k, v := range comp.RequestBodies.Value { go buildComponent[*RequestBody, *low.RequestBody](requestBodies, k.Value, v.Value, @@ -149,13 +150,13 @@ func buildComponent[N any, O any](comp int, key string, orig O, c chan component c <- componentResult[N]{comp: comp, res: f(orig), key: key} } -func buildSchema(key lowmodel.KeyReference[string], orig lowmodel.ValueReference[*base.SchemaProxy], c chan componentResult[*SchemaProxy]) { - var sch *SchemaProxy - sch = &SchemaProxy{schema: &lowmodel.NodeReference[*base.SchemaProxy]{ +func buildSchema(key lowmodel.KeyReference[string], orig lowmodel.ValueReference[*base.SchemaProxy], c chan componentResult[*highbase.SchemaProxy]) { + var sch *highbase.SchemaProxy + sch = highbase.NewSchemaProxy(&lowmodel.NodeReference[*base.SchemaProxy]{ Value: orig.Value, ValueNode: orig.ValueNode, - }} - c <- componentResult[*SchemaProxy]{res: sch, key: key.Value} + }) + c <- componentResult[*highbase.SchemaProxy]{res: sch, key: key.Value} } func (c *Components) GoLow() *low.Components { diff --git a/datamodel/high/3.0/document.go b/datamodel/high/3.0/document.go index b27deb7..9577e84 100644 --- a/datamodel/high/3.0/document.go +++ b/datamodel/high/3.0/document.go @@ -5,19 +5,20 @@ package v3 import ( "github.com/pb33f/libopenapi/datamodel/high" + "github.com/pb33f/libopenapi/datamodel/high/base" low "github.com/pb33f/libopenapi/datamodel/low/3.0" "github.com/pb33f/libopenapi/index" ) type Document struct { Version string - Info *Info + Info *base.Info Servers []*Server Paths *Paths Components *Components Security *SecurityRequirement - Tags []*Tag - ExternalDocs *ExternalDoc + Tags []*base.Tag + ExternalDocs *base.ExternalDoc Extensions map[string]any Index *index.SpecIndex low *low.Document @@ -27,7 +28,7 @@ func NewDocument(document *low.Document) *Document { d := new(Document) d.low = document if !document.Info.IsEmpty() { - d.Info = NewInfo(document.Info.Value) + d.Info = base.NewInfo(document.Info.Value) } if !document.Version.IsEmpty() { d.Version = document.Version.Value @@ -37,13 +38,13 @@ func NewDocument(document *low.Document) *Document { servers = append(servers, NewServer(ser.Value)) } d.Servers = servers - var tags []*Tag + var tags []*base.Tag for _, tag := range document.Tags.Value { - tags = append(tags, NewTag(tag.Value)) + tags = append(tags, base.NewTag(tag.Value)) } d.Tags = tags if !document.ExternalDocs.IsEmpty() { - d.ExternalDocs = NewExternalDoc(document.ExternalDocs.Value) + d.ExternalDocs = base.NewExternalDoc(document.ExternalDocs.Value) } if len(document.Extensions) > 0 { d.Extensions = high.ExtractExtensions(document.Extensions) diff --git a/datamodel/high/3.0/header.go b/datamodel/high/3.0/header.go index 371b4ee..3c1d080 100644 --- a/datamodel/high/3.0/header.go +++ b/datamodel/high/3.0/header.go @@ -4,6 +4,7 @@ package v3 import ( + highbase "github.com/pb33f/libopenapi/datamodel/high/base" lowmodel "github.com/pb33f/libopenapi/datamodel/low" low "github.com/pb33f/libopenapi/datamodel/low/3.0" "github.com/pb33f/libopenapi/datamodel/low/base" @@ -17,9 +18,9 @@ type Header struct { Style string Explode bool AllowReserved bool - Schema *SchemaProxy + Schema *highbase.SchemaProxy Example any - Examples map[string]*Example + Examples map[string]*highbase.Example Content map[string]*MediaType Extensions map[string]any low *low.Header @@ -36,15 +37,15 @@ func NewHeader(header *low.Header) *Header { h.Explode = header.Explode.Value h.AllowReserved = header.AllowReserved.Value if !header.Schema.IsEmpty() { - h.Schema = &SchemaProxy{schema: &lowmodel.NodeReference[*base.SchemaProxy]{ + h.Schema = highbase.NewSchemaProxy(&lowmodel.NodeReference[*base.SchemaProxy]{ Value: header.Schema.Value, KeyNode: header.Schema.KeyNode, ValueNode: header.Schema.ValueNode, - }} + }) } h.Content = ExtractContent(header.Content.Value) h.Example = header.Example.Value - h.Examples = ExtractExamples(header.Examples.Value) + h.Examples = highbase.ExtractExamples(header.Examples.Value) return h } diff --git a/datamodel/high/3.0/media_type.go b/datamodel/high/3.0/media_type.go index 3ef8d9b..49c593b 100644 --- a/datamodel/high/3.0/media_type.go +++ b/datamodel/high/3.0/media_type.go @@ -5,15 +5,16 @@ package v3 import ( "github.com/pb33f/libopenapi/datamodel/high" + "github.com/pb33f/libopenapi/datamodel/high/base" lowmodel "github.com/pb33f/libopenapi/datamodel/low" low "github.com/pb33f/libopenapi/datamodel/low/3.0" "sync" ) type MediaType struct { - Schema *SchemaProxy + Schema *base.SchemaProxy Example any - Examples map[string]*Example + Examples map[string]*base.Example Encoding map[string]*Encoding Extensions map[string]any low *low.MediaType @@ -23,10 +24,10 @@ func NewMediaType(mediaType *low.MediaType) *MediaType { m := new(MediaType) m.low = mediaType if !mediaType.Schema.IsEmpty() { - m.Schema = &SchemaProxy{schema: &mediaType.Schema} + m.Schema = base.NewSchemaProxy(&mediaType.Schema) } m.Example = mediaType.Example - m.Examples = ExtractExamples(mediaType.Examples.Value) + m.Examples = base.ExtractExamples(mediaType.Examples.Value) m.Extensions = high.ExtractExtensions(mediaType.Extensions) m.Encoding = ExtractEncoding(mediaType.Encoding.Value) return m diff --git a/datamodel/high/3.0/operation.go b/datamodel/high/3.0/operation.go index 3c0ad8d..fb3cb11 100644 --- a/datamodel/high/3.0/operation.go +++ b/datamodel/high/3.0/operation.go @@ -4,6 +4,7 @@ package v3 import ( + "github.com/pb33f/libopenapi/datamodel/high/base" low "github.com/pb33f/libopenapi/datamodel/low/3.0" ) @@ -11,7 +12,7 @@ type Operation struct { Tags []string Summary string Description string - ExternalDocs *ExternalDoc + ExternalDocs *base.ExternalDoc OperationId string Parameters []*Parameter RequestBody *RequestBody @@ -37,7 +38,7 @@ func NewOperation(operation *low.Operation) *Operation { o.Summary = operation.Summary.Value o.Description = operation.Description.Value if !operation.ExternalDocs.IsEmpty() { - o.ExternalDocs = NewExternalDoc(operation.ExternalDocs.Value) + o.ExternalDocs = base.NewExternalDoc(operation.ExternalDocs.Value) } o.OperationId = operation.OperationId.Value if !operation.Parameters.IsEmpty() { diff --git a/datamodel/high/3.0/parameter.go b/datamodel/high/3.0/parameter.go index 011f233..ff4e32a 100644 --- a/datamodel/high/3.0/parameter.go +++ b/datamodel/high/3.0/parameter.go @@ -5,6 +5,7 @@ package v3 import ( "github.com/pb33f/libopenapi/datamodel/high" + "github.com/pb33f/libopenapi/datamodel/high/base" low "github.com/pb33f/libopenapi/datamodel/low/3.0" ) @@ -18,9 +19,9 @@ type Parameter struct { Style string Explode bool AllowReserved bool - Schema *SchemaProxy + Schema *base.SchemaProxy Example any - Examples map[string]*Example + Examples map[string]*base.Example Content map[string]*MediaType Extensions map[string]any low *low.Parameter @@ -38,11 +39,11 @@ func NewParameter(param *low.Parameter) *Parameter { p.Explode = param.Explode.Value p.AllowReserved = param.AllowReserved.Value if !param.Schema.IsEmpty() { - p.Schema = &SchemaProxy{schema: ¶m.Schema} + p.Schema = base.NewSchemaProxy(¶m.Schema) } p.Required = param.Required.Value p.Example = param.Example.Value - p.Examples = ExtractExamples(param.Examples.Value) + p.Examples = base.ExtractExamples(param.Examples.Value) p.Content = ExtractContent(param.Content.Value) p.Extensions = high.ExtractExtensions(param.Extensions) return p diff --git a/datamodel/high/3.0/contact.go b/datamodel/high/base/contact.go similarity index 97% rename from datamodel/high/3.0/contact.go rename to datamodel/high/base/contact.go index 6030b6a..4703bcf 100644 --- a/datamodel/high/3.0/contact.go +++ b/datamodel/high/base/contact.go @@ -1,7 +1,7 @@ // Copyright 2022 Princess B33f Heavy Industries / Dave Shanley // SPDX-License-Identifier: MIT -package v3 +package base import ( low "github.com/pb33f/libopenapi/datamodel/low/base" diff --git a/datamodel/high/3.0/discriminator.go b/datamodel/high/base/discriminator.go similarity index 97% rename from datamodel/high/3.0/discriminator.go rename to datamodel/high/base/discriminator.go index 055dc0d..7bef8d0 100644 --- a/datamodel/high/3.0/discriminator.go +++ b/datamodel/high/base/discriminator.go @@ -1,7 +1,7 @@ // Copyright 2022 Princess B33f Heavy Industries / Dave Shanley // SPDX-License-Identifier: MIT -package v3 +package base import ( low "github.com/pb33f/libopenapi/datamodel/low/base" diff --git a/datamodel/high/3.0/example.go b/datamodel/high/base/example.go similarity index 98% rename from datamodel/high/3.0/example.go rename to datamodel/high/base/example.go index afee317..4475522 100644 --- a/datamodel/high/3.0/example.go +++ b/datamodel/high/base/example.go @@ -1,7 +1,7 @@ // Copyright 2022 Princess B33f Heavy Industries / Dave Shanley // SPDX-License-Identifier: MIT -package v3 +package base import ( "github.com/pb33f/libopenapi/datamodel/high" diff --git a/datamodel/high/3.0/external_doc.go b/datamodel/high/base/external_doc.go similarity index 98% rename from datamodel/high/3.0/external_doc.go rename to datamodel/high/base/external_doc.go index 547393f..a19c72e 100644 --- a/datamodel/high/3.0/external_doc.go +++ b/datamodel/high/base/external_doc.go @@ -1,7 +1,7 @@ // Copyright 2022 Princess B33f Heavy Industries / Dave Shanley // SPDX-License-Identifier: MIT -package v3 +package base import ( "github.com/pb33f/libopenapi/datamodel/high" diff --git a/datamodel/high/3.0/info.go b/datamodel/high/base/info.go similarity index 98% rename from datamodel/high/3.0/info.go rename to datamodel/high/base/info.go index a1188ba..e6d82b5 100644 --- a/datamodel/high/3.0/info.go +++ b/datamodel/high/base/info.go @@ -1,7 +1,7 @@ // Copyright 2022 Princess B33f Heavy Industries / Dave Shanley // SPDX-License-Identifier: MIT -package v3 +package base import ( low "github.com/pb33f/libopenapi/datamodel/low/base" diff --git a/datamodel/high/3.0/license.go b/datamodel/high/base/license.go similarity index 97% rename from datamodel/high/3.0/license.go rename to datamodel/high/base/license.go index b485044..2b0458a 100644 --- a/datamodel/high/3.0/license.go +++ b/datamodel/high/base/license.go @@ -1,7 +1,7 @@ // Copyright 2022 Princess B33f Heavy Industries / Dave Shanley // SPDX-License-Identifier: MIT -package v3 +package base import ( low "github.com/pb33f/libopenapi/datamodel/low/base" diff --git a/datamodel/high/3.0/schema.go b/datamodel/high/base/schema.go similarity index 99% rename from datamodel/high/3.0/schema.go rename to datamodel/high/base/schema.go index 35a2c3e..f1310aa 100644 --- a/datamodel/high/3.0/schema.go +++ b/datamodel/high/base/schema.go @@ -1,7 +1,7 @@ // Copyright 2022 Princess B33f Heavy Industries / Dave Shanley // SPDX-License-Identifier: MIT -package v3 +package base import ( "github.com/pb33f/libopenapi/datamodel/high" diff --git a/datamodel/high/3.0/schema_proxy.go b/datamodel/high/base/schema_proxy.go similarity index 80% rename from datamodel/high/3.0/schema_proxy.go rename to datamodel/high/base/schema_proxy.go index 2bd2968..4c9bfcc 100644 --- a/datamodel/high/3.0/schema_proxy.go +++ b/datamodel/high/base/schema_proxy.go @@ -1,7 +1,7 @@ // Copyright 2022 Princess B33f Heavy Industries / Dave Shanley // SPDX-License-Identifier: MIT -package v3 +package base import ( "github.com/pb33f/libopenapi/datamodel/low" @@ -13,6 +13,10 @@ type SchemaProxy struct { buildError error } +func NewSchemaProxy(schema *low.NodeReference[*v3.SchemaProxy]) *SchemaProxy { + return &SchemaProxy{schema: schema} +} + func (sp *SchemaProxy) Schema() *Schema { s := sp.schema.Value.Schema() if s == nil { diff --git a/datamodel/high/3.0/schema_test.go b/datamodel/high/base/schema_test.go similarity index 98% rename from datamodel/high/3.0/schema_test.go rename to datamodel/high/base/schema_test.go index 2922866..2565425 100644 --- a/datamodel/high/3.0/schema_test.go +++ b/datamodel/high/base/schema_test.go @@ -1,7 +1,7 @@ // Copyright 2022 Princess B33f Heavy Industries / Dave Shanley // SPDX-License-Identifier: MIT -package v3 +package base import ( "github.com/pb33f/libopenapi/datamodel/low" diff --git a/datamodel/high/3.0/tag.go b/datamodel/high/base/tag.go similarity index 99% rename from datamodel/high/3.0/tag.go rename to datamodel/high/base/tag.go index c176e68..958b0d6 100644 --- a/datamodel/high/3.0/tag.go +++ b/datamodel/high/base/tag.go @@ -1,7 +1,7 @@ // Copyright 2022 Princess B33f Heavy Industries / Dave Shanley // SPDX-License-Identifier: MIT -package v3 +package base import ( "github.com/pb33f/libopenapi/datamodel/high" diff --git a/datamodel/high/3.0/xml.go b/datamodel/high/base/xml.go similarity index 98% rename from datamodel/high/3.0/xml.go rename to datamodel/high/base/xml.go index f46567f..0fa3c83 100644 --- a/datamodel/high/3.0/xml.go +++ b/datamodel/high/base/xml.go @@ -1,7 +1,7 @@ // Copyright 2022 Princess B33f Heavy Industries / Dave Shanley // SPDX-License-Identifier: MIT -package v3 +package base import ( "github.com/pb33f/libopenapi/datamodel/high" diff --git a/datamodel/low/base/schema_proxy.go b/datamodel/low/base/schema_proxy.go index f6f2ea4..a6aaa8a 100644 --- a/datamodel/low/base/schema_proxy.go +++ b/datamodel/low/base/schema_proxy.go @@ -6,7 +6,6 @@ package base import ( "github.com/pb33f/libopenapi/datamodel/low" "github.com/pb33f/libopenapi/index" - "go.uber.org/zap" "gopkg.in/yaml.v3" ) @@ -32,10 +31,10 @@ func (sp *SchemaProxy) Schema() *Schema { _ = low.BuildModel(sp.vn, schema) err := schema.Build(sp.vn, sp.idx) if err != nil { - low.Log.Error("unable to build schema", - zap.Int("line", sp.vn.Line), - zap.Int("column", sp.vn.Column), - zap.String("error", err.Error())) + //low.Log.Error("unable to build schema", + // zap.Int("line", sp.vn.Line), + // zap.Int("column", sp.vn.Column), + // zap.String("error", err.Error())) sp.buildError = err return nil } diff --git a/datamodel/low/extraction_functions.go b/datamodel/low/extraction_functions.go index 9cb08e7..6ccdda7 100644 --- a/datamodel/low/extraction_functions.go +++ b/datamodel/low/extraction_functions.go @@ -8,7 +8,6 @@ import ( "github.com/pb33f/libopenapi/index" "github.com/pb33f/libopenapi/utils" "github.com/vmware-labs/yaml-jsonpath/pkg/yamlpath" - "go.uber.org/zap" "gopkg.in/yaml.v3" "strconv" "strings" @@ -73,12 +72,12 @@ func LocateRefNode(root *yaml.Node, idx *index.SpecIndex) (*yaml.Node, error) { if !IsCircular(found[rv].Node, idx) { return LocateRefNode(found[rv].Node, idx) } else { - Log.Error("circular reference found during lookup, and will remain un-resolved.", - zap.Int("line", found[rv].Node.Line), - zap.Int("column", found[rv].Node.Column), - zap.String("reference", found[rv].Definition), - zap.String("journey", - GetCircularReferenceResult(found[rv].Node, idx).GenerateJourneyPath())) + //Log.Error("circular reference found during lookup, and will remain un-resolved.", + // zap.Int("line", found[rv].Node.Line), + // zap.Int("column", found[rv].Node.Column), + // zap.String("reference", found[rv].Definition), + // zap.String("journey", + // GetCircularReferenceResult(found[rv].Node, idx).GenerateJourneyPath())) return found[rv].Node, fmt.Errorf("circular reference '%s' found during lookup at line %d, column %d, "+ "It cannot be resolved", diff --git a/datamodel/low/log.go b/datamodel/low/log.go index 4659712..5482145 100644 --- a/datamodel/low/log.go +++ b/datamodel/low/log.go @@ -3,10 +3,10 @@ package low -import "go.uber.org/zap" - -var Log *zap.Logger - -func init() { - Log, _ = zap.NewProduction() -} +//import "go.uber.org/zap" +// +//var Log *zap.Logger +// +//func init() { +// Log, _ = zap.NewProduction() +//} diff --git a/document.go b/document.go new file mode 100644 index 0000000..5cafa50 --- /dev/null +++ b/document.go @@ -0,0 +1,28 @@ +// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley +// SPDX-License-Identifier: MIT + +package main + +import ( + "github.com/pb33f/libopenapi/datamodel" + v2high "github.com/pb33f/libopenapi/datamodel/high/2.0" + v3high "github.com/pb33f/libopenapi/datamodel/high/3.0" +) + +type Document[T any] struct { + version string + info *datamodel.SpecInfo + Model T +} + +func (d *Document[T]) GetVersion() string { + return d.version +} + +func (d *Document[T]) BuildV2Document() (*v2high.Swagger, error) { + return nil, nil +} + +func (d *Document[T]) BuildV3Document() (*v3high.Document, error) { + return nil, nil +} diff --git a/document_test.go b/document_test.go new file mode 100644 index 0000000..d602a10 --- /dev/null +++ b/document_test.go @@ -0,0 +1,26 @@ +// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley +// SPDX-License-Identifier: MIT + +package main + +import ( + "testing" +) + +func TestLoadDocument_Simple(t *testing.T) { + // + //yml := `openapi: 3.0.1` + //doc, err := LoadDocument([]byte(yml)) + //assert.NoError(t, err) + //assert.Equal(t, "3.0.1", doc.GetVersion()) + +} + +func TestLoadDocument_WithInfo(t *testing.T) { + + //yml := `openapi: 3.0.1` + //doc, err := LoadDocument([]byte(yml)) + //assert.NoError(t, err) + //assert.Equal(t, "3.0.1", doc.GetVersion()) + +} diff --git a/go.mod b/go.mod index 2d5cfe2..71611ff 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/pb33f/libopenapi go 1.18 require ( + github.com/iancoleman/strcase v0.2.0 github.com/stretchr/testify v1.8.0 github.com/vmware-labs/yaml-jsonpath v0.3.2 gopkg.in/yaml.v3 v3.0.1 @@ -11,9 +12,5 @@ require ( require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/dprotaso/go-yit v0.0.0-20220510233725-9ba8df137936 // indirect - github.com/iancoleman/strcase v0.2.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - go.uber.org/atomic v1.10.0 // indirect - go.uber.org/multierr v1.8.0 // indirect - go.uber.org/zap v1.23.0 // indirect ) diff --git a/test_specs/petstorev2-complete.yaml b/test_specs/petstorev2-complete.yaml index b4320a5..53e85d5 100644 --- a/test_specs/petstorev2-complete.yaml +++ b/test_specs/petstorev2-complete.yaml @@ -21,6 +21,10 @@ parameters: name: simple type: string x-chicken: nuggets +security: + - global_auth: + - write:pets + - read:pets tags: - name: pet description: Everything about your Pets