CESNET / libnetconf2

C NETCONF library
BSD 3-Clause "New" or "Revised" License
202 stars 146 forks source link

" Mandatory node "content-id" instance does not exist. (path: Schema location /ietf-yang-library:yang-library/content-id, line number 1.)" #329

Closed sterlite-del closed 3 years ago

sterlite-del commented 3 years ago

Hi,

I am executing the following program:

#include <libyang/libyang.h>

const char *my_module = "module my_module {"
    "yang-version 1.1;"
    "namespace \"http://example.com/\";"
    "prefix coze;"

    "leaf leafInt32 {"
        "description \"A 32-bit integer leaf.\";"
        "type int32;"
    "}"
"}";

const char *data = "{"
    "\"my_module:leafInt32\": 420"
"}";

int main()
{
    struct ly_ctx* ctx;
    struct lyd_node* node;
    ly_ctx_new(NULL, 0, &ctx);
    lys_parse_mem(ctx, my_module, LYS_IN_YANG, NULL);
    if (lyd_parse_data_mem(ctx, data, LYD_JSON, 0, 0, &node))
        printf("Failed!!!\n");
    ly_ctx_destroy(ctx);
}

On running the program I get the following output:

_sh$ ./a.out
libyang[0]: Mandatory node "content-id" instance does not exist. (path: Schema location /ietf-yang-library:yang-library/content-id, line number 1.)
Failed!!!
sh$_

How can we solve this issue so that JSON gets parsed correctly into the node ?

michalvasko commented 3 years ago

There are several ways of avoiding this error but most likely you want to use the flag LYD_VALIDATE_PRESENT when parsing the data.

sterlite-del commented 3 years ago

ok thanks, closing this issue.