Consider reworking Jackson modules to support nanosecond precision #9460
Labels
in: oauth2
An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose)
type: enhancement
A general enhancement
As indicated in FasterXML/jackson-modules-java8#307,
ObjectMapper#readTree
cannot delay the evaluation ofInstant
values in the same fashion asObjectMapper#readValue
, which causes a loss of precision at deserialization whenInstant
values have nanosecond precision.For example, this test will fail when
OAuth2ClientJacksonModule
is included in the configuration ofObjectMapper
:This is because Spring Security's Jackson modules include deserializers for unmodifiable collections, each of which relies on
ObjectMapper#readTree
, which can only support microsecond precision.It is not necessary for
OAuth2ClientJacksonModule
to use these deserializers, though since any unmodifiable collections can be converted to their modifiable counterparts at serialization time using the@JsonSerialize
annotation in the corresponding mixin constructor arguments.For example, if
OidcIdTokenMixin
is changed to:where
MapCopier
is a class that takes a collection and returns aLinkedHashMap
copy, then the above test passes since theUnmodifiableMapSerialzier
is no longer necessary and thusObjectMapper#parseTree
is no longer employed.This would be applied for any unmodifiable collections referred to in the
OAuth2ClientJacksonModule
since there are constructs likeOAuth2AuthenticationToken
that contains an unmodifiable list of authorities where one of those authorities is anOidcUserAuthority
that contains a claim set.Alternative
There are performance implications that still need to be investigated to determine if this is a viable option. If not, the alternative is to document that the Spring Security Jackson modules only support microsecond-level precision for
Instant
values, updatingOAuth2AuthenticationTokenMixinTests
accordingly. Applications can achieve nanosecond precision on their own in this fashion by adding:to their
ObjectMapper
configuration.Additionally, the documentation could be updated in the future should Jackson find a way to be able to support nanosecond precision in
ObjectMapper#readTree
by default.Additional Details
This can be verified on Windows using JDK 11 and modifying
OAuth2AuthenticationTokenMixinTests
to exclude the line:Occassionally, the
Instant
obtained from the OS contains enough precision to cause rounding, making the assertions fail.The text was updated successfully, but these errors were encountered: