blob: 1f463aa1b00abaa4fa0e0f8787ddf95f40cf283c [file] [log] [blame]
Michal Vasko90932a92020-02-12 14:33:03 +01001/**
2 * @file parser_json.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief JSON data parser for libyang
5 *
Radek Krejci1798aae2020-07-14 13:26:06 +02006 * Copyright (c) 2020 CESNET, z.s.p.o.
Michal Vasko90932a92020-02-12 14:33:03 +01007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
Michal Vasko90932a92020-02-12 14:33:03 +010016
Radek Krejci1798aae2020-07-14 13:26:06 +020017#include <assert.h>
Radek Krejci47fab892020-11-05 17:02:41 +010018#include <stdint.h>
Michal Vasko90932a92020-02-12 14:33:03 +010019#include <stdlib.h>
20#include <string.h>
21
Radek Krejci535ea9f2020-05-29 16:01:05 +020022#include "common.h"
aPiecek49e2a3a2021-05-13 10:36:46 +020023#include "compat.h"
Michal Vasko90932a92020-02-12 14:33:03 +010024#include "context.h"
Radek Krejci47fab892020-11-05 17:02:41 +010025#include "dict.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020026#include "in_internal.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020027#include "json.h"
Radek Krejci47fab892020-11-05 17:02:41 +010028#include "log.h"
Radek Krejci77114102021-03-10 15:21:57 +010029#include "parser_data.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020030#include "parser_internal.h"
Radek Krejci47fab892020-11-05 17:02:41 +010031#include "set.h"
32#include "tree.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020033#include "tree_data.h"
34#include "tree_data_internal.h"
35#include "tree_schema.h"
Radek Krejci47fab892020-11-05 17:02:41 +010036#include "tree_schema_internal.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020037#include "validation.h"
Michal Vasko90932a92020-02-12 14:33:03 +010038
39/**
Radek Krejci1798aae2020-07-14 13:26:06 +020040 * @brief Internal context for JSON YANG data parser.
41 *
42 * Note that the structure maps to the lyd_ctx which is common for all the data parsers
43 */
44struct lyd_json_ctx {
Radek Krejcif16e2542021-02-17 15:39:23 +010045 const struct lysc_ext_instance *ext; /**< extension instance possibly changing document root context of the data being parsed */
Michal Vaskoe0665742021-02-11 11:08:44 +010046 uint32_t parse_opts; /**< various @ref dataparseroptions. */
47 uint32_t val_opts; /**< various @ref datavalidationoptions. */
Radek Krejci1798aae2020-07-14 13:26:06 +020048 uint32_t int_opts; /**< internal data parser options */
49 uint32_t path_len; /**< used bytes in the path buffer */
50 char path[LYD_PARSER_BUFSIZE]; /**< buffer for the generated path */
Michal Vaskoc43c8ab2021-03-05 13:32:44 +010051 struct ly_set node_when; /**< set of nodes with "when" conditions */
Radek Krejci4f2e3e52021-03-30 14:20:28 +020052 struct ly_set node_exts; /**< set of nodes and extensions connected with a plugin providing own validation callback */
Michal Vasko49c39d82020-11-06 17:20:27 +010053 struct ly_set node_types; /**< set of nodes validated with LY_EINCOMPLETE result */
54 struct ly_set meta_types; /**< set of metadata validated with LY_EINCOMPLETE result */
Radek Krejci1798aae2020-07-14 13:26:06 +020055 struct lyd_node *op_node; /**< if an RPC/action/notification is being parsed, store the pointer to it */
56
57 /* callbacks */
58 lyd_ctx_free_clb free; /* destructor */
Radek Krejci1798aae2020-07-14 13:26:06 +020059
60 struct lyjson_ctx *jsonctx; /**< JSON context */
61};
62
63/**
64 * @brief Free the JSON data parser context.
65 *
66 * JSON implementation of lyd_ctx_free_clb().
67 */
68static void
69lyd_json_ctx_free(struct lyd_ctx *lydctx)
70{
71 struct lyd_json_ctx *ctx = (struct lyd_json_ctx *)lydctx;
72
73 if (lydctx) {
74 lyd_ctx_free(lydctx);
75 lyjson_ctx_free(ctx->jsonctx);
76 free(ctx);
77 }
78}
79
80/**
Radek Krejci1798aae2020-07-14 13:26:06 +020081 * @brief Parse JSON member-name as [\@][prefix:][name]
82 *
Michal Vaskocabe0702020-08-12 10:14:36 +020083 * \@ - metadata flag, maps to 1 in @p is_meta_p
Radek Krejci1798aae2020-07-14 13:26:06 +020084 * prefix - name of the module of the data node
85 * name - name of the data node
86 *
87 * All the output parameter are mandatory. Function only parse the member-name, all the appropriate checks are up to the caller.
88 *
89 * @param[in] value String to parse
90 * @param[in] value_len Length of the @p str.
91 * @param[out] name_p Pointer to the beginning of the parsed name.
92 * @param[out] name_len_p Pointer to the length of the parsed name.
93 * @param[out] prefix_p Pointer to the beginning of the parsed prefix. If the member-name does not contain prefix, result is NULL.
94 * @param[out] prefix_len_p Pointer to the length of the parsed prefix. If the member-name does not contain prefix, result is 0.
Michal Vaskocabe0702020-08-12 10:14:36 +020095 * @param[out] is_meta_p Pointer to the metadata flag, set to 1 if the member-name contains \@, 0 otherwise.
Radek Krejci1798aae2020-07-14 13:26:06 +020096 */
97static void
Michal Vaskocabe0702020-08-12 10:14:36 +020098lydjson_parse_name(const char *value, size_t value_len, const char **name_p, size_t *name_len_p, const char **prefix_p,
Radek Krejci857189e2020-09-01 13:26:36 +020099 size_t *prefix_len_p, ly_bool *is_meta_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200100{
101 const char *name, *prefix = NULL;
102 size_t name_len, prefix_len = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200103 ly_bool is_meta = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200104
105 name = memchr(value, ':', value_len);
106 if (name != NULL) {
107 prefix = value;
108 if (*prefix == '@') {
Michal Vaskocabe0702020-08-12 10:14:36 +0200109 is_meta = 1;
Radek Krejci1798aae2020-07-14 13:26:06 +0200110 prefix++;
111 }
112 prefix_len = name - prefix;
113 name++;
Michal Vaskocabe0702020-08-12 10:14:36 +0200114 name_len = value_len - (prefix_len + 1) - is_meta;
Radek Krejci1798aae2020-07-14 13:26:06 +0200115 } else {
116 name = value;
117 if (name[0] == '@') {
Michal Vaskocabe0702020-08-12 10:14:36 +0200118 is_meta = 1;
Radek Krejci1798aae2020-07-14 13:26:06 +0200119 name++;
120 }
Michal Vaskocabe0702020-08-12 10:14:36 +0200121 name_len = value_len - is_meta;
Michal Vasko90932a92020-02-12 14:33:03 +0100122 }
123
Radek Krejci1798aae2020-07-14 13:26:06 +0200124 *name_p = name;
125 *name_len_p = name_len;
126 *prefix_p = prefix;
127 *prefix_len_p = prefix_len;
Michal Vaskocabe0702020-08-12 10:14:36 +0200128 *is_meta_p = is_meta;
Radek Krejci1798aae2020-07-14 13:26:06 +0200129}
130
131/**
132 * @brief Get correct prefix (module_name) inside the @p node.
133 *
134 * @param[in] node Data node to get inherited prefix.
135 * @param[in] local_prefix Local prefix to replace the inherited prefix.
136 * @param[in] local_prefix_len Length of the @p local_prefix string. In case of 0, the inherited prefix is taken.
137 * @param[out] prefix_p Pointer to the resulting prefix string, Note that the result can be NULL in case of no local prefix
138 * and no context @p node to get inherited prefix.
139 * @param[out] prefix_len_p Pointer to the length of the resulting @p prefix_p string. Note that the result can be 0 in case
140 * of no local prefix and no context @p node to get inherited prefix.
141 * @return LY_ERR value.
142 */
143static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +0200144lydjson_get_node_prefix(struct lyd_node *node, const char *local_prefix, size_t local_prefix_len, const char **prefix_p,
Radek Krejci0f969882020-08-21 16:56:47 +0200145 size_t *prefix_len_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200146{
147 struct lyd_node_opaq *onode;
148 const char *module_name = NULL;
149
150 assert(prefix_p && prefix_len_p);
151
152 if (local_prefix_len) {
153 *prefix_p = local_prefix;
154 *prefix_len_p = local_prefix_len;
155 return LY_SUCCESS;
156 }
157
158 *prefix_p = NULL;
159 while (node) {
160 if (node->schema) {
161 *prefix_p = node->schema->module->name;
162 break;
163 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200164 onode = (struct lyd_node_opaq *)node;
Michal Vaskoad92b672020-11-12 13:11:31 +0100165 if (onode->name.module_name) {
166 *prefix_p = onode->name.module_name;
Radek Krejci1798aae2020-07-14 13:26:06 +0200167 break;
Michal Vaskoad92b672020-11-12 13:11:31 +0100168 } else if (onode->name.prefix) {
169 *prefix_p = onode->name.prefix;
Radek Krejci1798aae2020-07-14 13:26:06 +0200170 break;
171 }
Michal Vasko9e685082021-01-29 14:49:09 +0100172 node = lyd_parent(node);
Radek Krejci1798aae2020-07-14 13:26:06 +0200173 }
174 *prefix_len_p = ly_strlen(module_name);
175
176 return LY_SUCCESS;
177}
178
179/**
180 * @brief Get schema node corresponding to the input parameters.
181 *
182 * @param[in] lydctx JSON data parser context.
183 * @param[in] is_attr Flag if the reference to the node is an attribute, for logging only.
184 * @param[in] prefix Requested node's prefix (module name).
185 * @param[in] prefix_len Length of the @p prefix.
186 * @param[in] name Requested node's name.
187 * @param[in] name_len Length of the @p name.
Michal Vasko2552ea32020-12-08 15:32:34 +0100188 * @param[in] parent Parent of the node being processed, can be NULL in case of top-level.
Radek Krejci1798aae2020-07-14 13:26:06 +0200189 * @param[out] snode_p Pointer to the found schema node corresponding to the input parameters.
190 * @return LY_SUCCES on success, note that even in this case the returned value of @p snode_p can be NULL, so the data are expected to be parsed as opaq.
191 * @return LY_EVALID on failure, error message is logged
192 * @return LY_ENOT in case the input data are expected to be skipped
193 */
194static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200195lydjson_get_snode(const struct lyd_json_ctx *lydctx, ly_bool is_attr, const char *prefix, size_t prefix_len, const char *name,
Radek Krejci0f969882020-08-21 16:56:47 +0200196 size_t name_len, const struct lyd_node_inner *parent, const struct lysc_node **snode_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200197{
Radek Krejci2efc45b2020-12-22 16:25:44 +0100198 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +0200199 struct lys_module *mod = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100200 uint32_t getnext_opts = lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200201
202 /* init return value */
203 *snode_p = NULL;
204
Michal Vasko9e543402021-06-11 13:55:30 +0200205 LOG_LOCSET(NULL, parent ? &parent->node : NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100206
Radek Krejci1798aae2020-07-14 13:26:06 +0200207 /* get the element module */
Michal Vasko2552ea32020-12-08 15:32:34 +0100208 if (prefix_len) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200209 mod = ly_ctx_get_module_implemented2(lydctx->jsonctx->ctx, prefix, prefix_len);
210 } else if (parent) {
211 if (parent->schema) {
212 mod = parent->schema->module;
213 }
214 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100215 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "Top-level JSON object member \"%.*s\" must be namespace-qualified.",
Radek Krejci422afb12021-03-04 16:38:16 +0100216 (int)(is_attr ? name_len + 1 : name_len), is_attr ? name - 1 : name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100217 ret = LY_EVALID;
218 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +0200219 }
220 if (!mod) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100221 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci422afb12021-03-04 16:38:16 +0100222 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "No module named \"%.*s\" in the context.", (int)prefix_len, prefix);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100223 ret = LY_EVALID;
224 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +0200225 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100226 if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100227 ret = LY_ENOT;
228 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +0200229 }
230 }
231
Radek Krejci1798aae2020-07-14 13:26:06 +0200232 /* get the schema node */
233 if (mod && (!parent || parent->schema)) {
Radek Krejcif16e2542021-02-17 15:39:23 +0100234 if (!parent && lydctx->ext) {
Radek Krejciba05eab2021-03-10 13:19:29 +0100235 *snode_p = lysc_ext_find_node(lydctx->ext, mod, name, name_len, 0, getnext_opts);
Radek Krejcif16e2542021-02-17 15:39:23 +0100236 } else {
237 *snode_p = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, getnext_opts);
238 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200239 if (!*snode_p) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100240 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejcif16e2542021-02-17 15:39:23 +0100241 if (lydctx->ext) {
242 if (lydctx->ext->argument) {
243 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" %s extension instance.",
Radek Krejci422afb12021-03-04 16:38:16 +0100244 (int)name_len, name, lydctx->ext->argument, lydctx->ext->def->name);
Radek Krejcif16e2542021-02-17 15:39:23 +0100245 } else {
246 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the %s extension instance.",
Radek Krejci422afb12021-03-04 16:38:16 +0100247 (int)name_len, name, lydctx->ext->def->name);
Radek Krejcif16e2542021-02-17 15:39:23 +0100248 }
249 } else {
250 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.",
Radek Krejci422afb12021-03-04 16:38:16 +0100251 (int)name_len, name, mod->name);
Radek Krejcif16e2542021-02-17 15:39:23 +0100252 }
Radek Krejci2efc45b2020-12-22 16:25:44 +0100253 ret = LY_EVALID;
254 goto cleanup;
Michal Vaskoe0665742021-02-11 11:08:44 +0100255 } else if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200256 /* skip element with children */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100257 ret = LY_ENOT;
258 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +0200259 }
260 } else {
261 /* check that schema node is valid and can be used */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100262 ret = lyd_parser_check_schema((struct lyd_ctx *)lydctx, *snode_p);
Radek Krejci1798aae2020-07-14 13:26:06 +0200263 }
264 }
265
Radek Krejci2efc45b2020-12-22 16:25:44 +0100266cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +0100267 LOG_LOCBACK(0, parent ? 1 : 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100268 return ret;
Radek Krejci1798aae2020-07-14 13:26:06 +0200269}
270
271/**
272 * @brief Skip the currently open JSON object/array
273 * @param[in] jsonctx JSON context with the input data to skip.
274 * @return LY_ERR value.
275 */
276static LY_ERR
277lydjson_data_skip(struct lyjson_ctx *jsonctx)
278{
279 enum LYJSON_PARSER_STATUS status, current;
280 size_t sublevels = 1;
281
282 status = lyjson_ctx_status(jsonctx, 0);
283
284 /* skip after the content */
285 do {
286 LY_CHECK_RET(lyjson_ctx_next(jsonctx, &current));
287 if (current == status) {
288 sublevels++;
289 } else if (current == status + 1) {
290 sublevels--;
291 }
Radek Krejci79856c42020-11-19 11:44:08 +0100292 } while (current != status + 1 || sublevels);
Radek Krejci1798aae2020-07-14 13:26:06 +0200293 /* open the next sibling */
294 LY_CHECK_RET(lyjson_ctx_next(jsonctx, NULL));
295
296 return LY_SUCCESS;
297}
298
299/**
Radek Krejci1798aae2020-07-14 13:26:06 +0200300 * @brief Check that the input data are parseable as the @p list.
301 *
302 * Checks for all the list's keys. Function does not revert the context state.
303 *
304 * @param[in] jsonctx JSON parser context.
305 * @param[in] list List schema node corresponding to the input data object.
306 * @return LY_SUCCESS in case the data are ok for the @p list
307 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance.
308 */
309static LY_ERR
310lydjson_check_list(struct lyjson_ctx *jsonctx, const struct lysc_node *list)
311{
312 LY_ERR ret = LY_SUCCESS;
313 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(jsonctx, 0);
314 struct ly_set key_set = {0};
315 const struct lysc_node *snode;
316 uint32_t i, status_count;
317
318 assert(list && (list->nodetype == LYS_LIST));
319 assert(status == LYJSON_OBJECT);
320
321 /* get all keys into a set (keys do not have if-features or anything) */
322 snode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100323 while ((snode = lys_getnext(snode, list, NULL, 0)) && (snode->flags & LYS_KEY)) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200324 ret = ly_set_add(&key_set, (void *)snode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200325 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200326 }
327
328 if (status != LYJSON_OBJECT_EMPTY) {
329 status_count = jsonctx->status.count;
330
331 while (key_set.count && status != LYJSON_OBJECT_CLOSED) {
332 const char *name, *prefix;
333 size_t name_len, prefix_len;
Radek Krejci857189e2020-09-01 13:26:36 +0200334 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200335
336 /* match the key */
337 snode = NULL;
338 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
339
340 if (!is_attr && !prefix) {
341 for (i = 0; i < key_set.count; ++i) {
342 snode = (const struct lysc_node *)key_set.objs[i];
343 if (!ly_strncmp(snode->name, name, name_len)) {
344 break;
345 }
346 }
347 /* go into the item to a) process it as a key or b) start skipping it as another list child */
348 ret = lyjson_ctx_next(jsonctx, &status);
349 LY_CHECK_GOTO(ret, cleanup);
350
351 if (snode) {
352 /* we have the key, validate the value */
353 if (status < LYJSON_NUMBER) {
354 /* not a terminal */
355 ret = LY_ENOT;
356 goto cleanup;
357 }
358
Radek Krejci8df109d2021-04-23 12:19:08 +0200359 ret = lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_VALUE_JSON, NULL);
Radek Krejci1798aae2020-07-14 13:26:06 +0200360 LY_CHECK_GOTO(ret, cleanup);
361
362 /* key with a valid value, remove from the set */
363 ly_set_rm_index(&key_set, i, NULL);
364 }
365 } else {
366 /* start skipping the member we are not interested in */
367 ret = lyjson_ctx_next(jsonctx, &status);
368 LY_CHECK_GOTO(ret, cleanup);
369 }
370 /* move to the next child */
371 while (status_count < jsonctx->status.count) {
372 ret = lyjson_ctx_next(jsonctx, &status);
373 LY_CHECK_GOTO(ret, cleanup);
374 }
375 }
376 }
377
378 if (key_set.count) {
379 /* some keys are missing/did not validate */
380 ret = LY_ENOT;
381 }
382
383cleanup:
384 ly_set_erase(&key_set, NULL);
385 return ret;
386}
387
388/**
389 * @brief Get the hint for the data type parsers according to the current JSON parser context.
390 *
391 * @param[in] lydctx JSON data parser context. The context is supposed to be on a value.
392 * @param[in,out] status Pointer to the current context status,
393 * in some circumstances the function manipulates with the context so the status is updated.
394 * @param[out] type_hint_p Pointer to the variable to store the result.
395 * @return LY_SUCCESS in case of success.
396 * @return LY_EINVAL in case of invalid context status not referring to a value.
397 */
398static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200399lydjson_value_type_hint(struct lyd_json_ctx *lydctx, enum LYJSON_PARSER_STATUS *status_p, uint32_t *type_hint_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200400{
401 *type_hint_p = 0;
402
403 if (*status_p == LYJSON_ARRAY) {
404 /* only [null] */
405 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
406 LY_CHECK_RET(*status_p != LYJSON_NULL, LY_EINVAL);
407
408 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, NULL));
409 LY_CHECK_RET(lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_ARRAY_CLOSED, LY_EINVAL);
410
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200411 *type_hint_p = LYD_VALHINT_EMPTY;
Radek Krejci1798aae2020-07-14 13:26:06 +0200412 } else if (*status_p == LYJSON_STRING) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200413 *type_hint_p = LYD_VALHINT_STRING | LYD_VALHINT_NUM64;
Radek Krejci1798aae2020-07-14 13:26:06 +0200414 } else if (*status_p == LYJSON_NUMBER) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200415 *type_hint_p = LYD_VALHINT_DECNUM;
Michal Vasko69730152020-10-09 16:30:07 +0200416 } else if ((*status_p == LYJSON_FALSE) || (*status_p == LYJSON_TRUE)) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200417 *type_hint_p = LYD_VALHINT_BOOLEAN;
Radek Krejci1798aae2020-07-14 13:26:06 +0200418 } else if (*status_p == LYJSON_NULL) {
419 *type_hint_p = 0;
420 } else {
421 return LY_EINVAL;
422 }
423
424 return LY_SUCCESS;
425}
426
427/**
428 * @brief Check in advance if the input data are parsable according to the provided @p snode.
429 *
430 * Note that the checks are done only in case the LYD_PARSE_OPAQ is allowed. Otherwise the same checking
431 * is naturally done when the data are really parsed.
432 *
433 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
434 * as before calling, despite it is necessary to process input data for checking.
435 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
436 * @param[out] type_hint_p Pointer to a variable to store detected value type hint in case of leaf or leaf-list.
437 * @return LY_SUCCESS in case the data are ok for the @p snode or the LYD_PARSE_OPAQ is not enabled.
438 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance
439 * @return LY_EINVAL in case of invalid leaf JSON encoding
440 * and they are expected to be parsed as opaq nodes.
441 */
442static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200443lydjson_data_check_opaq(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, uint32_t *type_hint_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200444{
445 LY_ERR ret = LY_SUCCESS;
446 struct lyjson_ctx *jsonctx = lydctx->jsonctx;
447 enum LYJSON_PARSER_STATUS status;
448
449 assert(snode);
Radek Krejci1798aae2020-07-14 13:26:06 +0200450
Michal Vasko32ac9942020-08-12 14:35:12 +0200451 if (!(snode->nodetype & (LYD_NODE_TERM | LYS_LIST))) {
452 /* can always be parsed as a data node if we have the schema node */
453 return LY_SUCCESS;
454 }
455
Michal Vaskoe0665742021-02-11 11:08:44 +0100456 if (lydctx->parse_opts & LYD_PARSE_OPAQ) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200457 /* backup parser */
458 lyjson_ctx_backup(jsonctx);
459 status = lyjson_ctx_status(jsonctx, 0);
460
Michal Vasko32ac9942020-08-12 14:35:12 +0200461 /* check if the node is parseable. if not, NULL the snode to announce that it is supposed to be parsed
462 * as an opaq node */
Radek Krejci1798aae2020-07-14 13:26:06 +0200463 switch (snode->nodetype) {
464 case LYS_LEAFLIST:
465 case LYS_LEAF:
466 /* value may not be valid in which case we parse it as an opaque node */
467 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
468 if (ret) {
469 break;
470 }
471
Radek Krejci8df109d2021-04-23 12:19:08 +0200472 if (lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_VALUE_JSON, NULL)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200473 ret = LY_ENOT;
474 }
475 break;
476 case LYS_LIST:
477 /* lists may not have all its keys */
478 if (lydjson_check_list(jsonctx, snode)) {
479 /* invalid list, parse as opaque if it missing/has invalid some keys */
480 ret = LY_ENOT;
481 }
Michal Vasko32ac9942020-08-12 14:35:12 +0200482 break;
Radek Krejci1798aae2020-07-14 13:26:06 +0200483 }
484
485 /* restore parser */
486 lyjson_ctx_restore(jsonctx);
Michal Vasko32ac9942020-08-12 14:35:12 +0200487 } else if (snode->nodetype & LYD_NODE_TERM) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200488 status = lyjson_ctx_status(jsonctx, 0);
489 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
490 }
491
492 return ret;
493}
494
495/**
496 * @brief Join the forward-referencing metadata with their target data nodes.
497 *
498 * Note that JSON encoding for YANG data allows forward-referencing metadata only for leafs/leaf-lists.
499 *
500 * @param[in] lydctx JSON data parser context.
501 * @param[in,out] first_p Pointer to the first sibling node variable (top-level or in a particular parent node)
502 * as a starting point to search for the metadata's target data node
503 * @return LY_SUCCESS on success
504 * @return LY_EVALID in case there are some metadata with unresolved target data node instance
505 */
506static LY_ERR
507lydjson_metadata_finish(struct lyd_json_ctx *lydctx, struct lyd_node **first_p)
508{
509 LY_ERR ret = LY_SUCCESS;
aPiecek5057c2e2021-05-17 10:28:03 +0200510 struct lyd_node *node, *attr, *next, *meta_iter;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200511 uint64_t instance = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200512 const char *prev = NULL;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100513 uint32_t log_location_items = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200514
515 /* finish linking metadata */
516 LY_LIST_FOR_SAFE(*first_p, next, attr) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200517 struct lyd_node_opaq *meta_container = (struct lyd_node_opaq *)attr;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200518 uint64_t match = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200519 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200520 const char *name, *prefix;
521 size_t name_len, prefix_len;
522 const struct lysc_node *snode;
523
Michal Vaskoad92b672020-11-12 13:11:31 +0100524 if (attr->schema || (meta_container->name.name[0] != '@')) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200525 /* not an opaq metadata node */
526 continue;
527 }
528
Radek Krejciddace2c2021-01-08 11:30:56 +0100529 LOG_LOCSET(NULL, attr, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100530 log_location_items++;
531
Michal Vaskoad92b672020-11-12 13:11:31 +0100532 if (prev != meta_container->name.name) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200533 /* metas' names are stored in dictionary, so checking pointers must works */
534 lydict_remove(lydctx->jsonctx->ctx, prev);
Michal Vaskoad92b672020-11-12 13:11:31 +0100535 LY_CHECK_GOTO(ret = lydict_insert(lydctx->jsonctx->ctx, meta_container->name.name, 0, &prev), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200536 instance = 1;
537 } else {
538 instance++;
539 }
540
aPiecek5057c2e2021-05-17 10:28:03 +0200541 /* find the corresponding data node */
542 LY_LIST_FOR(*first_p, node) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200543 if (!node->schema) {
544 /* opaq node - we are going to put into it just a generic attribute. */
Michal Vaskoad92b672020-11-12 13:11:31 +0100545 if (strcmp(&meta_container->name.name[1], ((struct lyd_node_opaq *)node)->name.name)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200546 continue;
547 }
548
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200549 if (((struct lyd_node_opaq *)node)->hints & LYD_NODEHINT_LIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100550 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX, "Metadata container references a sibling list node %s.",
551 ((struct lyd_node_opaq *)node)->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200552 ret = LY_EVALID;
553 goto cleanup;
554 }
555
556 /* match */
557 match++;
558 if (match != instance) {
559 continue;
560 }
561
562 LY_LIST_FOR(meta_container->child, meta_iter) {
563 /* convert opaq node to a attribute of the opaq node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200564 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejcibb27df32020-11-13 15:39:16 +0100565
Michal Vaskoad92b672020-11-12 13:11:31 +0100566 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, meta->name.name, strlen(meta->name.name),
567 meta->name.prefix, ly_strlen(meta->name.prefix), meta->name.module_name,
Radek Krejci8df109d2021-04-23 12:19:08 +0200568 ly_strlen(meta->name.module_name), meta->value, ly_strlen(meta->value), NULL, LY_VALUE_JSON,
Michal Vaskoad92b672020-11-12 13:11:31 +0100569 NULL, meta->hints);
Radek Krejci1798aae2020-07-14 13:26:06 +0200570 LY_CHECK_GOTO(ret, cleanup);
571 }
572
573 /* done */
574 break;
575 } else {
576 /* this is the second time we are resolving the schema node, so it must succeed,
577 * but remember that snode can be still NULL */
Michal Vaskoad92b672020-11-12 13:11:31 +0100578 lydjson_parse_name(meta_container->name.name, strlen(meta_container->name.name), &name, &name_len,
579 &prefix, &prefix_len, &is_attr);
Radek Krejci1798aae2020-07-14 13:26:06 +0200580 assert(is_attr);
581 ret = lydjson_get_snode(lydctx, is_attr, prefix, prefix_len, name, name_len, (*first_p)->parent, &snode);
582 assert(ret == LY_SUCCESS);
583
584 if (snode != node->schema) {
585 continue;
586 }
587
588 /* match */
589 match++;
590 if (match != instance) {
591 continue;
592 }
593
594 LY_LIST_FOR(meta_container->child, meta_iter) {
595 /* convert opaq node to a metadata of the node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200596 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejci1798aae2020-07-14 13:26:06 +0200597 struct lys_module *mod = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200598 ly_bool dynamic = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200599
Michal Vaskoad92b672020-11-12 13:11:31 +0100600 mod = ly_ctx_get_module_implemented(lydctx->jsonctx->ctx, meta->name.prefix);
Radek Krejci1798aae2020-07-14 13:26:06 +0200601 if (mod) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200602 ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, NULL, mod,
Michal Vaskoad92b672020-11-12 13:11:31 +0100603 meta->name.name, strlen(meta->name.name), meta->value, ly_strlen(meta->value),
Radek Krejci8df109d2021-04-23 12:19:08 +0200604 &dynamic, LY_VALUE_JSON, NULL, meta->hints);
Radek Krejci1798aae2020-07-14 13:26:06 +0200605 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +0100606 } else if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100607 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200608 "Unknown (or not implemented) YANG module \"%s\" for metadata \"%s%s%s\".",
Michal Vaskoad92b672020-11-12 13:11:31 +0100609 meta->name.prefix, meta->name.prefix, ly_strlen(meta->name.prefix) ? ":" : "", meta->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200610 ret = LY_EVALID;
611 goto cleanup;
612 }
613 }
614 /* add/correct flags */
Radek Krejci4f2e3e52021-03-30 14:20:28 +0200615 lyd_parse_set_data_flags(node, &lydctx->node_when, &lydctx->node_exts, &node->meta, lydctx->parse_opts);
Radek Krejci1798aae2020-07-14 13:26:06 +0200616
617 /* done */
618 break;
619 }
620 }
621
622 if (match != instance) {
623 /* there is no corresponding data node for the metadata */
624 if (instance > 1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100625 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing %d%s JSON data instance to be coupled with %s metadata.",
626 instance, instance == 2 ? "nd" : (instance == 3 ? "rd" : "th"), meta_container->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200627 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100628 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing JSON data instance to be coupled with %s metadata.",
629 meta_container->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200630 }
631 ret = LY_EVALID;
632 } else {
633 /* remove the opaq attr */
634 if (attr == (*first_p)) {
aPiecek5057c2e2021-05-17 10:28:03 +0200635 *first_p = attr->next;
Radek Krejci1798aae2020-07-14 13:26:06 +0200636 }
637 lyd_free_tree(attr);
638 }
Radek Krejci2efc45b2020-12-22 16:25:44 +0100639
Radek Krejciddace2c2021-01-08 11:30:56 +0100640 LOG_LOCBACK(0, log_location_items, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100641 log_location_items = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200642 }
643
644cleanup:
645 lydict_remove(lydctx->jsonctx->ctx, prev);
646
Radek Krejciddace2c2021-01-08 11:30:56 +0100647 LOG_LOCBACK(0, log_location_items, 0, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200648 return ret;
649}
650
651/**
652 * @brief Parse a metadata member.
653 *
654 * @param[in] lydctx JSON data parser context.
655 * @param[in] snode Schema node of the metadata parent.
656 * @param[in] node Parent node in case the metadata is not forward-referencing (only LYD_NODE_TERM)
657 * so the data node does not exists. In such a case the metadata is stored in the context for the later
658 * processing by lydjson_metadata_finish().
659 * @return LY_SUCCESS on success
660 * @return Various LY_ERR values in case of failure.
661 */
662static LY_ERR
663lydjson_metadata(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lyd_node *node)
664{
665 LY_ERR ret = LY_SUCCESS;
666 enum LYJSON_PARSER_STATUS status;
667 const char *expected;
Radek Krejci857189e2020-09-01 13:26:36 +0200668 ly_bool in_parent = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200669 const char *name, *prefix = NULL;
aPiecek303fb252021-06-16 11:54:26 +0200670 char *dynamic_prefname = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200671 size_t name_len, prefix_len = 0;
672 struct lys_module *mod;
673 struct lyd_meta *meta = NULL;
674 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
Radek Krejci857189e2020-09-01 13:26:36 +0200675 ly_bool is_attr = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200676 struct lyd_node *prev = node;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200677 uint32_t instance = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200678 uint16_t nodetype;
679
680 assert(snode || node);
681
682 nodetype = snode ? snode->nodetype : LYS_CONTAINER;
683
684 /* move to the second item in the name/X pair */
685 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
686 LY_CHECK_GOTO(ret, cleanup);
687
688 /* check attribute encoding */
689 switch (nodetype) {
690 case LYS_LEAFLIST:
691 expected = "@name/array of objects/nulls";
692
693 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
694
695next_entry:
696 instance++;
697
698 /* move into array / next entry */
699 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
700 LY_CHECK_GOTO(ret, cleanup);
701
702 if (status == LYJSON_ARRAY_CLOSED) {
703 /* we are done, move after the array */
704 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
705 goto cleanup;
706 }
707 LY_CHECK_GOTO(status != LYJSON_OBJECT && status != LYJSON_NULL, representation_error);
708
Michal Vasko69730152020-10-09 16:30:07 +0200709 if (!node || (node->schema != prev->schema)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100710 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing JSON data instance no. %u of %s:%s to be coupled with metadata.",
Michal Vasko69730152020-10-09 16:30:07 +0200711 instance, prev->schema->module->name, prev->schema->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200712 ret = LY_EVALID;
713 goto cleanup;
714 }
715
716 if (status == LYJSON_NULL) {
717 /* continue with the next entry in the leaf-list array */
718 prev = node;
719 node = node->next;
720 goto next_entry;
721 }
722 break;
723 case LYS_LEAF:
724 case LYS_ANYXML:
725 expected = "@name/object";
726
aPiecek5b115fc2021-05-24 14:32:42 +0200727 LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error);
Radek Krejci1798aae2020-07-14 13:26:06 +0200728 break;
729 case LYS_CONTAINER:
730 case LYS_LIST:
731 case LYS_ANYDATA:
732 case LYS_NOTIF:
733 case LYS_ACTION:
734 case LYS_RPC:
735 in_parent = 1;
736 expected = "@/object";
737 LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error);
738 break;
Radek Krejci8f5fad22020-09-15 16:50:54 +0200739 default:
740 LOGINT_RET(ctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200741 }
742
743 /* process all the members inside a single metadata object */
744 assert(status == LYJSON_OBJECT);
745
Radek Krejciddace2c2021-01-08 11:30:56 +0100746 LOG_LOCSET(snode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100747
Radek Krejci1798aae2020-07-14 13:26:06 +0200748 while (status != LYJSON_OBJECT_CLOSED) {
aPiecek303fb252021-06-16 11:54:26 +0200749 if (dynamic_prefname) {
750 free(dynamic_prefname);
751 dynamic_prefname = NULL;
752 }
753 if (lydctx->jsonctx->dynamic) {
754 /* Duplicate ::lyjson_ctx.value because it is dynamically
755 * allocated and later ::lyjson_ctx_next() will release it
756 * which will cause local pointers 'name' and 'prefix'
757 * to be invalid.
758 */
759 dynamic_prefname = strndup(lydctx->jsonctx->value, lydctx->jsonctx->value_len);
760 if (!dynamic_prefname) {
761 LOGMEM(lydctx->jsonctx->ctx);
762 ret = LY_EMEM;
763 goto cleanup;
764 }
765 lydjson_parse_name(dynamic_prefname, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
766 } else {
767 lydjson_parse_name(lydctx->jsonctx->value, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
768 }
769
Radek Krejci1798aae2020-07-14 13:26:06 +0200770 if (!prefix) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100771 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Metadata in JSON must be namespace-qualified, missing prefix for \"%.*s\".",
Radek Krejci422afb12021-03-04 16:38:16 +0100772 (int)lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200773 ret = LY_EVALID;
774 goto cleanup;
775 } else if (is_attr) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100776 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Invalid format of the Metadata identifier in JSON, unexpected '@' in \"%.*s\"",
Radek Krejci422afb12021-03-04 16:38:16 +0100777 (int)lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200778 ret = LY_EVALID;
779 goto cleanup;
780 }
781
782 /* get the element module */
783 mod = ly_ctx_get_module_implemented2(ctx, prefix, prefix_len);
784 if (!mod) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100785 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100786 LOGVAL(ctx, LYVE_REFERENCE, "Prefix \"%.*s\" of the metadata \"%.*s\" does not match any module in the context.",
Radek Krejci422afb12021-03-04 16:38:16 +0100787 (int)prefix_len, prefix, (int)name_len, name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200788 ret = LY_EVALID;
789 goto cleanup;
790 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100791 if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200792 /* skip element with children */
793 ret = lydjson_data_skip(lydctx->jsonctx);
794 LY_CHECK_GOTO(ret, cleanup);
795 status = lyjson_ctx_status(lydctx->jsonctx, 0);
796 /* end of the item */
797 continue;
798 }
799 }
800
801 /* get the value */
802 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
803 LY_CHECK_GOTO(ret, cleanup);
804
805 if (node->schema) {
806 /* create metadata */
807 meta = NULL;
Michal Vasko22df3f02020-08-24 13:29:22 +0200808 ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, &meta, mod, name, name_len, lydctx->jsonctx->value,
Radek Krejci8df109d2021-04-23 12:19:08 +0200809 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_VALUE_JSON, NULL,
Michal Vasko69730152020-10-09 16:30:07 +0200810 LYD_HINT_DATA);
Radek Krejci1798aae2020-07-14 13:26:06 +0200811 LY_CHECK_GOTO(ret, cleanup);
812
813 /* add/correct flags */
Radek Krejci4f2e3e52021-03-30 14:20:28 +0200814 lyd_parse_set_data_flags(node, &lydctx->node_when, &lydctx->node_exts, &meta, lydctx->parse_opts);
Radek Krejci1798aae2020-07-14 13:26:06 +0200815 } else {
816 /* create attribute */
Radek Krejci1798aae2020-07-14 13:26:06 +0200817 const char *module_name;
818 size_t module_name_len;
819
820 lydjson_get_node_prefix(node, prefix, prefix_len, &module_name, &module_name_len);
821
Radek Krejci1798aae2020-07-14 13:26:06 +0200822 /* attr2 is always changed to the created attribute */
Michal Vasko501af032020-11-11 20:27:44 +0100823 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name,
824 module_name_len, lydctx->jsonctx->value, lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic,
Radek Krejci8df109d2021-04-23 12:19:08 +0200825 LY_VALUE_JSON, NULL, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200826 LY_CHECK_GOTO(ret, cleanup);
827 }
828 /* next member */
829 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
830 LY_CHECK_GOTO(ret, cleanup);
aPiecekde3c6922021-05-24 14:42:10 +0200831 LY_CHECK_GOTO((status != LYJSON_OBJECT) && (status != LYJSON_OBJECT_CLOSED), representation_error);
Radek Krejci1798aae2020-07-14 13:26:06 +0200832 }
833
834 if (nodetype == LYS_LEAFLIST) {
835 /* continue by processing another metadata object for the following
836 * leaf-list instance since they are allways instantiated in JSON array */
837 prev = node;
838 node = node->next;
839 goto next_entry;
840 }
841
842 /* move after the metadata */
843 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
844 LY_CHECK_GOTO(ret, cleanup);
845
Radek Krejci5813c362021-01-05 10:51:57 +0100846 /* success */
847 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +0200848
849representation_error:
Radek Krejci2efc45b2020-12-22 16:25:44 +0100850 LOGVAL(ctx, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +0200851 "The attribute(s) of %s \"%s\" is expected to be represented as JSON %s, but input data contains @%s/%s.",
Michal Vaskoad92b672020-11-12 13:11:31 +0100852 lys_nodetype2str(nodetype), node->schema ? node->schema->name : ((struct lyd_node_opaq *)node)->name.name,
Michal Vasko69730152020-10-09 16:30:07 +0200853 expected, lyjson_token2str(status), in_parent ? "" : "name");
Radek Krejci1798aae2020-07-14 13:26:06 +0200854
Radek Krejci2efc45b2020-12-22 16:25:44 +0100855 ret = LY_EVALID;
Radek Krejci5813c362021-01-05 10:51:57 +0100856
857cleanup:
aPiecek303fb252021-06-16 11:54:26 +0200858 free(dynamic_prefname);
Radek Krejciddace2c2021-01-08 11:30:56 +0100859 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci5813c362021-01-05 10:51:57 +0100860 return ret;
Radek Krejci1798aae2020-07-14 13:26:06 +0200861}
862
863/**
864 * @brief Eat the node pointed by @p node_p by inserting it into @p parent and maintain the @p first_p pointing to the first child node.
865 *
866 * @param[in] parent Parent node to insert to, can be NULL in case of top-level (or provided first_p).
867 * @param[in, out] first_p Pointer to the first sibling node in case of top-level.
868 * @param[in, out] node_p pointer to the new node to insert, after the insert is done, pointer is set to NULL.
869 */
870static void
871lydjson_maintain_children(struct lyd_node_inner *parent, struct lyd_node **first_p, struct lyd_node **node_p)
872{
873 if (*node_p) {
874 /* insert, keep first pointer correct */
Michal Vasko08e9b112021-06-11 15:41:17 +0200875 lyd_insert_node(parent ? &parent->node : NULL, first_p, *node_p);
Radek Krejci1798aae2020-07-14 13:26:06 +0200876 if (first_p) {
877 if (parent) {
878 *first_p = parent->child;
879 } else {
880 while ((*first_p)->prev->next) {
881 *first_p = (*first_p)->prev;
882 }
883 }
884 }
885 *node_p = NULL;
886 }
887}
888
aPiecek4bf47212021-05-27 10:55:47 +0200889/**
890 * @brief Wrapper for ::lyd_create_opaq().
891 *
892 * @param[in] lydctx JSON data parser context.
893 * @param[in] name Name of the opaq node to create.
894 * @param[in] name_len Length of the @p name string.
895 * @param[in] prefix Prefix of the opaq node to create.
896 * @param[in] prefix_len Length of the @p prefx string.
897 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
898 * but must be set if @p first is not.
899 * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone
900 * context status of the array content. Otherwise, it is supposed to be the same as @p status_p.
901 * @param[out] node_p Pointer to the created opaq node.
902 * @return LY_ERR value.
903 */
904static LY_ERR
905lydjson_create_opaq(struct lyd_json_ctx *lydctx, const char *name, size_t name_len, const char *prefix, size_t prefix_len,
906 struct lyd_node_inner *parent, enum LYJSON_PARSER_STATUS *status_inner_p, struct lyd_node **node_p)
907{
908 LY_ERR ret = LY_SUCCESS;
909 const char *value = NULL, *module_name;
910 size_t value_len = 0, module_name_len = 0;
911 ly_bool dynamic = 0;
912 uint32_t type_hint = 0;
913
914 if ((*status_inner_p != LYJSON_OBJECT) && (*status_inner_p != LYJSON_OBJECT_EMPTY)) {
915 /* prepare for creating opaq node with a value */
916 value = lydctx->jsonctx->value;
917 value_len = lydctx->jsonctx->value_len;
918 dynamic = lydctx->jsonctx->dynamic;
919 lydctx->jsonctx->dynamic = 0;
920
921 LY_CHECK_RET(lydjson_value_type_hint(lydctx, status_inner_p, &type_hint));
922 }
923
924 /* create node */
Michal Vaskod8e774e2021-06-15 14:58:07 +0200925 lydjson_get_node_prefix(parent ? &parent->node : NULL, prefix, prefix_len, &module_name, &module_name_len);
aPiecek4bf47212021-05-27 10:55:47 +0200926 ret = lyd_create_opaq(lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name, module_name_len, value,
927 value_len, &dynamic, LY_VALUE_JSON, NULL, type_hint, node_p);
928 if (dynamic) {
929 free((char *)value);
930 }
931
932 return ret;
933}
934
Michal Vaskoe0665742021-02-11 11:08:44 +0100935static LY_ERR lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p,
936 struct ly_set *parsed);
Radek Krejci1798aae2020-07-14 13:26:06 +0200937
938/**
939 * @brief Parse opaq node from the input.
940 *
941 * In case of processing array, the whole array is being processed and the resulting @p node_p is the last item of the array.
942 *
943 * @param[in] lydctx JSON data parser context.
944 * @param[in] name Name of the opaq node to create.
945 * @param[in] name_len Length of the @p name string.
946 * @param[in] prefix Prefix of the opaq node to create.
947 * @param[in] prefix_len Length of the @p prefx string.
948 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
949 * but must be set if @p first is not.
950 * @param[in,out] status_p Pointer to the current status of the parser context,
951 * since the function manipulates with the context and process the input, the status can be updated.
952 * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone
953 * context status of the array content. Otherwise, it is supposed to be the same as @p status_p.
954 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
955 * @param[out] node_p Pointer to the created opaq node.
956 * @return LY_ERR value.
957 */
958static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +0200959lydjson_parse_opaq(struct lyd_json_ctx *lydctx, const char *name, size_t name_len, const char *prefix, size_t prefix_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200960 struct lyd_node_inner *parent, enum LYJSON_PARSER_STATUS *status_p,
961 enum LYJSON_PARSER_STATUS *status_inner_p, struct lyd_node **first_p, struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200962{
963 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +0200964
aPiecek4bf47212021-05-27 10:55:47 +0200965 ret = lydjson_create_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_inner_p, node_p);
Radek Krejcid46e46a2020-09-15 14:22:42 +0200966 LY_CHECK_RET(ret);
Radek Krejci1798aae2020-07-14 13:26:06 +0200967
aPiecekdbb8baf2021-05-27 11:01:51 +0200968 while ((*status_p == LYJSON_ARRAY) || (*status_p == LYJSON_ARRAY_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200969 /* process another instance of the same node */
970 /* but first mark the node to be expected a list or a leaf-list */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200971 ((struct lyd_node_opaq *)*node_p)->hints |= LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST;
Radek Krejci1798aae2020-07-14 13:26:06 +0200972
Michal Vasko69730152020-10-09 16:30:07 +0200973 if ((*status_inner_p == LYJSON_OBJECT) || (*status_inner_p == LYJSON_OBJECT_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200974 /* but first process children of the object in the array */
975 while (*status_inner_p != LYJSON_OBJECT_CLOSED && *status_inner_p != LYJSON_OBJECT_EMPTY) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100976 LY_CHECK_RET(lydjson_subtree_r(lydctx, *node_p, lyd_node_child_p(*node_p), NULL));
Radek Krejci1798aae2020-07-14 13:26:06 +0200977 *status_inner_p = lyjson_ctx_status(lydctx->jsonctx, 0);
978 }
979 }
980
981 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_inner_p));
aPiecekdbb8baf2021-05-27 11:01:51 +0200982 if (*status_inner_p == LYJSON_ARRAY_CLOSED) {
983 goto finish;
984 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200985
986 /* continue with the next instance */
aPiecekdbb8baf2021-05-27 11:01:51 +0200987 assert(node_p);
988 lydjson_maintain_children(parent, first_p, node_p);
989 ret = lydjson_create_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_inner_p, node_p);
990 LY_CHECK_RET(ret);
991 }
992
993 if ((*status_p == LYJSON_OBJECT) || (*status_p == LYJSON_OBJECT_EMPTY)) {
994 /* process children */
995 while (*status_p != LYJSON_OBJECT_CLOSED && *status_p != LYJSON_OBJECT_EMPTY) {
996 LY_CHECK_RET(lydjson_subtree_r(lydctx, *node_p, lyd_node_child_p(*node_p), NULL));
997 *status_p = lyjson_ctx_status(lydctx->jsonctx, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200998 }
999 }
1000
aPiecekdbb8baf2021-05-27 11:01:51 +02001001finish:
Radek Krejci1798aae2020-07-14 13:26:06 +02001002 /* finish linking metadata */
Michal Vaskoe0665742021-02-11 11:08:44 +01001003 LY_CHECK_RET(lydjson_metadata_finish(lydctx, lyd_node_child_p(*node_p)));
Radek Krejci1798aae2020-07-14 13:26:06 +02001004
1005 /* move after the item */
1006 return lyjson_ctx_next(lydctx->jsonctx, status_p);
1007}
1008
1009/**
aPiecek6cee5d02021-05-12 10:32:00 +02001010 * @brief Move to the second item in the name/X pair and parse opaq node from the input.
1011 *
1012 * This function is basically the wrapper of the ::lydjson_parse_opaq().
1013 * In addition, it calls the ::json_ctx_next() and prepares the status_inner_p parameter
1014 * for ::lydjson_parse_opaq().
1015 *
1016 * @param[in] lydctx JSON data parser context.
1017 * @param[in] name Name of the opaq node to create.
1018 * @param[in] name_len Length of the @p name string.
1019 * @param[in] prefix Prefix of the opaq node to create.
1020 * @param[in] prefix_len Length of the @p prefx string.
1021 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
1022 * but must be set if @p first is not.
1023 * @param[in,out] status_p Pointer to the current status of the parser context,
1024 * since the function manipulates with the context and process the input, the status can be updated.
1025 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
1026 * @param[out] node_p Pointer to the created opaq node.
1027 * @return LY_ERR value.
1028 */
1029static LY_ERR
1030lydjson_ctx_next_parse_opaq(struct lyd_json_ctx *lydctx, const char *name, size_t name_len,
1031 const char *prefix, size_t prefix_len, struct lyd_node_inner *parent, enum LYJSON_PARSER_STATUS *status_p,
1032 struct lyd_node **first_p, struct lyd_node **node_p)
1033{
1034 enum LYJSON_PARSER_STATUS status_inner = 0;
1035
1036 /* move to the second item in the name/X pair */
1037 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
1038
1039 if (*status_p == LYJSON_ARRAY) {
1040 /* move into the array */
1041 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, &status_inner));
1042 } else {
1043 /* just a flag to pass correct parameters into lydjson_parse_opaq() */
1044 status_inner = LYJSON_ERROR;
1045 }
1046
1047 if (status_inner == LYJSON_ERROR) {
1048 status_inner = *status_p;
1049 }
1050
1051 /* parse opaq node from the input */
1052 LY_CHECK_RET(lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_p, &status_inner, first_p, node_p));
1053
1054 return LY_SUCCESS;
1055}
1056
1057/**
Radek Krejci1798aae2020-07-14 13:26:06 +02001058 * @brief Process the attribute container (starting by @)
1059 *
1060 * @param[in] lydctx JSON data parser context.
1061 * @param[in] attr_node The data node referenced by the attribute container, if already known.
1062 * @param[in] snode The schema node of the data node referenced by the attribute container, if known.
1063 * @param[in] name Name of the opaq node to create.
1064 * @param[in] name_len Length of the @p name string.
1065 * @param[in] prefix Prefix of the opaq node to create.
1066 * @param[in] prefix_len Length of the @p prefx string.
1067 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
1068 * but must be set if @p first is not.
1069 * @param[in,out] status_p Pointer to the current status of the parser context,
1070 * since the function manipulates with the context and process the input, the status can be updated.
1071 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
1072 * @param[out] node_p Pointer to the created opaq node.
1073 * @return LY_ERR value.
1074 */
1075static LY_ERR
1076lydjson_parse_attribute(struct lyd_json_ctx *lydctx, struct lyd_node *attr_node, const struct lysc_node *snode,
Michal Vaskoe0665742021-02-11 11:08:44 +01001077 const char *name, size_t name_len, const char *prefix, size_t prefix_len, struct lyd_node *parent,
1078 enum LYJSON_PARSER_STATUS *status_p, struct lyd_node **first_p, struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001079{
1080 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001081
1082 /* parse as an attribute to a node */
1083 if (!attr_node && snode) {
1084 /* try to find the instance */
1085 for (struct lyd_node *iter = *first_p; iter; iter = iter->next) {
1086 if (iter->schema == snode) {
1087 attr_node = iter;
1088 break;
1089 }
1090 }
1091 }
1092 if (!attr_node) {
1093 /* parse just as an opaq node with the name beginning with @,
1094 * later we have to check that it belongs to a standard node
1095 * and it is supposed to be converted to a metadata */
1096 uint32_t prev_opts;
1097
Radek Krejci1798aae2020-07-14 13:26:06 +02001098 /* backup parser options to parse unknown metadata as opaq nodes and try to resolve them later */
Michal Vaskoe0665742021-02-11 11:08:44 +01001099 prev_opts = lydctx->parse_opts;
1100 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
1101 lydctx->parse_opts |= LYD_PARSE_OPAQ;
Radek Krejci1798aae2020-07-14 13:26:06 +02001102
aPiecek6cee5d02021-05-12 10:32:00 +02001103 ret = lydjson_ctx_next_parse_opaq(lydctx, prefix ? prefix - 1 : name - 1, prefix ? prefix_len + name_len + 2 : name_len + 1,
1104 NULL, 0, (struct lyd_node_inner *)parent, status_p, first_p, node_p);
Radek Krejci1798aae2020-07-14 13:26:06 +02001105
1106 /* restore the parser options */
Michal Vaskoe0665742021-02-11 11:08:44 +01001107 lydctx->parse_opts = prev_opts;
Radek Krejci1798aae2020-07-14 13:26:06 +02001108 } else {
1109 ret = lydjson_metadata(lydctx, snode, attr_node);
1110 }
1111
1112 return ret;
1113}
1114
1115/**
Michal Vasko32ac9942020-08-12 14:35:12 +02001116 * @brief Parse a single instance of a node.
1117 *
1118 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
1119 * as before calling, despite it is necessary to process input data for checking.
1120 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1121 * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not.
1122 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
1123 * @param[in] name Parsed JSON node name.
1124 * @param[in] name_len Lenght of @p name.
1125 * @param[in] prefix Parsed JSON node prefix.
1126 * @param[in] prefix_len Length of @p prefix.
1127 * @param[in,out] status JSON parser status, is updated.
1128 * @param[out] node Parsed data (or opaque) node.
1129 * @return LY_SUCCESS if a node was successfully parsed,
1130 * @return LY_EINVAL in case of invalid JSON encoding,
1131 * @return LY_ERR on other errors.
1132 */
1133static LY_ERR
1134lydjson_parse_instance(struct lyd_json_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first_p,
Radek Krejci0f969882020-08-21 16:56:47 +02001135 const struct lysc_node *snode, const char *name, size_t name_len, const char *prefix, size_t prefix_len,
1136 enum LYJSON_PARSER_STATUS *status, struct lyd_node **node)
Michal Vasko32ac9942020-08-12 14:35:12 +02001137{
1138 LY_ERR ret;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001139 uint32_t type_hints = 0;
Michal Vasko32ac9942020-08-12 14:35:12 +02001140 uint32_t prev_opts;
1141 struct lyd_node *tree = NULL;
1142
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001143 ret = lydjson_data_check_opaq(lydctx, snode, &type_hints);
Michal Vasko32ac9942020-08-12 14:35:12 +02001144 if (ret == LY_SUCCESS) {
1145 assert(snode->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER | LYD_NODE_ANY));
1146 if (snode->nodetype & LYD_NODE_TERM) {
1147 /* create terminal node */
1148 ret = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, lydctx->jsonctx->value,
Radek Krejci8df109d2021-04-23 12:19:08 +02001149 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_VALUE_JSON, NULL,
Michal Vasko69730152020-10-09 16:30:07 +02001150 type_hints, node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001151 LY_CHECK_RET(ret);
1152
1153 /* move JSON parser */
1154 ret = lyjson_ctx_next(lydctx->jsonctx, status);
1155 LY_CHECK_RET(ret);
1156 } else if (snode->nodetype & LYD_NODE_INNER) {
1157 /* create inner node */
1158 LY_CHECK_RET(*status != LYJSON_OBJECT && *status != LYJSON_OBJECT_EMPTY, LY_EINVAL);
1159
1160 ret = lyd_create_inner(snode, node);
1161 LY_CHECK_RET(ret);
1162
Radek Krejciddace2c2021-01-08 11:30:56 +01001163 LOG_LOCSET(snode, *node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001164
Michal Vasko32ac9942020-08-12 14:35:12 +02001165 /* process children */
1166 while (*status != LYJSON_OBJECT_CLOSED && *status != LYJSON_OBJECT_EMPTY) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001167 ret = lydjson_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +01001168 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001169 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1170 }
1171
1172 /* finish linking metadata */
Michal Vaskoe0665742021-02-11 11:08:44 +01001173 ret = lydjson_metadata_finish(lydctx, lyd_node_child_p(*node));
Radek Krejciddace2c2021-01-08 11:30:56 +01001174 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001175
1176 if (snode->nodetype == LYS_LIST) {
1177 /* check all keys exist */
1178 ret = lyd_parse_check_keys(*node);
Radek Krejciddace2c2021-01-08 11:30:56 +01001179 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001180 }
1181
Michal Vaskoe0665742021-02-11 11:08:44 +01001182 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001183 /* new node validation, autodelete CANNOT occur, all nodes are new */
Michal Vaskoe0665742021-02-11 11:08:44 +01001184 ret = lyd_validate_new(lyd_node_child_p(*node), snode, NULL, NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +01001185 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001186
1187 /* add any missing default children */
Radek Krejci4f2e3e52021-03-30 14:20:28 +02001188 ret = lyd_new_implicit_r(*node, lyd_node_child_p(*node), NULL, NULL, &lydctx->node_when, &lydctx->node_exts,
1189 &lydctx->node_types, (lydctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +01001190 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001191 }
1192
Radek Krejciddace2c2021-01-08 11:30:56 +01001193 LOG_LOCBACK(1, 1, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001194
Michal Vasko32ac9942020-08-12 14:35:12 +02001195 /* move JSON parser */
1196 ret = lyjson_ctx_next(lydctx->jsonctx, status);
1197 LY_CHECK_RET(ret);
1198 } else if (snode->nodetype & LYD_NODE_ANY) {
1199 /* create any node */
1200 LY_CHECK_RET(*status != LYJSON_OBJECT && *status != LYJSON_OBJECT_EMPTY, LY_EINVAL);
1201
1202 /* parse any data tree with correct options */
1203 /* first backup the current options and then make the parser to process data as opaq nodes */
Michal Vaskoe0665742021-02-11 11:08:44 +01001204 prev_opts = lydctx->parse_opts;
1205 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
1206 lydctx->parse_opts |= LYD_PARSE_OPAQ;
Michal Vasko32ac9942020-08-12 14:35:12 +02001207
1208 /* process the anydata content */
1209 while (*status != LYJSON_OBJECT_CLOSED && *status != LYJSON_OBJECT_EMPTY) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001210 ret = lydjson_subtree_r(lydctx, NULL, &tree, NULL);
Michal Vasko32ac9942020-08-12 14:35:12 +02001211 LY_CHECK_RET(ret);
1212 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1213 }
1214
1215 /* restore parser options */
Michal Vaskoe0665742021-02-11 11:08:44 +01001216 lydctx->parse_opts = prev_opts;
Michal Vasko32ac9942020-08-12 14:35:12 +02001217
1218 /* finish linking metadata */
1219 ret = lydjson_metadata_finish(lydctx, &tree);
1220 LY_CHECK_RET(ret);
1221
Michal Vasko366a4a12020-12-04 16:23:57 +01001222 ret = lyd_create_any(snode, tree, LYD_ANYDATA_DATATREE, 1, node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001223 LY_CHECK_RET(ret);
1224 }
1225 } else if (ret == LY_ENOT) {
1226 /* parse it again as an opaq node */
1227 ret = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent,
Michal Vasko69730152020-10-09 16:30:07 +02001228 status, status, first_p, node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001229 LY_CHECK_RET(ret);
1230
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001231 if (snode->nodetype == LYS_LIST) {
1232 ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LIST;
1233 } else if (snode->nodetype == LYS_LEAFLIST) {
1234 ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LEAFLIST;
Michal Vasko32ac9942020-08-12 14:35:12 +02001235 }
1236 }
1237
1238 return ret;
1239}
1240
1241/**
Michal Vaskoa5da3292020-08-12 13:10:50 +02001242 * @brief Parse JSON subtree. All leaf-list and list instances of a node are considered one subtree.
Radek Krejci1798aae2020-07-14 13:26:06 +02001243 *
1244 * @param[in] lydctx JSON data parser context.
1245 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1246 * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not.
Michal Vaskoe0665742021-02-11 11:08:44 +01001247 * @param[in,out] parsed Optional set to add all the parsed siblings into.
Radek Krejci1798aae2020-07-14 13:26:06 +02001248 * @return LY_ERR value.
1249 */
1250static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +01001251lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed)
Radek Krejci1798aae2020-07-14 13:26:06 +02001252{
1253 LY_ERR ret = LY_SUCCESS;
1254 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(lydctx->jsonctx, 0);
aPiecek49e2a3a2021-05-13 10:36:46 +02001255 const char *name, *prefix = NULL, *expected = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001256 size_t name_len, prefix_len = 0;
Radek Krejci857189e2020-09-01 13:26:36 +02001257 ly_bool is_meta = 0;
Michal Vaskocabe0702020-08-12 10:14:36 +02001258 const struct lysc_node *snode = NULL;
Michal Vasko32ac9942020-08-12 14:35:12 +02001259 struct lyd_node *node = NULL, *attr_node = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001260 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
aPiecek49e2a3a2021-05-13 10:36:46 +02001261 char *value = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001262
1263 assert(parent || first_p);
1264 assert(status == LYJSON_OBJECT);
1265
aPiecek49e2a3a2021-05-13 10:36:46 +02001266 /* Duplicate ::lyjson_ctx.value because it can be dynamically allocated and later
1267 * ::lyjson_ctx_next() will release it although this string is needed for the ::lydjson_parse_opaq().
1268 */
1269 value = strndup(lydctx->jsonctx->value, lydctx->jsonctx->value_len);
1270 if (!value) {
1271 LOGMEM(lydctx->jsonctx->ctx);
1272 ret = LY_EMEM;
1273 goto cleanup;
1274 }
1275
Radek Krejci1798aae2020-07-14 13:26:06 +02001276 /* process the node name */
aPiecek49e2a3a2021-05-13 10:36:46 +02001277 lydjson_parse_name(value, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_meta);
Radek Krejci1798aae2020-07-14 13:26:06 +02001278
Michal Vaskocabe0702020-08-12 10:14:36 +02001279 if (!is_meta || name_len || prefix_len) {
1280 /* get the schema node */
Michal Vaskoe0665742021-02-11 11:08:44 +01001281 ret = lydjson_get_snode(lydctx, is_meta, prefix, prefix_len, name, name_len, (struct lyd_node_inner *)parent, &snode);
Michal Vaskocabe0702020-08-12 10:14:36 +02001282 if (ret == LY_ENOT) {
1283 /* skip element with children */
1284 ret = lydjson_data_skip(lydctx->jsonctx);
1285 LY_CHECK_GOTO(ret, cleanup);
1286 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1287 /* nothing for now, continue with another call of lydjson_subtree_r() */
Radek Krejci1798aae2020-07-14 13:26:06 +02001288 goto cleanup;
1289 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001290 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001291
Michal Vaskocabe0702020-08-12 10:14:36 +02001292 if (!snode) {
1293 /* we will not be parsing it as metadata */
1294 is_meta = 0;
1295 }
1296 }
1297
1298 if (is_meta) {
1299 /* parse as metadata */
1300 if (!name_len && !prefix_len) {
1301 /* parent's metadata without a name - use the schema from the parent */
1302 if (!parent) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001303 LOGVAL(ctx, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +02001304 "Invalid metadata format - \"@\" can be used only inside anydata, container or list entries.");
Michal Vaskocabe0702020-08-12 10:14:36 +02001305 ret = LY_EVALID;
1306 goto cleanup;
1307 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001308 attr_node = parent;
Michal Vaskocabe0702020-08-12 10:14:36 +02001309 snode = attr_node->schema;
1310 }
1311 ret = lydjson_parse_attribute(lydctx, attr_node, snode, name, name_len, prefix, prefix_len, parent, &status,
Michal Vasko69730152020-10-09 16:30:07 +02001312 first_p, &node);
Michal Vaskocabe0702020-08-12 10:14:36 +02001313 LY_CHECK_GOTO(ret, cleanup);
1314 } else if (!snode) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001315 /* parse as an opaq node */
Michal Vaskoe0665742021-02-11 11:08:44 +01001316 assert((lydctx->parse_opts & LYD_PARSE_OPAQ) || (lydctx->int_opts));
Radek Krejci1798aae2020-07-14 13:26:06 +02001317
aPiecekb7b29e62021-05-11 10:02:43 +02001318 /* opaq node cannot have an empty string as the name. */
1319 if (name_len == 0) {
1320 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "A JSON object member name cannot be a zero-length string.");
1321 ret = LY_EVALID;
1322 goto cleanup;
1323 }
1324
aPiecek6cee5d02021-05-12 10:32:00 +02001325 /* move to the second item in the name/X pair and parse opaq */
1326 ret = lydjson_ctx_next_parse_opaq(lydctx, name, name_len, prefix, prefix_len,
1327 (struct lyd_node_inner *)parent, &status, first_p, &node);
Radek Krejci1798aae2020-07-14 13:26:06 +02001328 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskocabe0702020-08-12 10:14:36 +02001329 } else {
Michal Vasko32ac9942020-08-12 14:35:12 +02001330 /* parse as a standard lyd_node but it can still turn out to be an opaque node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001331
1332 /* move to the second item in the name/X pair */
1333 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1334 LY_CHECK_GOTO(ret, cleanup);
1335
1336 /* first check the expected representation according to the nodetype and then continue with the content */
1337 switch (snode->nodetype) {
1338 case LYS_LEAFLIST:
Michal Vasko32ac9942020-08-12 14:35:12 +02001339 case LYS_LIST:
1340 if (snode->nodetype == LYS_LEAFLIST) {
1341 expected = "name/array of values";
1342 } else {
1343 expected = "name/array of objects";
1344 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001345
1346 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
1347
1348 /* move into array */
1349 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1350 LY_CHECK_GOTO(ret, cleanup);
1351
Michal Vasko32ac9942020-08-12 14:35:12 +02001352 /* process all the values/objects */
1353 do {
Michal Vaskoe0665742021-02-11 11:08:44 +01001354 lydjson_maintain_children((struct lyd_node_inner *)parent, first_p, &node);
Radek Krejci1798aae2020-07-14 13:26:06 +02001355
Michal Vaskoe0665742021-02-11 11:08:44 +01001356 ret = lydjson_parse_instance(lydctx, (struct lyd_node_inner *)parent, first_p, snode, name, name_len,
1357 prefix, prefix_len, &status, &node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001358 if (ret == LY_EINVAL) {
1359 goto representation_error;
1360 } else if (ret) {
1361 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001362 }
Michal Vasko32ac9942020-08-12 14:35:12 +02001363 } while (status != LYJSON_ARRAY_CLOSED);
Radek Krejci1798aae2020-07-14 13:26:06 +02001364
Michal Vasko32ac9942020-08-12 14:35:12 +02001365 /* move after the array */
1366 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1367 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001368
1369 break;
Michal Vasko32ac9942020-08-12 14:35:12 +02001370 case LYS_LEAF:
Radek Krejci1798aae2020-07-14 13:26:06 +02001371 case LYS_CONTAINER:
1372 case LYS_NOTIF:
1373 case LYS_ACTION:
1374 case LYS_RPC:
Michal Vasko32ac9942020-08-12 14:35:12 +02001375 case LYS_ANYDATA:
1376 case LYS_ANYXML:
1377 if (snode->nodetype == LYS_LEAF) {
1378 if (status == LYJSON_ARRAY) {
1379 expected = "name/[null]";
1380 } else {
1381 expected = "name/value";
1382 }
1383 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +02001384 expected = "name/object";
1385 }
1386
Michal Vasko32ac9942020-08-12 14:35:12 +02001387 /* process the value/object */
Michal Vaskoe0665742021-02-11 11:08:44 +01001388 ret = lydjson_parse_instance(lydctx, (struct lyd_node_inner *)parent, first_p, snode, name, name_len,
1389 prefix, prefix_len, &status, &node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001390 if (ret == LY_EINVAL) {
1391 goto representation_error;
1392 } else if (ret) {
1393 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001394 }
1395
Michal Vasko32ac9942020-08-12 14:35:12 +02001396 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001397 /* rememeber the RPC/action/notification */
1398 lydctx->op_node = node;
1399 }
1400
1401 break;
Radek Krejci1798aae2020-07-14 13:26:06 +02001402 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001403 }
1404
Michal Vasko32ac9942020-08-12 14:35:12 +02001405 /* finally connect the parsed node */
Michal Vaskoe0665742021-02-11 11:08:44 +01001406 lydjson_maintain_children((struct lyd_node_inner *)parent, first_p, &node);
1407
1408 /* rememeber a successfully parsed node */
1409 if (parsed) {
1410 ly_set_add(parsed, node, 1, NULL);
1411 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001412
Radek Krejci5813c362021-01-05 10:51:57 +01001413 /* success */
1414 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001415
1416representation_error:
Michal Vaskoe0665742021-02-11 11:08:44 +01001417 LOG_LOCSET(NULL, parent, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001418 LOGVAL(ctx, LYVE_SYNTAX_JSON, "The %s \"%s\" is expected to be represented as JSON %s, but input data contains name/%s.",
Michal Vasko69730152020-10-09 16:30:07 +02001419 lys_nodetype2str(snode->nodetype), snode->name, expected, lyjson_token2str(status));
Radek Krejciddace2c2021-01-08 11:30:56 +01001420 LOG_LOCBACK(0, parent ? 1 : 0, 0, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +02001421 ret = LY_EVALID;
Radek Krejci5813c362021-01-05 10:51:57 +01001422
1423cleanup:
aPiecek49e2a3a2021-05-13 10:36:46 +02001424 free(value);
Radek Krejci5813c362021-01-05 10:51:57 +01001425 lyd_free_tree(node);
1426 return ret;
Radek Krejci1798aae2020-07-14 13:26:06 +02001427}
1428
1429/**
1430 * @brief Common start of JSON parser processing different types of the input data.
1431 *
1432 * @param[in] ctx libyang context
1433 * @param[in] in Input structure.
Michal Vaskoe0665742021-02-11 11:08:44 +01001434 * @param[in] parse_opts Options for parser, see @ref dataparseroptions.
1435 * @param[in] val_opts Options for the validation phase, see @ref datavalidationoptions.
Radek Krejci1798aae2020-07-14 13:26:06 +02001436 * @param[out] lydctx_p Data parser context to finish validation.
Radek Krejcicd5f6222020-11-12 08:54:13 +01001437 * @param[out] status Storage for the current context's status
Radek Krejci1798aae2020-07-14 13:26:06 +02001438 * @return LY_ERR value.
1439 */
1440static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +01001441lyd_parse_json_init(const struct ly_ctx *ctx, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts,
Radek Krejcicd5f6222020-11-12 08:54:13 +01001442 struct lyd_json_ctx **lydctx_p, enum LYJSON_PARSER_STATUS *status)
Radek Krejci1798aae2020-07-14 13:26:06 +02001443{
1444 LY_ERR ret = LY_SUCCESS;
1445 struct lyd_json_ctx *lydctx;
Radek Krejcid54412f2020-12-17 20:25:35 +01001446 size_t i;
Radek Krejci1798aae2020-07-14 13:26:06 +02001447
Radek Krejcicd5f6222020-11-12 08:54:13 +01001448 assert(lydctx_p);
1449 assert(status);
1450
Radek Krejci1798aae2020-07-14 13:26:06 +02001451 /* init context */
1452 lydctx = calloc(1, sizeof *lydctx);
1453 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
Michal Vaskoe0665742021-02-11 11:08:44 +01001454 lydctx->parse_opts = parse_opts;
1455 lydctx->val_opts = val_opts;
Radek Krejci1798aae2020-07-14 13:26:06 +02001456 lydctx->free = lyd_json_ctx_free;
Radek Krejci1798aae2020-07-14 13:26:06 +02001457
Radek Krejci284f31f2020-09-18 15:40:29 +02001458 /* starting top-level */
1459 for (i = 0; in->current[i] != '\0' && is_jsonws(in->current[i]); i++) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001460 if (in->current[i] == '\n') {
1461 /* new line */
Radek Krejcid54412f2020-12-17 20:25:35 +01001462 LY_IN_NEW_LINE(in);
Michal Vasko61d76362020-10-07 10:47:30 +02001463 }
Radek Krejci284f31f2020-09-18 15:40:29 +02001464 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001465
Radek Krejci284f31f2020-09-18 15:40:29 +02001466 LY_CHECK_ERR_RET(ret = lyjson_ctx_new(ctx, in, &lydctx->jsonctx), free(lydctx), ret);
Radek Krejcicd5f6222020-11-12 08:54:13 +01001467 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
Radek Krejcibb27df32020-11-13 15:39:16 +01001468 if ((*status == LYJSON_END) || (*status == LYJSON_OBJECT_EMPTY) || (*status == LYJSON_OBJECT)) {
Radek Krejci284f31f2020-09-18 15:40:29 +02001469 *lydctx_p = lydctx;
1470 return LY_SUCCESS;
Radek Krejcicd5f6222020-11-12 08:54:13 +01001471 } else {
1472 /* expecting top-level object */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001473 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Expected top-level JSON object, but %s found.",
Radek Krejcicd5f6222020-11-12 08:54:13 +01001474 lyjson_token2str(*status));
1475 *lydctx_p = NULL;
Michal Vasko93509012020-11-13 10:26:09 +01001476 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
Radek Krejcicd5f6222020-11-12 08:54:13 +01001477 return LY_EVALID;
Radek Krejci284f31f2020-09-18 15:40:29 +02001478 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001479}
1480
1481LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +01001482lyd_parse_json(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1483 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type,
1484 struct ly_set *parsed, struct lyd_ctx **lydctx_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001485{
Michal Vaskoe0665742021-02-11 11:08:44 +01001486 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001487 struct lyd_json_ctx *lydctx = NULL;
1488 enum LYJSON_PARSER_STATUS status;
Michal Vaskoe0665742021-02-11 11:08:44 +01001489 uint32_t int_opts;
Radek Krejci1798aae2020-07-14 13:26:06 +02001490
Michal Vaskoe0665742021-02-11 11:08:44 +01001491 rc = lyd_parse_json_init(ctx, in, parse_opts, val_opts, &lydctx, &status);
1492 LY_CHECK_GOTO(rc || status == LYJSON_END || status == LYJSON_OBJECT_EMPTY, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001493
Radek Krejcicd5f6222020-11-12 08:54:13 +01001494 assert(status == LYJSON_OBJECT);
Radek Krejci1798aae2020-07-14 13:26:06 +02001495
Michal Vaskoe0665742021-02-11 11:08:44 +01001496 switch (data_type) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001497 case LYD_TYPE_DATA_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001498 int_opts = LYD_INTOPT_WITH_SIBLINGS;
1499 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001500 case LYD_TYPE_RPC_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001501 int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NO_SIBLINGS;
1502 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001503 case LYD_TYPE_NOTIF_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001504 int_opts = LYD_INTOPT_NOTIF | LYD_INTOPT_NO_SIBLINGS;
1505 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001506 case LYD_TYPE_REPLY_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001507 int_opts = LYD_INTOPT_REPLY | LYD_INTOPT_NO_SIBLINGS;
1508 break;
1509 default:
1510 LOGINT(ctx);
1511 rc = LY_EINT;
1512 goto cleanup;
1513 }
1514 lydctx->int_opts = int_opts;
Radek Krejcif16e2542021-02-17 15:39:23 +01001515 lydctx->ext = ext;
Michal Vaskoe0665742021-02-11 11:08:44 +01001516
1517 /* find the operation node if it exists already */
1518 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
1519
Radek Krejci1798aae2020-07-14 13:26:06 +02001520 /* read subtree(s) */
Michal Vaskoe0665742021-02-11 11:08:44 +01001521 while (lydctx->jsonctx->in->current[0] && (status != LYJSON_OBJECT_CLOSED)) {
1522 rc = lydjson_subtree_r(lydctx, parent, first_p, parsed);
1523 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001524
1525 status = lyjson_ctx_status(lydctx->jsonctx, 0);
Michal Vaskoe0665742021-02-11 11:08:44 +01001526
1527 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1528 break;
1529 }
1530 }
1531
1532 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lydctx->jsonctx->in->current[0] &&
1533 (lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_OBJECT_CLOSED)) {
1534 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
1535 rc = LY_EVALID;
1536 goto cleanup;
1537 }
1538 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) {
1539 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
1540 rc = LY_EVALID;
1541 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001542 }
1543
1544 /* finish linking metadata */
Michal Vaskoe0665742021-02-11 11:08:44 +01001545 rc = lydjson_metadata_finish(lydctx, parent ? lyd_node_child_p(parent) : first_p);
1546 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001547
1548cleanup:
1549 /* there should be no unresolved types stored */
Michal Vaskoe0665742021-02-11 11:08:44 +01001550 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count &&
Michal Vasko49c39d82020-11-06 17:20:27 +01001551 !lydctx->node_when.count));
Radek Krejci1798aae2020-07-14 13:26:06 +02001552
Michal Vaskoe0665742021-02-11 11:08:44 +01001553 if (rc) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001554 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +02001555 } else {
1556 *lydctx_p = (struct lyd_ctx *)lydctx;
1557 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001558 return rc;
Michal Vasko90932a92020-02-12 14:33:03 +01001559}