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

Claim asString returns null for types with valid string representations #614

Closed
worthc21 opened this issue Oct 7, 2022 · 4 comments · Fixed by #615
Closed

Claim asString returns null for types with valid string representations #614

worthc21 opened this issue Oct 7, 2022 · 4 comments · Fixed by #615

Comments

@worthc21
Copy link

worthc21 commented Oct 7, 2022

Describe the problem

getClaim("X").asX returns null for values that have valid toString representations

When creating a private claim with an integer value -
withClaim("ID", 1234)
the decoded JWT
getClaim("ID").asString()
returns null

When creating a private claim with a boolean value -
withClaim("flag", true)
the decoded JWT
getClaim("flag").asString()
returns null

What was the expected behavior?

An integer value would return the string representation of the value (just as Integer.toString() does)
A boolean value would return the string representation of the value (just as Boolean.toString() does)

Versions

java-jwt: 4.0.0
java - JDK 11
spring boot - 2.7.4

@jimmyjames
Copy link
Contributor

Thanks for raising @worthc21. Yes, that is the expected behavior, as we first check that the node is textual. For the string value, you can use toString(). Hope that helps!

@jimmyjames
Copy link
Contributor

I updated the javadocs in #615 to be more accurate with the actual behavior. Thanks!

@worthc21
Copy link
Author

Nice - thanks for taking the time to address!

@kelalaka153
Copy link

I did not find this to be a good idea. Why can't we have the claim just as a string instead we get the null? The programmer decides to have it as a raw string or not. Instead, the library forces it to parse as .asObject(PoJo::class.java).

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

Successfully merging a pull request may close this issue.

3 participants