* XML to JSON CDATA translation is NOT stripped

* Example, assume XML: `<s><![CDATA[  z > x  & x < y ]]></s>`
  * Previous bevavior:
    * JSON: {"s":"  z > x  & x < y "}
  * New behavior:
    * JSON: `{"s":"<![CDATA[  z > x  & x < y ]]>"}`
  * To keep old behavior, set `JSON_CDATA_STRIP` in clixon_custom.h
This commit is contained in:
Olof hagsand 2021-11-13 17:11:00 +01:00
parent 7976c11f11
commit 81da71ffd7
5 changed files with 61 additions and 16 deletions

View file

@ -49,6 +49,13 @@ Thanks netgate for providing the dispatcher code!
Users may have to change how they access the system
* XML to JSON CDATA translation is NOT stripped
* Example, assume XML: `<s><![CDATA[ z > x & x < y ]]></s>`
* Previous bevavior:
* JSON: {"s":" z > x & x < y "}
* New behavior:
* JSON: `{"s":"<![CDATA[ z > x & x < y ]]>"}`
* To keep old behavior, set `JSON_CDATA_STRIP` in clixon_custom.h
* New `clixon-config@2021-11-11.yang` revision
* Modified options:
* CLICON_CLI_GENMODEL_TYPE: added OC_COMPRESS enum

View file

@ -117,3 +117,14 @@
* As defined in draft-wwlh-netconf-list-pagination-00 using Yang metadata value [RFC7952]
*/
#undef LIST_PAGINATION_REMAINING
/*! XML CDATA encoding in payload is stripped when translating to JSON
* Example: XML: "<s><![CDATA[ z > x & x < y ]]></s>" is translated to
* JSON: {"s":" z > x & x < y "}
* If not set:
* JSON: {"s":"<![CDATA[ z > x & x < y ]]>"}`
* Clixon stripped CDATA in 5.3 and earlier versions.
* To keep this backward-compatible enable this option
* Consider removing this option after 5.4
*/
#undef JSON_CDATA_STRIP

View file

@ -256,9 +256,11 @@ json_str_escape_cdata(cbuf *cb,
{
int retval = -1;
int i;
int esc = 0; /* cdata escape */
size_t len;
#ifdef JSON_CDATA_STRIP
int esc = 0; /* cdata escape */
#endif
len = strlen(str);
for (i=0; i<len; i++)
switch (str[i]){
@ -271,6 +273,7 @@ json_str_escape_cdata(cbuf *cb,
case '\\':
cprintf(cb, "\\\\");
break;
#ifdef JSON_CDATA_STRIP
case '<':
if (!esc &&
strncmp(&str[i], "<![CDATA[", strlen("<![CDATA[")) == 0){
@ -289,6 +292,7 @@ json_str_escape_cdata(cbuf *cb,
else
cprintf(cb, "%c", str[i]);
break;
#endif /* JSON_CDATA_STRIP */
default: /* fall thru */
cprintf(cb, "%c", str[i]);
break;

View file

@ -134,12 +134,9 @@ JSON='{"data": {"a": [],"b": [{"name": 17},{"name": []},{"name": 99}]}}'
new "empty list followed by list again empty"
expecteofx "$clixon_util_json" 0 "$JSON" "<data><a/><b><name>17</name></b><b><name/></b><b><name>99</name></b></data>"
# XXX CDATA translation, should work but does not
if false; then
JSON='{"json:c": {"s": "<![CDATA[ z > x & x < y ]]>"}}'
JSON='{"json:c":{"s":"<![CDATA[ z > x & x < y ]]>"}}'
new "json parse cdata xml"
expecteofx "$clixon_util_json -j -y $fyang" 0 "$JSON" "$JSON"
fi
rm -rf $dir

View file

@ -2,11 +2,16 @@
# Test: XML parser tests and JSON translation
# @see https://www.w3.org/TR/2008/REC-xml-20081126
# https://www.w3.org/TR/2009/REC-xml-names-20091208
#PROG="valgrind --leak-check=full --show-leak-kinds=all ../util/clixon_util_xml"
# Note CDATA to JSON: in earlier versions, CDATA was stripped when converting to JSON
# but this has been changed so that the CDATA is a part of the payload, eg shows up also in
# JSON strings
# Magic line must be first in script (see README.md)
s="$_" ; . ./lib.sh || if [ "$s" = $0 ]; then exit 0; else return 0; fi
: ${clixon_util_xml:="clixon_util_xml"}
: ${clixon_util_json:="clixon_util_json"}
new "xml parse"
expecteof "$clixon_util_xml -o" 0 "<a><b/></a>" "^<a><b/></a>$"
@ -42,10 +47,10 @@ new "xml CDATA right square bracket: ]"
expecteofx "$clixon_util_xml -o" 0 "<a><![CDATA[]]]></a>" "<a><![CDATA[]]]></a>"
new "xml simple CDATA to json"
expecteofx "$clixon_util_xml -o -j" 0 '<a><![CDATA[a text]]></a>' '{"a":"a text"}'
new "xml complex CDATA"
XML=$(cat <<EOF
expecteofx "$clixon_util_xml -o -j" 0 '<a><![CDATA[a text]]></a>' '{"a":"<![CDATA[a text]]>"}'
# Example partly from https://www.w3resource.com/xml/attribute.php
# XML complex CDATA (with comments for debug):;
DUMMY=$(cat <<EOF
<a><description>An example of escaped CENDs</description>
<sometext><![CDATA[ They're saying "x < y" & that "z > y" so I guess that means that z > x ]]></sometext>
<!-- This text contains a CEND ]]> -->
@ -56,20 +61,41 @@ XML=$(cat <<EOF
and the ]> in the second CDATA block -->
<alternative><![CDATA[This text contains a CEND ]]]><![CDATA[]>]]></alternative>
</a>
EOF
)
# without comments
XML=$(cat <<EOF
<a><description>An example of escaped CENDs</description>
<sometext><![CDATA[ They're saying "x < y" & that "z > y" so I guess that means that z > x ]]></sometext>
<data><![CDATA[This text contains a CEND ]]]]><![CDATA[>]]></data>
<alternative><![CDATA[This text contains a CEND ]]]><![CDATA[]>]]></alternative>
</a>
EOF
)
XML=$(cat <<'EOF'
<a><description>An example of escaped CENDs</description><sometext><![CDATA[ They're saying "x < y" & that "z > y" so I guess that means that z > x ]]></sometext><data><![CDATA[This text contains a CEND ]]]]><![CDATA[>]]></data><alternative><![CDATA[This text contains a CEND ]]]><![CDATA[]>]]></alternative></a>
EOF
)
expecteof "$clixon_util_xml -o" 0 "$XML" "^<a><description>An example of escaped CENDs</description><sometext>
<![CDATA[ They're saying \"x < y\" & that \"z > y\" so I guess that means that z > x ]]>
</sometext><data><![CDATA[This text contains a CEND ]]]]><![CDATA[>]]></data><alternative><![CDATA[This text contains a CEND ]]]><![CDATA[]>]]></alternative></a>$"
new "complex CDATA xml to xml"
expecteof "$clixon_util_xml -o" 0 "$XML" "^$XML
$"
JSON=$(cat <<EOF
{"a":{"description":"An example of escaped CENDs","sometext":" They're saying \"x < y\" & that \"z > y\" so I guess that means that z > x ","data":"This text contains a CEND ]]>","alternative":"This text contains a CEND ]]>"}}
{"a":{"description":"An example of escaped CENDs","sometext":"<![CDATA[ They're saying \"x < y\" & that \"z > y\" so I guess that means that z > x ]]>","data":"<![CDATA[This text contains a CEND ]]]]><![CDATA[>]]>","alternative":"<![CDATA[This text contains a CEND ]]]><![CDATA[]>]]>"}}
EOF
)
new "xml complex CDATA to json"
new "complex CDATA xml to json"
expecteofx "$clixon_util_xml -oj" 0 "$XML" "$JSON"
# reverse
new "complex CDATA json to json"
expecteofx "$clixon_util_json -j" 0 "$JSON" "$JSON"
# reverse
new "complex CDATA json to xml"
expecteofx "$clixon_util_json" 0 "$JSON" "$XML"
XML=$(cat <<EOF
<message>Less than: &lt; , greater than: &gt; ampersand: &amp; </message>
EOF