From 1ef9417765dc655e20b601bc286b3f384b11c475 Mon Sep 17 00:00:00 2001 From: Mihai Doarna Date: Wed, 20 Dec 2023 16:30:25 +0200 Subject: [PATCH] Auth: Use camelCase in SSO Settings API (#79755) use camelCase in SSO Settings API --- pkg/services/ssosettings/models/models.go | 42 ++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/pkg/services/ssosettings/models/models.go b/pkg/services/ssosettings/models/models.go index 0f699a4bca7..405c57fd34b 100644 --- a/pkg/services/ssosettings/models/models.go +++ b/pkg/services/ssosettings/models/models.go @@ -4,6 +4,8 @@ import ( "encoding/json" "fmt" "time" + + "github.com/grafana/grafana/pkg/services/featuremgmt/strcase" ) type SettingsSource int @@ -39,4 +41,42 @@ func (s SSOSettings) TableName() string { return "sso_setting" } -// TODO: check if we need custom marshalling/unmarshalling functions for converting the settings keys to camelCase +// MarshalJSON implements the json.Marshaler interface and converts the s.Settings from map[string]any in snake_case to map[string]any in camelCase +func (s SSOSettings) MarshalJSON() ([]byte, error) { + type Alias SSOSettings + aux := &struct { + *Alias + }{ + Alias: (*Alias)(&s), + } + + settings := make(map[string]any) + for k, v := range aux.Settings { + settings[strcase.ToLowerCamel(k)] = v + } + + aux.Settings = settings + return json.Marshal(aux) +} + +// UnmarshalJSON implements the json.Unmarshaler interface and converts the settings from map[string]any camelCase to map[string]interface{} snake_case +func (s *SSOSettings) UnmarshalJSON(data []byte) error { + type Alias SSOSettings + aux := &struct { + *Alias + }{ + Alias: (*Alias)(s), + } + + if err := json.Unmarshal(data, &aux); err != nil { + return err + } + + settings := make(map[string]any) + for k, v := range aux.Settings { + settings[strcase.ToSnake(k)] = v + } + + s.Settings = settings + return nil +}