From 5a1d0dac28c1a78e90d3906693228584adcf3ab1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Kundr=C3=A1t?= <jan.kundrat@cesnet.cz>
Date: Thu, 20 Nov 2025 16:13:43 +0100
Subject: [PATCH 38/38] port to libyang v4.2
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Organization: Wires

Depends-on: https://gerrit.cesnet.cz/c/CzechLight/dependencies/+/9012
Change-Id: I1ac6b25a3c1034f06917594732f066fd77d4b26b
Signed-off-by: Mattias Walström <lazzer@gmail.com>
---
 CMakeLists.txt                              |  2 +-
 src/restconf/Server.cpp                     | 10 +++++-----
 src/restconf/utils/yang.cpp                 |  2 +-
 src/sr/AllEvents.cpp                        |  4 ++--
 src/sr/OpticalEvents.cpp                    |  2 +-
 tests/event_watchers.cpp                    |  2 +-
 tests/restconf-subscribed-notifications.cpp |  2 +-
 7 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 4a91128..4bb029b 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -76,7 +76,7 @@ find_package(Boost 1.66 REQUIRED CONFIG COMPONENTS system thread)
 
 pkg_check_modules(SYSREPO REQUIRED sysrepo IMPORTED_TARGET)
 pkg_check_modules(SYSREPO-CPP REQUIRED IMPORTED_TARGET sysrepo-cpp>=7)
-pkg_check_modules(LIBYANG-CPP REQUIRED IMPORTED_TARGET libyang-cpp>=5)
+pkg_check_modules(LIBYANG-CPP REQUIRED IMPORTED_TARGET libyang-cpp>=6)
 pkg_check_modules(SYSTEMD IMPORTED_TARGET libsystemd)
 pkg_check_modules(PAM REQUIRED IMPORTED_TARGET pam)
 pkg_check_modules(DOCOPT REQUIRED IMPORTED_TARGET docopt)
diff --git a/src/restconf/Server.cpp b/src/restconf/Server.cpp
index d166236..3c7c3c7 100644
--- a/src/restconf/Server.cpp
+++ b/src/restconf/Server.cpp
@@ -111,7 +111,7 @@ void rejectWithErrorImpl(libyang::Context ctx, const libyang::DataFormat& dataFo
     }
 
     res.write_head(code, headers);
-    res.end(*parent.printStr(dataFormat, libyang::PrintFlags::WithSiblings));
+    res.end(*parent.printStr(dataFormat, libyang::PrintFlags::Siblings));
 }
 
 void rejectWithError(libyang::Context ctx, const libyang::DataFormat& dataFormat, const request& req, const response& res, const int code, const std::string errorType, const std::string& errorTag, const std::string& errorMessage, const std::optional<std::string>& errorPath)
@@ -543,7 +543,7 @@ void processActionOrRPC(std::shared_ptr<RequestContext> requestCtx, const std::c
                                         contentType(requestCtx->dataFormat.response),
                                         CORS,
                                     });
-    requestCtx->res.end(*envelope->printStr(requestCtx->dataFormat.response, libyang::PrintFlags::WithSiblings | libyang::PrintFlags::KeepEmptyCont));
+    requestCtx->res.end(*envelope->printStr(requestCtx->dataFormat.response, libyang::PrintFlags::Siblings | libyang::PrintFlags::EmptyContainers));
 }
 
 void processPost(std::shared_ptr<RequestContext> requestCtx, const std::chrono::milliseconds timeout)
@@ -709,7 +709,7 @@ void processYangPatch(std::shared_ptr<RequestContext> requestCtx, const std::chr
     yangPatchStatus->newExtPath(yangPatchStatusExt, "/ietf-yang-patch:yang-patch-status/ok", std::nullopt);
 
     requestCtx->res.write_head(200, {contentType(requestCtx->dataFormat.response), CORS});
-    requestCtx->res.end(*yangPatchStatus->printStr(requestCtx->dataFormat.response, libyang::PrintFlags::WithSiblings));
+    requestCtx->res.end(*yangPatchStatus->printStr(requestCtx->dataFormat.response, libyang::PrintFlags::Siblings));
 }
 
 void processPutOrPlainPatch(std::shared_ptr<RequestContext> requestCtx, const std::chrono::milliseconds timeout)
@@ -842,7 +842,7 @@ libyang::PrintFlags libyangPrintFlags(const libyang::DataNode& dataNode, const s
     } catch(const libyang::Error& e) {
     }
 
-    libyang::PrintFlags ret = libyang::PrintFlags::WithSiblings | libyang::PrintFlags::KeepEmptyCont;
+    libyang::PrintFlags ret = libyang::PrintFlags::Siblings | libyang::PrintFlags::EmptyContainers;
 
     if (!withDefaults && node && (node->schema().nodeType() == libyang::NodeType::Leaf || node->schema().nodeType() == libyang::NodeType::Leaflist) && node->asTerm().isImplicitDefault()) {
         return ret | libyang::PrintFlags::WithDefaultsAll;
@@ -1116,7 +1116,7 @@ Server::Server(
                 case RestconfRequest::Type::ListRPC:
                     res.write_head(200, {contentType(dataFormat.response), CORS});
                     res.end(*apiResource(sess.getContext(), restconfRequest.type, dataFormat.response)
-                                 .printStr(dataFormat.response, libyang::PrintFlags::WithSiblings | libyang::PrintFlags::KeepEmptyCont));
+                                 .printStr(dataFormat.response, libyang::PrintFlags::Siblings | libyang::PrintFlags::EmptyContainers));
                     break;
 
                 case RestconfRequest::Type::GetData: {
diff --git a/src/restconf/utils/yang.cpp b/src/restconf/utils/yang.cpp
index 9358840..5c311c1 100644
--- a/src/restconf/utils/yang.cpp
+++ b/src/restconf/utils/yang.cpp
@@ -112,7 +112,7 @@ std::string as_restconf_notification(const libyang::Context& ctx, libyang::DataF
     envelope->insertChild(*eventTime);
     envelope->insertChild(notification);
 
-    auto res = *envelope->printStr(dataFormat, libyang::PrintFlags::WithSiblings | libyang::PrintFlags::KeepEmptyCont);
+    auto res = *envelope->printStr(dataFormat, libyang::PrintFlags::Siblings | libyang::PrintFlags::EmptyContainers);
 
     // notification node comes from sysrepo and sysrepo will free this; if not unlinked then envelope destructor would try to free this as well
     notification.unlink();
diff --git a/src/sr/AllEvents.cpp b/src/sr/AllEvents.cpp
index 7b6226b..2202b36 100644
--- a/src/sr/AllEvents.cpp
+++ b/src/sr/AllEvents.cpp
@@ -110,10 +110,10 @@ sysrepo::ErrorCode AllEvents::onChange(sysrepo::Session session, const std::stri
                 break;
             }
         };
-        auto json = *copy.printStr(libyang::DataFormat::JSON, libyang::PrintFlags::WithSiblings | libyang::PrintFlags::KeepEmptyCont);
+        auto json = *copy.printStr(libyang::DataFormat::JSON, libyang::PrintFlags::Siblings | libyang::PrintFlags::EmptyContainers);
         spdlog::info("JSON: {}", json);
         spdlog::warn("FULL JSON: {}",
-                *session.getData('/' + module + ":*")->printStr(libyang::DataFormat::JSON, libyang::PrintFlags::WithSiblings));
+                *session.getData('/' + module + ":*")->printStr(libyang::DataFormat::JSON, libyang::PrintFlags::Siblings));
         change(module, json);
     }
 
diff --git a/src/sr/OpticalEvents.cpp b/src/sr/OpticalEvents.cpp
index 25906a8..3f78ddc 100644
--- a/src/sr/OpticalEvents.cpp
+++ b/src/sr/OpticalEvents.cpp
@@ -15,7 +15,7 @@
 namespace {
 std::string dumpDataFrom(sysrepo::Session session, const std::string& module)
 {
-    return *session.getData('/' + module + ":*")->printStr(libyang::DataFormat::JSON, libyang::PrintFlags::WithSiblings | libyang::PrintFlags::KeepEmptyCont);
+    return *session.getData('/' + module + ":*")->printStr(libyang::DataFormat::JSON, libyang::PrintFlags::Siblings | libyang::PrintFlags::EmptyContainers);
 }
 }
 
diff --git a/tests/event_watchers.cpp b/tests/event_watchers.cpp
index 26ba951..38ee7be 100644
--- a/tests/event_watchers.cpp
+++ b/tests/event_watchers.cpp
@@ -23,7 +23,7 @@ void datastoreChanges(auto session, auto& dsChangesMock, auto path)
 
 void datastoreNewState(auto session, auto& dsChangesMock, auto path)
 {
-    dsChangesMock.contentAfterChange(session.getData(path)->printStr(libyang::DataFormat::JSON, libyang::PrintFlags::WithSiblings));
+    dsChangesMock.contentAfterChange(session.getData(path)->printStr(libyang::DataFormat::JSON, libyang::PrintFlags::Siblings));
 }
 }
 
diff --git a/tests/restconf-subscribed-notifications.cpp b/tests/restconf-subscribed-notifications.cpp
index d42fa96..73b9fab 100644
--- a/tests/restconf-subscribed-notifications.cpp
+++ b/tests/restconf-subscribed-notifications.cpp
@@ -92,7 +92,7 @@ EstablishSubscriptionResult establishSubscription(
     data->unlinkWithSiblings();
     envelope->insertChild(*data);
 
-    auto body = *envelope->printStr(rpcEncoding, libyang::PrintFlags::WithSiblings);
+    auto body = *envelope->printStr(rpcEncoding, libyang::PrintFlags::Siblings);
     auto resp = post(RESTCONF_OPER_ROOT "/ietf-subscribed-notifications:establish-subscription", requestHeaders, body);
     REQUIRE(resp.equalStatusCodeAndHeaders(Response{200, expectedHeaders, ""}));
 
-- 
2.43.0

