yangerd: Return {} instead of 404 if no match

This commit is contained in:
Mattias Walström
2026-06-27 08:41:22 +02:00
parent 6a669e6d92
commit d90d0d2a9c
2 changed files with 15 additions and 5 deletions
+8 -3
View File
@@ -131,10 +131,15 @@ func (s *Server) handleGet(conn net.Conn, req *Request) {
data := s.tree.Get(key)
if data == nil {
// An absent subtree is a normal answer for operational data --
// the feature is simply not active (e.g. NTP unconfigured).
// Answer ok with an empty object rather than an error, so every
// client gets "no data" without special-casing. Deliberately
// NOT {"<key>": {}}: that would make libyang instantiate the
// container, which for presence containers is real data.
WriteResponse(conn, &Response{
Status: "error",
Code: 404,
Message: "path not found: " + path,
Status: "ok",
Data: json.RawMessage(`{}`),
})
return
}
+7 -2
View File
@@ -33,8 +33,13 @@ func TestServerGetNotFound(t *testing.T) {
tr := tree.New()
resp := serverRoundTrip(t, tr, true, &Request{Method: "get", Path: "/nonexistent"})
if resp.Status != "error" || resp.Code != 404 {
t.Fatalf("expected 404 error, got %+v", resp)
// An absent subtree is "no data", not an error: ok + empty object,
// so clients (statd, yangerctl) need no special-casing.
if resp.Status != "ok" {
t.Fatalf("expected ok, got %+v", resp)
}
if string(resp.Data) != "{}" {
t.Fatalf("expected empty object data, got %s", resp.Data)
}
}