Skip to content

Commit 7b2c967

Browse files
committed
fix parsing JavaScript shared value
fix parsing struct with private fields
1 parent 91900fc commit 7b2c967

3 files changed

Lines changed: 63 additions & 1 deletion

File tree

js/mokapi/shared.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,8 @@ func (p *SharedValue) ToValue() goja.Value {
200200
return p.source
201201
}
202202
}
203+
204+
// Export is used by the json schema parser interface Exportable
205+
func (p *SharedValue) Export() any {
206+
return p.source.Export()
207+
}

schema/json/parser/parser_object.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func (p *Parser) parseStruct(v reflect.Value, s *schema.Schema, evaluated map[st
143143
obj := sortedmap.NewLinkedHashMap()
144144
for i := 0; i < v.NumField(); i++ {
145145
ft := t.Field(i)
146-
if ft.PkgPath == "" {
146+
if ft.PkgPath != "" {
147147
continue
148148
}
149149
name := unTitle(ft.Name)

schema/json/parser/parser_object_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package parser_test
22

33
import (
4+
"mokapi/js/mokapi"
45
"mokapi/schema/json/parser"
56
"mokapi/schema/json/schema"
67
"mokapi/schema/json/schema/schematest"
78
"mokapi/sortedmap"
89
"testing"
910

11+
"github.com/dop251/goja"
1012
"github.com/stretchr/testify/require"
1113
)
1214

@@ -459,6 +461,61 @@ func TestParser_ParseObject(t *testing.T) {
459461
require.Equal(t, map[string]interface{}{"foo": "bar", "bar": "abc"}, v)
460462
},
461463
},
464+
{
465+
name: "unevaluatedProperties",
466+
schema: schematest.NewTypes(nil,
467+
schematest.WithAllOf(
468+
schematest.New("object",
469+
schematest.WithAllOf(schematest.New("object",
470+
schematest.WithProperty("city", schematest.New("string")),
471+
)),
472+
),
473+
),
474+
schematest.WithProperty("type", schematest.NewTypes(nil, schematest.WithEnumValues("residential", "business"))),
475+
schematest.WithRequired("type"),
476+
schematest.WithUnevaluatedProperties(schematest.NewBool(false)),
477+
),
478+
data: map[string]any{"type": "residential", "city": "Washington"},
479+
test: func(t *testing.T, v interface{}, err error) {
480+
require.NoError(t, err)
481+
},
482+
},
483+
{
484+
name: "parse from JavaScript shared value",
485+
schema: schematest.NewTypes(nil,
486+
schematest.WithProperty("type", schematest.NewTypes(nil, schematest.WithEnumValues("residential", "business"))),
487+
schematest.WithRequired("type"),
488+
),
489+
data: func() any {
490+
vm := goja.New()
491+
v := vm.ToValue(map[string]any{"type": "residential"})
492+
return mokapi.NewSharedValue(v, vm)
493+
}(),
494+
test: func(t *testing.T, v interface{}, err error) {
495+
require.NoError(t, err)
496+
require.Equal(t, map[string]interface{}{"type": "residential"}, v)
497+
},
498+
},
499+
{
500+
name: "parse struct with private fields",
501+
schema: schematest.NewTypes(nil,
502+
schematest.WithProperty("bar", schematest.New("string")),
503+
schematest.WithRequired("bar"),
504+
),
505+
data: func() any {
506+
return &struct {
507+
foo string
508+
Bar string
509+
}{
510+
foo: "bar",
511+
Bar: "abc",
512+
}
513+
}(),
514+
test: func(t *testing.T, v interface{}, err error) {
515+
require.NoError(t, err)
516+
require.Equal(t, map[string]interface{}{"bar": "abc"}, v)
517+
},
518+
},
462519
}
463520

464521
t.Parallel()

0 commit comments

Comments
 (0)