Skip to content

Commit db5b9f2

Browse files
authored
fix: handle empty attribute_schema in account type handler (#1275)
Convert empty proto string to nil json.RawMessage instead of json.RawMessage("") which PostgreSQL rejects as invalid JSON syntax when inserting into jsonb columns. Co-authored-by: Ben Coombs <bjcoombs@users.noreply.github.com>
1 parent 71cf90b commit db5b9f2

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

services/reference-data/handler/account_type_handler.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func (s *AccountTypeService) CreateDraft(ctx context.Context, req *pb.CreateDraf
195195
ValidationCEL: req.GetValidationCel(),
196196
BucketingCEL: req.GetBucketingCel(),
197197
EligibilityCEL: req.GetEligibilityCel(),
198-
AttributeSchema: json.RawMessage(req.GetAttributeSchema()),
198+
AttributeSchema: toRawJSON(req.GetAttributeSchema()),
199199
Attributes: stringMapToAnyMap(req.GetAttributes()),
200200
Compiler: s.compiler,
201201
})
@@ -244,7 +244,7 @@ func (s *AccountTypeService) UpdateDefinition(ctx context.Context, req *pb.Updat
244244
ValidationCEL: req.GetValidationCel(),
245245
BucketingCEL: req.GetBucketingCel(),
246246
EligibilityCEL: req.GetEligibilityCel(),
247-
AttributeSchema: json.RawMessage(req.GetAttributeSchema()),
247+
AttributeSchema: toRawJSON(req.GetAttributeSchema()),
248248
Attributes: stringMapToAnyMap(req.GetAttributes()),
249249
}
250250

@@ -709,6 +709,16 @@ func parseConversionMethodPair(idStr string, version int32) (*uuid.UUID, *int, e
709709
return &id, &v, nil
710710
}
711711

712+
// toRawJSON converts a proto string field to json.RawMessage, returning nil for
713+
// empty strings so that PostgreSQL jsonb columns receive NULL rather than
714+
// invalid empty-string JSON.
715+
func toRawJSON(s string) json.RawMessage {
716+
if s == "" {
717+
return nil
718+
}
719+
return json.RawMessage(s)
720+
}
721+
712722
func stringMapToAnyMap(m map[string]string) map[string]any {
713723
if m == nil {
714724
return nil

0 commit comments

Comments
 (0)