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:
Tim Möhlmann 2025-03-14 12:30:08 +02:00 committed by GitHub
parent 7a767d8568
commit efd6fdad7a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 53 additions and 20 deletions

View file

@ -3,6 +3,7 @@ package oidc
import (
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"
@ -77,16 +78,25 @@ func (l *Locale) MarshalJSON() ([]byte, error) {
}
// UnmarshalJSON implements json.Unmarshaler.
// All unmarshal errors for are ignored.
// When an error is encountered, the containing tag will be set
// When [language.ValueError] is encountered, the containing tag will be set
// 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 {
err := json.Unmarshal(data, &l.tag)
if err != nil {
l.tag = language.Tag{}
if len(data) == 0 || string(data) == "\"\"" {
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