Merge "Fix crashes on leafref value printing"
diff --git a/src/libyang_utils.cpp b/src/libyang_utils.cpp
index aa2b3f0..a7dd682 100644
--- a/src/libyang_utils.cpp
+++ b/src/libyang_utils.cpp
@@ -41,6 +41,11 @@
         auto v = value->dec64();
         return v.value * std::pow(10, -v.digits);
     }
+    case LY_TYPE_LEAFREF:
+    {
+        libyang::Data_Node_Leaf_List toPrint{value->leafref()};
+        return leafValueFromValue(toPrint.value(), toPrint.value_type());
+    }
     default: // TODO: implement all types
         return "(can't print)"s;
     }
@@ -67,7 +72,7 @@
         }
         if (it->schema()->nodetype() == LYS_LEAF || it->schema()->nodetype() == LYS_LEAFLIST) {
             libyang::Data_Node_Leaf_List leaf(it);
-            auto value = leafValueFromValue(leaf.value(), leaf.leaf_type()->base());
+            auto value = leafValueFromValue(leaf.value(), leaf.value_type());
             res.emplace_back(stripXPathPrefix(it->path()), value);
         }
     }
diff --git a/tests/datastore_access.cpp b/tests/datastore_access.cpp
index e505851..10b6a6f 100644
--- a/tests/datastore_access.cpp
+++ b/tests/datastore_access.cpp
@@ -276,29 +276,28 @@
             datastore.commitChanges();
         }
 
-        // The commitChanges method has to be called in each of the
-        // SECTIONs, because the REQUIRE_CALL only works inside the given
-        // SECTION.
+        std::string value;
         SECTION("Dan")
         {
-            REQUIRE_CALL(mock, write("/example-schema:bossPerson", std::nullopt, "Dan"s));
-            datastore.setLeaf("/example-schema:bossPerson", std::string{"Dan"});
-            datastore.commitChanges();
+            value = "Dan";
         }
 
         SECTION("Elfi")
         {
-            REQUIRE_CALL(mock, write("/example-schema:bossPerson", std::nullopt, "Elfi"s));
-            datastore.setLeaf("/example-schema:bossPerson", std::string{"Elfi"});
-            datastore.commitChanges();
+            value = "Elfi";
         }
 
         SECTION("Kolafa")
         {
-            REQUIRE_CALL(mock, write("/example-schema:bossPerson", std::nullopt, "Kolafa"s));
-            datastore.setLeaf("/example-schema:bossPerson", std::string{"Kolafa"});
+            value = "Kolafa";
+        }
+
+        datastore.setLeaf("/example-schema:bossPerson", value);
+        {
+            REQUIRE_CALL(mock, write("/example-schema:bossPerson", std::nullopt, value));
             datastore.commitChanges();
         }
+        REQUIRE(datastore.getItems("/example-schema:bossPerson") == DatastoreAccess::Tree{{"/example-schema:bossPerson", value}});
     }
     SECTION("bool values get correctly represented as bools")
     {