fix: ignore empty json strings for locale (#678)
* Revert "fix: ignore all unmarshal errors from locale (#673)"
This reverts commit fbf009fe75
.
* fix: ignore empty json strings for locale
This commit is contained in:
parent
7a767d8568
commit
efd6fdad7a
2 changed files with 53 additions and 20 deletions
|
@ -3,6 +3,7 @@ package oidc
|
||||||
import (
|
import (
|
||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -77,17 +78,26 @@ func (l *Locale) MarshalJSON() ([]byte, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshalJSON implements json.Unmarshaler.
|
// UnmarshalJSON implements json.Unmarshaler.
|
||||||
// All unmarshal errors for are ignored.
|
// When [language.ValueError] is encountered, the containing tag will be set
|
||||||
// When an error is encountered, the containing tag will be set
|
|
||||||
// to an empty value (language "und") and no error will be returned.
|
// to an empty value (language "und") and no error will be returned.
|
||||||
// This state can be checked with the `l.Tag().IsRoot()` method.
|
// This state can be checked with the `l.Tag().IsRoot()` method.
|
||||||
func (l *Locale) UnmarshalJSON(data []byte) error {
|
func (l *Locale) UnmarshalJSON(data []byte) error {
|
||||||
err := json.Unmarshal(data, &l.tag)
|
if len(data) == 0 || string(data) == "\"\"" {
|
||||||
if err != nil {
|
|
||||||
l.tag = language.Tag{}
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
err := json.Unmarshal(data, &l.tag)
|
||||||
|
if err == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// catch "well-formed but unknown" errors
|
||||||
|
var target language.ValueError
|
||||||
|
if errors.As(err, &target) {
|
||||||
|
l.tag = language.Tag{}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
type Locales []language.Tag
|
type Locales []language.Tag
|
||||||
|
|
||||||
|
|
|
@ -217,6 +217,30 @@ func TestLocale_UnmarshalJSON(t *testing.T) {
|
||||||
want dst
|
want dst
|
||||||
wantErr bool
|
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",
|
name: "afrikaans, ok",
|
||||||
input: `{"locale": "af"}`,
|
input: `{"locale": "af"}`,
|
||||||
|
@ -234,13 +258,11 @@ func TestLocale_UnmarshalJSON(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "bad form, error",
|
name: "bad form, error",
|
||||||
input: `{"locale": "g!!!!!"}`,
|
input: `{"locale": "g!!!!!"}`,
|
||||||
want: dst{
|
wantErr: true,
|
||||||
Locale: &Locale{},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
var got dst
|
var got dst
|
||||||
err := json.Unmarshal([]byte(tt.input), &got)
|
err := json.Unmarshal([]byte(tt.input), &got)
|
||||||
if tt.wantErr {
|
if tt.wantErr {
|
||||||
|
@ -249,6 +271,7 @@ func TestLocale_UnmarshalJSON(t *testing.T) {
|
||||||
}
|
}
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, tt.want, got)
|
assert.Equal(t, tt.want, got)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue