mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-10 20:47:44 +00:00
Fleshing out high level model for v2 (swagger)
This commit is contained in:
@@ -3,8 +3,31 @@
|
||||
|
||||
package v2
|
||||
|
||||
import (
|
||||
highbase "github.com/pb33f/libopenapi/datamodel/high/base"
|
||||
lowmodel "github.com/pb33f/libopenapi/datamodel/low"
|
||||
low "github.com/pb33f/libopenapi/datamodel/low/2.0"
|
||||
lowbase "github.com/pb33f/libopenapi/datamodel/low/base"
|
||||
)
|
||||
|
||||
type Definitions struct {
|
||||
Definitions map[string]*highbase.SchemaProxy
|
||||
low *low.Definitions
|
||||
}
|
||||
|
||||
type ParameterDefinitions struct {
|
||||
func NewDefinitions(definitions *low.Definitions) *Definitions {
|
||||
rd := new(Definitions)
|
||||
rd.low = definitions
|
||||
defs := make(map[string]*highbase.SchemaProxy)
|
||||
for k := range definitions.Schemas {
|
||||
defs[k.Value] = highbase.NewSchemaProxy(&lowmodel.NodeReference[*lowbase.SchemaProxy]{
|
||||
Value: definitions.Schemas[k].Value,
|
||||
})
|
||||
}
|
||||
rd.Definitions = defs
|
||||
return rd
|
||||
}
|
||||
|
||||
func (d *Definitions) GoLow() *low.Definitions {
|
||||
return d.low
|
||||
}
|
||||
|
||||
115
datamodel/high/2.0/parameter.go
Normal file
115
datamodel/high/2.0/parameter.go
Normal file
@@ -0,0 +1,115 @@
|
||||
// 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 Parameter struct {
|
||||
Name string
|
||||
In string
|
||||
Type string
|
||||
Format string
|
||||
Description string
|
||||
Required bool
|
||||
AllowEmptyValue bool
|
||||
Schema *base.SchemaProxy
|
||||
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.Parameter
|
||||
}
|
||||
|
||||
func NewParameter(parameter *low.Parameter) *Parameter {
|
||||
p := new(Parameter)
|
||||
p.low = parameter
|
||||
p.Extensions = high.ExtractExtensions(parameter.Extensions)
|
||||
if !parameter.Name.IsEmpty() {
|
||||
p.Name = parameter.Name.Value
|
||||
}
|
||||
if !parameter.In.IsEmpty() {
|
||||
p.In = parameter.In.Value
|
||||
}
|
||||
if !parameter.Type.IsEmpty() {
|
||||
p.Type = parameter.Type.Value
|
||||
}
|
||||
if !parameter.Format.IsEmpty() {
|
||||
p.Format = parameter.Type.Value
|
||||
}
|
||||
if !parameter.Description.IsEmpty() {
|
||||
p.Description = parameter.Description.Value
|
||||
}
|
||||
if !parameter.Required.IsEmpty() {
|
||||
p.Required = parameter.Required.Value
|
||||
}
|
||||
if !parameter.AllowEmptyValue.IsEmpty() {
|
||||
p.AllowEmptyValue = parameter.AllowEmptyValue.Value
|
||||
}
|
||||
if !parameter.Schema.IsEmpty() {
|
||||
p.Schema = base.NewSchemaProxy(¶meter.Schema)
|
||||
}
|
||||
if !parameter.Items.IsEmpty() {
|
||||
p.Items = NewItems(parameter.Items.Value)
|
||||
}
|
||||
if !parameter.CollectionFormat.IsEmpty() {
|
||||
p.CollectionFormat = parameter.CollectionFormat.Value
|
||||
}
|
||||
if !parameter.Default.IsEmpty() {
|
||||
p.Default = parameter.Default.Value
|
||||
}
|
||||
if !parameter.Maximum.IsEmpty() {
|
||||
p.Maximum = parameter.Maximum.Value
|
||||
}
|
||||
if !parameter.ExclusiveMaximum.IsEmpty() {
|
||||
p.ExclusiveMaximum = parameter.ExclusiveMaximum.Value
|
||||
}
|
||||
if !parameter.Minimum.IsEmpty() {
|
||||
p.Minimum = parameter.Minimum.Value
|
||||
}
|
||||
if !parameter.ExclusiveMinimum.Value {
|
||||
p.ExclusiveMinimum = parameter.ExclusiveMinimum.Value
|
||||
}
|
||||
if !parameter.MaxLength.IsEmpty() {
|
||||
p.MaxLength = parameter.MaxLength.Value
|
||||
}
|
||||
if !parameter.MinLength.IsEmpty() {
|
||||
p.MinLength = parameter.MinLength.Value
|
||||
}
|
||||
if !parameter.Pattern.IsEmpty() {
|
||||
p.Pattern = parameter.Pattern.Value
|
||||
}
|
||||
if !parameter.MinItems.IsEmpty() {
|
||||
p.MinItems = parameter.MinItems.Value
|
||||
}
|
||||
if !parameter.MaxItems.IsEmpty() {
|
||||
p.MaxItems = parameter.MaxItems.Value
|
||||
}
|
||||
if !parameter.UniqueItems.IsEmpty() {
|
||||
p.UniqueItems = parameter.UniqueItems.IsEmpty()
|
||||
}
|
||||
if !parameter.Enum.IsEmpty() {
|
||||
p.Enum = parameter.Enum.Value
|
||||
}
|
||||
if !parameter.MultipleOf.IsEmpty() {
|
||||
p.MultipleOf = parameter.MultipleOf.Value
|
||||
}
|
||||
|
||||
return p
|
||||
}
|
||||
22
datamodel/high/2.0/parameter_definitions.go
Normal file
22
datamodel/high/2.0/parameter_definitions.go
Normal file
@@ -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 ParameterDefinitions struct {
|
||||
Definitions map[string]*Parameter
|
||||
low *low.ParameterDefinitions
|
||||
}
|
||||
|
||||
func NewParametersDefinitions(parametersDefinitions *low.ParameterDefinitions) *ParameterDefinitions {
|
||||
pd := new(ParameterDefinitions)
|
||||
pd.low = parametersDefinitions
|
||||
params := make(map[string]*Parameter)
|
||||
for k := range parametersDefinitions.Definitions {
|
||||
params[k.Value] = NewParameter(parametersDefinitions.Definitions[k].Value)
|
||||
}
|
||||
pd.Definitions = params
|
||||
return pd
|
||||
}
|
||||
@@ -6,7 +6,7 @@ package v2
|
||||
import low "github.com/pb33f/libopenapi/datamodel/low/2.0"
|
||||
|
||||
type ResponsesDefinitions struct {
|
||||
Values map[string]*Response
|
||||
Definitions map[string]*Response
|
||||
low *low.ResponsesDefinitions
|
||||
}
|
||||
|
||||
@@ -17,6 +17,6 @@ func NewResponsesDefinitions(responsesDefinitions *low.ResponsesDefinitions) *Re
|
||||
for k := range responsesDefinitions.Definitions {
|
||||
responses[k.Value] = NewResponse(responsesDefinitions.Definitions[k].Value)
|
||||
}
|
||||
rd.Values = responses
|
||||
rd.Definitions = responses
|
||||
return rd
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ package v2
|
||||
import low "github.com/pb33f/libopenapi/datamodel/low/2.0"
|
||||
|
||||
type SecurityDefinitions struct {
|
||||
Values map[string]*SecurityScheme
|
||||
Definitions map[string]*SecurityScheme
|
||||
low *low.SecurityDefinitions
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ func NewSecurityDefinitions(definitions *low.SecurityDefinitions) *SecurityDefin
|
||||
for k := range definitions.Definitions {
|
||||
schemes[k.Value] = NewSecurityScheme(definitions.Definitions[k].Value)
|
||||
}
|
||||
sd.Values = schemes
|
||||
sd.Definitions = schemes
|
||||
return sd
|
||||
}
|
||||
|
||||
|
||||
@@ -67,10 +67,19 @@ func NewSwaggerDocument(document *low.Swagger) *Swagger {
|
||||
}
|
||||
// TODO: Paths
|
||||
|
||||
if !document.Definitions.IsEmpty() {
|
||||
d.Definitions = NewDefinitions(document.Definitions.Value)
|
||||
}
|
||||
if !document.Parameters.IsEmpty() {
|
||||
d.Parameters = NewParametersDefinitions(document.Parameters.Value)
|
||||
}
|
||||
|
||||
if !document.Responses.IsEmpty() {
|
||||
d.Responses = NewResponsesDefinitions(document.Responses.Value)
|
||||
}
|
||||
if !document.SecurityDefinitions.IsEmpty() {
|
||||
d.SecurityDefinitions = NewSecurityDefinitions(document.SecurityDefinitions.Value)
|
||||
}
|
||||
|
||||
if !document.Security.IsEmpty() {
|
||||
var security []*SecurityRequirement
|
||||
for s := range document.Security.Value {
|
||||
|
||||
@@ -5,15 +5,15 @@ package base
|
||||
|
||||
import (
|
||||
"github.com/pb33f/libopenapi/datamodel/low"
|
||||
v3 "github.com/pb33f/libopenapi/datamodel/low/base"
|
||||
"github.com/pb33f/libopenapi/datamodel/low/base"
|
||||
)
|
||||
|
||||
type SchemaProxy struct {
|
||||
schema *low.NodeReference[*v3.SchemaProxy]
|
||||
schema *low.NodeReference[*base.SchemaProxy]
|
||||
buildError error
|
||||
}
|
||||
|
||||
func NewSchemaProxy(schema *low.NodeReference[*v3.SchemaProxy]) *SchemaProxy {
|
||||
func NewSchemaProxy(schema *low.NodeReference[*base.SchemaProxy]) *SchemaProxy {
|
||||
return &SchemaProxy{schema: schema}
|
||||
}
|
||||
|
||||
|
||||
@@ -47,5 +47,17 @@ func (p *Parameter) FindExtension(ext string) *low.ValueReference[any] {
|
||||
|
||||
func (p *Parameter) Build(root *yaml.Node, idx *index.SpecIndex) error {
|
||||
p.Extensions = low.ExtractExtensions(root)
|
||||
sch, sErr := base.ExtractSchema(root, idx)
|
||||
if sErr != nil {
|
||||
return sErr
|
||||
}
|
||||
if sch != nil {
|
||||
p.Schema = *sch
|
||||
}
|
||||
items, iErr := low.ExtractObject[*Items](ItemsLabel, root, idx)
|
||||
if iErr != nil {
|
||||
return iErr
|
||||
}
|
||||
p.Items = items
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user