Skip to content

Commit a4ad09a

Browse files
fix: continued moving everything to orderedmaps plus cleaned up most the tests
1 parent 0f3d0cb commit a4ad09a

File tree

169 files changed

+3392
-3721
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

169 files changed

+3392
-3721
lines changed

datamodel/high/base/discriminator.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import (
2020
//
2121
// v3 - https://spec.openapis.org/oas/v3.1.0#discriminator-object
2222
type Discriminator struct {
23-
PropertyName string `json:"propertyName,omitempty" yaml:"propertyName,omitempty"`
24-
Mapping orderedmap.Map[string, string] `json:"mapping,omitempty" yaml:"mapping,omitempty"`
23+
PropertyName string `json:"propertyName,omitempty" yaml:"propertyName,omitempty"`
24+
Mapping *orderedmap.Map[string, string] `json:"mapping,omitempty" yaml:"mapping,omitempty"`
2525
low *low.Discriminator
2626
}
2727

datamodel/high/base/dynamic_value_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func TestDynamicValue_MarshalYAMLInline(t *testing.T) {
127127

128128
// convert node into yaml
129129
bits, _ := yaml.Marshal(rend)
130-
assert.Equal(t, "properties:\n rice:\n $ref: '#/components/schemas/rice'", strings.TrimSpace(string(bits)))
130+
assert.Equal(t, "properties:\n rice:\n type: array\n items:\n type: string", strings.TrimSpace(string(bits)))
131131
}
132132

133133
func TestDynamicValue_MarshalYAMLInline_Error(t *testing.T) {

datamodel/high/base/example.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ import (
1515
//
1616
// v3 - https://spec.openapis.org/oas/v3.1.0#example-object
1717
type Example struct {
18-
Summary string `json:"summary,omitempty" yaml:"summary,omitempty"`
19-
Description string `json:"description,omitempty" yaml:"description,omitempty"`
20-
Value any `json:"value,omitempty" yaml:"value,omitempty"`
21-
ExternalValue string `json:"externalValue,omitempty" yaml:"externalValue,omitempty"`
22-
Extensions map[string]any `json:"-" yaml:"-"`
18+
Summary string `json:"summary,omitempty" yaml:"summary,omitempty"`
19+
Description string `json:"description,omitempty" yaml:"description,omitempty"`
20+
Value *yaml.Node `json:"value,omitempty" yaml:"value,omitempty"`
21+
ExternalValue string `json:"externalValue,omitempty" yaml:"externalValue,omitempty"`
22+
Extensions *orderedmap.Map[string, *yaml.Node] `json:"-" yaml:"-"`
2323
low *low.Example
2424
}
2525

@@ -58,7 +58,7 @@ func (e *Example) MarshalYAML() (interface{}, error) {
5858

5959
// ExtractExamples will convert a low-level example map, into a high level one that is simple to navigate.
6060
// no fidelity is lost, everything is still available via GoLow()
61-
func ExtractExamples(elements orderedmap.Map[lowmodel.KeyReference[string], lowmodel.ValueReference[*low.Example]]) orderedmap.Map[string, *Example] {
61+
func ExtractExamples(elements *orderedmap.Map[lowmodel.KeyReference[string], lowmodel.ValueReference[*low.Example]]) *orderedmap.Map[string, *Example] {
6262
extracted := orderedmap.New[string, *Example]()
6363
for pair := orderedmap.First(elements); pair != nil; pair = pair.Next() {
6464
extracted.Set(pair.Key().Value, NewExample(pair.Value().Value))

datamodel/high/base/example_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
)
1818

1919
func TestNewExample(t *testing.T) {
20-
2120
var cNode yaml.Node
2221

2322
yml := `summary: an example
@@ -37,18 +36,23 @@ x-hack: code`
3736
// build high
3837
highExample := NewExample(&lowExample)
3938

39+
var xHack string
40+
_ = highExample.Extensions.GetOrZero("x-hack").Decode(&xHack)
41+
42+
var example string
43+
_ = highExample.Value.Decode(&example)
44+
4045
assert.Equal(t, "an example", highExample.Summary)
4146
assert.Equal(t, "something more", highExample.Description)
4247
assert.Equal(t, "https://pb33f.io", highExample.ExternalValue)
43-
assert.Equal(t, "code", highExample.Extensions["x-hack"])
44-
assert.Equal(t, "a thing", highExample.Value)
48+
assert.Equal(t, "code", xHack)
49+
assert.Equal(t, "a thing", example)
4550
assert.Equal(t, 4, highExample.GoLow().ExternalValue.ValueNode.Line)
4651
assert.NotNil(t, highExample.GoLowUntyped())
4752

4853
// render the example as YAML
4954
rendered, _ := highExample.Render()
50-
assert.Equal(t, strings.TrimSpace(string(rendered)), yml)
51-
55+
assert.Equal(t, yml, strings.TrimSpace(string(rendered)))
5256
}
5357

5458
func TestExtractExamples(t *testing.T) {
@@ -71,11 +75,9 @@ func TestExtractExamples(t *testing.T) {
7175
)
7276

7377
assert.Equal(t, "herbs", ExtractExamples(examplesMap).GetOrZero("green").Summary)
74-
7578
}
7679

7780
func ExampleNewExample() {
78-
7981
// create some example yaml (or can be JSON, it does not matter)
8082
yml := `summary: something interesting
8183
description: something more interesting with detail
@@ -98,5 +100,4 @@ x-hack: code`
98100

99101
fmt.Print(highExample.ExternalValue)
100102
// Output: https://pb33f.io
101-
102103
}

datamodel/high/base/external_doc.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package base
66
import (
77
"github.com/pb33f/libopenapi/datamodel/high"
88
low "github.com/pb33f/libopenapi/datamodel/low/base"
9+
"github.com/pb33f/libopenapi/orderedmap"
910
"gopkg.in/yaml.v3"
1011
)
1112

@@ -16,9 +17,9 @@ import (
1617
// v2 - https://swagger.io/specification/v2/#externalDocumentationObject
1718
// v3 - https://spec.openapis.org/oas/v3.1.0#external-documentation-object
1819
type ExternalDoc struct {
19-
Description string `json:"description,omitempty" yaml:"description,omitempty"`
20-
URL string `json:"url,omitempty" yaml:"url,omitempty"`
21-
Extensions map[string]any `json:"-" yaml:"-"`
20+
Description string `json:"description,omitempty" yaml:"description,omitempty"`
21+
URL string `json:"url,omitempty" yaml:"url,omitempty"`
22+
Extensions *orderedmap.Map[string, *yaml.Node] `json:"-" yaml:"-"`
2223
low *low.ExternalDoc
2324
}
2425

@@ -46,7 +47,7 @@ func (e *ExternalDoc) GoLowUntyped() any {
4647
return e.low
4748
}
4849

49-
func (e *ExternalDoc) GetExtensions() map[string]any {
50+
func (e *ExternalDoc) GetExtensions() *orderedmap.Map[string, *yaml.Node] {
5051
return e.Extensions
5152
}
5253

datamodel/high/base/external_doc_test.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ package base
55

66
import (
77
"context"
8-
"fmt"
8+
"strings"
9+
"testing"
10+
911
lowmodel "github.com/pb33f/libopenapi/datamodel/low"
1012
lowbase "github.com/pb33f/libopenapi/datamodel/low/base"
13+
"github.com/pb33f/libopenapi/orderedmap"
1114
"github.com/stretchr/testify/assert"
1215
"gopkg.in/yaml.v3"
13-
"strings"
14-
"testing"
1516
)
1617

1718
func TestNewExternalDoc(t *testing.T) {
18-
1919
var cNode yaml.Node
2020

2121
yml := `description: hack code
@@ -31,22 +31,23 @@ x-hack: code`
3131

3232
highExt := NewExternalDoc(&lowExt)
3333

34+
var xHack string
35+
_ = highExt.Extensions.GetOrZero("x-hack").Decode(&xHack)
36+
3437
assert.Equal(t, "hack code", highExt.Description)
3538
assert.Equal(t, "https://pb33f.io", highExt.URL)
36-
assert.Equal(t, "code", highExt.Extensions["x-hack"])
39+
assert.Equal(t, "code", xHack)
3740

3841
wentLow := highExt.GoLow()
3942
assert.Equal(t, 2, wentLow.URL.ValueNode.Line)
40-
assert.Len(t, highExt.GetExtensions(), 1)
43+
assert.Equal(t, 1, orderedmap.Len(highExt.GetExtensions()))
4144

4245
// render the high-level object as YAML
4346
rendered, _ := highExt.Render()
4447
assert.Equal(t, strings.TrimSpace(string(rendered)), yml)
45-
4648
}
4749

48-
func ExampleNewExternalDoc() {
49-
50+
func TestExampleNewExternalDoc(t *testing.T) {
5051
// create a new external documentation spec reference
5152
// this can be YAML or JSON.
5253
yml := `description: hack code docs
@@ -67,7 +68,8 @@ x-hack: code`
6768
// create new high-level ExternalDoc
6869
highExt := NewExternalDoc(&lowExt)
6970

70-
// print out a extension
71-
fmt.Print(highExt.Extensions["x-hack"])
72-
// Output: code
71+
var xHack string
72+
_ = highExt.Extensions.GetOrZero("x-hack").Decode(&xHack)
73+
74+
assert.Equal(t, "code", xHack)
7375
}

datamodel/high/base/info.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package base
66
import (
77
"github.com/pb33f/libopenapi/datamodel/high"
88
low "github.com/pb33f/libopenapi/datamodel/low/base"
9+
"github.com/pb33f/libopenapi/orderedmap"
910
"gopkg.in/yaml.v3"
1011
)
1112

@@ -17,14 +18,14 @@ import (
1718
// v2 - https://swagger.io/specification/v2/#infoObject
1819
// v3 - https://spec.openapis.org/oas/v3.1.0#info-object
1920
type Info struct {
20-
Summary string `json:"summary,omitempty" yaml:"summary,omitempty"`
21-
Title string `json:"title,omitempty" yaml:"title,omitempty"`
22-
Description string `json:"description,omitempty" yaml:"description,omitempty"`
23-
TermsOfService string `json:"termsOfService,omitempty" yaml:"termsOfService,omitempty"`
24-
Contact *Contact `json:"contact,omitempty" yaml:"contact,omitempty"`
25-
License *License `json:"license,omitempty" yaml:"license,omitempty"`
26-
Version string `json:"version,omitempty" yaml:"version,omitempty"`
27-
Extensions map[string]any
21+
Summary string `json:"summary,omitempty" yaml:"summary,omitempty"`
22+
Title string `json:"title,omitempty" yaml:"title,omitempty"`
23+
Description string `json:"description,omitempty" yaml:"description,omitempty"`
24+
TermsOfService string `json:"termsOfService,omitempty" yaml:"termsOfService,omitempty"`
25+
Contact *Contact `json:"contact,omitempty" yaml:"contact,omitempty"`
26+
License *License `json:"license,omitempty" yaml:"license,omitempty"`
27+
Version string `json:"version,omitempty" yaml:"version,omitempty"`
28+
Extensions *orderedmap.Map[string, *yaml.Node] `json:"-" yaml:"-"`
2829
low *low.Info
2930
}
3031

@@ -53,7 +54,7 @@ func NewInfo(info *low.Info) *Info {
5354
if !info.Version.IsEmpty() {
5455
i.Version = info.Version.Value
5556
}
56-
if len(info.Extensions) > 0 {
57+
if orderedmap.Len(info.Extensions) > 0 {
5758
i.Extensions = high.ExtractExtensions(info.Extensions)
5859
}
5960
return i

datamodel/high/base/info_test.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010

1111
lowmodel "github.com/pb33f/libopenapi/datamodel/low"
1212
lowbase "github.com/pb33f/libopenapi/datamodel/low/base"
13+
"github.com/pb33f/libopenapi/orderedmap"
14+
"github.com/pb33f/libopenapi/utils"
1315
"github.com/stretchr/testify/assert"
1416
"gopkg.in/yaml.v3"
1517
)
@@ -37,6 +39,9 @@ x-cli-name: chicken cli`
3739

3840
highInfo := NewInfo(&lowInfo)
3941

42+
var xCliName string
43+
_ = highInfo.Extensions.GetOrZero("x-cli-name").Decode(&xCliName)
44+
4045
assert.Equal(t, "chicken", highInfo.Title)
4146
assert.Equal(t, "a chicken nugget", highInfo.Summary)
4247
assert.Equal(t, "nugget", highInfo.Description)
@@ -45,7 +50,7 @@ x-cli-name: chicken cli`
4550
assert.Equal(t, "pb33f", highInfo.License.Name)
4651
assert.Equal(t, "https://pb33f.io", highInfo.License.URL)
4752
assert.Equal(t, "99.99", highInfo.Version)
48-
assert.Equal(t, "chicken cli", highInfo.Extensions["x-cli-name"])
53+
assert.Equal(t, "chicken cli", xCliName)
4954

5055
wentLow := highInfo.GoLow()
5156
assert.Equal(t, 10, wentLow.Version.ValueNode.Line)
@@ -109,13 +114,12 @@ url: https://opensource.org/licenses/MIT`
109114
}
110115

111116
func TestInfo_Render(t *testing.T) {
112-
113-
ext := make(map[string]any)
114-
ext["x-pizza"] = "pepperoni"
115-
ext["x-cake"] = &License{
117+
ext := orderedmap.New[string, *yaml.Node]()
118+
ext.Set("x-pizza", utils.CreateStringNode("pepperoni"))
119+
ext.Set("x-cake", utils.CreateYamlNode(&License{
116120
Name: "someone",
117121
URL: "nowhere",
118-
}
122+
}))
119123
highI := &Info{
120124
Title: "hey",
121125
Description: "there you",
@@ -146,6 +150,9 @@ func TestInfo_Render(t *testing.T) {
146150
// build high
147151
highInfo := NewInfo(&lowInfo)
148152

153+
var xPizza string
154+
_ = highInfo.Extensions.GetOrZero("x-pizza").Decode(&xPizza)
155+
149156
assert.Equal(t, "hey", highInfo.Title)
150157
assert.Equal(t, "there you", highInfo.Description)
151158
assert.Equal(t, "have you got any money", highInfo.TermsOfService)
@@ -154,12 +161,11 @@ func TestInfo_Render(t *testing.T) {
154161
assert.Equal(t, "MIT", highInfo.License.Name)
155162
assert.Equal(t, "https://opensource.org/licenses/MIT", highInfo.License.URL)
156163
assert.Equal(t, "1.2.3", highInfo.Version)
157-
assert.Equal(t, "pepperoni", highInfo.Extensions["x-pizza"])
164+
assert.Equal(t, "pepperoni", xPizza)
158165
assert.NotNil(t, highInfo.GoLowUntyped())
159166
}
160167

161168
func TestInfo_RenderOrder(t *testing.T) {
162-
163169
yml := `title: hey
164170
description: there you
165171
termsOfService: have you got any money
@@ -187,6 +193,9 @@ x-cake:
187193
// build high
188194
highInfo := NewInfo(&lowInfo)
189195

196+
var xPizza string
197+
_ = highInfo.Extensions.GetOrZero("x-pizza").Decode(&xPizza)
198+
190199
assert.Equal(t, "hey", highInfo.Title)
191200
assert.Equal(t, "there you", highInfo.Description)
192201
assert.Equal(t, "have you got any money", highInfo.TermsOfService)
@@ -195,7 +204,7 @@ x-cake:
195204
assert.Equal(t, "MIT", highInfo.License.Name)
196205
assert.Equal(t, "https://opensource.org/licenses/MIT", highInfo.License.URL)
197206
assert.Equal(t, "1.2.3", highInfo.Version)
198-
assert.Equal(t, "pepperoni", highInfo.Extensions["x-pizza"])
207+
assert.Equal(t, "pepperoni", xPizza)
199208

200209
// marshal high back to yaml, should be the same as the original, in same order.
201210
bytes, _ := highInfo.Render()

0 commit comments

Comments
 (0)