yin parser CHANGE add support for refine element
diff --git a/src/parser_yin.c b/src/parser_yin.c
index 8532b9a..012190f 100644
--- a/src/parser_yin.c
+++ b/src/parser_yin.c
@@ -166,7 +166,7 @@
  */
 static void free_arg_rec(struct yin_parser_ctx *ctx, struct yin_arg_record *record) {
     (void)ctx; /* unused */
-    if (record->dynamic_content) {
+    if (record && record->dynamic_content) {
         free(record->content);
     }
 }
@@ -1255,6 +1255,46 @@
 }
 
 /**
+ * @brief Parse refine element.
+ *
+ * @param[in,out] ctx YIN parser context for logging and to store current state.
+ * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
+ * @param[in,out] data Data to read from, always moved to currently handled character.
+ * @param[in,out] refines Refines to add to.
+ *
+ * @return LY_ERR values.
+ */
+static LY_ERR
+yin_parse_refine(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
+                 struct lysp_refine **refines)
+{
+    struct lysp_refine *rf;
+
+    /* allocate new refine */
+    LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *refines, rf, LY_EMEM);
+
+    /* parse attribute */
+    LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_TARGET_NODE, &rf->nodeid, Y_STR_ARG, YANG_REFINE));
+    YANG_CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(rf->nodeid), "refine");
+
+    /* parse content */
+    struct yin_subelement subelems[11] = {
+                                            {YANG_CONFIG, &rf->flags, YIN_SUBELEM_UNIQUE},
+                                            {YANG_DEFAULT, &rf->dflts, 0},
+                                            {YANG_DESCRIPTION, &rf->dsc, YIN_SUBELEM_UNIQUE},
+                                            {YANG_IF_FEATURE, &rf->iffeatures, 0},
+                                            {YANG_MANDATORY, &rf->flags, YIN_SUBELEM_UNIQUE},
+                                            {YANG_MAX_ELEMENTS, rf, YIN_SUBELEM_UNIQUE},
+                                            {YANG_MIN_ELEMENTS, rf, YIN_SUBELEM_UNIQUE},
+                                            {YANG_MUST, &rf->musts, 0},
+                                            {YANG_PRESENCE, &rf->presence, YIN_SUBELEM_UNIQUE},
+                                            {YANG_REFERENCE, &rf->ref, YIN_SUBELEM_UNIQUE},
+                                            {YANG_CUSTOM, NULL, 0},
+                                         };
+    return yin_parse_content(ctx, subelems, 11, data, YANG_REFINE, NULL, &rf->exts);
+}
+
+/**
  * @brief Map keyword type to substatement info.
  *
  * @param[in] kw Keyword type.
@@ -1582,6 +1622,7 @@
                     type->flags |=  LYS_SET_RANGE;
                     break;
                 case YANG_REFINE:
+                    ret = yin_parse_refine(ctx, attrs, data, (struct lysp_refine **)subelem->dest);
                     break;
                 case YANG_REQUIRE_INSTANCE:
                     ret = yin_pasrse_reqinstance(ctx, attrs, data, (struct lysp_type *)subelem->dest);
diff --git a/src/tree_schema_free.c b/src/tree_schema_free.c
index ae23ff0..8895385 100644
--- a/src/tree_schema_free.c
+++ b/src/tree_schema_free.c
@@ -305,7 +305,7 @@
     FREE_ARRAY(ctx, dev->exts, lysp_ext_instance_free);
 }
 
-static void
+void
 lysp_refine_free(struct ly_ctx *ctx, struct lysp_refine *ref)
 {
     FREE_STRING(ctx, ref->nodeid);
diff --git a/tests/src/test_parser_yin.c b/tests/src/test_parser_yin.c
index 1494e64..4549d0b 100644
--- a/tests/src/test_parser_yin.c
+++ b/tests/src/test_parser_yin.c
@@ -34,6 +34,7 @@
 void lysp_type_free(struct ly_ctx *ctx, struct lysp_type *type);
 void lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node);
 void lysp_tpdf_free(struct ly_ctx *ctx, struct lysp_tpdf *tpdf);
+void lysp_refine_free(struct ly_ctx *ctx, struct lysp_refine *ref);
 
 struct state {
     struct ly_ctx *ctx;
@@ -2236,6 +2237,54 @@
     st->finished_correctly = true;
 }
 
+static void
+test_refine_elem(void **state)
+{
+    struct state *st = *state;
+    const char *data;
+    struct lysp_refine *refines = NULL;
+
+    /* max subelems */
+    data = ELEMENT_WRAPPER_START
+                "<refine target-node=\"target\">"
+                    "<if-feature name=\"feature\" />"
+                    "<must condition=\"cond\" />"
+                    "<presence value=\"presence\" />"
+                    "<default value=\"def\" />"
+                    "<config value=\"true\" />"
+                    "<mandatory value=\"true\" />"
+                    "<min-elements value=\"10\" />"
+                    "<max-elements value=\"20\" />"
+                    "<description><text>desc</text></description>"
+                    "<reference><text>ref</text></reference>"
+                "</refine>"
+           ELEMENT_WRAPPER_END;
+    assert_int_equal(test_element_helper(st, &data, &refines, NULL, NULL, true), LY_SUCCESS);
+    assert_string_equal(refines->nodeid, "target");
+    assert_string_equal(*refines->dflts, "def");
+    assert_string_equal(refines->dsc, "desc");
+    assert_null(refines->exts);
+    assert_true(refines->flags & LYS_CONFIG_W);
+    assert_true(refines->flags & LYS_MAND_TRUE);
+    assert_string_equal(*refines->iffeatures, "feature");
+    assert_int_equal(refines->max, 20);
+    assert_int_equal(refines->min, 10);
+    assert_string_equal(refines->musts->arg, "cond");
+    assert_string_equal(refines->presence, "presence");
+    assert_string_equal(refines->ref, "ref");
+    FREE_ARRAY(st->ctx, refines, lysp_refine_free);
+    refines = NULL;
+
+    /* min subelems */
+    data = ELEMENT_WRAPPER_START "<refine target-node=\"target\" />" ELEMENT_WRAPPER_END;
+    assert_int_equal(test_element_helper(st, &data, &refines, NULL, NULL, true), LY_SUCCESS);
+    assert_string_equal(refines->nodeid, "target");
+    FREE_ARRAY(st->ctx, refines, lysp_refine_free);
+    refines = NULL;
+
+    st->finished_correctly = true;
+}
+
 int
 main(void)
 {
@@ -2290,7 +2339,7 @@
         cmocka_unit_test_setup_teardown(test_presence_elem, setup_element_test, teardown_element_test),
         cmocka_unit_test_setup_teardown(test_key_elem, setup_element_test, teardown_element_test),
         cmocka_unit_test_setup_teardown(test_typedef_elem, setup_element_test, teardown_element_test),
-
+        cmocka_unit_test_setup_teardown(test_refine_elem, setup_element_test, teardown_element_test),
     };
 
     return cmocka_run_group_tests(tests, setup_ly_ctx, destroy_ly_ctx);