Skip to content

Commit 35ee661

Browse files
committed
data parsers CHANGE try to keep as specific error messages as possible
1 parent 5dca593 commit 35ee661

3 files changed

Lines changed: 31 additions & 11 deletions

File tree

src/parser.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,11 +1267,6 @@ lyp_parse_value(struct lys_type *type, const char **value_, struct lyxml_elem *x
12671267

12681268
ident = resolve_identref(type, value, (struct lyd_node*)leaf);
12691269
if (!ident) {
1270-
if (leaf) {
1271-
LOGVAL(LYE_INVAL, LY_VLOG_LYD, leaf, *value_, leaf->schema->name);
1272-
} else {
1273-
LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid identityref value \"%s\" (%s).", value, *value_);
1274-
}
12751270
goto cleanup;
12761271
} else if (leaf) {
12771272
/* store the result */
@@ -1308,11 +1303,11 @@ lyp_parse_value(struct lys_type *type, const char **value_, struct lyxml_elem *x
13081303
goto cleanup;
13091304
}
13101305
}
1311-
if (resolvable && tree && !resolve_instid(tree, value) && type->info.inst.req) {
1306+
if (resolvable && tree && !resolve_instid(tree, value) && (ly_errno || type->info.inst.req)) {
13121307
if (leaf) {
13131308
LOGVAL(LYE_INVAL, LY_VLOG_LYD, leaf, *value_, leaf->schema->name);
13141309
} else {
1315-
LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid instance-identifier value \"%s\" (%s).", value, *value_);
1310+
LOGVAL(LYE_SPEC, leaf ? LY_VLOG_LYD : LY_VLOG_NONE, leaf, "Invalid instance-identifier value \"%s\" (%s).", value, *value_);
13161311
}
13171312
goto cleanup;
13181313
} else if (!resolvable && leaf) {

tests/data/test_values.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,30 @@ test_xmltojson_identityref(void **state)
144144
assert_string_equal(st->data, result);
145145
}
146146

147+
static void
148+
test_xmltojson_identityref2(void **state)
149+
{
150+
struct state *st = (*state);
151+
const char *yang = "module y {"
152+
" namespace urn:y;"
153+
" prefix y;"
154+
" identity vehicle;"
155+
" identity car { base vehicle; }"
156+
" leaf y { type identityref { base y:vehicle; } default y:car; }"
157+
"}";
158+
const char *result = "<y xmlns=\"urn:y\" xmlns:y=\"urn:y\">y:car</y>";
159+
160+
assert_ptr_not_equal(lys_parse_mem(st->ctx, yang, LYS_IN_YANG), NULL);
161+
162+
st->dt = NULL;
163+
lyd_validate(&st->dt, LYD_OPT_CONFIG, st->ctx);
164+
assert_ptr_not_equal(st->dt, NULL);
165+
166+
lyd_print_mem(&st->data, st->dt, LYD_XML, LYP_WITHSIBLINGS | LYP_WD_ALL);
167+
assert_ptr_not_equal(st->data, NULL);
168+
assert_string_equal(st->data, result);
169+
}
170+
147171
static void
148172
test_xmltojson_instanceid(void **state)
149173
{
@@ -270,6 +294,7 @@ int main(void)
270294
const struct CMUnitTest tests[] = {
271295
cmocka_unit_test_setup_teardown(test_default_int, setup_f, teardown_f),
272296
cmocka_unit_test_setup_teardown(test_xmltojson_identityref, setup_f, teardown_f),
297+
cmocka_unit_test_setup_teardown(test_xmltojson_identityref2, setup_f, teardown_f),
273298
cmocka_unit_test_setup_teardown(test_xmltojson_instanceid, setup_f, teardown_f),
274299
cmocka_unit_test_setup_teardown(test_canonical, setup_f, teardown_f),};
275300

tests/schema/test_typedef.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,12 @@ test_typedef_11_multidents_yang(void **state)
250250

251251
root = lyd_parse_mem(st->ctx, data1, LYD_XML, LYD_OPT_CONFIG);
252252
assert_ptr_equal(root, NULL);
253-
assert_string_equal(ly_errmsg(), "Invalid value \"des\" in \"l2\" element.");
253+
assert_string_equal(ly_errmsg(), "Failed to resolve identityref \"des\".");
254254
assert_string_equal(ly_errpath(), "/x:l2");
255255

256256
root = lyd_parse_mem(st->ctx, data2, LYD_XML, LYD_OPT_CONFIG);
257257
assert_ptr_equal(root, NULL);
258-
assert_string_equal(ly_errmsg(), "Invalid value \"des3\" in \"l2\" element.");
258+
assert_string_equal(ly_errmsg(), "Failed to resolve identityref \"des3\".");
259259
assert_string_equal(ly_errpath(), "/x:l2");
260260

261261
root = lyd_parse_mem(st->ctx, data3, LYD_XML, LYD_OPT_CONFIG);
@@ -290,12 +290,12 @@ test_typedef_11_multidents_yin(void **state)
290290

291291
root = lyd_parse_mem(st->ctx, data1, LYD_XML, LYD_OPT_CONFIG);
292292
assert_ptr_equal(root, NULL);
293-
assert_string_equal(ly_errmsg(), "Invalid value \"des\" in \"l2\" element.");
293+
assert_string_equal(ly_errmsg(), "Failed to resolve identityref \"des\".");
294294
assert_string_equal(ly_errpath(), "/x:l2");
295295

296296
root = lyd_parse_mem(st->ctx, data2, LYD_XML, LYD_OPT_CONFIG);
297297
assert_ptr_equal(root, NULL);
298-
assert_string_equal(ly_errmsg(), "Invalid value \"des3\" in \"l2\" element.");
298+
assert_string_equal(ly_errmsg(), "Failed to resolve identityref \"des3\".");
299299
assert_string_equal(ly_errpath(), "/x:l2");
300300

301301
root = lyd_parse_mem(st->ctx, data3, LYD_XML, LYD_OPT_CONFIG);

0 commit comments

Comments
 (0)