mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-10 12:37:48 +00:00
Fleshing out high level model for v2 (swagger)
This commit is contained in:
@@ -3,8 +3,31 @@
|
|||||||
|
|
||||||
package v2
|
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 {
|
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,8 +6,8 @@ package v2
|
|||||||
import low "github.com/pb33f/libopenapi/datamodel/low/2.0"
|
import low "github.com/pb33f/libopenapi/datamodel/low/2.0"
|
||||||
|
|
||||||
type ResponsesDefinitions struct {
|
type ResponsesDefinitions struct {
|
||||||
Values map[string]*Response
|
Definitions map[string]*Response
|
||||||
low *low.ResponsesDefinitions
|
low *low.ResponsesDefinitions
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewResponsesDefinitions(responsesDefinitions *low.ResponsesDefinitions) *ResponsesDefinitions {
|
func NewResponsesDefinitions(responsesDefinitions *low.ResponsesDefinitions) *ResponsesDefinitions {
|
||||||
@@ -17,6 +17,6 @@ func NewResponsesDefinitions(responsesDefinitions *low.ResponsesDefinitions) *Re
|
|||||||
for k := range responsesDefinitions.Definitions {
|
for k := range responsesDefinitions.Definitions {
|
||||||
responses[k.Value] = NewResponse(responsesDefinitions.Definitions[k].Value)
|
responses[k.Value] = NewResponse(responsesDefinitions.Definitions[k].Value)
|
||||||
}
|
}
|
||||||
rd.Values = responses
|
rd.Definitions = responses
|
||||||
return rd
|
return rd
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ package v2
|
|||||||
import low "github.com/pb33f/libopenapi/datamodel/low/2.0"
|
import low "github.com/pb33f/libopenapi/datamodel/low/2.0"
|
||||||
|
|
||||||
type SecurityDefinitions struct {
|
type SecurityDefinitions struct {
|
||||||
Values map[string]*SecurityScheme
|
Definitions map[string]*SecurityScheme
|
||||||
low *low.SecurityDefinitions
|
low *low.SecurityDefinitions
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSecurityDefinitions(definitions *low.SecurityDefinitions) *SecurityDefinitions {
|
func NewSecurityDefinitions(definitions *low.SecurityDefinitions) *SecurityDefinitions {
|
||||||
@@ -17,7 +17,7 @@ func NewSecurityDefinitions(definitions *low.SecurityDefinitions) *SecurityDefin
|
|||||||
for k := range definitions.Definitions {
|
for k := range definitions.Definitions {
|
||||||
schemes[k.Value] = NewSecurityScheme(definitions.Definitions[k].Value)
|
schemes[k.Value] = NewSecurityScheme(definitions.Definitions[k].Value)
|
||||||
}
|
}
|
||||||
sd.Values = schemes
|
sd.Definitions = schemes
|
||||||
return sd
|
return sd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -66,11 +66,20 @@ func NewSwaggerDocument(document *low.Swagger) *Swagger {
|
|||||||
d.Produces = produces
|
d.Produces = produces
|
||||||
}
|
}
|
||||||
// TODO: Paths
|
// 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() {
|
if !document.SecurityDefinitions.IsEmpty() {
|
||||||
d.SecurityDefinitions = NewSecurityDefinitions(document.SecurityDefinitions.Value)
|
d.SecurityDefinitions = NewSecurityDefinitions(document.SecurityDefinitions.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !document.Security.IsEmpty() {
|
if !document.Security.IsEmpty() {
|
||||||
var security []*SecurityRequirement
|
var security []*SecurityRequirement
|
||||||
for s := range document.Security.Value {
|
for s := range document.Security.Value {
|
||||||
|
|||||||
@@ -5,15 +5,15 @@ package base
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/pb33f/libopenapi/datamodel/low"
|
"github.com/pb33f/libopenapi/datamodel/low"
|
||||||
v3 "github.com/pb33f/libopenapi/datamodel/low/base"
|
"github.com/pb33f/libopenapi/datamodel/low/base"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SchemaProxy struct {
|
type SchemaProxy struct {
|
||||||
schema *low.NodeReference[*v3.SchemaProxy]
|
schema *low.NodeReference[*base.SchemaProxy]
|
||||||
buildError error
|
buildError error
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSchemaProxy(schema *low.NodeReference[*v3.SchemaProxy]) *SchemaProxy {
|
func NewSchemaProxy(schema *low.NodeReference[*base.SchemaProxy]) *SchemaProxy {
|
||||||
return &SchemaProxy{schema: schema}
|
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 {
|
func (p *Parameter) Build(root *yaml.Node, idx *index.SpecIndex) error {
|
||||||
p.Extensions = low.ExtractExtensions(root)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user