From dcfb480095563fd2962b1ad604912fbb7fc443e0 Mon Sep 17 00:00:00 2001 From: Dave Shanley Date: Sat, 29 Apr 2023 14:20:40 -0400 Subject: [PATCH] Fixed broken tests Signed-off-by: Dave Shanley --- datamodel/high/node_builder.go | 23 +- datamodel/high/node_builder_test.go | 1198 +++++++++++++-------------- datamodel/spec_info_test.go | 172 ++-- document_examples_test.go | 2 +- 4 files changed, 703 insertions(+), 692 deletions(-) diff --git a/datamodel/high/node_builder.go b/datamodel/high/node_builder.go index 2e7b9a3..925f2b8 100644 --- a/datamodel/high/node_builder.go +++ b/datamodel/high/node_builder.go @@ -4,6 +4,7 @@ package high import ( + "fmt" "github.com/pb33f/libopenapi/datamodel/low" "github.com/pb33f/libopenapi/utils" "gopkg.in/yaml.v3" @@ -16,11 +17,12 @@ import ( // NodeEntry represents a single node used by NodeBuilder. type NodeEntry struct { - Tag string - Key string - Value any - Line int - RenderZero bool + Tag string + Key string + Value any + StringValue string + Line int + RenderZero bool } // NodeBuilder is a structure used by libopenapi high-level objects, to render themselves back to YAML. @@ -131,8 +133,11 @@ func (n *NodeBuilder) add(key string, i int) { switch value.Kind() { case reflect.Float64, reflect.Float32: nodeEntry.Value = value.Float() + x := float64(int(value.Float()*100)) / 100 // trim this down + nodeEntry.StringValue = strconv.FormatFloat(x, 'f', -1, 64) case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: nodeEntry.Value = value.Int() + nodeEntry.StringValue = value.String() case reflect.String: nodeEntry.Value = value.String() case reflect.Bool: @@ -316,7 +321,13 @@ func (n *NodeBuilder) AddYAMLNode(parent *yaml.Node, entry *NodeEntry) *yaml.Nod break case reflect.Float64: - val := strconv.FormatFloat(value.(float64), 'f', -1, 64) + + precision := -1 + if entry.StringValue != "" && strings.Contains(entry.StringValue, ".") { + precision = len(strings.Split(fmt.Sprint(entry.StringValue), ".")[1]) + } + + val := strconv.FormatFloat(value.(float64), 'f', precision, 64) valueNode = utils.CreateFloatNode(val) valueNode.Line = line break diff --git a/datamodel/high/node_builder_test.go b/datamodel/high/node_builder_test.go index 97117f3..5c2e1d0 100644 --- a/datamodel/high/node_builder_test.go +++ b/datamodel/high/node_builder_test.go @@ -4,218 +4,218 @@ package high import ( - "github.com/pb33f/libopenapi/datamodel/low" - "github.com/pb33f/libopenapi/utils" - "github.com/stretchr/testify/assert" - "gopkg.in/yaml.v3" - "reflect" - "strings" - "testing" + "github.com/pb33f/libopenapi/datamodel/low" + "github.com/pb33f/libopenapi/utils" + "github.com/stretchr/testify/assert" + "gopkg.in/yaml.v3" + "reflect" + "strings" + "testing" ) type key struct { - Name string `yaml:"name,omitempty"` - ref bool - refStr string - ln int - nilval bool - v any - kn *yaml.Node - low.IsReferenced `yaml:"-"` + Name string `yaml:"name,omitempty"` + ref bool + refStr string + ln int + nilval bool + v any + kn *yaml.Node + low.IsReferenced `yaml:"-"` } func (k key) GetKeyNode() *yaml.Node { - if k.kn != nil { - return k.kn - } - kn := utils.CreateStringNode("meddy") - kn.Line = k.ln - return kn + if k.kn != nil { + return k.kn + } + kn := utils.CreateStringNode("meddy") + kn.Line = k.ln + return kn } func (k key) GetValueUntyped() any { - return k.v + return k.v } func (k key) GetValueNode() *yaml.Node { - return k.GetValueNodeUntyped() + return k.GetValueNodeUntyped() } func (k key) GetValueNodeUntyped() *yaml.Node { - if k.nilval { - return nil - } - kn := utils.CreateStringNode("maddy") - kn.Line = k.ln - return kn + if k.nilval { + return nil + } + kn := utils.CreateStringNode("maddy") + kn.Line = k.ln + return kn } func (k key) IsReference() bool { - return k.ref + return k.ref } func (k key) GetReference() string { - return k.refStr + return k.refStr } func (k key) SetReference(ref string) { - k.refStr = ref + k.refStr = ref } func (k key) GoLowUntyped() any { - return &k + return &k } func (k key) MarshalYAML() (interface{}, error) { - return utils.CreateStringNode("pizza"), nil + return utils.CreateStringNode("pizza"), nil } func (k key) MarshalYAMLInline() (interface{}, error) { - return utils.CreateStringNode("pizza-inline!"), nil + return utils.CreateStringNode("pizza-inline!"), nil } type plug struct { - Name []string `yaml:"name,omitempty"` + Name []string `yaml:"name,omitempty"` } type test1 struct { - Thrig map[string]*plug `yaml:"thrig,omitempty"` - Thing string `yaml:"thing,omitempty"` - Thong int `yaml:"thong,omitempty"` - Thrum int64 `yaml:"thrum,omitempty"` - Thang float32 `yaml:"thang,omitempty"` - Thung float64 `yaml:"thung,omitempty"` - Thyme bool `yaml:"thyme,omitempty"` - Thurm any `yaml:"thurm,omitempty"` - Thugg *bool `yaml:"thugg,renderZero"` - Thurr *int64 `yaml:"thurr,omitempty"` - Thral *float64 `yaml:"thral,omitempty"` - Tharg []string `yaml:"tharg,omitempty"` - Type []string `yaml:"type,omitempty"` - Throg []*key `yaml:"throg,omitempty"` - Thrat []interface{} `yaml:"thrat,omitempty"` - Thrag []map[string][]string `yaml:"thrag,omitempty"` - Thrug map[string]string `yaml:"thrug,omitempty"` - Thoom []map[string]string `yaml:"thoom,omitempty"` - Thomp map[key]string `yaml:"thomp,omitempty"` - Thump key `yaml:"thump,omitempty"` - Thane key `yaml:"thane,omitempty"` - Thunk key `yaml:"thunk,omitempty"` - Thrim *key `yaml:"thrim,omitempty"` - Thril map[string]*key `yaml:"thril,omitempty"` - Extensions map[string]any `yaml:"-"` - ignoreMe string `yaml:"-"` - IgnoreMe string `yaml:"-"` + Thrig map[string]*plug `yaml:"thrig,omitempty"` + Thing string `yaml:"thing,omitempty"` + Thong int `yaml:"thong,omitempty"` + Thrum int64 `yaml:"thrum,omitempty"` + Thang float32 `yaml:"thang,omitempty"` + Thung float64 `yaml:"thung,omitempty"` + Thyme bool `yaml:"thyme,omitempty"` + Thurm any `yaml:"thurm,omitempty"` + Thugg *bool `yaml:"thugg,renderZero"` + Thurr *int64 `yaml:"thurr,omitempty"` + Thral *float64 `yaml:"thral,omitempty"` + Tharg []string `yaml:"tharg,omitempty"` + Type []string `yaml:"type,omitempty"` + Throg []*key `yaml:"throg,omitempty"` + Thrat []interface{} `yaml:"thrat,omitempty"` + Thrag []map[string][]string `yaml:"thrag,omitempty"` + Thrug map[string]string `yaml:"thrug,omitempty"` + Thoom []map[string]string `yaml:"thoom,omitempty"` + Thomp map[key]string `yaml:"thomp,omitempty"` + Thump key `yaml:"thump,omitempty"` + Thane key `yaml:"thane,omitempty"` + Thunk key `yaml:"thunk,omitempty"` + Thrim *key `yaml:"thrim,omitempty"` + Thril map[string]*key `yaml:"thril,omitempty"` + Extensions map[string]any `yaml:"-"` + ignoreMe string `yaml:"-"` + IgnoreMe string `yaml:"-"` } func (te *test1) GetExtensions() map[low.KeyReference[string]]low.ValueReference[any] { - g := make(map[low.KeyReference[string]]low.ValueReference[any]) + g := make(map[low.KeyReference[string]]low.ValueReference[any]) - for i := range te.Extensions { + for i := range te.Extensions { - f := reflect.TypeOf(te.Extensions[i]) - switch f.Kind() { - case reflect.String: - vn := utils.CreateStringNode(te.Extensions[i].(string)) - vn.Line = 999999 // weighted to the bottom. - g[low.KeyReference[string]{ - Value: i, - KeyNode: vn, - }] = low.ValueReference[any]{ - ValueNode: vn, - Value: te.Extensions[i].(string), - } - case reflect.Map: - kn := utils.CreateStringNode(i) - var vn yaml.Node - _ = vn.Decode(te.Extensions[i]) + f := reflect.TypeOf(te.Extensions[i]) + switch f.Kind() { + case reflect.String: + vn := utils.CreateStringNode(te.Extensions[i].(string)) + vn.Line = 999999 // weighted to the bottom. + g[low.KeyReference[string]{ + Value: i, + KeyNode: vn, + }] = low.ValueReference[any]{ + ValueNode: vn, + Value: te.Extensions[i].(string), + } + case reflect.Map: + kn := utils.CreateStringNode(i) + var vn yaml.Node + _ = vn.Decode(te.Extensions[i]) - kn.Line = 999999 // weighted to the bottom. - g[low.KeyReference[string]{ - Value: i, - KeyNode: kn, - }] = low.ValueReference[any]{ - ValueNode: &vn, - Value: te.Extensions[i], - } - } + kn.Line = 999999 // weighted to the bottom. + g[low.KeyReference[string]{ + Value: i, + KeyNode: kn, + }] = low.ValueReference[any]{ + ValueNode: &vn, + Value: te.Extensions[i], + } + } - } - return g + } + return g } func (te *test1) MarshalYAML() (interface{}, error) { - nb := NewNodeBuilder(te, te) - return nb.Render(), nil + nb := NewNodeBuilder(te, te) + return nb.Render(), nil } func (te *test1) GetKeyNode() *yaml.Node { - kn := utils.CreateStringNode("meddy") - kn.Line = 20 - return kn + kn := utils.CreateStringNode("meddy") + kn.Line = 20 + return kn } func (te *test1) GoesLowUntyped() any { - return te + return te } func TestNewNodeBuilder(t *testing.T) { - b := true - c := int64(12345) - d := 1234.1234 + b := true + c := int64(12345) + d := 1234.1234 - t1 := test1{ - ignoreMe: "I should never be seen!", - Thing: "ding", - Thong: 1, - Thurm: nil, - Thrum: 1234567, - Thang: 2.2, - Thung: 3.33333, - Thyme: true, - Thugg: &b, - Thurr: &c, - Thral: &d, - Tharg: []string{"chicken", "nuggets"}, - Type: []string{"chicken"}, - Thoom: []map[string]string{ - { - "maddy": "champion", - }, - { - "ember": "naughty", - }, - }, - Thomp: map[key]string{ - {ln: 1}: "princess", - }, - Thane: key{ // this is going to be ignored, needs to be a ValueReference - ln: 2, - ref: true, - refStr: "ripples", - }, - Thrug: map[string]string{ - "chicken": "nuggets", - }, - Thump: key{Name: "I will be ignored", ln: 3}, - Thunk: key{ln: 4, nilval: true}, - Extensions: map[string]any{ - "x-pizza": "time", - }, - } + t1 := test1{ + ignoreMe: "I should never be seen!", + Thing: "ding", + Thong: 1, + Thurm: nil, + Thrum: 1234567, + Thang: 2.2, + Thung: 3.33333, + Thyme: true, + Thugg: &b, + Thurr: &c, + Thral: &d, + Tharg: []string{"chicken", "nuggets"}, + Type: []string{"chicken"}, + Thoom: []map[string]string{ + { + "maddy": "champion", + }, + { + "ember": "naughty", + }, + }, + Thomp: map[key]string{ + {ln: 1}: "princess", + }, + Thane: key{ // this is going to be ignored, needs to be a ValueReference + ln: 2, + ref: true, + refStr: "ripples", + }, + Thrug: map[string]string{ + "chicken": "nuggets", + }, + Thump: key{Name: "I will be ignored", ln: 3}, + Thunk: key{ln: 4, nilval: true}, + Extensions: map[string]any{ + "x-pizza": "time", + }, + } - nb := NewNodeBuilder(&t1, nil) - node := nb.Render() + nb := NewNodeBuilder(&t1, nil) + node := nb.Render() - data, _ := yaml.Marshal(node) + data, _ := yaml.Marshal(node) - desired := `thing: ding -thong: "1" -thrum: "1234567" -thang: 2.20 -thung: 3.33333 + desired := `thing: ding +thong: 1 +thrum: 1234567 +thang: 2.2 +thung: 3.33 thyme: true thugg: true thurr: 12345 @@ -233,755 +233,755 @@ thomp: meddy: princess x-pizza: time` - assert.Equal(t, desired, strings.TrimSpace(string(data))) + assert.Equal(t, desired, strings.TrimSpace(string(data))) } func TestNewNodeBuilder_Type(t *testing.T) { - t1 := test1{ - Type: []string{"chicken", "soup"}, - } + t1 := test1{ + Type: []string{"chicken", "soup"}, + } - nb := NewNodeBuilder(&t1, &t1) - node := nb.Render() + nb := NewNodeBuilder(&t1, &t1) + node := nb.Render() - data, _ := yaml.Marshal(node) + data, _ := yaml.Marshal(node) - desired := `type: + desired := `type: - chicken - soup` - assert.Equal(t, desired, strings.TrimSpace(string(data))) + assert.Equal(t, desired, strings.TrimSpace(string(data))) } func TestNewNodeBuilder_IsReferenced(t *testing.T) { - t1 := key{ - Name: "cotton", - ref: true, - refStr: "#/my/heart", - ln: 2, - } + t1 := key{ + Name: "cotton", + ref: true, + refStr: "#/my/heart", + ln: 2, + } - nb := NewNodeBuilder(&t1, &t1) - node := nb.Render() + nb := NewNodeBuilder(&t1, &t1) + node := nb.Render() - data, _ := yaml.Marshal(node) + data, _ := yaml.Marshal(node) - desired := `$ref: '#/my/heart'` + desired := `$ref: '#/my/heart'` - assert.Equal(t, desired, strings.TrimSpace(string(data))) + assert.Equal(t, desired, strings.TrimSpace(string(data))) } func TestNewNodeBuilder_Extensions(t *testing.T) { - t1 := test1{ - Thing: "ding", - Extensions: map[string]any{ - "x-pizza": "time", - "x-money": "time", - }, - Thong: 1, - } + t1 := test1{ + Thing: "ding", + Extensions: map[string]any{ + "x-pizza": "time", + "x-money": "time", + }, + Thong: 1, + } - nb := NewNodeBuilder(&t1, &t1) - node := nb.Render() + nb := NewNodeBuilder(&t1, &t1) + node := nb.Render() - data, _ := yaml.Marshal(node) - assert.Len(t, data, 51) + data, _ := yaml.Marshal(node) + assert.Len(t, data, 49) } func TestNewNodeBuilder_LowValueNode(t *testing.T) { - t1 := test1{ - Thing: "ding", - Extensions: map[string]any{ - "x-pizza": "time", - "x-money": "time", - }, - Thong: 1, - } + t1 := test1{ + Thing: "ding", + Extensions: map[string]any{ + "x-pizza": "time", + "x-money": "time", + }, + Thong: 1, + } - nb := NewNodeBuilder(&t1, &t1) - node := nb.Render() + nb := NewNodeBuilder(&t1, &t1) + node := nb.Render() - data, _ := yaml.Marshal(node) + data, _ := yaml.Marshal(node) - assert.Len(t, data, 51) + assert.Len(t, data, 49) } func TestNewNodeBuilder_NoValue(t *testing.T) { - t1 := test1{ - Thing: "", - } + t1 := test1{ + Thing: "", + } - nodeEnty := NodeEntry{} - nb := NewNodeBuilder(&t1, &t1) - node := nb.AddYAMLNode(nil, &nodeEnty) - assert.Nil(t, node) + nodeEnty := NodeEntry{} + nb := NewNodeBuilder(&t1, &t1) + node := nb.AddYAMLNode(nil, &nodeEnty) + assert.Nil(t, node) } func TestNewNodeBuilder_EmptyString(t *testing.T) { - t1 := new(test1) - nodeEnty := NodeEntry{} - nb := NewNodeBuilder(t1, t1) - node := nb.AddYAMLNode(nil, &nodeEnty) - assert.Nil(t, node) + t1 := new(test1) + nodeEnty := NodeEntry{} + nb := NewNodeBuilder(t1, t1) + node := nb.AddYAMLNode(nil, &nodeEnty) + assert.Nil(t, node) } func TestNewNodeBuilder_EmptyStringRenderZero(t *testing.T) { - t1 := new(test1) - nodeEnty := NodeEntry{RenderZero: true, Value: ""} - nb := NewNodeBuilder(t1, t1) - m := utils.CreateEmptyMapNode() - node := nb.AddYAMLNode(m, &nodeEnty) - assert.NotNil(t, node) + t1 := new(test1) + nodeEnty := NodeEntry{RenderZero: true, Value: ""} + nb := NewNodeBuilder(t1, t1) + m := utils.CreateEmptyMapNode() + node := nb.AddYAMLNode(m, &nodeEnty) + assert.NotNil(t, node) } func TestNewNodeBuilder_Bool(t *testing.T) { - t1 := new(test1) - nb := NewNodeBuilder(t1, t1) - nodeEnty := NodeEntry{} - node := nb.AddYAMLNode(nil, &nodeEnty) - assert.Nil(t, node) + t1 := new(test1) + nb := NewNodeBuilder(t1, t1) + nodeEnty := NodeEntry{} + node := nb.AddYAMLNode(nil, &nodeEnty) + assert.Nil(t, node) } func TestNewNodeBuilder_BoolRenderZero(t *testing.T) { - type yui struct { - Thrit bool `yaml:"thrit,renderZero"` - } - t1 := new(yui) - t1.Thrit = false - nb := NewNodeBuilder(t1, t1) - r := nb.Render() - assert.NotNil(t, r) + type yui struct { + Thrit bool `yaml:"thrit,renderZero"` + } + t1 := new(yui) + t1.Thrit = false + nb := NewNodeBuilder(t1, t1) + r := nb.Render() + assert.NotNil(t, r) } func TestNewNodeBuilder_Int(t *testing.T) { - t1 := new(test1) - nb := NewNodeBuilder(t1, t1) - p := utils.CreateEmptyMapNode() - nodeEnty := NodeEntry{Tag: "p", Value: 12, Key: "p"} - node := nb.AddYAMLNode(p, &nodeEnty) - assert.NotNil(t, node) - assert.Len(t, node.Content, 2) - assert.Equal(t, "12", node.Content[1].Value) + t1 := new(test1) + nb := NewNodeBuilder(t1, t1) + p := utils.CreateEmptyMapNode() + nodeEnty := NodeEntry{Tag: "p", Value: 12, Key: "p"} + node := nb.AddYAMLNode(p, &nodeEnty) + assert.NotNil(t, node) + assert.Len(t, node.Content, 2) + assert.Equal(t, "12", node.Content[1].Value) } func TestNewNodeBuilder_Int64(t *testing.T) { - t1 := new(test1) - nb := NewNodeBuilder(t1, t1) - p := utils.CreateEmptyMapNode() - nodeEnty := NodeEntry{Tag: "p", Value: int64(234556), Key: "p"} - node := nb.AddYAMLNode(p, &nodeEnty) - assert.NotNil(t, node) - assert.Len(t, node.Content, 2) - assert.Equal(t, "234556", node.Content[1].Value) + t1 := new(test1) + nb := NewNodeBuilder(t1, t1) + p := utils.CreateEmptyMapNode() + nodeEnty := NodeEntry{Tag: "p", Value: int64(234556), Key: "p"} + node := nb.AddYAMLNode(p, &nodeEnty) + assert.NotNil(t, node) + assert.Len(t, node.Content, 2) + assert.Equal(t, "234556", node.Content[1].Value) } func TestNewNodeBuilder_Float32(t *testing.T) { - t1 := new(test1) - nb := NewNodeBuilder(t1, t1) - p := utils.CreateEmptyMapNode() - nodeEnty := NodeEntry{Tag: "p", Value: float32(1234.23), Key: "p"} - node := nb.AddYAMLNode(p, &nodeEnty) - assert.NotNil(t, node) - assert.Len(t, node.Content, 2) - assert.Equal(t, "1234.23", node.Content[1].Value) + t1 := new(test1) + nb := NewNodeBuilder(t1, t1) + p := utils.CreateEmptyMapNode() + nodeEnty := NodeEntry{Tag: "p", Value: float32(1234.23), Key: "p"} + node := nb.AddYAMLNode(p, &nodeEnty) + assert.NotNil(t, node) + assert.Len(t, node.Content, 2) + assert.Equal(t, "1234.23", node.Content[1].Value) } func TestNewNodeBuilder_Float64(t *testing.T) { - t1 := new(test1) - nb := NewNodeBuilder(t1, t1) - p := utils.CreateEmptyMapNode() - nodeEnty := NodeEntry{Tag: "p", Value: 1234.232323, Key: "p"} - node := nb.AddYAMLNode(p, &nodeEnty) - assert.NotNil(t, node) - assert.Len(t, node.Content, 2) - assert.Equal(t, "1234.232323", node.Content[1].Value) + t1 := new(test1) + nb := NewNodeBuilder(t1, t1) + p := utils.CreateEmptyMapNode() + nodeEnty := NodeEntry{Tag: "p", Value: 1234.232323, Key: "p", StringValue: "1234.232323"} + node := nb.AddYAMLNode(p, &nodeEnty) + assert.NotNil(t, node) + assert.Len(t, node.Content, 2) + assert.Equal(t, "1234.232323", node.Content[1].Value) } func TestNewNodeBuilder_EmptyNode(t *testing.T) { - t1 := new(test1) - nb := NewNodeBuilder(t1, t1) - nb.Nodes = nil - m := nb.Render() - assert.Len(t, m.Content, 0) + t1 := new(test1) + nb := NewNodeBuilder(t1, t1) + nb.Nodes = nil + m := nb.Render() + assert.Len(t, m.Content, 0) } func TestNewNodeBuilder_MapKeyHasValue(t *testing.T) { - t1 := test1{ - Thrug: map[string]string{ - "dump": "trump", - }, - } + t1 := test1{ + Thrug: map[string]string{ + "dump": "trump", + }, + } - type test1low struct { - Thrug key `yaml:"thrug"` - Thugg *bool `yaml:"thugg"` - } + type test1low struct { + Thrug key `yaml:"thrug"` + Thugg *bool `yaml:"thugg"` + } - t2 := test1low{ - Thrug: key{ - v: map[string]string{ - "dump": "trump", - }, - ln: 2, - }, - } + t2 := test1low{ + Thrug: key{ + v: map[string]string{ + "dump": "trump", + }, + ln: 2, + }, + } - nb := NewNodeBuilder(&t1, &t2) - node := nb.Render() + nb := NewNodeBuilder(&t1, &t2) + node := nb.Render() - data, _ := yaml.Marshal(node) + data, _ := yaml.Marshal(node) - desired := `thrug: + desired := `thrug: dump: trump` - assert.Equal(t, desired, strings.TrimSpace(string(data))) + assert.Equal(t, desired, strings.TrimSpace(string(data))) } func TestNewNodeBuilder_MapKeyHasValueThatHasValue(t *testing.T) { - t1 := test1{ - Thomp: map[key]string{ - {v: "who"}: "princess", - }, - } + t1 := test1{ + Thomp: map[key]string{ + {v: "who"}: "princess", + }, + } - type test1low struct { - Thomp key `yaml:"thomp"` - Thugg *bool `yaml:"thugg"` - } + type test1low struct { + Thomp key `yaml:"thomp"` + Thugg *bool `yaml:"thugg"` + } - t2 := test1low{ - Thomp: key{ - v: map[key]string{ - { - v: key{ - v: "ice", - kn: utils.CreateStringNode("limes"), - }, - kn: utils.CreateStringNode("chimes"), - ln: 6}: "princess", - }, - ln: 2, - }, - } + t2 := test1low{ + Thomp: key{ + v: map[key]string{ + { + v: key{ + v: "ice", + kn: utils.CreateStringNode("limes"), + }, + kn: utils.CreateStringNode("chimes"), + ln: 6}: "princess", + }, + ln: 2, + }, + } - nb := NewNodeBuilder(&t1, &t2) - node := nb.Render() + nb := NewNodeBuilder(&t1, &t2) + node := nb.Render() - data, _ := yaml.Marshal(node) + data, _ := yaml.Marshal(node) - desired := `thomp: + desired := `thomp: meddy: princess` - assert.Equal(t, desired, strings.TrimSpace(string(data))) + assert.Equal(t, desired, strings.TrimSpace(string(data))) } func TestNewNodeBuilder_MapKeyHasValueThatHasValueMatch(t *testing.T) { - t1 := test1{ - Thomp: map[key]string{ - {v: "who"}: "princess", - }, - } + t1 := test1{ + Thomp: map[key]string{ + {v: "who"}: "princess", + }, + } - type test1low struct { - Thomp low.NodeReference[map[key]string] `yaml:"thomp"` - Thugg *bool `yaml:"thugg"` - } + type test1low struct { + Thomp low.NodeReference[map[key]string] `yaml:"thomp"` + Thugg *bool `yaml:"thugg"` + } - g := low.NodeReference[map[key]string]{ - Value: map[key]string{ - {v: "my", kn: utils.CreateStringNode("limes")}: "princess", - }, - } + g := low.NodeReference[map[key]string]{ + Value: map[key]string{ + {v: "my", kn: utils.CreateStringNode("limes")}: "princess", + }, + } - t2 := test1low{ - Thomp: g, - } + t2 := test1low{ + Thomp: g, + } - nb := NewNodeBuilder(&t1, &t2) - node := nb.Render() + nb := NewNodeBuilder(&t1, &t2) + node := nb.Render() - data, _ := yaml.Marshal(node) + data, _ := yaml.Marshal(node) - desired := `thomp: + desired := `thomp: meddy: princess` - assert.Equal(t, desired, strings.TrimSpace(string(data))) + assert.Equal(t, desired, strings.TrimSpace(string(data))) } func TestNewNodeBuilder_MapKeyHasValueThatHasValueMatchKeyNode(t *testing.T) { - t1 := test1{ - Thomp: map[key]string{ - {v: "who"}: "princess", - }, - } + t1 := test1{ + Thomp: map[key]string{ + {v: "who"}: "princess", + }, + } - type test1low struct { - Thomp low.NodeReference[map[key]string] `yaml:"thomp"` - Thugg *bool `yaml:"thugg"` - } + type test1low struct { + Thomp low.NodeReference[map[key]string] `yaml:"thomp"` + Thugg *bool `yaml:"thugg"` + } - g := low.NodeReference[map[key]string]{ - Value: map[key]string{ - {v: "my", kn: utils.CreateStringNode("limes")}: "princess", - }, - } + g := low.NodeReference[map[key]string]{ + Value: map[key]string{ + {v: "my", kn: utils.CreateStringNode("limes")}: "princess", + }, + } - t2 := test1low{ - Thomp: g, - } + t2 := test1low{ + Thomp: g, + } - nb := NewNodeBuilder(&t1, &t2) - node := nb.Render() + nb := NewNodeBuilder(&t1, &t2) + node := nb.Render() - data, _ := yaml.Marshal(node) + data, _ := yaml.Marshal(node) - desired := `thomp: + desired := `thomp: meddy: princess` - assert.Equal(t, desired, strings.TrimSpace(string(data))) + assert.Equal(t, desired, strings.TrimSpace(string(data))) } func TestNewNodeBuilder_MapKeyHasValueThatHasValueMatch_NoWrap(t *testing.T) { - t1 := test1{ - Thomp: map[key]string{ - {v: "who"}: "princess", - }, - } + t1 := test1{ + Thomp: map[key]string{ + {v: "who"}: "princess", + }, + } - type test1low struct { - Thomp map[key]string `yaml:"thomp"` - Thugg *bool `yaml:"thugg"` - } + type test1low struct { + Thomp map[key]string `yaml:"thomp"` + Thugg *bool `yaml:"thugg"` + } - t2 := test1low{ - Thomp: map[key]string{ - {v: "my", kn: utils.CreateStringNode("meddy")}: "princess", - }, - } + t2 := test1low{ + Thomp: map[key]string{ + {v: "my", kn: utils.CreateStringNode("meddy")}: "princess", + }, + } - nb := NewNodeBuilder(&t1, &t2) - node := nb.Render() + nb := NewNodeBuilder(&t1, &t2) + node := nb.Render() - data, _ := yaml.Marshal(node) + data, _ := yaml.Marshal(node) - desired := `thomp: + desired := `thomp: meddy: princess` - assert.Equal(t, desired, strings.TrimSpace(string(data))) + assert.Equal(t, desired, strings.TrimSpace(string(data))) } func TestNewNodeBuilder_MissingLabel(t *testing.T) { - t1 := new(test1) - nb := NewNodeBuilder(t1, t1) - p := utils.CreateEmptyMapNode() - nodeEnty := NodeEntry{Value: 1234.232323, Key: "p"} - node := nb.AddYAMLNode(p, &nodeEnty) - assert.NotNil(t, node) - assert.Len(t, node.Content, 0) + t1 := new(test1) + nb := NewNodeBuilder(t1, t1) + p := utils.CreateEmptyMapNode() + nodeEnty := NodeEntry{Value: 1234.232323, Key: "p"} + node := nb.AddYAMLNode(p, &nodeEnty) + assert.NotNil(t, node) + assert.Len(t, node.Content, 0) } func TestNewNodeBuilder_ExtensionMap(t *testing.T) { - t1 := test1{ - Thing: "ding", - Extensions: map[string]any{ - "x-pizza": map[string]string{ - "dump": "trump", - }, - "x-money": "time", - }, - Thong: 1, - } + t1 := test1{ + Thing: "ding", + Extensions: map[string]any{ + "x-pizza": map[string]string{ + "dump": "trump", + }, + "x-money": "time", + }, + Thong: 1, + } - nb := NewNodeBuilder(&t1, &t1) - node := nb.Render() + nb := NewNodeBuilder(&t1, &t1) + node := nb.Render() - data, _ := yaml.Marshal(node) + data, _ := yaml.Marshal(node) - assert.Len(t, data, 62) + assert.Len(t, data, 60) } func TestNewNodeBuilder_MapKeyHasValueThatHasValueMismatch(t *testing.T) { - t1 := test1{ - Extensions: map[string]any{ - "x-pizza": map[string]string{ - "dump": "trump", - }, - "x-cake": map[string]string{ - "maga": "nomore", - }, - }, - Thril: map[string]*key{ - "princess": {v: "who", Name: "beef", ln: 2}, - "heavy": {v: "who", Name: "industries", ln: 3}, - }, - } + t1 := test1{ + Extensions: map[string]any{ + "x-pizza": map[string]string{ + "dump": "trump", + }, + "x-cake": map[string]string{ + "maga": "nomore", + }, + }, + Thril: map[string]*key{ + "princess": {v: "who", Name: "beef", ln: 2}, + "heavy": {v: "who", Name: "industries", ln: 3}, + }, + } - nb := NewNodeBuilder(&t1, nil) - node := nb.Render() + nb := NewNodeBuilder(&t1, nil) + node := nb.Render() - data, _ := yaml.Marshal(node) + data, _ := yaml.Marshal(node) - assert.Len(t, data, 94) + assert.Len(t, data, 94) } func TestNewNodeBuilder_SliceRef(t *testing.T) { - c := key{ref: true, refStr: "#/red/robin/yummmmm", Name: "milky"} - ty := []*key{&c} - t1 := test1{ - Throg: ty, - } + c := key{ref: true, refStr: "#/red/robin/yummmmm", Name: "milky"} + ty := []*key{&c} + t1 := test1{ + Throg: ty, + } - nb := NewNodeBuilder(&t1, &t1) - node := nb.Render() + nb := NewNodeBuilder(&t1, &t1) + node := nb.Render() - data, _ := yaml.Marshal(node) + data, _ := yaml.Marshal(node) - desired := `throg: + desired := `throg: - $ref: '#/red/robin/yummmmm'` - assert.Equal(t, desired, strings.TrimSpace(string(data))) + assert.Equal(t, desired, strings.TrimSpace(string(data))) } func TestNewNodeBuilder_SliceRef_Inline(t *testing.T) { - c := key{ref: true, refStr: "#/red/robin/yummmmm", Name: "milky"} - ty := []*key{&c} - t1 := test1{ - Throg: ty, - } + c := key{ref: true, refStr: "#/red/robin/yummmmm", Name: "milky"} + ty := []*key{&c} + t1 := test1{ + Throg: ty, + } - nb := NewNodeBuilder(&t1, &t1) - nb.Resolve = true - node := nb.Render() + nb := NewNodeBuilder(&t1, &t1) + nb.Resolve = true + node := nb.Render() - data, _ := yaml.Marshal(node) + data, _ := yaml.Marshal(node) - desired := `throg: + desired := `throg: - pizza-inline!` - assert.Equal(t, desired, strings.TrimSpace(string(data))) + assert.Equal(t, desired, strings.TrimSpace(string(data))) } type testRender struct { } func (t testRender) MarshalYAML() (interface{}, error) { - return utils.CreateStringNode("testy!"), nil + return utils.CreateStringNode("testy!"), nil } type testRenderRawNode struct { } func (t testRenderRawNode) MarshalYAML() (interface{}, error) { - return yaml.Node{Kind: yaml.ScalarNode, Value: "zesty!"}, nil + return yaml.Node{Kind: yaml.ScalarNode, Value: "zesty!"}, nil } func TestNewNodeBuilder_SliceRef_Inline_NotCompatible(t *testing.T) { - ty := []interface{}{testRender{}} - t1 := test1{ - Thrat: ty, - } + ty := []interface{}{testRender{}} + t1 := test1{ + Thrat: ty, + } - nb := NewNodeBuilder(&t1, &t1) - nb.Resolve = true - node := nb.Render() + nb := NewNodeBuilder(&t1, &t1) + nb.Resolve = true + node := nb.Render() - data, _ := yaml.Marshal(node) + data, _ := yaml.Marshal(node) - desired := `thrat: + desired := `thrat: - testy!` - assert.Equal(t, desired, strings.TrimSpace(string(data))) + assert.Equal(t, desired, strings.TrimSpace(string(data))) } func TestNewNodeBuilder_SliceRef_Inline_NotCompatible_NotPointer(t *testing.T) { - ty := []interface{}{testRenderRawNode{}} - t1 := test1{ - Thrat: ty, - } + ty := []interface{}{testRenderRawNode{}} + t1 := test1{ + Thrat: ty, + } - nb := NewNodeBuilder(&t1, &t1) - nb.Resolve = true - node := nb.Render() + nb := NewNodeBuilder(&t1, &t1) + nb.Resolve = true + node := nb.Render() - data, _ := yaml.Marshal(node) + data, _ := yaml.Marshal(node) - desired := `thrat: + desired := `thrat: - zesty!` - assert.Equal(t, desired, strings.TrimSpace(string(data))) + assert.Equal(t, desired, strings.TrimSpace(string(data))) } func TestNewNodeBuilder_PointerRef_Inline_NotCompatible_RawNode(t *testing.T) { - ty := testRenderRawNode{} - t1 := test1{ - Thurm: &ty, - } + ty := testRenderRawNode{} + t1 := test1{ + Thurm: &ty, + } - nb := NewNodeBuilder(&t1, &t1) - nb.Resolve = true - node := nb.Render() + nb := NewNodeBuilder(&t1, &t1) + nb.Resolve = true + node := nb.Render() - data, _ := yaml.Marshal(node) + data, _ := yaml.Marshal(node) - desired := `thurm: zesty!` + desired := `thurm: zesty!` - assert.Equal(t, desired, strings.TrimSpace(string(data))) + assert.Equal(t, desired, strings.TrimSpace(string(data))) } func TestNewNodeBuilder_PointerRef_Inline_NotCompatible(t *testing.T) { - ty := key{} - t1 := test1{ - Thurm: &ty, - } + ty := key{} + t1 := test1{ + Thurm: &ty, + } - nb := NewNodeBuilder(&t1, &t1) - nb.Resolve = true - node := nb.Render() + nb := NewNodeBuilder(&t1, &t1) + nb.Resolve = true + node := nb.Render() - data, _ := yaml.Marshal(node) + data, _ := yaml.Marshal(node) - desired := `thurm: pizza-inline!` + desired := `thurm: pizza-inline!` - assert.Equal(t, desired, strings.TrimSpace(string(data))) + assert.Equal(t, desired, strings.TrimSpace(string(data))) } func TestNewNodeBuilder_SliceNoRef(t *testing.T) { - c := key{ref: false, Name: "milky"} - ty := []*key{&c} - t1 := test1{ - Throg: ty, - } + c := key{ref: false, Name: "milky"} + ty := []*key{&c} + t1 := test1{ + Throg: ty, + } - nb := NewNodeBuilder(&t1, &t1) - node := nb.Render() + nb := NewNodeBuilder(&t1, &t1) + node := nb.Render() - data, _ := yaml.Marshal(node) + data, _ := yaml.Marshal(node) - desired := `throg: + desired := `throg: - pizza` - assert.Equal(t, desired, strings.TrimSpace(string(data))) + assert.Equal(t, desired, strings.TrimSpace(string(data))) } func TestNewNodeBuilder_TestStructAny(t *testing.T) { - t1 := test1{ - Thurm: low.ValueReference[any]{ - ValueNode: utils.CreateStringNode("beer"), - }, - } + t1 := test1{ + Thurm: low.ValueReference[any]{ + ValueNode: utils.CreateStringNode("beer"), + }, + } - nb := NewNodeBuilder(&t1, &t1) - node := nb.Render() + nb := NewNodeBuilder(&t1, &t1) + node := nb.Render() - data, _ := yaml.Marshal(node) + data, _ := yaml.Marshal(node) - desired := `thurm: beer` + desired := `thurm: beer` - assert.Equal(t, desired, strings.TrimSpace(string(data))) + assert.Equal(t, desired, strings.TrimSpace(string(data))) } func TestNewNodeBuilder_TestStructString(t *testing.T) { - t1 := test1{ - Thurm: low.ValueReference[string]{ - ValueNode: utils.CreateStringNode("beer"), - }, - } + t1 := test1{ + Thurm: low.ValueReference[string]{ + ValueNode: utils.CreateStringNode("beer"), + }, + } - nb := NewNodeBuilder(&t1, &t1) - node := nb.Render() + nb := NewNodeBuilder(&t1, &t1) + node := nb.Render() - data, _ := yaml.Marshal(node) + data, _ := yaml.Marshal(node) - desired := `thurm: beer` + desired := `thurm: beer` - assert.Equal(t, desired, strings.TrimSpace(string(data))) + assert.Equal(t, desired, strings.TrimSpace(string(data))) } func TestNewNodeBuilder_TestStructPointer(t *testing.T) { - t1 := test1{ - Thrim: &key{ - ref: true, - refStr: "#/cash/money", - Name: "pizza", - }, - } + t1 := test1{ + Thrim: &key{ + ref: true, + refStr: "#/cash/money", + Name: "pizza", + }, + } - nb := NewNodeBuilder(&t1, &t1) - node := nb.Render() + nb := NewNodeBuilder(&t1, &t1) + node := nb.Render() - data, _ := yaml.Marshal(node) + data, _ := yaml.Marshal(node) - desired := `thrim: + desired := `thrim: $ref: '#/cash/money'` - assert.Equal(t, desired, strings.TrimSpace(string(data))) + assert.Equal(t, desired, strings.TrimSpace(string(data))) } func TestNewNodeBuilder_TestStructRef(t *testing.T) { - fkn := utils.CreateStringNode("pizzaBurgers") - fkn.Line = 22 + fkn := utils.CreateStringNode("pizzaBurgers") + fkn.Line = 22 - t1 := test1{ - Thurm: low.NodeReference[string]{ - Reference: "#/cash/money", - ReferenceNode: true, - KeyNode: fkn, - ValueNode: fkn, - }, - } + t1 := test1{ + Thurm: low.NodeReference[string]{ + Reference: "#/cash/money", + ReferenceNode: true, + KeyNode: fkn, + ValueNode: fkn, + }, + } - nb := NewNodeBuilder(&t1, &t1) - node := nb.Render() + nb := NewNodeBuilder(&t1, &t1) + node := nb.Render() - data, _ := yaml.Marshal(node) + data, _ := yaml.Marshal(node) - desired := `thurm: pizzaBurgers` + desired := `thurm: pizzaBurgers` - assert.Equal(t, desired, strings.TrimSpace(string(data))) + assert.Equal(t, desired, strings.TrimSpace(string(data))) } func TestNewNodeBuilder_TestStructDefaultEncode(t *testing.T) { - f := 1 - t1 := test1{ - Thurm: &f, - } + f := 1 + t1 := test1{ + Thurm: &f, + } - nb := NewNodeBuilder(&t1, &t1) - node := nb.Render() + nb := NewNodeBuilder(&t1, &t1) + node := nb.Render() - data, _ := yaml.Marshal(node) + data, _ := yaml.Marshal(node) - desired := `thurm: 1` + desired := `thurm: 1` - assert.Equal(t, desired, strings.TrimSpace(string(data))) + assert.Equal(t, desired, strings.TrimSpace(string(data))) } func TestNewNodeBuilder_TestSliceMapSliceStruct(t *testing.T) { - a := []map[string][]string{ - {"pizza": {"beer", "wine"}}, - } + a := []map[string][]string{ + {"pizza": {"beer", "wine"}}, + } - t1 := test1{ - Thrag: a, - } + t1 := test1{ + Thrag: a, + } - nb := NewNodeBuilder(&t1, &t1) - node := nb.Render() + nb := NewNodeBuilder(&t1, &t1) + node := nb.Render() - data, _ := yaml.Marshal(node) + data, _ := yaml.Marshal(node) - desired := `thrag: + desired := `thrag: - pizza: - beer - wine` - assert.Equal(t, desired, strings.TrimSpace(string(data))) + assert.Equal(t, desired, strings.TrimSpace(string(data))) } func TestNewNodeBuilder_TestRenderZero(t *testing.T) { - f := false - t1 := test1{ - Thugg: &f, - } + f := false + t1 := test1{ + Thugg: &f, + } - nb := NewNodeBuilder(&t1, &t1) - node := nb.Render() + nb := NewNodeBuilder(&t1, &t1) + node := nb.Render() - data, _ := yaml.Marshal(node) + data, _ := yaml.Marshal(node) - desired := `thugg: false` + desired := `thugg: false` - assert.Equal(t, desired, strings.TrimSpace(string(data))) + assert.Equal(t, desired, strings.TrimSpace(string(data))) } func TestNewNodeBuilder_TestRenderServerVariableSimulation(t *testing.T) { - t1 := test1{ - Thrig: map[string]*plug{ - "pork": {Name: []string{"gammon", "bacon"}}, - }, - } + t1 := test1{ + Thrig: map[string]*plug{ + "pork": {Name: []string{"gammon", "bacon"}}, + }, + } - nb := NewNodeBuilder(&t1, &t1) - node := nb.Render() + nb := NewNodeBuilder(&t1, &t1) + node := nb.Render() - data, _ := yaml.Marshal(node) + data, _ := yaml.Marshal(node) - desired := `thrig: + desired := `thrig: pork: name: - gammon - bacon` - assert.Equal(t, desired, strings.TrimSpace(string(data))) + assert.Equal(t, desired, strings.TrimSpace(string(data))) } func TestNewNodeBuilder_ShouldHaveNotDoneTestsLikeThisOhWell(t *testing.T) { - m := make(map[low.KeyReference[string]]low.ValueReference[*key]) + m := make(map[low.KeyReference[string]]low.ValueReference[*key]) - m[low.KeyReference[string]{ - KeyNode: utils.CreateStringNode("pizza"), - Value: "pizza", - }] = low.ValueReference[*key]{ - ValueNode: utils.CreateStringNode("beer"), - Value: &key{}, - } + m[low.KeyReference[string]{ + KeyNode: utils.CreateStringNode("pizza"), + Value: "pizza", + }] = low.ValueReference[*key]{ + ValueNode: utils.CreateStringNode("beer"), + Value: &key{}, + } - d := make(map[string]*key) - d["pizza"] = &key{} + d := make(map[string]*key) + d["pizza"] = &key{} - type t1low struct { - Thril low.NodeReference[map[low.KeyReference[string]]low.ValueReference[*key]] - Thugg *bool `yaml:"thugg"` - } + type t1low struct { + Thril low.NodeReference[map[low.KeyReference[string]]low.ValueReference[*key]] + Thugg *bool `yaml:"thugg"` + } - t1 := test1{ - Thril: d, - } + t1 := test1{ + Thril: d, + } - t2 := t1low{ - Thril: low.NodeReference[map[low.KeyReference[string]]low.ValueReference[*key]]{ - Value: m, - ValueNode: utils.CreateStringNode("beer"), - }, - } + t2 := t1low{ + Thril: low.NodeReference[map[low.KeyReference[string]]low.ValueReference[*key]]{ + Value: m, + ValueNode: utils.CreateStringNode("beer"), + }, + } - nb := NewNodeBuilder(&t1, &t2) - node := nb.Render() + nb := NewNodeBuilder(&t1, &t2) + node := nb.Render() - data, _ := yaml.Marshal(node) + data, _ := yaml.Marshal(node) - desired := `thril: + desired := `thril: pizza: pizza` - assert.Equal(t, desired, strings.TrimSpace(string(data))) + assert.Equal(t, desired, strings.TrimSpace(string(data))) } diff --git a/datamodel/spec_info_test.go b/datamodel/spec_info_test.go index 51df202..ceb5e38 100644 --- a/datamodel/spec_info_test.go +++ b/datamodel/spec_info_test.go @@ -4,31 +4,31 @@ package datamodel import ( - "fmt" - "io/ioutil" - "testing" + "fmt" + "io/ioutil" + "testing" - "github.com/pb33f/libopenapi/utils" - "github.com/stretchr/testify/assert" + "github.com/pb33f/libopenapi/utils" + "github.com/stretchr/testify/assert" ) const ( - // OpenApi3 is used by all OpenAPI 3+ docs - OpenApi3 = "openapi" + // OpenApi3 is used by all OpenAPI 3+ docs + OpenApi3 = "openapi" - // OpenApi2 is used by all OpenAPI 2 docs, formerly known as swagger. - OpenApi2 = "swagger" + // OpenApi2 is used by all OpenAPI 2 docs, formerly known as swagger. + OpenApi2 = "swagger" - // AsyncApi is used by akk AsyncAPI docs, all versions. - AsyncApi = "asyncapi" + // AsyncApi is used by akk AsyncAPI docs, all versions. + AsyncApi = "asyncapi" ) func TestSpecInfo_GetJSONParsingChannel(t *testing.T) { - // dumb, but we need to ensure coverage is as high as we can make it. - bchan := make(chan bool) - si := &SpecInfo{JsonParsingChannel: bchan} - assert.Equal(t, si.GetJSONParsingChannel(), bchan) + // dumb, but we need to ensure coverage is as high as we can make it. + bchan := make(chan bool) + si := &SpecInfo{JsonParsingChannel: bchan} + assert.Equal(t, si.GetJSONParsingChannel(), bchan) } @@ -115,147 +115,147 @@ info: version: '0.1.0'` func TestExtractSpecInfo_ValidJSON(t *testing.T) { - r, e := ExtractSpecInfo([]byte(goodJSON)) - assert.Greater(t, len(*r.SpecJSONBytes), 0) - assert.Error(t, e) + r, e := ExtractSpecInfo([]byte(goodJSON)) + assert.Greater(t, len(*r.SpecJSONBytes), 0) + assert.Error(t, e) } func TestExtractSpecInfo_InvalidJSON(t *testing.T) { - _, e := ExtractSpecInfo([]byte(badJSON)) - assert.Error(t, e) + _, e := ExtractSpecInfo([]byte(badJSON)) + assert.Error(t, e) } func TestExtractSpecInfo_Nothing(t *testing.T) { - _, e := ExtractSpecInfo([]byte("")) - assert.Error(t, e) + _, e := ExtractSpecInfo([]byte("")) + assert.Error(t, e) } func TestExtractSpecInfo_ValidYAML(t *testing.T) { - r, e := ExtractSpecInfo([]byte(goodYAML)) - assert.Greater(t, len(*r.SpecJSONBytes), 0) - assert.Error(t, e) + r, e := ExtractSpecInfo([]byte(goodYAML)) + assert.Greater(t, len(*r.SpecJSONBytes), 0) + assert.Error(t, e) } func TestExtractSpecInfo_InvalidYAML(t *testing.T) { - _, e := ExtractSpecInfo([]byte(badYAML)) - assert.Error(t, e) + _, e := ExtractSpecInfo([]byte(badYAML)) + assert.Error(t, e) } func TestExtractSpecInfo_InvalidOpenAPIVersion(t *testing.T) { - _, e := ExtractSpecInfo([]byte(OpenApiOne)) - assert.Error(t, e) + _, e := ExtractSpecInfo([]byte(OpenApiOne)) + assert.Error(t, e) } func TestExtractSpecInfo_OpenAPI3(t *testing.T) { - r, e := ExtractSpecInfo([]byte(OpenApi3Spec)) - assert.Nil(t, e) - assert.Equal(t, utils.OpenApi3, r.SpecType) - assert.Equal(t, "3.0.1", r.Version) - assert.Greater(t, len(*r.SpecJSONBytes), 0) - assert.Contains(t, r.APISchema, "https://spec.openapis.org/oas/3.0/schema/2021-09-28") + r, e := ExtractSpecInfo([]byte(OpenApi3Spec)) + assert.Nil(t, e) + assert.Equal(t, utils.OpenApi3, r.SpecType) + assert.Equal(t, "3.0.1", r.Version) + assert.Greater(t, len(*r.SpecJSONBytes), 0) + assert.Contains(t, r.APISchema, "https://spec.openapis.org/oas/3.0/schema/2021-09-28") } func TestExtractSpecInfo_OpenAPIWat(t *testing.T) { - r, e := ExtractSpecInfo([]byte(OpenApiWat)) - assert.Nil(t, e) - assert.Equal(t, OpenApi3, r.SpecType) - assert.Equal(t, "3.2", r.Version) + r, e := ExtractSpecInfo([]byte(OpenApiWat)) + assert.Nil(t, e) + assert.Equal(t, OpenApi3, r.SpecType) + assert.Equal(t, "3.2", r.Version) } func TestExtractSpecInfo_OpenAPI31(t *testing.T) { - r, e := ExtractSpecInfo([]byte(OpenApi31)) - assert.Nil(t, e) - assert.Equal(t, OpenApi3, r.SpecType) - assert.Equal(t, "3.1", r.Version) - assert.Contains(t, r.APISchema, "https://spec.openapis.org/oas/3.1/schema/2022-02-27") + r, e := ExtractSpecInfo([]byte(OpenApi31)) + assert.Nil(t, e) + assert.Equal(t, OpenApi3, r.SpecType) + assert.Equal(t, "3.1", r.Version) + assert.Contains(t, r.APISchema, "https://spec.openapis.org/oas/3.1/schema/2022-10-07") } func TestExtractSpecInfo_OpenAPIFalse(t *testing.T) { - spec, e := ExtractSpecInfo([]byte(OpenApiFalse)) - assert.NoError(t, e) - assert.Equal(t, "false", spec.Version) + spec, e := ExtractSpecInfo([]byte(OpenApiFalse)) + assert.NoError(t, e) + assert.Equal(t, "false", spec.Version) } func TestExtractSpecInfo_OpenAPI2(t *testing.T) { - r, e := ExtractSpecInfo([]byte(OpenApi2Spec)) - assert.Nil(t, e) - assert.Equal(t, OpenApi2, r.SpecType) - assert.Equal(t, "2.0.1", r.Version) - assert.Greater(t, len(*r.SpecJSONBytes), 0) - assert.Contains(t, r.APISchema, "http://swagger.io/v2/schema.json#") + r, e := ExtractSpecInfo([]byte(OpenApi2Spec)) + assert.Nil(t, e) + assert.Equal(t, OpenApi2, r.SpecType) + assert.Equal(t, "2.0.1", r.Version) + assert.Greater(t, len(*r.SpecJSONBytes), 0) + assert.Contains(t, r.APISchema, "http://swagger.io/v2/schema.json#") } func TestExtractSpecInfo_OpenAPI2_OddVersion(t *testing.T) { - _, e := ExtractSpecInfo([]byte(OpenApi2SpecOdd)) - assert.NotNil(t, e) - assert.Equal(t, - "spec is defined as a swagger (openapi 2.0) spec, but is an openapi 3 or unknown version", e.Error()) + _, e := ExtractSpecInfo([]byte(OpenApi2SpecOdd)) + assert.NotNil(t, e) + assert.Equal(t, + "spec is defined as a swagger (openapi 2.0) spec, but is an openapi 3 or unknown version", e.Error()) } func TestExtractSpecInfo_AsyncAPI(t *testing.T) { - r, e := ExtractSpecInfo([]byte(AsyncAPISpec)) - assert.Nil(t, e) - assert.Equal(t, AsyncApi, r.SpecType) - assert.Equal(t, "2.0.0", r.Version) - assert.Greater(t, len(*r.SpecJSONBytes), 0) + r, e := ExtractSpecInfo([]byte(AsyncAPISpec)) + assert.Nil(t, e) + assert.Equal(t, AsyncApi, r.SpecType) + assert.Equal(t, "2.0.0", r.Version) + assert.Greater(t, len(*r.SpecJSONBytes), 0) } func TestExtractSpecInfo_AsyncAPI_OddVersion(t *testing.T) { - _, e := ExtractSpecInfo([]byte(AsyncAPISpecOdd)) - assert.NotNil(t, e) - assert.Equal(t, - "spec is defined as asyncapi, but has a major version that is invalid", e.Error()) + _, e := ExtractSpecInfo([]byte(AsyncAPISpecOdd)) + assert.NotNil(t, e) + assert.Equal(t, + "spec is defined as asyncapi, but has a major version that is invalid", e.Error()) } func TestExtractSpecInfo_BadVersion_OpenAPI3(t *testing.T) { - yml := `openapi: + yml := `openapi: should: fail` - _, err := ExtractSpecInfo([]byte(yml)) - assert.Error(t, err) + _, err := ExtractSpecInfo([]byte(yml)) + assert.Error(t, err) } func TestExtractSpecInfo_BadVersion_Swagger(t *testing.T) { - yml := `swagger: + yml := `swagger: should: fail` - _, err := ExtractSpecInfo([]byte(yml)) - assert.Error(t, err) + _, err := ExtractSpecInfo([]byte(yml)) + assert.Error(t, err) } func TestExtractSpecInfo_BadVersion_AsyncAPI(t *testing.T) { - yml := `asyncapi: + yml := `asyncapi: should: fail` - _, err := ExtractSpecInfo([]byte(yml)) - assert.Error(t, err) + _, err := ExtractSpecInfo([]byte(yml)) + assert.Error(t, err) } func ExampleExtractSpecInfo() { - // load bytes from openapi spec file. - bytes, _ := ioutil.ReadFile("../test_specs/petstorev3.json") + // load bytes from openapi spec file. + bytes, _ := ioutil.ReadFile("../test_specs/petstorev3.json") - // create a new *SpecInfo instance from loaded bytes - specInfo, err := ExtractSpecInfo(bytes) - if err != nil { - panic(fmt.Sprintf("cannot extract spec info: %e", err)) - } + // create a new *SpecInfo instance from loaded bytes + specInfo, err := ExtractSpecInfo(bytes) + if err != nil { + panic(fmt.Sprintf("cannot extract spec info: %e", err)) + } - // print out the version, format and filetype - fmt.Printf("the version of the spec is %s, the format is %s and the file type is %s", - specInfo.Version, specInfo.SpecFormat, specInfo.SpecFileType) + // print out the version, format and filetype + fmt.Printf("the version of the spec is %s, the format is %s and the file type is %s", + specInfo.Version, specInfo.SpecFormat, specInfo.SpecFileType) - // Output: the version of the spec is 3.0.2, the format is oas3 and the file type is json + // Output: the version of the spec is 3.0.2, the format is oas3 and the file type is json } diff --git a/document_examples_test.go b/document_examples_test.go index e89df79..7c44bb6 100644 --- a/document_examples_test.go +++ b/document_examples_test.go @@ -665,5 +665,5 @@ func ExampleNewDocument_modifyAndReRender() { fmt.Printf("There were %d original paths. There are now %d paths in the document\n", originalPaths, newPaths) fmt.Printf("The original spec had %d bytes, the new one has %d\n", len(petstore), len(rawBytes)) // Output: There were 13 original paths. There are now 14 paths in the document - //The original spec had 31143 bytes, the new one has 27857 + //The original spec had 31143 bytes, the new one has 27841 }