refactor: improve PATCH handling for module updates with nullable field support
CI / changes (push) Successful in 7s
CI / openapi (push) Has been skipped
CI / go (push) Successful in 47s
CI / docker-web (deploy/docker/evobgp-web/Dockerfile, , evobgp-web) (push) Has been skipped
CI / docker-web (deploy/docker/evobgp-web/Dockerfile, evobgp-all, evobgp-web-all) (push) Has been skipped
CI / docker-bird (push) Has been skipped
CI / bird2 (push) Successful in 17s
CI / docker-go-prime (push) Successful in 24s
CI / docker-go (deploy/docker/evobgp-agent/Dockerfile, , evobgp-agent) (push) Successful in 1m4s
CI / docker-go (evobgp-all, 1, deploy/docker/gobinary/Dockerfile, , evobgp-all) (push) Successful in 2m44s
CI / docker-go (evobgp-api, 1, deploy/docker/gobinary/Dockerfile, , evobgp-api) (push) Successful in 1m30s
CI / docker-go (evobgp-deploy, 0, deploy/docker/gobinary/Dockerfile, , evobgp-deploy) (push) Successful in 1m27s
CI / docker-go (evobgp-ingest, 0, deploy/docker/gobinary/Dockerfile, , evobgp-ingest) (push) Successful in 1m31s
CI / docker-go (evobgp-node, 0, deploy/docker/gobinary/Dockerfile, , evobgp-node) (push) Successful in 1m11s
CI / docker-go (evobgp-render, 0, deploy/docker/gobinary/Dockerfile, , evobgp-render) (push) Successful in 1m22s
CI / docker-go (evobgp-scheduler, 0, deploy/docker/gobinary/Dockerfile, , evobgp-scheduler) (push) Successful in 1m27s

Enhanced the handlePatchModule function to read the request body more robustly and handle nullable fields explicitly. This allows clients to differentiate between omitted fields and fields set to null, improving data integrity during module updates. The changes streamline the JSON unmarshalling process and ensure proper handling of nullable values for community and DoH profile selections.
This commit is contained in:
Denozordec
2026-04-08 21:08:09 +07:00
parent 1765ab89e7
commit e700f90c47
+29 -1
View File
@@ -105,11 +105,39 @@ func (s *Server) handlePatchModule(w http.ResponseWriter, r *http.Request) {
if !ok || !s.requireAtLeast(w, a, "editor") {
return
}
rawBody, err := io.ReadAll(r.Body)
if err != nil {
writeProblem(w, http.StatusBadRequest, "Bad Request", "invalid body")
return
}
var body store.ModulePatch
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
if err := json.Unmarshal(rawBody, &body); err != nil {
writeProblem(w, http.StatusBadRequest, "Bad Request", "invalid json")
return
}
// NOTE:
// In Go, unmarshalling JSON `null` into pointer fields results in nil,
// which is indistinguishable from "field omitted". For PATCH we need to
// distinguish these cases so clients can explicitly clear nullable fields.
var raw map[string]json.RawMessage
if err := json.Unmarshal(rawBody, &raw); err == nil {
if v, ok := raw["default_community_id"]; ok && string(v) == "null" {
empty := ""
body.DefaultCommunityID = &empty
}
if v, ok := raw["doh_profile_id"]; ok && string(v) == "null" {
empty := ""
body.DohProfileID = &empty
}
if v, ok := raw["cron_expr"]; ok && string(v) == "null" {
empty := ""
body.CronExpr = &empty
}
if v, ok := raw["refresh_interval_sec"]; ok && string(v) == "null" {
zero := 0
body.RefreshIntervalSec = &zero
}
}
mod, err := s.store.UpdateModule(a.TenantID, r.PathValue("module_id"), &body)
if err != nil {
writeStoreErr(w, err)