mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-07 12:37:48 +00:00
Support for DynamicValue inline rendering.
Signed-off-by: Dave Shanley <dave@quobix.com>
This commit is contained in:
@@ -4,9 +4,9 @@
|
|||||||
package base
|
package base
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/pb33f/libopenapi/datamodel/high"
|
"github.com/pb33f/libopenapi/datamodel/high"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
"reflect"
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DynamicValue is used to hold multiple possible values for a schema property. There are two values, a left
|
// DynamicValue is used to hold multiple possible values for a schema property. There are two values, a left
|
||||||
@@ -18,61 +18,101 @@ import (
|
|||||||
// The N value is a bit to make it each to know which value (A or B) is used, this prevents having to
|
// The N value is a bit to make it each to know which value (A or B) is used, this prevents having to
|
||||||
// if/else on the value to determine which one is set.
|
// if/else on the value to determine which one is set.
|
||||||
type DynamicValue[A any, B any] struct {
|
type DynamicValue[A any, B any] struct {
|
||||||
N int // 0 == A, 1 == B
|
N int // 0 == A, 1 == B
|
||||||
A A
|
A A
|
||||||
B B
|
B B
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsA will return true if the 'A' or left value is set. (OpenAPI 3)
|
// IsA will return true if the 'A' or left value is set. (OpenAPI 3)
|
||||||
func (d *DynamicValue[A, B]) IsA() bool {
|
func (d *DynamicValue[A, B]) IsA() bool {
|
||||||
return d.N == 0
|
return d.N == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsB will return true if the 'B' or right value is set (OpenAPI 3.1)
|
// IsB will return true if the 'B' or right value is set (OpenAPI 3.1)
|
||||||
func (d *DynamicValue[A, B]) IsB() bool {
|
func (d *DynamicValue[A, B]) IsB() bool {
|
||||||
return d.N == 1
|
return d.N == 1
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DynamicValue[A, B]) Render() ([]byte, error) {
|
func (d *DynamicValue[A, B]) Render() ([]byte, error) {
|
||||||
return yaml.Marshal(d)
|
return yaml.Marshal(d)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarshalYAML will create a ready to render YAML representation of the DynamicValue object.
|
// MarshalYAML will create a ready to render YAML representation of the DynamicValue object.
|
||||||
func (d *DynamicValue[A, B]) MarshalYAML() (interface{}, error) {
|
func (d *DynamicValue[A, B]) MarshalYAML() (interface{}, error) {
|
||||||
// this is a custom renderer, we can't use the NodeBuilder out of the gate.
|
// this is a custom renderer, we can't use the NodeBuilder out of the gate.
|
||||||
var n yaml.Node
|
var n yaml.Node
|
||||||
var err error
|
var err error
|
||||||
var value any
|
var value any
|
||||||
|
|
||||||
if d.IsA() {
|
if d.IsA() {
|
||||||
value = d.A
|
value = d.A
|
||||||
}
|
}
|
||||||
if d.IsB() {
|
if d.IsB() {
|
||||||
value = d.B
|
value = d.B
|
||||||
}
|
}
|
||||||
to := reflect.TypeOf(value)
|
to := reflect.TypeOf(value)
|
||||||
switch to.Kind() {
|
switch to.Kind() {
|
||||||
case reflect.Ptr:
|
case reflect.Ptr:
|
||||||
if r, ok := value.(high.Renderable); ok {
|
if r, ok := value.(high.Renderable); ok {
|
||||||
return r.MarshalYAML()
|
return r.MarshalYAML()
|
||||||
} else {
|
} else {
|
||||||
_ = n.Encode(value)
|
_ = n.Encode(value)
|
||||||
}
|
}
|
||||||
case reflect.Bool:
|
case reflect.Bool:
|
||||||
_ = n.Encode(value.(bool))
|
_ = n.Encode(value.(bool))
|
||||||
case reflect.Int:
|
case reflect.Int:
|
||||||
_ = n.Encode(value.(int))
|
_ = n.Encode(value.(int))
|
||||||
case reflect.String:
|
case reflect.String:
|
||||||
_ = n.Encode(value.(string))
|
_ = n.Encode(value.(string))
|
||||||
case reflect.Int64:
|
case reflect.Int64:
|
||||||
_ = n.Encode(value.(int64))
|
_ = n.Encode(value.(int64))
|
||||||
case reflect.Float64:
|
case reflect.Float64:
|
||||||
_ = n.Encode(value.(float64))
|
_ = n.Encode(value.(float64))
|
||||||
case reflect.Float32:
|
case reflect.Float32:
|
||||||
_ = n.Encode(value.(float32))
|
_ = n.Encode(value.(float32))
|
||||||
case reflect.Int32:
|
case reflect.Int32:
|
||||||
_ = n.Encode(value.(int32))
|
_ = n.Encode(value.(int32))
|
||||||
|
|
||||||
}
|
}
|
||||||
return &n, err
|
return &n, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalYAML will create a ready to render YAML representation of the DynamicValue object.
|
||||||
|
func (d *DynamicValue[A, B]) MarshalYAMLInline() (interface{}, error) {
|
||||||
|
// this is a custom renderer, we can't use the NodeBuilder out of the gate.
|
||||||
|
var n yaml.Node
|
||||||
|
var err error
|
||||||
|
var value any
|
||||||
|
|
||||||
|
if d.IsA() {
|
||||||
|
value = d.A
|
||||||
|
}
|
||||||
|
if d.IsB() {
|
||||||
|
value = d.B
|
||||||
|
}
|
||||||
|
to := reflect.TypeOf(value)
|
||||||
|
switch to.Kind() {
|
||||||
|
case reflect.Ptr:
|
||||||
|
if r, ok := value.(high.RenderableInline); ok {
|
||||||
|
return r.MarshalYAMLInline()
|
||||||
|
} else {
|
||||||
|
_ = n.Encode(value)
|
||||||
|
}
|
||||||
|
case reflect.Bool:
|
||||||
|
_ = n.Encode(value.(bool))
|
||||||
|
case reflect.Int:
|
||||||
|
_ = n.Encode(value.(int))
|
||||||
|
case reflect.String:
|
||||||
|
_ = n.Encode(value.(string))
|
||||||
|
case reflect.Int64:
|
||||||
|
_ = n.Encode(value.(int64))
|
||||||
|
case reflect.Float64:
|
||||||
|
_ = n.Encode(value.(float64))
|
||||||
|
case reflect.Float32:
|
||||||
|
_ = n.Encode(value.(float32))
|
||||||
|
case reflect.Int32:
|
||||||
|
_ = n.Encode(value.(int32))
|
||||||
|
|
||||||
|
}
|
||||||
|
return &n, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,12 +4,12 @@
|
|||||||
package v3
|
package v3
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/pb33f/libopenapi/datamodel/high"
|
"github.com/pb33f/libopenapi/datamodel/high"
|
||||||
low "github.com/pb33f/libopenapi/datamodel/low/v3"
|
low "github.com/pb33f/libopenapi/datamodel/low/v3"
|
||||||
"github.com/pb33f/libopenapi/utils"
|
"github.com/pb33f/libopenapi/utils"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
"sort"
|
"sort"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Responses represents a high-level OpenAPI 3+ Responses object that is backed by a low-level one.
|
// Responses represents a high-level OpenAPI 3+ Responses object that is backed by a low-level one.
|
||||||
@@ -27,123 +27,128 @@ import (
|
|||||||
// be the response for a successful operation call.
|
// be the response for a successful operation call.
|
||||||
// - https://spec.openapis.org/oas/v3.1.0#responses-object
|
// - https://spec.openapis.org/oas/v3.1.0#responses-object
|
||||||
type Responses struct {
|
type Responses struct {
|
||||||
Codes map[string]*Response `json:"-" yaml:"-"`
|
Codes map[string]*Response `json:"-" yaml:"-"`
|
||||||
Default *Response `json:"default,omitempty" yaml:"default,omitempty"`
|
Default *Response `json:"default,omitempty" yaml:"default,omitempty"`
|
||||||
Extensions map[string]any `json:"-" yaml:"-"`
|
Extensions map[string]any `json:"-" yaml:"-"`
|
||||||
low *low.Responses
|
low *low.Responses
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewResponses will create a new high-level Responses instance from a low-level one. It operates asynchronously
|
// NewResponses will create a new high-level Responses instance from a low-level one. It operates asynchronously
|
||||||
// internally, as each response may be considerable in complexity.
|
// internally, as each response may be considerable in complexity.
|
||||||
func NewResponses(responses *low.Responses) *Responses {
|
func NewResponses(responses *low.Responses) *Responses {
|
||||||
r := new(Responses)
|
r := new(Responses)
|
||||||
r.low = responses
|
r.low = responses
|
||||||
r.Extensions = high.ExtractExtensions(responses.Extensions)
|
r.Extensions = high.ExtractExtensions(responses.Extensions)
|
||||||
if !responses.Default.IsEmpty() {
|
if !responses.Default.IsEmpty() {
|
||||||
r.Default = NewResponse(responses.Default.Value)
|
r.Default = NewResponse(responses.Default.Value)
|
||||||
}
|
}
|
||||||
codes := make(map[string]*Response)
|
codes := make(map[string]*Response)
|
||||||
|
|
||||||
// struct to hold response and code sent over chan.
|
// struct to hold response and code sent over chan.
|
||||||
type respRes struct {
|
type respRes struct {
|
||||||
code string
|
code string
|
||||||
resp *Response
|
resp *Response
|
||||||
}
|
}
|
||||||
|
|
||||||
// build each response async for speed
|
// build each response async for speed
|
||||||
rChan := make(chan respRes)
|
rChan := make(chan respRes)
|
||||||
var buildResponse = func(code string, resp *low.Response, c chan respRes) {
|
var buildResponse = func(code string, resp *low.Response, c chan respRes) {
|
||||||
c <- respRes{code: code, resp: NewResponse(resp)}
|
c <- respRes{code: code, resp: NewResponse(resp)}
|
||||||
}
|
}
|
||||||
for k, v := range responses.Codes {
|
for k, v := range responses.Codes {
|
||||||
go buildResponse(k.Value, v.Value, rChan)
|
go buildResponse(k.Value, v.Value, rChan)
|
||||||
}
|
}
|
||||||
totalCodes := len(responses.Codes)
|
totalCodes := len(responses.Codes)
|
||||||
codesParsed := 0
|
codesParsed := 0
|
||||||
for codesParsed < totalCodes {
|
for codesParsed < totalCodes {
|
||||||
select {
|
select {
|
||||||
case re := <-rChan:
|
case re := <-rChan:
|
||||||
codesParsed++
|
codesParsed++
|
||||||
codes[re.code] = re.resp
|
codes[re.code] = re.resp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
r.Codes = codes
|
r.Codes = codes
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindResponseByCode is a shortcut for looking up code by an integer vs. a string
|
// FindResponseByCode is a shortcut for looking up code by an integer vs. a string
|
||||||
func (r *Responses) FindResponseByCode(code int) *Response {
|
func (r *Responses) FindResponseByCode(code int) *Response {
|
||||||
return r.Codes[fmt.Sprintf("%d", code)]
|
return r.Codes[fmt.Sprintf("%d", code)]
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetDefaultResponse will
|
||||||
|
func (r *Responses) GetDefaultResponse(code int) *Response {
|
||||||
|
return r.Codes[fmt.Sprintf("%d", code)]
|
||||||
}
|
}
|
||||||
|
|
||||||
// GoLow returns the low-level Response object used to create the high-level one.
|
// GoLow returns the low-level Response object used to create the high-level one.
|
||||||
func (r *Responses) GoLow() *low.Responses {
|
func (r *Responses) GoLow() *low.Responses {
|
||||||
return r.low
|
return r.low
|
||||||
}
|
}
|
||||||
|
|
||||||
// GoLowUntyped will return the low-level Responses instance that was used to create the high-level one, with no type
|
// GoLowUntyped will return the low-level Responses instance that was used to create the high-level one, with no type
|
||||||
func (r *Responses) GoLowUntyped() any {
|
func (r *Responses) GoLowUntyped() any {
|
||||||
return r.low
|
return r.low
|
||||||
}
|
}
|
||||||
|
|
||||||
// Render will return a YAML representation of the Responses object as a byte slice.
|
// Render will return a YAML representation of the Responses object as a byte slice.
|
||||||
func (r *Responses) Render() ([]byte, error) {
|
func (r *Responses) Render() ([]byte, error) {
|
||||||
return yaml.Marshal(r)
|
return yaml.Marshal(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarshalYAML will create a ready to render YAML representation of the Responses object.
|
// MarshalYAML will create a ready to render YAML representation of the Responses object.
|
||||||
func (r *Responses) MarshalYAML() (interface{}, error) {
|
func (r *Responses) MarshalYAML() (interface{}, error) {
|
||||||
// map keys correctly.
|
// map keys correctly.
|
||||||
m := utils.CreateEmptyMapNode()
|
m := utils.CreateEmptyMapNode()
|
||||||
type responseItem struct {
|
type responseItem struct {
|
||||||
resp *Response
|
resp *Response
|
||||||
code string
|
code string
|
||||||
line int
|
line int
|
||||||
ext *yaml.Node
|
ext *yaml.Node
|
||||||
}
|
}
|
||||||
var mapped []*responseItem
|
var mapped []*responseItem
|
||||||
|
|
||||||
for k, re := range r.Codes {
|
for k, re := range r.Codes {
|
||||||
ln := 9999 // default to a high value to weight new content to the bottom.
|
ln := 9999 // default to a high value to weight new content to the bottom.
|
||||||
if r.low != nil {
|
if r.low != nil {
|
||||||
for lKey := range r.low.Codes {
|
for lKey := range r.low.Codes {
|
||||||
if lKey.Value == k {
|
if lKey.Value == k {
|
||||||
ln = lKey.KeyNode.Line
|
ln = lKey.KeyNode.Line
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mapped = append(mapped, &responseItem{re, k, ln, nil})
|
mapped = append(mapped, &responseItem{re, k, ln, nil})
|
||||||
}
|
}
|
||||||
|
|
||||||
// extract extensions
|
// extract extensions
|
||||||
nb := high.NewNodeBuilder(r, r.low)
|
nb := high.NewNodeBuilder(r, r.low)
|
||||||
extNode := nb.Render()
|
extNode := nb.Render()
|
||||||
if extNode != nil && extNode.Content != nil {
|
if extNode != nil && extNode.Content != nil {
|
||||||
var label string
|
var label string
|
||||||
for u := range extNode.Content {
|
for u := range extNode.Content {
|
||||||
if u%2 == 0 {
|
if u%2 == 0 {
|
||||||
label = extNode.Content[u].Value
|
label = extNode.Content[u].Value
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
mapped = append(mapped, &responseItem{nil, label,
|
mapped = append(mapped, &responseItem{nil, label,
|
||||||
extNode.Content[u].Line, extNode.Content[u]})
|
extNode.Content[u].Line, extNode.Content[u]})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Slice(mapped, func(i, j int) bool {
|
sort.Slice(mapped, func(i, j int) bool {
|
||||||
return mapped[i].line < mapped[j].line
|
return mapped[i].line < mapped[j].line
|
||||||
})
|
})
|
||||||
for j := range mapped {
|
for j := range mapped {
|
||||||
if mapped[j].resp != nil {
|
if mapped[j].resp != nil {
|
||||||
rendered, _ := mapped[j].resp.MarshalYAML()
|
rendered, _ := mapped[j].resp.MarshalYAML()
|
||||||
m.Content = append(m.Content, utils.CreateStringNode(mapped[j].code))
|
m.Content = append(m.Content, utils.CreateStringNode(mapped[j].code))
|
||||||
m.Content = append(m.Content, rendered.(*yaml.Node))
|
m.Content = append(m.Content, rendered.(*yaml.Node))
|
||||||
}
|
}
|
||||||
if mapped[j].ext != nil {
|
if mapped[j].ext != nil {
|
||||||
m.Content = append(m.Content, utils.CreateStringNode(mapped[j].code))
|
m.Content = append(m.Content, utils.CreateStringNode(mapped[j].code))
|
||||||
m.Content = append(m.Content, mapped[j].ext)
|
m.Content = append(m.Content, mapped[j].ext)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user