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

YearKeyDeserializer doesn't work with non-padded year values #51

Closed
sladkoff opened this issue Feb 2, 2018 · 3 comments
Closed

YearKeyDeserializer doesn't work with non-padded year values #51

sladkoff opened this issue Feb 2, 2018 · 3 comments
Milestone

Comments

@sladkoff
Copy link

sladkoff commented Feb 2, 2018

Description & proposal

The YearKeyDeserializer is unable to deserialize keys that are not in the format yyyy (e.g. "1"). It expects the year to be padded with zeros ("0001").

In contrast, the YearDeserializer is in fact working correctly and can handle non-padded values as well.

This could probably be fixed by changing the YearKeyDeserializer#deserialize:30 to something like this (we've currently registered a custom deserializer that does this):

@Override
protected Year deserialize(String key, DeserializationContext ctxt) throws IOException {
-    try {
-        return Year.parse(key, FORMATTER);
+        return Year.of(Integer.parseInt(key));
-    } catch (DateTimeException e) {
-        return _rethrowDateTimeException(ctxt, Year.class, e, key);
-    }
}

Steps to reproduce

Here's a simple test to demonstrate the behavior:

private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper().registerModule(new JavaTimeModule());

@Test
public void serializeAndDeserializeYearKey() throws IOException {
    Map<Year, Float> testMap = Collections.singletonMap(Year.of(1), 1F);

    String serialized = OBJECT_MAPPER.writeValueAsString(testMap);

    TypeReference<Map<Year, Float>> yearFloatTypeReference = new TypeReference<Map<Year, Float>>() {};

    Map<Year, Float> deserialized = OBJECT_MAPPER.readValue(serialized, yearFloatTypeReference);

    assertThat(deserialized).isEqualTo(testMap);
}

The above will throw java.time.format.DateTimeParseException: Text '1' could not be parsed at index 0

@cowtowncoder
Copy link
Member

Thank you for reporting this, sounds like a bug indeed.

@cowtowncoder cowtowncoder added 2.10 active Issue being actively investigated and/or worked on good first issue Issue that seems easy to resolve and is likely a good candidate for contributors new to project and removed 2.9 active Issue being actively investigated and/or worked on labels Aug 13, 2019
@cowtowncoder
Copy link
Member

Marking as easy so new contributors could have a look.

One comment on solution: note that keys can only be Strings, whereas JSON values can be numbers, so handling is bit different by necessity. Still, solution as suggested above may be fine as long as it reports good parse exception for non-integer values.

@cowtowncoder cowtowncoder changed the title YearKeyDeserializer doesn't work with non-padded year values YearKeyDeserializer doesn't work with non-padded year values Sep 5, 2019
cowtowncoder added a commit that referenced this issue Sep 5, 2019
@cowtowncoder cowtowncoder removed the good first issue Issue that seems easy to resolve and is likely a good candidate for contributors new to project label Sep 5, 2019
@cowtowncoder cowtowncoder added this to the 2.10.0 milestone Sep 5, 2019
@cowtowncoder
Copy link
Member

Merged the fix, thank you @kupci for fixing as per @sladkoff's suggestion. Will be in 2.10.0 official (or, pr3, if such is released).

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

No branches or pull requests

2 participants