@@ -6,12 +6,11 @@ package renderer
66import (
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
1716const (
@@ -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
0 commit comments