Skip to content

Commit

Permalink
out BUGFIX trim printing NP conts with only explicit dflt values
Browse files Browse the repository at this point in the history
  • Loading branch information
michalvasko committed Apr 25, 2024
1 parent 4e8c0e5 commit ee3539a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/out.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ lyd_node_should_print(const struct lyd_node *node, uint32_t options)
/* explicit default node */
return 0;
}
} else if (lysc_is_np_cont(node->schema)) {
if (options & LYD_PRINT_KEEPEMPTYCONT) {
/* explicit request to print, redundant to check */
return 1;
}

LY_LIST_FOR(lyd_child(node), elem) {
if (lyd_node_should_print(elem, options)) {
return 1;
}
}

/* NP container without any printed children (such as other NP containers with only nodes set to their default values) */
return 0;
}
} else if ((node->flags & LYD_DEFAULT) && (node->schema->nodetype == LYS_CONTAINER)) {
if (options & LYD_PRINT_KEEPEMPTYCONT) {
Expand Down
33 changes: 32 additions & 1 deletion tests/utests/data/test_printer_json.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/**
* @file test_printer_json.c
* @author: Radek Krejci <[email protected]>
* @brief unit tests for functions from printer_yang.c
Expand All @@ -21,9 +21,20 @@ setup(void **state)
"revision 2014-05-08;"
"anydata data;"
"}";
const char *schema2 = "module schema2 {namespace urn:tests:schema2;prefix s2;yang-version 1.1;"
" container a {"
" container b {"
" leaf c {"
" type string;"
" default \"dflt\";"
" }"
" }"
" }"
"}";

UTEST_SETUP;
UTEST_ADD_MODULE(schema1, LYS_IN_YANG, NULL, NULL);
UTEST_ADD_MODULE(schema2, LYS_IN_YANG, NULL, NULL);
return 0;
}

Expand All @@ -41,11 +52,31 @@ test_container_presence(void **state)
lyd_free_all(tree);
}

static void
test_empty_container_wd_trim(void **state)
{
struct lyd_node *tree;
char *buffer = NULL;
const char *data = "{\"schema2:a\":{\"b\":{\"c\":\"dflt\"}}}";

CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
assert_int_equal(LY_SUCCESS, lyd_print_mem(&buffer, tree, LYD_JSON, LYD_PRINT_SHRINK | LYD_PRINT_WD_TRIM));
CHECK_STRING(buffer, "{}");
free(buffer);

assert_int_equal(LY_SUCCESS, lyd_print_mem(&buffer, tree, LYD_JSON, LYD_PRINT_SHRINK | LYD_PRINT_WD_TRIM | LYD_PRINT_KEEPEMPTYCONT));
CHECK_STRING(buffer, "{\"schema2:a\":{\"b\":{}}}");
free(buffer);

lyd_free_all(tree);
}

int
main(void)
{
const struct CMUnitTest tests[] = {
UTEST(test_container_presence, setup),
UTEST(test_empty_container_wd_trim, setup),
};

return cmocka_run_group_tests(tests, NULL, NULL);
Expand Down

0 comments on commit ee3539a

Please sign in to comment.