Skip to content

Commit 819c088

Browse files
committed
Merge remote-tracking branch 'origin/develop' into develop
2 parents ec87ace + f846a3f commit 819c088

File tree

22 files changed

+672
-116
lines changed

22 files changed

+672
-116
lines changed

api/handler_schema_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ func TestHandler_Schema_Example(t *testing.T) {
247247
},
248248
},
249249
{
250-
name: "string or number",
250+
name: "string or integer",
251251
app: &runtime.App{
252252
Monitor: monitor.New(),
253253
},
@@ -256,7 +256,7 @@ func TestHandler_Schema_Example(t *testing.T) {
256256
http.MethodGet,
257257
"http://foo.api/api/schema/example",
258258
nil,
259-
`{"name": "", "schema": {"type": ["string","number"]}}`,
259+
`{"name": "", "schema": {"type": ["string","integer"]}}`,
260260
h,
261261
try.HasStatusCode(200),
262262
try.HasHeader("Content-Type", "application/json"),
@@ -266,10 +266,10 @@ func TestHandler_Schema_Example(t *testing.T) {
266266
require.NoError(t, err)
267267
b, err := base64.StdEncoding.DecodeString(data[0]["value"])
268268
require.NoError(t, err)
269-
require.Equal(t, "609859.0117483337", string(b))
269+
require.Equal(t, "-168643", string(b))
270270

271271
}),
272-
try.HasBody(`[{"contentType":"application/json","value":"NjA5ODU5LjAxMTc0ODMzMzc="}]`))
272+
try.HasBody(`[{"contentType":"application/json","value":"LTE2ODY0Mw=="}]`))
273273
},
274274
},
275275
{

examples/mokapi/services_http.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ export let events = [
588588
url: "http://127.0.0.1:18080/pet",
589589
parameters: [
590590
{
591-
name: 'Acceot-Encoding',
591+
name: 'Accept-Encoding',
592592
type: 'header',
593593
raw: 'gzip, deflate'
594594
}
@@ -625,7 +625,7 @@ export let events = [
625625
url: "http://127.0.0.1:18080/pet",
626626
parameters: [
627627
{
628-
name: 'Acceot-Encoding',
628+
name: 'Accept-Encoding',
629629
type: 'header',
630630
raw: 'gzip, deflate'
631631
}

js/faker/faker_schema_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,10 @@ func TestFaker_Schema(t *testing.T) {
281281
},
282282
{
283283
name: "minItems",
284-
schema: "{ minItems: 3, }",
284+
schema: "{ minItems: 3, items: { type: 'string' } }",
285285
test: func(t *testing.T, v goja.Value, err error) {
286286
r.NoError(t, err)
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())
287+
r.Equal(t, []interface{}{"", "idZ", "wYx?vY5elXhlD", "VhgPevuwyrNrL", "lVeCZKW1JKqG", "sffl", "tR", "h"}, v.Export())
288288
},
289289
},
290290
{
@@ -326,10 +326,10 @@ func TestFaker_Schema(t *testing.T) {
326326
},
327327
{
328328
name: "contains",
329-
schema: "{ contains: { type: 'string' } }",
329+
schema: "{ contains: { const: 'foo' }, items: { 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(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())
332+
r.Equal(t, []any{"VhgPevuwyrNrL", "foo", "idZ", "wYx?vY5elXhlD", "foo"}, v.Export())
333333
},
334334
},
335335
{
@@ -356,10 +356,10 @@ func TestFaker_Schema(t *testing.T) {
356356
},
357357
{
358358
name: "maxContains",
359-
schema: "{ contains: { type: 'string' }, maxContains: 2 }",
359+
schema: "{ contains: { type: 'string' }, maxContains: 2, items: { type: 'string' } }",
360360
test: func(t *testing.T, v goja.Value, err error) {
361361
r.NoError(t, err)
362-
r.Equal(t, []any{" vY5elXhlD4ezl", -859791.1845789105, 326768.021588166, int64(628235), "idZ"}, v.Export())
362+
r.Equal(t, []any{"wYx?vY5elXhlD", "idZ"}, v.Export())
363363
},
364364
},
365365
{
@@ -401,10 +401,10 @@ func TestFaker_Schema(t *testing.T) {
401401
},
402402
{
403403
name: "minProperties",
404-
schema: "{ minProperties: 3 }",
404+
schema: "{ minProperties: 3, additionalProperties: { type: 'string' } }",
405405
test: func(t *testing.T, v goja.Value, err error) {
406406
r.NoError(t, err)
407-
r.Equal(t, map[string]any{"bunch": 326768.021588166, "gang": int64(628235), "growth": " vY5elXhlD4ezl", "woman": -859791.1845789105}, v.Export())
407+
r.Equal(t, map[string]any{"bunch": "VhgPevuwyrNrL", "gang": "lVeCZKW1JKqG", "growth": "sffl", "woman": "wYx?vY5elXhlD"}, v.Export())
408408
},
409409
},
410410
{
@@ -454,13 +454,13 @@ func TestFaker_Schema(t *testing.T) {
454454
},
455455
{
456456
name: "properties, patternProperties and additionalProperties",
457-
schema: "{ properties: { builtin: { type: 'number' } }, patternProperties: { '^S_': { type: 'string' }, '^I_': { type: 'integer' } }, additionalProperties: { type: 'string' } }",
457+
schema: "{ properties: { builtin: { type: 'integer' } }, patternProperties: { '^S_': { type: 'string' }, '^I_': { type: 'integer' } }, additionalProperties: { type: 'string' } }",
458458
test: func(t *testing.T, v goja.Value, err error) {
459459
r.NoError(t, err)
460460
r.Equal(t, map[string]any{
461461
"I_4VX": int64(-908662),
462462
"S_kY9X3W": "m",
463-
"builtin": -383134.1033810867,
463+
"builtin": int64(-86829),
464464
"group": "CKu",
465465
"ocean": "LJgmr9arWgSfi",
466466
"party": "m",
@@ -584,12 +584,12 @@ func TestFaker_Schema(t *testing.T) {
584584
},
585585
{
586586
name: "required but not defined in properties",
587-
schema: "{ properties: { foo: { type: 'string' } }, required: ['bar'] }",
587+
schema: "{ properties: { foo: { type: 'string' }}, additionalProperties: { type: 'string' }, required: ['bar'] }",
588588
test: func(t *testing.T, v goja.Value, err error) {
589589
r.NoError(t, err)
590590
m := v.Export()
591591
r.Contains(t, m, "bar")
592-
r.Equal(t, map[string]any{"bar": 256208.42538087885, "foo": "XidZuoWq "}, m)
592+
r.Equal(t, map[string]any{"bar": "Pevuwy", "woman": "q vY5elXhlD4ez"}, m)
593593
},
594594
},
595595
{

js/faker/faker_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,10 @@ func TestModule(t *testing.T) {
173173
test: func(t *testing.T, vm *goja.Runtime, _ *enginetest.Host) {
174174
v, err := vm.RunString(`
175175
const m = require('faker')
176-
m.fake({ type: 'object', properties: { foo: { type: 'string' }, bar: { type: 'string' }}, required: ['foo', 'bar','x', 'y', 'z'] } )
176+
m.fake({ type: 'object', properties: { foo: { type: 'string' }, bar: { type: 'string' }}, additionalProperties: { 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(-117432), "y": int64(995706), "z": -383134.1033810867}, v.Export())
179+
r.Equal(t, map[string]any{"bar": "vm", "bunch": "LJgmr9arWgSfi", "foo": "Yx?vY5", "gang": "jLWxKtR4", "growth": "m", "woman": "hlD4ezlYehCIA0O", "x": "CKu", "y": "gaqpsEbkw", "z": "LuvMVE6iIqk"}, v.Export())
180180
},
181181
},
182182
}

js/script_faker_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,17 @@ func TestScript_Faker(t *testing.T) {
3232
},
3333
},
3434
{
35-
name: "fake string or number",
35+
name: "fake string or integer",
3636
test: func(t *testing.T, host *enginetest.Host) {
3737
s, err := jstest.New(jstest.WithSource(`import faker from 'mokapi/faker'
3838
export default function() {
39-
return faker.fake({ type: ['string', 'number'] })
39+
return faker.fake({ type: ['string', 'integer'] })
4040
}`),
4141
js.WithHost(host))
4242
r.NoError(t, err)
4343
v, err := s.RunDefault()
4444
r.NoError(t, err)
45-
r.Equal(t, 609859.0117483337, v.Export())
45+
r.Equal(t, int64(-168643), v.Export())
4646
},
4747
},
4848
{

providers/openapi/schema/generator_test.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ func TestGeneratorArray(t *testing.T) {
525525
{
526526
name: "unique items with error",
527527
schema: schematest.New("array", schematest.WithMinItems(5), schematest.WithMaxItems(10), schematest.WithUniqueItems(true),
528-
schematest.WithItems("integer", schematest.WithFormat("int32"), schematest.WithMinimum(0), schematest.WithMaximum(3)),
528+
schematest.WithItems("integer", schematest.WithMinimum(0), schematest.WithMaximum(3)),
529529
),
530530
test: func(t *testing.T, i interface{}, err error) {
531531
require.EqualError(t, err, "failed to generate valid array: reached attempt limit (10) caused by: cannot fill array with unique items")
@@ -616,11 +616,6 @@ func TestGeneratorObject(t *testing.T) {
616616
schema: schematest.New("object",
617617
schematest.WithAdditionalProperties(schematest.New("string"))),
618618
},
619-
{
620-
name: "no fields defined",
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)},
622-
schema: schematest.New("object"),
623-
},
624619
{
625620
name: "with property _metadata",
626621
exp: map[string]interface{}{"_metadata": int64(-837149)},

schema/json/generator/array.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,12 @@ func fakeArray(r *Request, fakeItem *faker) (interface{}, error) {
186186
return nil
187187
})
188188
if err != nil {
189-
return nil, fmt.Errorf("failed to generate valid array: %w", err)
189+
if minItems < length {
190+
length--
191+
i--
192+
} else {
193+
return nil, fmt.Errorf("failed to generate valid array: %w", err)
194+
}
190195
}
191196
}
192197

schema/json/generator/object.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,9 @@ func (r *resolver) fakeObject(req *Request) (*sortedmap.LinkedHashMap[string, *f
296296
// if additionalProperties=false no additional properties is allowed
297297
// if additionalProperties=true we don't add random properties, it is not expected by users
298298

299-
length := numProperties(1, 10, req.Schema)
299+
length := numProperties(1, 10, s)
300300
for i := 0; i < length; i++ {
301-
f, err := r.resolve(req.WithSchema(req.Schema.AdditionalProperties), true)
301+
f, err := r.resolve(req.WithSchema(s.AdditionalProperties), true)
302302
if err != nil {
303303
return nil, err
304304
}
@@ -312,7 +312,7 @@ func (r *resolver) fakeObject(req *Request) (*sortedmap.LinkedHashMap[string, *f
312312

313313
for _, name := range s.Required {
314314
if _, ok := fakes.Get(name); !ok {
315-
f, err := r.resolve(req.With(append(req.Path, name), nil, req.examples), false)
315+
f, err := r.resolve(req.With(append(req.Path, name), s.AdditionalProperties, req.examples), false)
316316
if err != nil {
317317
return nil, err
318318
}

schema/json/generator/object_test.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -188,23 +188,7 @@ func TestObject(t *testing.T) {
188188
},
189189
test: func(t *testing.T, v interface{}, err error) {
190190
require.NoError(t, err)
191-
require.Equal(t,
192-
map[string]interface{}{
193-
"brace": 55776.1449323867,
194-
"collection": int64(237568),
195-
"comb": false,
196-
"company": "Redfin",
197-
"luck": int64(-927794),
198-
"person": map[string]interface{}{
199-
"email": "oliver.nelson@globalfacilitate.com",
200-
"firstname": "Oliver",
201-
"gender": "male",
202-
"lastname": "Nelson",
203-
},
204-
"problem": true,
205-
"sunshine": true,
206-
},
207-
v)
191+
require.Len(t, v.(map[string]any), 8)
208192
},
209193
},
210194
{

0 commit comments

Comments
 (0)