From f4b35cb2706c0e4db08a9f6e467e2f00c8e42981 Mon Sep 17 00:00:00 2001 From: Olof hagsand Date: Mon, 29 Nov 2021 13:43:10 +0100 Subject: [PATCH] * [JSON leaf-list output single element leaf-list does not use array](https://github.com/clicon/clixon/issues/289) --- lib/src/clixon_json.c | 19 +++++++------------ test/test_json_list.sh | 31 ++++++++++++++++++------------- test/test_json_null.sh | 2 +- test/test_nacm_datanode_paths.sh | 4 ++-- test/test_restconf.sh | 4 ++-- 5 files changed, 30 insertions(+), 30 deletions(-) diff --git a/lib/src/clixon_json.c b/lib/src/clixon_json.c index 9c87d9efc..2d988347b 100644 --- a/lib/src/clixon_json.c +++ b/lib/src/clixon_json.c @@ -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; @@ -601,12 +597,6 @@ 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; @@ -614,7 +604,12 @@ xml2json_encode_leafs(cxobj *xb, /* 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: diff --git a/test/test_json_list.sh b/test/test_json_list.sh index 1283ed423..c03a671d0 100755 --- a/test/test_json_list.sh +++ b/test/test_json_list.sh @@ -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 @@ -58,6 +60,9 @@ testrun "two leaf-list" '{"json:c":{"l1":[1,2]}}' '123' +testrun "multiple leaf-list" '{"json:c":{"l1":[1,2],"extra":"abc"}}' '12abc' + + testrun "one list" '{"json:c":{"l2":[{"name":1,"value":"x"}]}}' '1x' testrun "two list" '{"json:c":{"l2":[{"name":1,"value":"x"},{"name":2,"value":"y"}]}}' '1x2y' diff --git a/test/test_json_null.sh b/test/test_json_null.sh index f66751070..b402ecd13 100755 --- a/test/test_json_null.sh +++ b/test/test_json_null.sh @@ -71,7 +71,7 @@ expecteofx "$clixon_util_xml -ovJy $fyang" 0 "$JSON" "$XML" # Leaf-lists single XML='' -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" diff --git a/test/test_nacm_datanode_paths.sh b/test/test_nacm_datanode_paths.sh index 776f660e5..cb163e9c5 100755 --- a/test/test_nacm_datanode_paths.sh +++ b/test/test_nacm_datanode_paths.sh @@ -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 'limited-acllimitedtable*read/ex:tablepermit')" 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"}\]}' @@ -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"}\]}' diff --git a/test/test_restconf.sh b/test/test_restconf.sh index 13baa5a99..e13d6222c 100755 --- a/test/test_restconf.sh +++ b/test/test_restconf.sh @@ -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 @@ -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