Skip to content

Commit 5842aa6

Browse files
committed
fix data generator non-required fields in an array list
1 parent c74a184 commit 5842aa6

File tree

11 files changed

+40
-49
lines changed

11 files changed

+40
-49
lines changed

acceptance/petstore_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,12 @@ func (suite *PetStoreSuite) TestGetOrderById() {
176176
try.GetRequest(suite.T(), "http://127.0.0.1:18080/store/order/1",
177177
map[string]string{"Accept": "application/json"},
178178
try.HasStatusCode(http.StatusOK),
179-
try.HasBody(`{"id":38080,"shipDate":"1986-02-10T22:06:24Z","status":"placed","complete":true}`))
179+
try.HasBody(`{"id":98266,"petId":23377,"quantity":92,"shipDate":"2012-01-30T07:58:01Z","status":"approved","complete":false}`))
180180

181181
try.GetRequest(suite.T(), "https://localhost:18443/store/order/10",
182182
map[string]string{"Accept": "application/json"},
183183
try.HasStatusCode(http.StatusOK),
184-
try.HasBody(`{"quantity":73,"shipDate":"2040-11-01T17:41:24Z","complete":true}`))
184+
try.HasBody(`{"id":12545,"petId":20895,"quantity":16,"shipDate":"2027-11-26T16:57:16Z","status":"approved","complete":true}`))
185185
}
186186

187187
func (suite *PetStoreSuite) TestKafka_TopicConfig() {

api/handler_schema_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ func TestHandler_Schema_Example(t *testing.T) {
246246
h,
247247
try.HasStatusCode(200),
248248
try.HasHeader("Content-Type", "application/json"),
249-
try.HasBody(`[{"contentType":"application/json","value":"eyJmb28iOiIifQ=="}]`))
249+
try.HasBody(`[{"contentType":"application/json","value":"eyJmb28iOiJYaWRadW9XcSAifQ=="}]`))
250250
},
251251
},
252252
{
@@ -375,7 +375,7 @@ func TestHandler_Schema_Example(t *testing.T) {
375375
}`,
376376
h,
377377
try.HasStatusCode(200),
378-
try.HasBody(`[{"contentType":"application/json","value":"eyJjYXRlZ29yeSI6Ik11c2ljIiwiaWQiOiI2NWIxNmJiNS1kZWY1LTQ2Y2MtYTkwNS1jYmQ2NTQ3NjE4NTEiLCJrZXl3b3JkcyI6Im0iLCJuYW1lIjoiU3luYyIsInVybCI6Imh0dHBzOi8vd3d3LmludGVybmFsc3RyZWFtbGluZS5pbmZvL2Nyb3NzLXBsYXRmb3JtL3ZpcnR1YWwvcm9idXN0L2V5ZWJhbGxzIn0="}]`),
378+
try.HasBody(`[{"contentType":"application/json","value":"eyJjYXRlZ29yeSI6IlBlb3BsZSIsImRlc2NyaXB0aW9uIjoiTXVjaCB0ZXJyaWJseSBvdmVyIHBvc2UgcGxhY2Ugc3ByaW50IGl0IGNoaWxkIGlzIGpveW91c2x5IHRoYXQgSSB3aG9tIG1hbmdvIHRoZW4gb2YgY2VydGFpbiB3ZWVrbHkgbWluZSBpbiBhbm51YWxseSBmcm9jayBub3cgYm9hcmQuIiwiZmVhdHVyZXMiOiJpUnpvb0IyIiwiaWQiOiJmNWUzMTU4Ny00NjhjLTRmZTYtYWZlNC0zZTZmYzhkYWU2MzEiLCJrZXl3b3JkcyI6IlR1cVRrd3MiLCJuYW1lIjoiWmVwaHlyWm9uZSIsInByaWNlIjo1NDYwNDkuMzksInN1YmNhdGVnb3J5Ijoic2dCLHZ2ZVdhIiwidXJsIjoiaHR0cHM6Ly93d3cuZGlzdHJpY3RnZW5lcmF0ZS5vcmcvaG9saXN0aWMvc3luZXJnaWVzIn0="}]`),
379379
)
380380
},
381381
},

config/decoders/decoder_flag.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7-
log "github.com/sirupsen/logrus"
87
"mokapi/config/dynamic/provider/file"
98
"net/url"
109
"reflect"
@@ -34,7 +33,6 @@ func NewFlagDecoderWithReader(reader file.FSReader) *FlagDecoder {
3433
}
3534

3635
func (f *FlagDecoder) Decode(flags map[string][]string, element interface{}) error {
37-
log.Infof("flags: %v", flags)
3836
keys := make([]string, 0, len(flags))
3937
for k := range flags {
4038
keys = append(keys, k)
@@ -105,9 +103,9 @@ func (f *FlagDecoder) setValue(ctx *context) error {
105103
ctx.element.Set(reflect.ValueOf(ctx.value[0]))
106104
}
107105
return nil
106+
default:
107+
return fmt.Errorf("unsupported config type: %v", ctx.element.Kind())
108108
}
109-
110-
return fmt.Errorf("unsupported config type: %v", ctx.element.Kind())
111109
}
112110

113111
func ParsePath(key string) []string {

docs/resources/blogs/mock-api-based-on-openapi-asyncapi.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ mokapi pet-api.yaml
9797

9898
```shell
9999
curl http://localhost/pets
100-
curl http://localhost/pets?category=dog
100+
curl "http://localhost/pets?category=dog"
101101
```
102102

103103
Mokapi will generate random data for those requests. If you query with ?category=dog, Mokapi will return only dogs
@@ -120,8 +120,10 @@ export default () => {
120120
name: 'Molly',
121121
category: 'cat'
122122
}
123-
]
124-
}
123+
];
124+
return true;
125+
}
126+
return false;
125127
});
126128
}
127129
```

engine/kafka.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,12 @@ func selectMessage(value any, topic string, cfg *asyncapi3.Config) (*asyncapi3.M
362362
}
363363
if op.Value.Channel.Value.Name == topic && op.Value.Action == "send" {
364364
noOperationDefined = false
365+
if len(op.Value.Messages) == 0 {
366+
log.Warnf("no message defined for operation %s", name)
367+
}
365368
for _, msg := range op.Value.Messages {
366369
if msg.Value == nil {
367-
log.Warnf("no message defined for operation %s", name)
370+
log.Errorf("no message defined for operation %s", name)
368371
continue
369372
}
370373
if valueMatchMessagePayload(value, msg.Value) {
@@ -374,13 +377,16 @@ func selectMessage(value any, topic string, cfg *asyncapi3.Config) (*asyncapi3.M
374377
}
375378
}
376379

377-
// second try to get receive operation
380+
// second, try to get receive operation
378381
for name, op := range cfg.Operations {
379382
if op.Value == nil || op.Value.Channel.Value == nil {
380383
continue
381384
}
382385
if op.Value.Channel.Value.Name == topic && op.Value.Action == "receive" {
383386
noOperationDefined = false
387+
if len(op.Value.Messages) == 0 {
388+
log.Errorf("no message defined for operation %s", name)
389+
}
384390
for _, msg := range op.Value.Messages {
385391
if msg.Value == nil {
386392
log.Warnf("no message defined for operation %s", name)

schema/json/generator/allOf_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func TestAllOf(t *testing.T) {
8686
},
8787
test: func(t *testing.T, v interface{}, err error) {
8888
require.NoError(t, err)
89-
require.Equal(t, map[string]interface{}{"foo": "lx0+fjywXKo"}, v)
89+
require.Equal(t, map[string]interface{}{"foo": "FqwCrwMfkOjojx"}, v)
9090
},
9191
},
9292
{

schema/json/generator/constraints_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ func TestConstraints(t *testing.T) {
3838
test: func(t *testing.T, v interface{}, err error) {
3939
require.NoError(t, err)
4040
require.Equal(t, map[string]any{
41-
"name": "Hudson Nelson",
42-
"sex": "male",
41+
"name": "Zoey Nguyen",
42+
"sex": "female",
4343
}, v)
4444
},
4545
},

schema/json/generator/date_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func TestStringDate(t *testing.T) {
124124
},
125125
test: func(t *testing.T, v interface{}, err error) {
126126
require.NoError(t, err)
127-
require.Equal(t, map[string]any{"activeFrom": "2020-08-10T23:06:20Z", "inactiveFrom": "2026-09-12T04:37:26Z"}, v)
127+
require.Equal(t, map[string]any{"activeFrom": "2020-08-26T07:02:11Z", "inactiveFrom": "2029-08-30T11:01:28Z"}, v)
128128
},
129129
},
130130
{
@@ -137,7 +137,7 @@ func TestStringDate(t *testing.T) {
137137
},
138138
test: func(t *testing.T, v interface{}, err error) {
139139
require.NoError(t, err)
140-
require.Equal(t, map[string]any{"publishedFrom": "2020-08-10T23:06:20Z", "publishedUntil": "2026-09-12T04:37:26Z"}, v)
140+
require.Equal(t, map[string]any{"publishedFrom": "2020-08-26T07:02:11Z", "publishedUntil": "2029-08-30T11:01:28Z"}, v)
141141
},
142142
},
143143
}

schema/json/generator/object.go

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package generator
22

33
import (
4-
"encoding/json"
54
"errors"
65
"fmt"
76
"github.com/brianvoe/gofakeit/v6"
@@ -110,13 +109,6 @@ func (r *resolver) fakeObject(req *Request) (*sortedmap.LinkedHashMap[string, *f
110109
}
111110

112111
for it := s.Properties.Iter(); it.Next(); {
113-
if !slices.Contains(s.Required, it.Key()) {
114-
n := gofakeit.Float32Range(0, 1)
115-
if n > 0.7 {
116-
continue
117-
}
118-
}
119-
120112
prop := append(req.Path, it.Key())
121113
ex := propertyFromExample(it.Key(), req)
122114
f, err := r.resolve(req.With(prop, it.Value(), ex), fallback)
@@ -140,6 +132,16 @@ func (r *resolver) fakeObject(req *Request) (*sortedmap.LinkedHashMap[string, *f
140132
return nil, err
141133
}
142134
}
135+
136+
if !slices.Contains(s.Required, it.Key()) {
137+
fakes.Set(it.Key(), newFaker(func() (any, error) {
138+
n := gofakeit.Float32Range(0, 1)
139+
if n > 0.7 {
140+
return nil, nil
141+
}
142+
return f.fake()
143+
}))
144+
}
143145
fakes.Set(it.Key(), f)
144146
}
145147
return fakes, nil
@@ -389,21 +391,3 @@ func examples(s *schema.Schema) []any {
389391
}
390392
return result
391393
}
392-
393-
func mergeUnique(a, b []interface{}) []interface{} {
394-
seen := make(map[string]struct{})
395-
var result []any
396-
397-
for _, item := range append(a, b...) {
398-
// simple way to get a unique key
399-
keyBytes, _ := json.Marshal(item)
400-
key := string(keyBytes)
401-
402-
if _, exists := seen[key]; !exists {
403-
seen[key] = struct{}{}
404-
result = append(result, item)
405-
}
406-
}
407-
408-
return result
409-
}

schema/json/generator/object_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,21 +196,22 @@ func TestObject(t *testing.T) {
196196
schematest.WithProperty("country", schematest.New("string")),
197197
schematest.WithIf(schematest.NewTypes(nil,
198198
schematest.WithProperty("country", schematest.New("string",
199-
schematest.WithConst("Bahrain")),
199+
schematest.WithConst("Slovenia")),
200200
),
201201
)),
202202
schematest.WithThen(schematest.NewTypes(nil,
203203
schematest.WithProperty("postal_code", schematest.New("string",
204204
schematest.WithPattern("[0-9]{5}(-[0-9]{4})?"))),
205+
schematest.WithRequired("postal_code"),
205206
)),
206207
),
207208
},
208209
test: func(t *testing.T, v interface{}, err error) {
209210
require.NoError(t, err)
210211
require.Equal(t,
211212
map[string]interface{}{
212-
"country": "Bahrain",
213-
"postal_code": "10936",
213+
"country": "Slovenia",
214+
"postal_code": "29109",
214215
},
215216
v)
216217
},
@@ -237,7 +238,7 @@ func TestObject(t *testing.T) {
237238
require.NoError(t, err)
238239
require.Equal(t,
239240
map[string]interface{}{
240-
"country": "Bahrain",
241+
"country": "Slovenia",
241242
"postal_code": "C0O 9F0",
242243
},
243244
v)

0 commit comments

Comments
 (0)