From 7d59ec1a3a2fc9a6bb96eeb06afa609e0b5665d7 Mon Sep 17 00:00:00 2001 From: Olof hagsand Date: Wed, 9 Jun 2021 11:44:28 +0200 Subject: [PATCH] * Fixed: [JSON parsing error for a specific input. #236](https://github.com/clicon/clixon/issues/236) * JSON empty list parse problems, eg `a:[]` --- CHANGELOG.md | 2 ++ lib/src/clixon_json_parse.y | 16 +++++----------- test/test_json.sh | 14 +++++++++++--- test/test_restconf.sh | 2 +- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 59c00e4d..b007ea0e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -97,6 +97,8 @@ Developers may need to change their code ### Corrected Bugs +* Fixed: [JSON parsing error for a specific input. #236](https://github.com/clicon/clixon/issues/236) + * JSON empty list parse problems, eg `a:[]` * Fixed: [restconf patch method unable to chage value to empty string #229](https://github.com/clicon/clixon/issues/229) * Fixed: [when condition error under augment in restconf #227](https://github.com/clicon/clixon/issues/227) * Fixed: [Using YANG union with decimal64 and string leads to regexp match fail #226](https://github.com/clicon/clixon/issues/226) diff --git a/lib/src/clixon_json_parse.y b/lib/src/clixon_json_parse.y index b203d921..d9db980c 100644 --- a/lib/src/clixon_json_parse.y +++ b/lib/src/clixon_json_parse.y @@ -209,6 +209,7 @@ json_current_new(clixon_json_yacc *jy, static int json_current_pop(clixon_json_yacc *jy) { + clicon_debug(2, "%s", __FUNCTION__); if (jy->jy_current) jy->jy_current = xml_parent(jy->jy_current); return 0; @@ -219,6 +220,7 @@ json_current_clone(clixon_json_yacc *jy) { cxobj *xn; + clicon_debug(2, "%s", __FUNCTION__); if (jy->jy_current == NULL){ return -1; } @@ -246,14 +248,6 @@ json_current_body(clixon_json_yacc *jy, return retval; } -static int -json_empty_list(clixon_json_yacc *jy) -{ - xml_rm(jy->jy_current); - jy->jy_current = NULL; - return 0; -} - %} %% @@ -284,15 +278,15 @@ objlist : pair { _PARSE_DEBUG("objlist->pair");} ; pair : string { json_current_new(_JY, $1);free($1);} ':' - value { json_current_pop(_JY);}{ _PARSE_DEBUG("pair->string : value");} + value { json_current_pop(_JY);}{ _PARSE_DEBUG("pair->string : value");} ; -array : '[' ']' { json_empty_list(_JY); _PARSE_DEBUG("array->[]"); } +array : '[' ']' { _PARSE_DEBUG("array->[]"); } | '[' valuelist ']' { _PARSE_DEBUG("array->[ valuelist ]"); } ; valuelist : value { _PARSE_DEBUG("valuelist->value"); } - | valuelist { if (json_current_clone(_JY)< 0) _YYERROR("stack?");} + | valuelist { if (json_current_clone(_JY)< 0) _YYERROR("stack?");} ',' value { _PARSE_DEBUG("valuelist->valuelist , value");} ; diff --git a/test/test_json.sh b/test/test_json.sh index 3cf33ca8..d40b9477 100755 --- a/test/test_json.sh +++ b/test/test_json.sh @@ -64,8 +64,7 @@ expecteofx "$clixon_util_json" 0 '{"a":[0,1,2,3]}' "0123 # See test_restconf.sh new "json parse empty list to xml" -expecteofx "$clixon_util_json" 0 '{"a":[]}' " -" # XXX empty +expecteofx "$clixon_util_json" 0 '{"a":[]}' "" new "json parse list json" # should be {"a":[0,1,2,3]} expecteofx "$clixon_util_json -j" 0 '{"a":[0,1,2,3]}' '{"a":"0"}{"a":"1"}{"a":"2"}{"a":"3"}' @@ -126,7 +125,16 @@ expecteofx "$clixon_util_json -jy $fyang" 0 "$JSON" '{"json:g2":"blues"}' new "xml indirect identity with explicit ns to json" expecteofx "$clixon_util_xml -ojvy $fyang" 0 'ex:blues' '{"json:g2":"blues"}' -# XXX CDATA translation, should work bit does not +# See https://github.com/clicon/clixon/issues/236 +JSON='{"data": {"a": [],"b": [{"name": 17},{"name": 42},{"name": 99}]}}' +new "empty list followed by list" +expecteofx "$clixon_util_json" 0 "$JSON" "174299" + +JSON='{"data": {"a": [],"b": [{"name": 17},{"name": []},{"name": 99}]}}' +new "empty list followed by list again empty" +expecteofx "$clixon_util_json" 0 "$JSON" "1799" + +# XXX CDATA translation, should work but does not if false; then JSON='{"json:c": {"s": " x & x < y ]]>"}}' new "json parse cdata xml" diff --git a/test/test_restconf.sh b/test/test_restconf.sh index 90f31067..6ba4b22d 100755 --- a/test/test_restconf.sh +++ b/test/test_restconf.sh @@ -314,7 +314,7 @@ function testrun() # See test_json.sh new "restconf empty list" - expectpart "$(curl $CURLOPTS -X POST -H "Accept: application/yang-data+json" -H "Content-Type: application/yang-data+json" -d '{"clixon-example:table":{"parameter":[]}}' $proto://$addr/restconf/data)" 0 "HTTP/$HVER 201" "Location: $proto://$addr/restconf/data/clixon-example:table" + expectpart "$(curl $CURLOPTS -X POST -H "Accept: application/yang-data+json" -H "Content-Type: application/yang-data+json" -d '{"clixon-example:table":[]}' $proto://$addr/restconf/data)" 0 "HTTP/$HVER 201" "Location: $proto://$addr/restconf/data/clixon-example:table" new "restconf Re-add subtree eth/0/0 which should give error" expectpart "$(curl $CURLOPTS -X POST -H "Content-Type: application/yang-data+json" -d '{"ietf-interfaces:interfaces":{"interface":{"name":"eth/0/0","type":"clixon-example:eth","enabled":true}}}' $proto://$addr/restconf/data)" 0 '{"ietf-restconf:errors":{"error":{"error-type":"application","error-tag":"data-exists","error-severity":"error","error-message":"Data already exists; cannot create new resource"}}}'