Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JwtBuilder doesn't respect System.Text.Json.Serialization.JsonPropertyName attribute when deserializing into a record #379

Closed
Iapetus-11 opened this issue Mar 14, 2022 · 5 comments
Assignees
Labels

Comments

@Iapetus-11
Copy link

public record UserJwtTokenPayload
{
    [JsonPropertyName("uid")]
    public uint UserId { get; set; }
    
    ...
}

I have this record and this code

var data = JwtBuilder.Create()
    .WithAlgorithm(new HMACSHA256Algorithm())
    .WithSecret(_jwtSecret)
    .MustVerifySignature()
    .Decode<UserJwtTokenPayload>(authHeader);

return data?.UserId;

In this case, data is an instance of UserJwtTokenPayload with default values because .Decode<...>(...) does not respect the [JsonPropertyName(...)] attribute.

If I were to rewrite this to use System.Text.Json, this works just fine. Example:

var data = JwtBuilder.Create()
    .WithAlgorithm(new HMACSHA256Algorithm())
    .WithSecret(_jwtSecret)
    .MustVerifySignature()
    .Decode(authHeader);

return JsonDocument.Parse(data).Deserialize<UserJwtTokenPayload>()?.UserId;

It seems that it should be respecting JsonPropertyName attributes when decoding as the library does when actually encoding.

@Iapetus-11 Iapetus-11 changed the title JwtBuilder doesn't respect JsonPropertyName attribute when deserializing into a class / record JwtBuilder doesn't respect JsonPropertyName attribute when deserializing into a record Mar 14, 2022
@abatishchev
Copy link
Member

Hi,
[JsonPropertyName] looks like a System.Text.Json attribute. Yeah, I think it is.
Jwt.Net uses Json.Net which will ignore it. You need to use its attribute [JsonProperty] to achieve the same.
Makes sense?
Or you can specify custom json serializer

public JwtBuilder WithSerializer(IJsonSerializer serializer)
{
_serializer = serializer;
return this;
}

Then ideally I'd ask you to contribute to the library and ship the implementation as an extension, see:

@abatishchev abatishchev self-assigned this Mar 14, 2022
@abatishchev abatishchev changed the title JwtBuilder doesn't respect JsonPropertyName attribute when deserializing into a record JwtBuilder doesn't respect System.Text.Json.Serialization.JsonPropertyName attribute when deserializing into a record Mar 14, 2022
@Iapetus-11
Copy link
Author

Iapetus-11 commented Mar 14, 2022

Alright thank you, though it's still weird that it does respect the attribute when serializing.

Then ideally I'd ask you to contribute to the library and ship the implementation as an extension

Maybe, I'm still new to C#, I'll think about it.

@abatishchev
Copy link
Member

To explain little more: those are 2 different JSON serializing libraries with their own set of attributes, what means one is unaware of the another.

@Iapetus-11
Copy link
Author

Oh I understand that, which is why it's so weird that one library is partially respecting the attributes of another.

@abatishchev
Copy link
Member

Sorry, it might be confusing indeed. System.Text.Json reuses some methods and attributes names from Json.Net. When the name is same it would look like one respects another.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants