From 0c81b66ef36ba1d97119a915e5e82b604d361197 Mon Sep 17 00:00:00 2001 From: Olof hagsand Date: Thu, 12 May 2022 12:04:13 +0200 Subject: [PATCH] Fixed: [Returning a string while Querying leaf-list for single entry](https://github.com/clicon/clixon/issues/326) --- CHANGELOG.md | 1 + lib/src/clixon_json.c | 22 ++++++++++++++++++---- test/test_autocli_editmode.sh | 2 +- test/test_json_list.sh | 26 ++++++++++++++++++++++++++ util/clixon_util_json.c | 10 +++++++++- util/clixon_util_xml.c | 13 +++++++++++-- 6 files changed, 66 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fb48647d..647b207e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -123,6 +123,7 @@ Users may have to change how they access the system ### Corrected Bugs +* Fixed: [Returning a string while Querying leaf-list for single entry](https://github.com/clicon/clixon/issues/326) * Fixed: A long TLS+HTTP/2 request such as by a browser causing block of other requests. * Fixed: [Error message seen twice in some cases](https://github.com/clicon/clixon/issues/325) * Fixed: [if choice is declared with multiple elements or leaf-list with in a case scope , addition or updation is not happening as expected](https://github.com/clicon/clixon/issues/327) diff --git a/lib/src/clixon_json.c b/lib/src/clixon_json.c index 592f6e7c..d394a9ab 100644 --- a/lib/src/clixon_json.c +++ b/lib/src/clixon_json.c @@ -1062,15 +1062,29 @@ xml2json_cbuf(cbuf *cb, cxobj *x, int pretty) { - int retval = 1; - int level = 0; - + int retval = 1; + int level = 0; + yang_stmt *y; + enum array_element_type arraytype = NO_ARRAY; + cprintf(cb, "%*s{%s", pretty?level*JSON_INDENT:0,"", pretty?"\n":""); + + if ((y = xml_spec(x)) != NULL){ + switch (yang_keyword_get(y)){ + case Y_LEAF_LIST: + case Y_LIST: + arraytype = SINGLE_ARRAY; + break; + default: + arraytype = NO_ARRAY; + break; + } + } if (xml2json1_cbuf(cb, x, - NO_ARRAY, + arraytype, level+1, pretty, 0, diff --git a/test/test_autocli_editmode.sh b/test/test_autocli_editmode.sh index 062d660d..feb659c3 100755 --- a/test/test_autocli_editmode.sh +++ b/test/test_autocli_editmode.sh @@ -293,7 +293,7 @@ edit table show config json EOF new "show config json" -expectpart "$(cat $fin | $clixon_cli -f $cfg 2>&1)" 0 '{"clixon-example:parameter":{"name":"a","value":"42"}}' +expectpart "$(cat $fin | $clixon_cli -f $cfg 2>&1)" 0 '{"clixon-example:parameter":\[{"name":"a","value":"42"}\]}' cat < $fin edit table diff --git a/test/test_json_list.sh b/test/test_json_list.sh index c03a671d..48d4d38c 100755 --- a/test/test_json_list.sh +++ b/test/test_json_list.sh @@ -29,6 +29,20 @@ module json{ type string; } } + leaf-list l1{ + description "top-level"; + type int32; + } + list l2{ + description "top-level"; + key name; + leaf name{ + type int32; + } + leaf value{ + type string; + } + } } EOF @@ -54,6 +68,14 @@ function testrun() new "test params: -y $fyang" +testrun "top one leaf-list" '{"json:l1":[1]}' '1' + +# XXX There is a problem with how clixon shows json and xml on top-level +# Typically, there is a loop over xml children to cope with the case if there is more +# than one top-level which is invalid in XML. +# See for example clixon_util_xml.c, clixon_util_json.c, cli_auto_show, cli_show_config +#testrun "top two leaf-list" '{"json:l1":[1,2]}' '12' + testrun "one leaf-list" '{"json:c":{"l1":[1]}}' '1' testrun "two leaf-list" '{"json:c":{"l1":[1,2]}}' '12' @@ -62,6 +84,10 @@ testrun "three leaf-list" '{"json:c":{"l1":[1,2,3]}}' '12abc' +testrun "top one list" '{"json:l2":[{"name":1,"value":"x"}]}' '1x' + +# XXX +#testrun "top two list" '{"json:l2":[{"name":1,"value":"x"},{"name":2,"value":"x"}]}' '1x2x' testrun "one list" '{"json:c":{"l2":[{"name":1,"value":"x"}]}}' '1x' diff --git a/util/clixon_util_json.c b/util/clixon_util_json.c index 269591b3..aae656bc 100644 --- a/util/clixon_util_json.c +++ b/util/clixon_util_json.c @@ -145,12 +145,20 @@ main(int argc, xml_print(stderr, xerr); goto done; } +#if 0 + if (json) + xml2json_cbuf(cb, xt, pretty); /* print json */ + else + clicon_xml2cbuf(cb, xt, 0, pretty, -1); /* print xml */ +#else xc = NULL; + /* XXX This is troublesome for JSON top-level lists */ while ((xc = xml_child_each(xt, xc, -1)) != NULL) if (json) - xml2json_cbuf(cb, xc, pretty); /* print xml */ + xml2json_cbuf(cb, xc, pretty); /* print json */ else clicon_xml2cbuf(cb, xc, 0, pretty, -1); /* print xml */ +#endif fprintf(stdout, "%s", cbuf_get(cb)); fflush(stdout); retval = 0; diff --git a/util/clixon_util_xml.c b/util/clixon_util_xml.c index 72f8bbb5..8845f86d 100644 --- a/util/clixon_util_xml.c +++ b/util/clixon_util_xml.c @@ -337,12 +337,21 @@ main(int argc, } /* 4. Output data (xml/json) */ if (output){ +#if 0 + if (jsonout) + xml2json_cbuf(cb, xt, pretty); /* print json */ + else + clicon_xml2cbuf(cb, xt, 0, pretty, -1); /* print xml */ +#else xc = NULL; - while ((xc = xml_child_each(xt, xc, -1)) != NULL) + /* XXX This is troublesome for JSON top-level lists */ + while ((xc = xml_child_each(xt, xc, -1)) != NULL){ if (jsonout) - xml2json_cbuf(cb, xc, pretty); /* print xml */ + xml2json_cbuf(cb, xc, pretty); /* print json */ else clicon_xml2cbuf(cb, xc, 0, pretty, -1); /* print xml */ + } +#endif fprintf(stdout, "%s", cbuf_get(cb)); fflush(stdout); }