From 54e1c4cf673d14a65157978e559ca178079eb506 Mon Sep 17 00:00:00 2001 From: "taekyu.kang" Date: Thu, 17 Oct 2024 10:57:44 +0900 Subject: [PATCH] bugfix. add null validation --- internal/delivery/http/policy.go | 38 ++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/internal/delivery/http/policy.go b/internal/delivery/http/policy.go index 710e43ed..a96b6edd 100644 --- a/internal/delivery/http/policy.go +++ b/internal/delivery/http/policy.go @@ -115,10 +115,19 @@ func (h *PolicyHandler) CreatePolicy(w http.ResponseWriter, r *http.Request) { // input.Match.Kinds = normaized // } - match, matchYaml, err := ValidateAndGetMatch(input.Target) - if err != nil { - ErrorJSON(w, r, httpErrors.NewBadRequestError(err, "P_INVALID_MATCH", "")) - return + var dto model.Policy + if err = serializer.Map(r.Context(), input, &dto); err != nil { + log.Info(r.Context(), err) + } + + if input.Target != nil { + match, matchYaml, err := ValidateAndGetMatch(input.Target) + if err != nil { + ErrorJSON(w, r, httpErrors.NewBadRequestError(err, "P_INVALID_MATCH", "")) + return + } + dto.Match = match + dto.MatchYaml = matchYaml } if len(input.PolicyResourceName) > 0 { @@ -130,14 +139,6 @@ func (h *PolicyHandler) CreatePolicy(w http.ResponseWriter, r *http.Request) { } } - var dto model.Policy - if err = serializer.Map(r.Context(), input, &dto); err != nil { - log.Info(r.Context(), err) - } - - dto.Match = match - dto.MatchYaml = matchYaml - policyId, err := h.usecase.Create(r.Context(), organizationId, dto) if err != nil { ErrorJSON(w, r, err) @@ -224,10 +225,15 @@ func (h *PolicyHandler) UpdatePolicy(w http.ResponseWriter, r *http.Request) { // input.Match.Kinds = normaized // } - match, matchYaml, err := ValidateAndGetMatch(input.Target) - if err != nil { - ErrorJSON(w, r, httpErrors.NewBadRequestError(err, "P_INVALID_MATCH", "")) - return + var match *domain.Match + var matchYaml *string + if input.Target != nil { + match, matchYaml, err = ValidateAndGetMatch(input.Target) + if err != nil { + ErrorJSON(w, r, httpErrors.NewBadRequestError(err, "P_INVALID_MATCH", "")) + return + } + } var templateId *uuid.UUID = nil