Skip to content

Commit

Permalink
fix: ignore empty json strings for locale
Browse files Browse the repository at this point in the history
  • Loading branch information
muhlemmer committed Nov 11, 2024
1 parent 7591055 commit e59961c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
3 changes: 3 additions & 0 deletions pkg/oidc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ func (l *Locale) MarshalJSON() ([]byte, error) {
// to an empty value (language "und") and no error will be returned.
// This state can be checked with the `l.Tag().IsRoot()` method.
func (l *Locale) UnmarshalJSON(data []byte) error {
if len(data) == 0 || string(data) == "\"\"" {
return nil
}
err := json.Unmarshal(data, &l.tag)
if err == nil {
return nil
Expand Down
43 changes: 34 additions & 9 deletions pkg/oidc/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,30 @@ func TestLocale_UnmarshalJSON(t *testing.T) {
want dst
wantErr bool
}{
{
name: "value not present",
input: `{}`,
wantErr: false,
want: dst{
Locale: nil,
},
},
{
name: "null",
input: `{"locale": null}`,
wantErr: false,
want: dst{
Locale: nil,
},
},
{
name: "empty, ignored",
input: `{"locale": ""}`,
wantErr: false,
want: dst{
Locale: &Locale{},
},
},
{
name: "afrikaans, ok",
input: `{"locale": "af"}`,
Expand All @@ -237,16 +261,17 @@ func TestLocale_UnmarshalJSON(t *testing.T) {
wantErr: true,
},
}

for _, tt := range tests {
var got dst
err := json.Unmarshal([]byte(tt.input), &got)
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
assert.Equal(t, tt.want, got)
t.Run(tt.name, func(t *testing.T) {
var got dst
err := json.Unmarshal([]byte(tt.input), &got)
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
assert.Equal(t, tt.want, got)
})
}
}

Expand Down

0 comments on commit e59961c

Please sign in to comment.