Skip to content

Commit 4dced91

Browse files
committed
prefer named example first
1 parent 3785db7 commit 4dced91

File tree

2 files changed

+100
-25
lines changed

2 files changed

+100
-25
lines changed

renderer/mock_generator.go

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ package renderer
66
import (
77
"encoding/json"
88
"fmt"
9-
"reflect"
10-
"strconv"
11-
129
highbase "github.com/pb33f/libopenapi/datamodel/high/base"
1310
"github.com/pb33f/libopenapi/orderedmap"
1411
"go.yaml.in/yaml/v4"
12+
"reflect"
13+
"strconv"
1514
)
1615

1716
const (
@@ -107,7 +106,25 @@ func (mg *MockGenerator) GenerateMock(mock any, name string) ([]byte, error) {
107106
"fields (%s, %s)", fieldCount, Example, Examples)
108107
}
109108

110-
// if the value has an example, try and render it out as is.
109+
var fallbackExample *highbase.Example = nil
110+
// trying to find a named example
111+
examples := v.FieldByName(Examples)
112+
examplesValue := examples.Interface()
113+
if examplesValue != nil && !examples.IsNil() {
114+
115+
// cast examples to *orderedmap.Map[string, *highbase.Example]
116+
examplesMap := examplesValue.(*orderedmap.Map[string, *highbase.Example])
117+
if examplesMap.Len() > 0 {
118+
if example, ok := examplesMap.Get(name); ok {
119+
return mg.renderMock(example.Value), nil
120+
} else {
121+
//take the first example from the list
122+
fallbackExample = examplesMap.Oldest().Value
123+
}
124+
}
125+
}
126+
127+
// looking for an inline example
111128
f := v.FieldByName(Example)
112129
if !f.IsNil() {
113130
// Pointer/Interface Shenanigans
@@ -120,30 +137,14 @@ func (mg *MockGenerator) GenerateMock(mock any, name string) ([]byte, error) {
120137
}
121138
}
122139
if ex != nil {
123-
// try and serialize the example value
140+
// try and serialize the example value (very hacky since ex can be anything)
124141
return mg.renderMock(ex), nil
125142
}
126143
}
127144

128-
// if there is no example, but there are multi-examples.
129-
examples := v.FieldByName(Examples)
130-
examplesValue := examples.Interface()
131-
if examplesValue != nil && !examples.IsNil() {
132-
133-
// cast examples to *orderedmap.Map[string, *highbase.Example]
134-
examplesMap := examplesValue.(*orderedmap.Map[string, *highbase.Example])
135-
136-
// if the name is not empty, try and find the example by name
137-
for k, exp := range examplesMap.FromOldest() {
138-
if k == name {
139-
return mg.renderMock(exp.Value), nil
140-
}
141-
}
142-
143-
// if the name is empty, just return the first example
144-
for exp := range examplesMap.ValuesFromOldest() {
145-
return mg.renderMock(exp.Value), nil
146-
}
145+
// rendering fallback if it's not nil
146+
if fallbackExample != nil {
147+
return mg.renderMock(fallbackExample.Value), nil
147148
}
148149

149150
// no examples? no problem, we can try and generate a mock from the schema.
@@ -167,7 +168,7 @@ func (mg *MockGenerator) GenerateMock(mock any, name string) ([]byte, error) {
167168

168169
if schemaValue != nil {
169170

170-
// now lets check the schema for `Examples` and `Example` fields.
171+
// now let's check the schema for `Examples` and `Example` fields.
171172
if schemaValue.Examples != nil {
172173
if name != "" {
173174
// try and convert the example to an integer

renderer/mock_generator_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,3 +523,77 @@ properties:
523523
// It should be different from the first two
524524
assert.NotEqual(t, string(mock1), string(mock3))
525525
}
526+
527+
func TestMockGenerator_GenerateMock_PickNamedExample(t *testing.T) {
528+
mg := NewMockGenerator(YAML)
529+
530+
type mockable struct {
531+
Example *base.Example
532+
Examples *orderedmap.Map[string, *base.Example]
533+
}
534+
535+
inlineExample := &base.Example{
536+
Value: utils.CreateStringNode("inline example"),
537+
}
538+
examples := orderedmap.New[string, *base.Example]()
539+
examples.Set("exampleOne", &base.Example{
540+
Value: utils.CreateStringNode("example 1"),
541+
})
542+
543+
mock, err := mg.GenerateMock(&mockable{
544+
Example: inlineExample,
545+
Examples: examples,
546+
}, "exampleOne")
547+
assert.NoError(t, err)
548+
assert.Equal(t, "example 1", strings.TrimSpace(string(mock)))
549+
}
550+
551+
func TestMockGenerator_GenerateMock_PickOldestExample(t *testing.T) {
552+
mg := NewMockGenerator(YAML)
553+
554+
type mockable struct {
555+
Example any
556+
Examples *orderedmap.Map[string, *base.Example]
557+
}
558+
559+
examples := orderedmap.New[string, *base.Example]()
560+
examples.Set("exampleOne", &base.Example{
561+
Value: utils.CreateStringNode("example 1"),
562+
})
563+
examples.Set("exampleTwo", &base.Example{
564+
Value: utils.CreateStringNode("example 2"),
565+
})
566+
567+
mock, err := mg.GenerateMock(&mockable{
568+
Examples: examples,
569+
}, "nonexisting example")
570+
assert.NoError(t, err)
571+
assert.Equal(t, "example 1", strings.TrimSpace(string(mock)))
572+
}
573+
574+
func TestMockGenerator_GenerateMock_GetInline(t *testing.T) {
575+
mg := NewMockGenerator(YAML)
576+
577+
type mockable struct {
578+
Example any
579+
Examples *orderedmap.Map[string, *base.Example]
580+
}
581+
inlineExample := &yaml.Node{
582+
Kind: yaml.ScalarNode,
583+
Value: "inline example",
584+
}
585+
examples := orderedmap.New[string, *base.Example]()
586+
examples.Set("exampleOne", &base.Example{
587+
Value: utils.CreateStringNode("example 1"),
588+
})
589+
examples.Set("exampleTwo", &base.Example{
590+
Value: utils.CreateStringNode("example 2"),
591+
})
592+
593+
mock, err := mg.GenerateMock(&mockable{
594+
Example: inlineExample,
595+
Examples: examples,
596+
}, "nonexisting example")
597+
assert.NoError(t, err)
598+
assert.Equal(t, "inline example", strings.TrimSpace(string(mock)))
599+
}

0 commit comments

Comments
 (0)