Skip to content

PublicClaims.AUDIENCE (aud) expected to be String or "Array of Strings", not Collection(aka List) by PayloadSerializer #508

@yeDor

Description

@yeDor

Describe the problem

By default jackson deserializes to ArrayList. Can not find an easy way to change this behavior.
so, having payload = {exp=1630398915, aud=["firma_x","firma_y"]} following code

    Map payloadMap = new ObjectMapper().readValue(payload, Map.class);
    String result = JWT.create()
                       .withPayload(payloadMap)
                       .sign(Algorithm.HMAC256("secret"));

will cause an exception:

Caused by: java.lang.ClassCastException: class java.util.ArrayList cannot be cast to class [Ljava.lang.String; (java.util.ArrayList and [Ljava.lang.String; are in module java.base of loader 'bootstrap')
	at com.auth0.jwt.impl.PayloadSerializer.serialize(PayloadSerializer.java:40)
	at com.auth0.jwt.impl.PayloadSerializer.serialize(PayloadSerializer.java:18)
	at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)

Not sure, why special handling for PublicClaims.AUDIENCE is needed in first place, but it could be much more convenient to support collections as well.

Note: no errors in other places(Claims) where collections are used.

What was the expected behavior?

Collections can be handelt as well.

Environment

  • Version of this library used: 3.18.1
  • Version of Java used: jdk-14.0.2
  • Other modules/plugins/libraries that might be involved: com.fasterxml.jackson.core:jackson-databind:2.12.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugThis points to a verified bug in the code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions