Skip to content

Commit 7ed5750

Browse files
committed
improve anyOf, allOf and oneOff with object in data generator
improve data generator for integer and number with default min and max value fix json schema parser with anyOf, allOf and oneOf fix json schema parser for objects with patternProperties
1 parent e25b205 commit 7ed5750

29 files changed

+816
-294
lines changed

api/handler_schema_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,16 @@ func TestHandler_Schema_Example(t *testing.T) {
260260
h,
261261
try.HasStatusCode(200),
262262
try.HasHeader("Content-Type", "application/json"),
263-
try.HasBody(`[{"contentType":"application/json","value":"MS40NDcwMTYyNDY3NTgxMDU4ZSszMDg="}]`))
263+
try.AssertBody(func(t *testing.T, body string) {
264+
var data []map[string]string
265+
err := json.Unmarshal([]byte(body), &data)
266+
require.NoError(t, err)
267+
b, err := base64.StdEncoding.DecodeString(data[0]["value"])
268+
require.NoError(t, err)
269+
require.Equal(t, "609859.0117483337", string(b))
270+
271+
}),
272+
try.HasBody(`[{"contentType":"application/json","value":"NjA5ODU5LjAxMTc0ODMzMzc="}]`))
264273
},
265274
},
266275
{

js/faker/faker_schema_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func TestFaker_Schema(t *testing.T) {
4343
schema: "{ type: ['string', 'integer'] }",
4444
test: func(t *testing.T, v goja.Value, err error) {
4545
r.NoError(t, err)
46-
r.Equal(t, 7.424164296119123e+18, v.Export())
46+
r.Equal(t, int64(-168643), v.Export())
4747
},
4848
},
4949
{
@@ -119,7 +119,7 @@ func TestFaker_Schema(t *testing.T) {
119119
schema: "{ multipleOf: 3 }",
120120
test: func(t *testing.T, v goja.Value, err error) {
121121
r.NoError(t, err)
122-
r.Equal(t, int64(6648), v.Export())
122+
r.Equal(t, int64(162), v.Export())
123123
},
124124
},
125125
{
@@ -134,7 +134,7 @@ func TestFaker_Schema(t *testing.T) {
134134
schema: "{ maximum: 3 }",
135135
test: func(t *testing.T, v goja.Value, err error) {
136136
r.NoError(t, err)
137-
r.Equal(t, -1.6332447240352712e+308, v.Export())
137+
r.Equal(t, -908522.2605665276, v.Export())
138138
},
139139
},
140140
{
@@ -149,7 +149,7 @@ func TestFaker_Schema(t *testing.T) {
149149
schema: "{ exclusiveMaximum: 3 }",
150150
test: func(t *testing.T, v goja.Value, err error) {
151151
r.NoError(t, err)
152-
r.Equal(t, -1.6332447240352712e+308, v.Export())
152+
r.Equal(t, -908522.2605665276, v.Export())
153153
},
154154
},
155155
{
@@ -164,7 +164,7 @@ func TestFaker_Schema(t *testing.T) {
164164
schema: "{ minimum: 3 }",
165165
test: func(t *testing.T, v goja.Value, err error) {
166166
r.NoError(t, err)
167-
r.Equal(t, 1.644484108270445e+307, v.Export())
167+
r.Equal(t, 91480.19056868234, v.Export())
168168
},
169169
},
170170
{
@@ -179,7 +179,7 @@ func TestFaker_Schema(t *testing.T) {
179179
schema: "{ exclusiveMinimum: 3 }",
180180
test: func(t *testing.T, v goja.Value, err error) {
181181
r.NoError(t, err)
182-
r.Equal(t, 1.644484108270445e+307, v.Export())
182+
r.Equal(t, 91480.19056868234, v.Export())
183183
},
184184
},
185185
{
@@ -284,7 +284,7 @@ func TestFaker_Schema(t *testing.T) {
284284
schema: "{ minItems: 3, }",
285285
test: func(t *testing.T, v goja.Value, err error) {
286286
r.NoError(t, err)
287-
r.Equal(t, []any{true, int64(6224634831868504800), "ZuoWq vY5elXhlD", []interface{}{2.3090412168364615e+307, "lYehCIA", map[string]interface{}{"caravan": true, "hail": 2.536044080333601e+307, "mob": int64(-287411453310397474), "scale": true}, false}, false, []interface{}{true, int64(1769588974883905016), 7.53537259781811e+307}, false, "LWxKt"}, v.Export())
287+
r.Equal(t, []any{true, int64(-354976), "ZuoWq vY5elXhlD", []interface{}{-743110.6375100765, "lYehCIA", map[string]interface{}{"caravan": true, "hail": -717855.7306413883, "mob": int64(-679449), "scale": true}, false}, false, []interface{}{true, int64(859972), -161661.97092417115}, false, "LWxKt"}, v.Export())
288288
},
289289
},
290290
{
@@ -299,7 +299,7 @@ func TestFaker_Schema(t *testing.T) {
299299
schema: "{ uniqueItems: true, }",
300300
test: func(t *testing.T, v goja.Value, err error) {
301301
r.NoError(t, err)
302-
r.Equal(t, []any{true, int64(6224634831868504800), "ZuoWq vY5elXhlD", []any{2.3090412168364615e+307, "lYehCIA", map[string]any{"caravan": true, "hail": 2.536044080333601e+307, "mob": int64(-287411453310397474), "scale": true}, false}, false}, v.Export())
302+
r.Equal(t, []any{true, int64(-354976), "ZuoWq vY5elXhlD", []any{-743110.6375100765, "lYehCIA", map[string]any{"caravan": true, "hail": -717855.7306413883, "mob": int64(-679449), "scale": true}, false}, false}, v.Export())
303303
},
304304
},
305305
{
@@ -329,7 +329,7 @@ func TestFaker_Schema(t *testing.T) {
329329
schema: "{ contains: { type: 'string' } }",
330330
test: func(t *testing.T, v goja.Value, err error) {
331331
r.NoError(t, err)
332-
r.Equal(t, []any{[]any{true, int64(1769588974883905016), 7.53537259781811e+307}, "wYx?vY5elXhlD", []any{2.3090412168364615e+307, "lYehCIA", map[string]any{"caravan": true, "hail": 2.536044080333601e+307, "mob": int64(-287411453310397474), "scale": true}, false}, false, "idZ"}, v.Export())
332+
r.Equal(t, []any{[]any{true, int64(859972), -161661.97092417115}, "wYx?vY5elXhlD", []any{-743110.6375100765, "lYehCIA", map[string]any{"caravan": true, "hail": -717855.7306413883, "mob": int64(-679449), "scale": true}, false}, false, "idZ"}, v.Export())
333333
},
334334
},
335335
{
@@ -344,7 +344,7 @@ func TestFaker_Schema(t *testing.T) {
344344
schema: "{ contains: { type: 'string' }, minContains: 4 }",
345345
test: func(t *testing.T, v goja.Value, err error) {
346346
r.NoError(t, err)
347-
r.Equal(t, []any{"LWxKt", "wYx?vY5elXhlD", "VhgPevuwyrNrL", 3.411011869388561e+307, 6.118679555323457e+307, "lVeCZKW1JKqG", true, []interface{}{}, "idZ"}, v.Export())
347+
r.Equal(t, []any{"LWxKt", "wYx?vY5elXhlD", "VhgPevuwyrNrL", -620512.3329182866, -319274.3036433653, "lVeCZKW1JKqG", true, []interface{}{}, "idZ"}, v.Export())
348348
},
349349
},
350350
{
@@ -359,7 +359,7 @@ func TestFaker_Schema(t *testing.T) {
359359
schema: "{ contains: { type: 'string' }, maxContains: 2 }",
360360
test: func(t *testing.T, v goja.Value, err error) {
361361
r.NoError(t, err)
362-
r.Equal(t, []any{" vY5elXhlD4ezl", 1.260262124648351e+307, 1.1925608819819514e+308, int64(3859340644268985534), "idZ"}, v.Export())
362+
r.Equal(t, []any{" vY5elXhlD4ezl", -859791.1845789105, 326768.021588166, int64(628235), "idZ"}, v.Export())
363363
},
364364
},
365365
{
@@ -389,7 +389,7 @@ func TestFaker_Schema(t *testing.T) {
389389
schema: "{ maxProperties: 3 }",
390390
test: func(t *testing.T, v goja.Value, err error) {
391391
r.NoError(t, err)
392-
r.Equal(t, map[string]any{"woman": int64(6224634831868504800)}, v.Export())
392+
r.Equal(t, map[string]any{"woman": int64(-354976)}, v.Export())
393393
},
394394
},
395395
{
@@ -404,7 +404,7 @@ func TestFaker_Schema(t *testing.T) {
404404
schema: "{ minProperties: 3 }",
405405
test: func(t *testing.T, v goja.Value, err error) {
406406
r.NoError(t, err)
407-
r.Equal(t, map[string]any{"bunch": 1.1925608819819514e+308, "gang": int64(3859340644268985534), "growth": " vY5elXhlD4ezl", "woman": 1.260262124648351e+307}, v.Export())
407+
r.Equal(t, map[string]any{"bunch": 326768.021588166, "gang": int64(628235), "growth": " vY5elXhlD4ezl", "woman": -859791.1845789105}, v.Export())
408408
},
409409
},
410410
{
@@ -458,9 +458,9 @@ func TestFaker_Schema(t *testing.T) {
458458
test: func(t *testing.T, v goja.Value, err error) {
459459
r.NoError(t, err)
460460
r.Equal(t, map[string]any{
461-
"I_4VX": int64(4336346668576341540),
461+
"I_4VX": int64(-908662),
462462
"S_kY9X3W": "m",
463-
"builtin": 5.544677937412537e+307,
463+
"builtin": -383134.1033810867,
464464
"group": "CKu",
465465
"ocean": "LJgmr9arWgSfi",
466466
"party": "m",
@@ -477,7 +477,7 @@ func TestFaker_Schema(t *testing.T) {
477477
r.Equal(t, map[string]any{
478478
"Gt8": "X",
479479
"Jd": "D4ezlYehCIA0O",
480-
"MTgAEPI": int64(-3961972703762434141),
480+
"MTgAEPI": int64(990471),
481481
"PgmZE3": false,
482482
}, v.Export())
483483
},
@@ -544,7 +544,7 @@ func TestFaker_Schema(t *testing.T) {
544544
r.Contains(t, m, "foo")
545545
r.Contains(t, m, "bar")
546546
r.Contains(t, m, "baz")
547-
r.Equal(t, map[string]any{"bar": int64(6224634831868504800), "baz": "ZuoWq vY5elXhlD", "foo": true}, m)
547+
r.Equal(t, map[string]any{"bar": int64(-354976), "baz": "ZuoWq vY5elXhlD", "foo": true}, m)
548548
},
549549
},
550550
{
@@ -589,7 +589,7 @@ func TestFaker_Schema(t *testing.T) {
589589
r.NoError(t, err)
590590
m := v.Export()
591591
r.Contains(t, m, "bar")
592-
r.Equal(t, map[string]any{"bar": 1.1291386311317026e+308, "foo": "XidZuoWq "}, m)
592+
r.Equal(t, map[string]any{"bar": 256208.42538087885, "foo": "XidZuoWq "}, m)
593593
},
594594
},
595595
{

js/faker/faker_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func TestModule(t *testing.T) {
176176
m.fake({ type: 'object', properties: { foo: { type: 'string' }, bar: { type: 'string' }}, required: ['foo', 'bar','x', 'y', 'z'] } )
177177
`)
178178
r.NoError(t, err)
179-
r.Equal(t, map[string]any{"bar": "", "foo": "XidZuoWq ", "x": int64(6892487422858870876), "y": int64(8673350504153079445), "z": 5.544677937412537e+307}, v.Export())
179+
r.Equal(t, map[string]any{"bar": "", "foo": "XidZuoWq ", "x": int64(-117432), "y": int64(995706), "z": -383134.1033810867}, v.Export())
180180
},
181181
},
182182
}

js/script_faker_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package js_test
22

33
import (
44
"encoding/json"
5-
"github.com/brianvoe/gofakeit/v6"
6-
r "github.com/stretchr/testify/require"
75
"mokapi/engine/enginetest"
86
"mokapi/js"
97
"mokapi/js/jstest"
108
"mokapi/schema/json/generator"
119
"testing"
10+
11+
"github.com/brianvoe/gofakeit/v6"
12+
r "github.com/stretchr/testify/require"
1213
)
1314

1415
func TestScript_Faker(t *testing.T) {
@@ -41,7 +42,7 @@ func TestScript_Faker(t *testing.T) {
4142
r.NoError(t, err)
4243
v, err := s.RunDefault()
4344
r.NoError(t, err)
44-
r.Equal(t, 1.4470162467581058e+308, v.Export())
45+
r.Equal(t, 609859.0117483337, v.Export())
4546
},
4647
},
4748
{

providers/openapi/schema/generator_test.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -264,23 +264,23 @@ func TestGeneratorInt(t *testing.T) {
264264
schema: schematest.New("integer", schematest.WithFormat("int32")),
265265
test: func(t *testing.T, i interface{}, err error) {
266266
require.NoError(t, err)
267-
require.Equal(t, int32(-1072427943), i)
267+
require.Equal(t, int32(-837149), i)
268268
},
269269
},
270270
{
271271
name: "int32 min",
272272
schema: schematest.New("integer", schematest.WithFormat("int32"), schematest.WithMinimum(10)),
273273
test: func(t *testing.T, i interface{}, err error) {
274274
require.NoError(t, err)
275-
require.Equal(t, int32(196446369), i)
275+
require.Equal(t, int32(91487), i)
276276
},
277277
},
278278
{
279279
name: "int32 max",
280280
schema: schematest.New("integer", schematest.WithFormat("int32"), schematest.WithMaximum(0)),
281281
test: func(t *testing.T, i interface{}, err error) {
282282
require.NoError(t, err)
283-
require.Equal(t, int32(-1951037288), i)
283+
require.Equal(t, int32(-908523), i)
284284
},
285285
},
286286
{
@@ -296,23 +296,23 @@ func TestGeneratorInt(t *testing.T) {
296296
schema: schematest.New("integer", schematest.WithFormat("int64")),
297297
test: func(t *testing.T, i interface{}, err error) {
298298
require.NoError(t, err)
299-
require.Equal(t, int64(-8379641344161477543), i)
299+
require.Equal(t, int64(-837149), i)
300300
},
301301
},
302302
{
303303
name: "int64 min",
304304
schema: schematest.New("integer", schematest.WithFormat("int64"), schematest.WithMinimum(10)),
305305
test: func(t *testing.T, i interface{}, err error) {
306306
require.NoError(t, err)
307-
require.Equal(t, int64(843730692693298304), i)
307+
require.Equal(t, int64(91487), i)
308308
},
309309
},
310310
{
311311
name: "int64 max",
312312
schema: schematest.New("integer", schematest.WithFormat("int64"), schematest.WithMaximum(0)),
313313
test: func(t *testing.T, i interface{}, err error) {
314314
require.NoError(t, err)
315-
require.Equal(t, int64(-8379641344161477632), i)
315+
require.Equal(t, int64(-908523), i)
316316
},
317317
},
318318
{
@@ -373,17 +373,17 @@ func TestGeneratorFloat(t *testing.T) {
373373
}{
374374
{
375375
name: "float",
376-
exp: float32(3.1128167e+37),
376+
exp: float32(-817045.06),
377377
schema: schematest.New("number", schematest.WithFormat("float")),
378378
},
379379
{
380380
name: "float min",
381-
exp: float32(3.1128167e+37),
381+
exp: float32(91486.55),
382382
schema: schematest.New("number", schematest.WithFormat("float"), schematest.WithMinimum(10)),
383383
},
384384
{
385385
name: "float max",
386-
exp: float32(-3.0915418e+38),
386+
exp: float32(-908522.56),
387387
schema: schematest.New("number", schematest.WithFormat("float"), schematest.WithMaximum(0)),
388388
},
389389
{
@@ -393,17 +393,17 @@ func TestGeneratorFloat(t *testing.T) {
393393
},
394394
{
395395
name: "double",
396-
exp: 1.644484108270445e+307,
396+
exp: -817045.0699978453,
397397
schema: schematest.New("number", schematest.WithFormat("double")),
398398
},
399399
{
400400
name: "double min",
401-
exp: 1.644484108270445e+307,
401+
exp: 91486.55022642734,
402402
schema: schematest.New("number", schematest.WithFormat("double"), schematest.WithMinimum(10)),
403403
},
404404
{
405405
name: "double max",
406-
exp: -1.6332447240352712e+308,
406+
exp: -908522.5349989226,
407407
schema: schematest.New("number", schematest.WithFormat("double"), schematest.WithMaximum(0)),
408408
},
409409
{
@@ -528,7 +528,7 @@ func TestGeneratorArray(t *testing.T) {
528528
schematest.WithItems("integer", schematest.WithFormat("int32"), schematest.WithMinimum(0), schematest.WithMaximum(3)),
529529
),
530530
test: func(t *testing.T, i interface{}, err error) {
531-
require.EqualError(t, err, "failed to generate valid array: reached attempt limit (10) caused by: can not fill array with unique items")
531+
require.EqualError(t, err, "failed to generate valid array: reached attempt limit (10) caused by: cannot fill array with unique items")
532532
},
533533
},
534534
{
@@ -558,7 +558,7 @@ func TestGeneratorArray(t *testing.T) {
558558
schema: schematest.New("array"),
559559
test: func(t *testing.T, i interface{}, err error) {
560560
require.NoError(t, err)
561-
require.Equal(t, []interface{}{true, int64(6224634831868504800), "ZuoWq vY5elXhlD", []interface{}{2.3090412168364615e+307, "lYehCIA", map[string]interface{}{"caravan": true, "hail": 2.536044080333601e+307, "mob": int64(-287411453310397474), "scale": true}, false}, false}, i)
561+
require.Equal(t, []interface{}{true, int64(-354976), "ZuoWq vY5elXhlD", []interface{}{-743110.6375100765, "lYehCIA", map[string]interface{}{"caravan": true, "hail": -717855.7306413883, "mob": int64(-679449), "scale": true}, false}, false}, i)
562562
},
563563
},
564564
}
@@ -618,12 +618,12 @@ func TestGeneratorObject(t *testing.T) {
618618
},
619619
{
620620
name: "no fields defined",
621-
exp: map[string]interface{}{"bunch": int64(8673350504153079445), "child": int64(5224568207835308195), "gang": 5.544677937412537e+307, "growth": int64(-8487131363427706431), "hall": 1.0615919996637124e+308, "shower": true, "uncle": int64(-7273558372469573415), "woman": int64(6892487422858870876)},
621+
exp: map[string]interface{}{"bunch": int64(995706), "child": int64(-489581), "gang": -383134.1033810867, "growth": int64(-83276), "hall": 181060.30342605617, "shower": true, "uncle": int64(-142346), "woman": int64(-117432)},
622622
schema: schematest.New("object"),
623623
},
624624
{
625625
name: "with property _metadata",
626-
exp: map[string]interface{}{"_metadata": int64(-8379641344161477543)},
626+
exp: map[string]interface{}{"_metadata": int64(-837149)},
627627
schema: schematest.New("object",
628628
schematest.WithProperty("_metadata", schematest.New("integer", schematest.WithFormat("int64"))),
629629
schematest.WithRequired("_metadata"),
@@ -716,7 +716,7 @@ func TestGenerator_AllOf(t *testing.T) {
716716
)),
717717
test: func(t *testing.T, result interface{}, err error) {
718718
require.NoError(t, err)
719-
require.Equal(t, map[string]interface{}{"foo": "XidZuoWq ", "bar": 5.58584061532191e+307}, result)
719+
require.Equal(t, map[string]interface{}{"foo": "XidZuoWq ", "bar": -378554.6034529718}, result)
720720
},
721721
},
722722
{
@@ -730,7 +730,7 @@ func TestGenerator_AllOf(t *testing.T) {
730730
),
731731
test: func(t *testing.T, result interface{}, err error) {
732732
require.NoError(t, err)
733-
require.Equal(t, map[string]interface{}{"bar": 1.644484108270445e+307}, result)
733+
require.Equal(t, map[string]interface{}{"bar": -817045.0699978453}, result)
734734
},
735735
},
736736
{
@@ -744,7 +744,7 @@ func TestGenerator_AllOf(t *testing.T) {
744744
),
745745
test: func(t *testing.T, result interface{}, err error) {
746746
require.NoError(t, err)
747-
require.Equal(t, map[string]interface{}{"bar": 1.644484108270445e+307}, result)
747+
require.Equal(t, map[string]interface{}{"bar": -817045.0699978453}, result)
748748
},
749749
},
750750
{
@@ -757,7 +757,7 @@ func TestGenerator_AllOf(t *testing.T) {
757757
),
758758
)),
759759
test: func(t *testing.T, result interface{}, err error) {
760-
require.EqualError(t, err, "generate random data for schema failed: all of (schema type=integer, schema type=object properties=[bar] required=[bar]): no shared types found")
760+
require.EqualError(t, err, "generate random data for schema failed: no shared types found: integer and object")
761761
require.Nil(t, result)
762762
},
763763
},
@@ -779,7 +779,7 @@ func TestGenerator_AllOf(t *testing.T) {
779779
schematest.New("object", schematest.WithProperty("bar", schematest.New("number"))),
780780
)),
781781
test: func(t *testing.T, result interface{}, err error) {
782-
require.EqualError(t, err, "generate random data for schema failed: schema type=object properties=[a] required=[a]: failed to generate valid object: reached attempt limit (10) caused by: failed to generate valid array: reached attempt limit (10) caused by: can not fill array with unique items")
782+
require.EqualError(t, err, "failed to generate valid object: reached attempt limit (10) caused by: failed to generate valid array: reached attempt limit (10) caused by: cannot fill array with unique items")
783783
require.Nil(t, result)
784784
},
785785
},

0 commit comments

Comments
 (0)