fix: nil pointer dereference on UserInfoAddress (#207)
* oidc: add test case to reproduce #203 Running the tests will always result in a nil pointer dereference on UserInfoAddress. Co-authored-by: Livio Spring <livio.a@gmail.com> * fix: nil pointer dereference on UserInfoAddress userinfo.UnmarshalJSON now only sets the Address field if it was present in the json. userinfo.GetAddress will always return a non-nil value of UserInfoAddress to allow for safe chaining of Get functions. Fixes #203 --------- Co-authored-by: Livio Spring <livio.a@gmail.com>
This commit is contained in:
parent
1535ea4f6c
commit
fa222c5efb
2 changed files with 44 additions and 1 deletions
|
@ -167,6 +167,9 @@ func (u *userinfo) IsPhoneNumberVerified() bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *userinfo) GetAddress() UserInfoAddress {
|
func (u *userinfo) GetAddress() UserInfoAddress {
|
||||||
|
if u.Address == nil {
|
||||||
|
return &userInfoAddress{}
|
||||||
|
}
|
||||||
return u.Address
|
return u.Address
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -389,7 +392,11 @@ func (u *userinfo) UnmarshalJSON(data []byte) error {
|
||||||
if err := json.Unmarshal(data, &a); err != nil {
|
if err := json.Unmarshal(data, &a); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
u.Address = a.Address
|
|
||||||
|
if a.Address != nil {
|
||||||
|
u.Address = a.Address
|
||||||
|
}
|
||||||
|
|
||||||
u.UpdatedAt = Time(time.Unix(a.UpdatedAt, 0).UTC())
|
u.UpdatedAt = Time(time.Unix(a.UpdatedAt, 0).UTC())
|
||||||
|
|
||||||
if err := json.Unmarshal(data, &u.claims); err != nil {
|
if err := json.Unmarshal(data, &u.claims); err != nil {
|
||||||
|
|
|
@ -81,3 +81,39 @@ func TestUserInfoEmailVerifiedUnmarshal(t *testing.T) {
|
||||||
}, uie)
|
}, uie)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// issue 203 test case.
|
||||||
|
func Test_userinfo_GetAddress_issue_203(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
data string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "with address",
|
||||||
|
data: `{"address":{"street_address":"Test 789\nPostfach 2"},"email":"test","email_verified":true,"name":"Test","phone_number":"0791234567","phone_number_verified":true,"private_claim":"test","sub":"test"}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "without address",
|
||||||
|
data: `{"email":"test","email_verified":true,"name":"Test","phone_number":"0791234567","phone_number_verified":true,"private_claim":"test","sub":"test"}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "null address",
|
||||||
|
data: `{"address":null,"email":"test","email_verified":true,"name":"Test","phone_number":"0791234567","phone_number_verified":true,"private_claim":"test","sub":"test"}`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
info := &userinfo{}
|
||||||
|
err := json.Unmarshal([]byte(tt.data), info)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
info.GetAddress().GetCountry() //<- used to panic
|
||||||
|
|
||||||
|
// now shortly assure that a marshalling still produces the same as was parsed into the struct
|
||||||
|
marshal, err := json.Marshal(info)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, tt.data, string(marshal))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue