mirror of
https://github.com/jwtk/jjwt.git
synced 2025-02-10 18:24:42 +00:00
Jackson chooses the target type for JSON numbers based on their value, while deserializing without correct typing information present. This leads to a confusing behavior: String token = Jwts.builder() .claim("byte", (byte) 42) .claim("short", (short) 42) .claim("int", 42) .claim("long_small", (long) 42) .claim("long_big", ((long) Integer.MAX_VALUE) + 42) .compact(); Claims claims = (Claims) Jwts.parser().parse(token).getBody(); claims.get("int", Integer.class); // => 42 claims.get("long_big", Long.class); // => ((long) Integer.MAX_VALUE) + 42 claims.get("long_small", Long.class); // throws RequiredTypeException: required=Long, found=Integer claims.get("short", Short.class); // throws RequiredTypeException: required=Short, found=Integer claims.get("byte", Byte.class); // throws RequiredTypeException: required=Byte, found=Integer With this commit, `DefaultClaims.getClaim(String, Class<T>)` will correctly handle cases when required type is `Long`, `Integer`, `Short` or `Byte`: check that value fits in the required type and cast to it. // ... setup is the same as above claims.get("int", Integer.class); // => 42 claims.get("long_big", Long.class); // => ((long) Integer.MAX_VALUE) + 42 claims.get("long_small", Long.class); // => (long) 42 claims.get("short", Short.class); // => (short) 42 claims.get("byte", Byte.class); // => (byte) 42 Fixes #142.