Skip to content

Commit

Permalink
Fix: JSON schema or in option (#953)
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky authored Dec 23, 2022
1 parent c77a4fe commit 36e8265
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 3 deletions.
80 changes: 80 additions & 0 deletions internal/bcd/ast/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,86 @@ func TestTypedAst_ToJSONSchema(t *testing.T) {
},
},
},
}, {
name: "Case 18: or in option",
data: `{"prim":"option","args":[{"prim":"or","args":[{"prim":"or","args":[{"prim":"nat","annots":["%t0"]},{"prim":"address","annots":["%a0"]}]},{"prim":"nat","annots":["%z1"]}]}]}`,
want: &JSONSchema{
Type: JSONSchemaTypeObject,
Prim: consts.OPTION,
Title: "@option_1",
OneOf: []*JSONSchema{
{
Title: consts.None,
Properties: map[string]*JSONSchema{
"schemaKey": noneSchema,
},
},
{
Title: consts.Some,
Properties: map[string]*JSONSchema{
"@or_2": {
Title: "@or_2",
Prim: consts.OR,
Type: JSONSchemaTypeObject,
OneOf: []*JSONSchema{
{
Title: "t0",
Properties: map[string]*JSONSchema{
"schemaKey": {
Type: JSONSchemaTypeString,
Const: "LL",
},
"t0": {
Type: JSONSchemaTypeInt,
Prim: consts.NAT,
Title: "t0",
Default: 0,
},
},
}, {
Title: "a0",
Properties: map[string]*JSONSchema{
"schemaKey": {
Type: JSONSchemaTypeString,
Const: "LR",
},
"a0": {
Type: JSONSchemaTypeString,
Prim: consts.ADDRESS,
Title: "a0",
MinLength: 36,
MaxLength: 36,
Default: "",
},
},
}, {
Title: "z1",
Properties: map[string]*JSONSchema{
"schemaKey": {
Type: JSONSchemaTypeString,
Const: "R",
},
"z1": {
Type: JSONSchemaTypeInt,
Prim: consts.NAT,
Title: "z1",
Default: 0,
},
},
},
},
},
"schemaKey": {
Type: JSONSchemaTypeString,
Const: "Some",
},
},
},
},
Default: &JSONSchema{
SchemaKey: noneSchema,
},
},
},
}
for _, tt := range tests {
Expand Down
7 changes: 4 additions & 3 deletions internal/bcd/ast/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,11 @@ func (opt *Option) ToJSONSchema() (*JSONSchema, error) {
return nil, err
}

if len(child.Properties) > 0 {
if opt.Type.IsPrim(consts.PAIR) {
if len(child.Properties) > 0 || len(child.OneOf) > 0 {
switch opt.Type.GetPrim() {
case consts.PAIR, consts.OR:
someSchema.Properties[opt.Type.GetName()] = child
} else {
default:
for key, value := range child.Properties {
someSchema.Properties[key] = value
}
Expand Down

0 comments on commit 36e8265

Please sign in to comment.