Skip to content

Commit 2b4d156

Browse files
committed
printer json BUGFIX empty (leaf-)list print fixes
Tests included.
1 parent dcbbc00 commit 2b4d156

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/printer_json.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,9 @@ json_print_inner(struct jsonpr_ctx *pctx, const struct lyd_node *node)
734734
while (snode) {
735735
/* print an empty (leaf-)list */
736736
LY_CHECK_RET(json_print_leaf_list_empty(pctx, snode));
737+
pctx->level_printed = pctx->level;
737738

739+
/* next iter */
738740
snode = json_print_next_empty_leaf_list(pctx, snode->next, NULL);
739741
}
740742
}
@@ -1083,6 +1085,7 @@ static LY_ERR
10831085
json_print_node(struct jsonpr_ctx *pctx, const struct lyd_node *node)
10841086
{
10851087
const struct lysc_node *snode;
1088+
ly_bool last_inst = 0;
10861089

10871090
if (!lyd_node_should_print(node, pctx->options)) {
10881091
if (json_print_array_is_last_inst(pctx, node)) {
@@ -1118,6 +1121,13 @@ json_print_node(struct jsonpr_ctx *pctx, const struct lyd_node *node)
11181121
}
11191122

11201123
if (pctx->options & LYD_PRINT_EMPTY_LEAF_LIST) {
1124+
/* first check whether the last instance of a schema node has been printed */
1125+
if (!node->next || (node->schema != node->next->schema)) {
1126+
last_inst = 1;
1127+
}
1128+
}
1129+
1130+
if (last_inst) {
11211131
snode = json_print_next_empty_leaf_list(pctx, node->schema->next, node);
11221132
while (snode) {
11231133
/* print an empty (leaf-)list */

tests/utests/data/test_printer_json.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
/**
22
* @file test_printer_json.c
33
* @author: Radek Krejci <[email protected]>
4+
* @author Michal Vasko <[email protected]>
45
* @brief unit tests for functions from printer_yang.c
56
*
6-
* Copyright (c) 2019-2020 CESNET, z.s.p.o.
7+
* Copyright (c) 2019 - 2025 CESNET, z.s.p.o.
78
*
89
* This source code is licensed under BSD 3-Clause License (the "License").
910
* You may not use this file except in compliance with the License.
@@ -34,6 +35,12 @@ setup(void **state)
3435
" type string;"
3536
" }"
3637
" }"
38+
" list l2 {"
39+
" key \"k2\";"
40+
" leaf k2 {"
41+
" type string;"
42+
" }"
43+
" }"
3744
" }"
3845
" leaf-list ll {"
3946
" type string;"
@@ -96,7 +103,14 @@ test_empty_leaf_list(void **state)
96103
data = "{\"schema2:a\":{\"b\":{\"c\":\"val\"}}}";
97104
CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
98105
assert_int_equal(LY_SUCCESS, lyd_print_mem(&buffer, tree, LYD_JSON, LYD_PRINT_SHRINK | LYD_PRINT_EMPTY_LEAF_LIST));
99-
CHECK_STRING(buffer, "{\"schema2:a\":{\"b\":{\"c\":\"val\",\"l\":[]},\"ll\":[]},\"schema2:tl\":[]}");
106+
CHECK_STRING(buffer, "{\"schema2:a\":{\"b\":{\"c\":\"val\",\"l\":[],\"l2\":[]},\"ll\":[]},\"schema2:tl\":[]}");
107+
free(buffer);
108+
lyd_free_all(tree);
109+
110+
data = "{\"schema2:a\":{\"b\":{\"l\":[{\"k\":\"key1\"},{\"k\":\"key2\"}]}}}";
111+
CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
112+
assert_int_equal(LY_SUCCESS, lyd_print_mem(&buffer, tree, LYD_JSON, LYD_PRINT_SHRINK | LYD_PRINT_EMPTY_LEAF_LIST));
113+
CHECK_STRING(buffer, "{\"schema2:a\":{\"b\":{\"l\":[{\"k\":\"key1\"},{\"k\":\"key2\"}],\"l2\":[]},\"ll\":[]},\"schema2:tl\":[]}");
100114
free(buffer);
101115
lyd_free_all(tree);
102116

@@ -107,6 +121,8 @@ test_empty_leaf_list(void **state)
107121
" \"schema2:a\": {\n"
108122
" \"b\": {\n"
109123
" \"l\": [\n"
124+
" ],\n"
125+
" \"l2\": [\n"
110126
" ]\n"
111127
" },\n"
112128
" \"ll\": [\n"

0 commit comments

Comments
 (0)