Skip to content

Commit

Permalink
* [JSON leaf-list output single element leaf-list does not use array](#…
Browse files Browse the repository at this point in the history
  • Loading branch information
olofhagsand committed Nov 29, 2021
1 parent 339b744 commit f4b35cb
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 30 deletions.
19 changes: 7 additions & 12 deletions lib/src/clixon_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,7 @@ array_eval(cxobj *xprev,
else if (eqnext)
arraytype = FIRST_ARRAY;
else if ((ys = xml_spec(x)) != NULL) {
if (yang_keyword_get(ys) == Y_LIST
#if 0 /* XXX instead see special case in xml2json_encode_leafs */
|| yang_keyword_get(ys) == Y_LEAF_LIST
#endif
)
if (yang_keyword_get(ys) == Y_LIST || yang_keyword_get(ys) == Y_LEAF_LIST)
arraytype = SINGLE_ARRAY;
else
arraytype = NO_ARRAY;
Expand Down Expand Up @@ -601,20 +597,19 @@ xml2json_encode_leafs(cxobj *xb,
case CGV_UINT64:
case CGV_DEC64:
case CGV_BOOL:
#if 1 /* Special case */
if (yang_keyword_get(yp) == Y_LEAF_LIST
&& xml_child_nr_type(xml_parent(xp), CX_ELMNT) == 1)
cprintf(cb, "[%s]", body);
else
#endif
cprintf(cb, "%s", body);
quote = 0;
break;
case CGV_VOID:
/* special case YANG empty type */
if (body == NULL && strcmp(restype, "empty")==0){
quote = 0;
cprintf(cb, "[null]");
if (keyword == Y_LEAF)
cprintf(cb, "[null]");
else if (keyword == Y_LEAF_LIST && strcmp(restype, "empty") == 0)
cprintf(cb, "[null]");
else
cprintf(cb, "null");
}
break;
default:
Expand Down
31 changes: 18 additions & 13 deletions test/test_json_list.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@ module json{
prefix ex;
namespace "urn:example:clixon";
container c{
leaf-list l1{
type int32;
}
list l2{
key name;
leaf name{
type int32;
}
leaf value{
type string;
}
}
leaf-list l1{
type int32;
}
list l2{
key name;
leaf name{
type int32;
}
leaf value{
type string;
}
}
leaf extra{
type string;
}
}
}
EOF
Expand Down Expand Up @@ -58,6 +60,9 @@ testrun "two leaf-list" '{"json:c":{"l1":[1,2]}}' '<c xmlns="urn:example:clixon"

testrun "three leaf-list" '{"json:c":{"l1":[1,2,3]}}' '<c xmlns="urn:example:clixon"><l1>1</l1><l1>2</l1><l1>3</l1></c>'

testrun "multiple leaf-list" '{"json:c":{"l1":[1,2],"extra":"abc"}}' '<c xmlns="urn:example:clixon"><l1>1</l1><l1>2</l1><extra>abc</extra></c>'


testrun "one list" '{"json:c":{"l2":[{"name":1,"value":"x"}]}}' '<c xmlns="urn:example:clixon"><l2><name>1</name><value>x</value></l2></c>'

testrun "two list" '{"json:c":{"l2":[{"name":1,"value":"x"},{"name":2,"value":"y"}]}}' '<c xmlns="urn:example:clixon"><l2><name>1</name><value>x</value></l2><l2><name>2</name><value>y</value></l2></c>'
Expand Down
2 changes: 1 addition & 1 deletion test/test_json_null.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ expecteofx "$clixon_util_xml -ovJy $fyang" 0 "$JSON" "$XML"

# Leaf-lists single
XML='<leaf-lists xmlns="urn:example:clixon"><a></a><b></b></leaf-lists>'
JSON='{"json:leaf-lists":{"a":[null],"b":""}}'
JSON='{"json:leaf-lists":{"a":[[null]],"b":[""]}}'

new "leaf-list single xml to json"
expecteofx "$clixon_util_xml -ovjy $fyang" 0 "$XML" "$JSON"
Expand Down
4 changes: 2 additions & 2 deletions test/test_nacm_datanode_paths.sh
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ new "Add NACM read path rule XML"
expectpart "$(curl -u andy:bar $CURLOPTS -X POST $RCPROTO://localhost/restconf/data/ietf-netconf-acm:nacm -H 'Content-Type: application/yang-data+xml' -d '<rule-list xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-acm"><name>limited-acl</name><group>limited</group><rule><name>table</name><module-name>*</module-name><access-operations>read</access-operations><path xmlns:ex="urn:example:nacm">/ex:table</path><action>permit</action></rule></rule-list>')" 0 "HTTP/$HVER 201"

new "Read NACM rule"
expectpart "$(curl -u andy:bar $CURLOPTS -X GET $RCPROTO://localhost/restconf/data/ietf-netconf-acm:nacm/rule-list=limited-acl)" 0 "HTTP/$HVER 200" '{"ietf-netconf-acm:rule-list":\[{"name":"limited-acl","group":"limited","rule":\[{"name":"table","module-name":"\*","path":"/ex:table","access-operations":"read","action":"permit"}\]}\]}'
expectpart "$(curl -u andy:bar $CURLOPTS -X GET $RCPROTO://localhost/restconf/data/ietf-netconf-acm:nacm/rule-list=limited-acl)" 0 "HTTP/$HVER 200" '{"ietf-netconf-acm:rule-list":\[{"name":"limited-acl","group":\["limited"\],"rule":\[{"name":"table","module-name":"\*","path":"/ex:table","access-operations":"read","action":"permit"}\]}\]}'

new "limit read OK"
expectpart "$(curl -u wilma:bar $CURLOPTS -X GET $RCPROTO://localhost/restconf/data/nacm-example:table/parameters/parameter=a)" 0 "HTTP/$HVER 200" '{"nacm-example:parameter":\[{"name":"a","value":"72"}\]}'
Expand All @@ -141,7 +141,7 @@ new "Add NACM read path rule JSON"
expectpart "$(curl -u andy:bar $CURLOPTS -X POST $RCPROTO://localhost/restconf/data/ietf-netconf-acm:nacm -H 'Content-Type: application/yang-data+json' -d '{"ietf-netconf-acm:rule-list":[{"name":"limited-acl","group":"limited","rule":[{"name":"table","module-name":"*","path":"/ex:table","access-operations":"read","action":"permit"}]}]}')" 0 "HTTP/$HVER 201"

new "Read NACM rule"
expectpart "$(curl -u andy:bar $CURLOPTS -X GET $RCPROTO://localhost/restconf/data/ietf-netconf-acm:nacm/rule-list=limited-acl)" 0 "HTTP/$HVER 200" '{"ietf-netconf-acm:rule-list":\[{"name":"limited-acl","group":"limited","rule":\[{"name":"table","module-name":"\*","path":"/ex:table","access-operations":"read","action":"permit"}\]}\]}'
expectpart "$(curl -u andy:bar $CURLOPTS -X GET $RCPROTO://localhost/restconf/data/ietf-netconf-acm:nacm/rule-list=limited-acl)" 0 "HTTP/$HVER 200" '{"ietf-netconf-acm:rule-list":\[{"name":"limited-acl","group":\["limited"\],"rule":\[{"name":"table","module-name":"\*","path":"/ex:table","access-operations":"read","action":"permit"}\]}\]}'

new "limit read OK (Set rul w JSON)"
expectpart "$(curl -u wilma:bar $CURLOPTS -X GET $RCPROTO://localhost/restconf/data/nacm-example:table/parameters/parameter=a)" 0 "HTTP/$HVER 200" '{"nacm-example:parameter":\[{"name":"a","value":"72"}\]}'
Expand Down
4 changes: 2 additions & 2 deletions test/test_restconf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ function testrun()
fi

new "restconf get data type json"
expectpart "$(curl $CURLOPTS -X GET $proto://$addr/restconf/data/clixon-example:state/op=42)" 0 '{"clixon-example:op":"42"}'
expectpart "$(curl $CURLOPTS -X GET $proto://$addr/restconf/data/clixon-example:state/op=42)" 0 '{"clixon-example:op":\["42"\]}'

new "restconf get state operation"
# Cant get shell macros to work, inline matching from lib.sh
Expand All @@ -398,7 +398,7 @@ function testrun()
fi

new "restconf get state operation type json"
expectpart "$(curl $CURLOPTS -X GET $proto://$addr/restconf/data/clixon-example:state/op=42)" 0 '{"clixon-example:op":"42"}'
expectpart "$(curl $CURLOPTS -X GET $proto://$addr/restconf/data/clixon-example:state/op=42)" 0 '{"clixon-example:op":\["42"\]}'

new "restconf get state operation type xml"
# Cant get shell macros to work, inline matching from lib.sh
Expand Down

0 comments on commit f4b35cb

Please sign in to comment.