Fixed broken tests and ready to keep chugging

This commit is contained in:
Dave Shanley
2022-08-06 18:06:22 -04:00
parent 2793504a92
commit 95e3cd9604
3 changed files with 90 additions and 90 deletions

View File

@@ -1,99 +1,99 @@
package v3
import (
"github.com/pb33f/libopenapi/datamodel/low"
"gopkg.in/yaml.v3"
"github.com/pb33f/libopenapi/datamodel/low"
"gopkg.in/yaml.v3"
)
const (
LinksLabel = "links"
LinksLabel = "links"
)
type Responses struct {
Codes map[low.KeyReference[string]]low.ValueReference[*Response]
Default low.NodeReference[*Response]
Codes map[low.KeyReference[string]]low.ValueReference[*Response]
Default low.NodeReference[*Response]
}
func (r *Responses) Build(root *yaml.Node) error {
codes, _, _, err := ExtractMapFlat[*Response](ResponsesLabel, root)
if err != nil {
return err
}
if codes != nil {
r.Codes = codes
}
return nil
codes, _, _, err := ExtractMapFlat[*Response](ResponsesLabel, root)
if err != nil {
return err
}
if codes != nil {
r.Codes = codes
}
return nil
}
func (r *Responses) FindResponseByCode(code string) *low.ValueReference[*Response] {
return FindItemInMap[*Response](code, r.Codes)
return FindItemInMap[*Response](code, r.Codes)
}
type Response struct {
Description low.NodeReference[string]
Headers low.NodeReference[map[low.KeyReference[string]]low.ValueReference[*Header]]
Content low.NodeReference[map[low.KeyReference[string]]low.ValueReference[*MediaType]]
Extensions map[low.KeyReference[string]]low.ValueReference[any]
Links low.NodeReference[map[low.KeyReference[string]]low.ValueReference[*Link]]
Description low.NodeReference[string]
Headers low.NodeReference[map[low.KeyReference[string]]low.ValueReference[*Header]]
Content low.NodeReference[map[low.KeyReference[string]]low.ValueReference[*MediaType]]
Extensions map[low.KeyReference[string]]low.ValueReference[any]
Links low.NodeReference[map[low.KeyReference[string]]low.ValueReference[*Link]]
}
func (r *Response) FindContent(cType string) *low.ValueReference[*MediaType] {
return FindItemInMap[*MediaType](cType, r.Content.Value)
return FindItemInMap[*MediaType](cType, r.Content.Value)
}
func (r *Response) FindHeader(hType string) *low.ValueReference[*Header] {
return FindItemInMap[*Header](hType, r.Headers.Value)
return FindItemInMap[*Header](hType, r.Headers.Value)
}
func (r *Response) FindLink(hType string) *low.ValueReference[*Link] {
return FindItemInMap[*Link](hType, r.Links.Value)
return FindItemInMap[*Link](hType, r.Links.Value)
}
func (r *Response) Build(root *yaml.Node) error {
extensionMap, err := ExtractExtensions(root)
if err != nil {
return err
}
r.Extensions = extensionMap
extensionMap, err := ExtractExtensions(root)
if err != nil {
return err
}
r.Extensions = extensionMap
// extract headers
headers, lN, kN, err := ExtractMapFlat[*Header](HeadersLabel, root)
if err != nil {
return err
}
if headers != nil {
r.Headers = low.NodeReference[map[low.KeyReference[string]]low.ValueReference[*Header]]{
Value: headers,
KeyNode: lN,
ValueNode: kN,
}
}
// extract headers
headers, lN, kN, err := ExtractMapFlat[*Header](HeadersLabel, root)
if err != nil {
return err
}
if headers != nil {
r.Headers = low.NodeReference[map[low.KeyReference[string]]low.ValueReference[*Header]]{
Value: headers,
KeyNode: lN,
ValueNode: kN,
}
}
// handle content, if set.
con, clN, cN, cErr := ExtractMapFlat[*MediaType](ContentLabel, root)
if cErr != nil {
return cErr
}
if con != nil {
r.Content = low.NodeReference[map[low.KeyReference[string]]low.ValueReference[*MediaType]]{
Value: con,
KeyNode: clN,
ValueNode: cN,
}
}
// handle content, if set.
con, clN, cN, cErr := ExtractMapFlat[*MediaType](ContentLabel, root)
if cErr != nil {
return cErr
}
if con != nil {
r.Content = low.NodeReference[map[low.KeyReference[string]]low.ValueReference[*MediaType]]{
Value: con,
KeyNode: clN,
ValueNode: cN,
}
}
// handle links if set
links, linkLabel, linkValue, lErr := ExtractMapFlat[*Link](LinksLabel, root)
if lErr != nil {
return lErr
}
if links != nil {
r.Links = low.NodeReference[map[low.KeyReference[string]]low.ValueReference[*Link]]{
Value: links,
KeyNode: linkLabel,
ValueNode: linkValue,
}
}
// handle links if set
links, linkLabel, linkValue, lErr := ExtractMapFlat[*Link](LinksLabel, root)
if lErr != nil {
return lErr
}
if links != nil {
r.Links = low.NodeReference[map[low.KeyReference[string]]low.ValueReference[*Link]]{
Value: links,
KeyNode: linkLabel,
ValueNode: linkValue,
}
}
return nil
return nil
}

View File

@@ -116,7 +116,7 @@ additionalProperties: true `
assert.Equal(t, "something object", sch.Description.Value)
assert.True(t, sch.AdditionalProperties.Value.(bool))
assert.Len(t, sch.Properties, 2)
assert.Len(t, sch.Properties.Value, 2)
v := sch.FindProperty("somethingB")
assert.Equal(t, "https://pb33f.io", v.Value.ExternalDocs.Value.URL.Value)
@@ -139,71 +139,71 @@ additionalProperties: true `
assert.Equal(t, true, addProps["thatIs"])
// check polymorphic values allOf
assert.Equal(t, "an allof thing", sch.AllOf[0].Value.Description.Value)
assert.Len(t, sch.AllOf[0].Value.Properties, 2)
assert.Equal(t, "an allof thing", sch.AllOf.Value[0].Value.Description.Value)
assert.Len(t, sch.AllOf.Value[0].Value.Properties.Value, 2)
v = sch.AllOf[0].Value.FindProperty("allOfA")
v = sch.AllOf.Value[0].Value.FindProperty("allOfA")
assert.NotNil(t, v)
assert.Equal(t, "allOfA description", v.Value.Description.Value)
assert.Equal(t, "allOfAExp", v.Value.Example.Value)
v = sch.AllOf[0].Value.FindProperty("allOfB")
v = sch.AllOf.Value[0].Value.FindProperty("allOfB")
assert.NotNil(t, v)
assert.Equal(t, "allOfB description", v.Value.Description.Value)
assert.Equal(t, "allOfBExp", v.Value.Example.Value)
// check polymorphic values anyOf
assert.Equal(t, "an anyOf thing", sch.AnyOf[0].Value.Description.Value)
assert.Len(t, sch.AnyOf[0].Value.Properties, 2)
assert.Equal(t, "an anyOf thing", sch.AnyOf.Value[0].Value.Description.Value)
assert.Len(t, sch.AnyOf.Value[0].Value.Properties.Value, 2)
v = sch.AnyOf[0].Value.FindProperty("anyOfA")
v = sch.AnyOf.Value[0].Value.FindProperty("anyOfA")
assert.NotNil(t, v)
assert.Equal(t, "anyOfA description", v.Value.Description.Value)
assert.Equal(t, "anyOfAExp", v.Value.Example.Value)
v = sch.AnyOf[0].Value.FindProperty("anyOfB")
v = sch.AnyOf.Value[0].Value.FindProperty("anyOfB")
assert.NotNil(t, v)
assert.Equal(t, "anyOfB description", v.Value.Description.Value)
assert.Equal(t, "anyOfBExp", v.Value.Example.Value)
// check polymorphic values oneOf
assert.Equal(t, "a oneof thing", sch.OneOf[0].Value.Description.Value)
assert.Len(t, sch.OneOf[0].Value.Properties, 2)
assert.Equal(t, "a oneof thing", sch.OneOf.Value[0].Value.Description.Value)
assert.Len(t, sch.OneOf.Value[0].Value.Properties.Value, 2)
v = sch.OneOf[0].Value.FindProperty("oneOfA")
v = sch.OneOf.Value[0].Value.FindProperty("oneOfA")
assert.NotNil(t, v)
assert.Equal(t, "oneOfA description", v.Value.Description.Value)
assert.Equal(t, "oneOfAExp", v.Value.Example.Value)
v = sch.OneOf[0].Value.FindProperty("oneOfB")
v = sch.OneOf.Value[0].Value.FindProperty("oneOfB")
assert.NotNil(t, v)
assert.Equal(t, "oneOfB description", v.Value.Description.Value)
assert.Equal(t, "oneOfBExp", v.Value.Example.Value)
// check values NOT
assert.Equal(t, "a not thing", sch.Not[0].Value.Description.Value)
assert.Len(t, sch.Not[0].Value.Properties, 2)
assert.Equal(t, "a not thing", sch.Not.Value[0].Value.Description.Value)
assert.Len(t, sch.Not.Value[0].Value.Properties.Value, 2)
v = sch.Not[0].Value.FindProperty("notA")
v = sch.Not.Value[0].Value.FindProperty("notA")
assert.NotNil(t, v)
assert.Equal(t, "notA description", v.Value.Description.Value)
assert.Equal(t, "notAExp", v.Value.Example.Value)
v = sch.Not[0].Value.FindProperty("notB")
v = sch.Not.Value[0].Value.FindProperty("notB")
assert.NotNil(t, v)
assert.Equal(t, "notB description", v.Value.Description.Value)
assert.Equal(t, "notBExp", v.Value.Example.Value)
// check values Items
assert.Equal(t, "an items thing", sch.Items[0].Value.Description.Value)
assert.Len(t, sch.Items[0].Value.Properties, 2)
assert.Equal(t, "an items thing", sch.Items.Value[0].Value.Description.Value)
assert.Len(t, sch.Items.Value[0].Value.Properties.Value, 2)
v = sch.Items[0].Value.FindProperty("itemsA")
v = sch.Items.Value[0].Value.FindProperty("itemsA")
assert.NotNil(t, v)
assert.Equal(t, "itemsA description", v.Value.Description.Value)
assert.Equal(t, "itemsAExp", v.Value.Example.Value)
v = sch.Items[0].Value.FindProperty("itemsB")
v = sch.Items.Value[0].Value.FindProperty("itemsB")
assert.NotNil(t, v)
assert.Equal(t, "itemsB description", v.Value.Description.Value)
assert.Equal(t, "itemsBExp", v.Value.Example.Value)

View File

@@ -162,7 +162,7 @@ func TestSpecIndex_PetstoreV3(t *testing.T) {
}
var mappedRefs = 8
var mappedRefs = 9
func TestSpecIndex_BurgerShop(t *testing.T) {
@@ -173,16 +173,16 @@ func TestSpecIndex_BurgerShop(t *testing.T) {
index := NewSpecIndex(&rootNode)
assert.Len(t, index.allRefs, mappedRefs)
assert.Len(t, index.allMappedRefs, mappedRefs)
assert.Equal(t, mappedRefs, len(index.GetMappedReferences()))
assert.Equal(t, mappedRefs, len(index.GetMappedReferencesSequenced()))
assert.Len(t, index.allMappedRefs, mappedRefs-1)
assert.Equal(t, mappedRefs-1, len(index.GetMappedReferences()))
assert.Equal(t, mappedRefs-1, len(index.GetMappedReferencesSequenced()))
assert.Equal(t, 7, index.pathCount)
assert.Equal(t, 7, index.GetPathCount())
assert.Equal(t, 5, len(index.GetAllSchemas()))
assert.Equal(t, 21, len(index.GetAllSequencedReferences()))
assert.Equal(t, 22, len(index.GetAllSequencedReferences()))
assert.NotNil(t, index.GetSchemasNode())
assert.NotNil(t, index.GetParametersNode())