Skip to content

You're viewing documentation for a pre-release version. View the latest stable version

Initializer

init(from:)

Because multi-value claims can take either singular or plural form in JSON, the default conformance to Decodable from JWTClaim isn’t good enough.
init(from decoder: any Decoder) throws

Discussion

Note

The spec is mute on what multi-value claims like aud with an empty list of values would be considered to represent - whether it would be the same as having no claim at all, or represent a token making the claim but with zero values. For maximal flexibility, this implementation accepts an empty unkeyed container (in JSON, []) silently.

Note

It would be preferable to be able to safely decode the empty array from a lack of any encoded value. This is precluded by the way Codable works, as either the claim would have to be marked optional in the payload, leading to the ambiguity of having both nil and [] representations, each payload type would have to manually implement init(from decoder:) to use decodeIfPresent(_:forKey:) and a fallback value, or we would have to export extensions on KeyedEncodingContainer and KeyedEncodingContainerProtocol to explicitly override behavior for types confroming to JWTMultiValueClaim, a tricky and error-prone approach relying on poorly-understood mechanics of static versus dynamic dispatch.

Note

The spec is also mute regarding the behavior of duplicate values in a list of more than one. This implementation behaves according to the semantics of the particular Collection type used as its value; Array will preserve ordering and duplicates, Set will not.