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

[#19921] Add RFC 3339 compatible Jackson module for java.time types #19925

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

beikov
Copy link
Contributor

@beikov beikov commented Oct 20, 2024

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh ./bin/configs/*.yaml
    ./bin/utils/export_docs_generators.sh
    
    (For Windows users, please run the script in Git BASH)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@bbdouglas (2017/07) @sreeshas (2017/08) @jfiala (2017/08) @lukoyanov (2017/09) @cbornet (2017/09) @jeff9finger (2018/01) @karismann (2019/03) @Zomzog (2019/04) @lwlee2608 (2019/10) @martin-mfg (2023/08)

@beikov beikov force-pushed the issues/19921-jackson-java-time-rfc-3339 branch from a4f83f8 to c0703e3 Compare October 20, 2024 15:04
@cbornet
Copy link
Member

cbornet commented Oct 21, 2024

I'm pretty sure Java implementations can already deserialize RFC3339 datetimes...

@@ -187,6 +187,7 @@ public class ApiClient {
{{/joda}}
objectMapper.registerModule(new JavaTimeModule());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JavaTimeModule can already deserialize RFC3339 datetimes

@beikov
Copy link
Contributor Author

beikov commented Oct 21, 2024

I'm pretty sure Java implementations can already deserialize RFC3339 datetimes...

I can tell you, it does not. The JavaTime module uses com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer for Instant, ZonedDateTime and OffsetDateTime types. This in turn, uses the DateTimeFormatter.ISO_INSTANT, DateTimeFormatter.ISO_OFFSET_DATE_TIME and DateTimeFormatter.ISO_ZONED_DATE_TIME formats respectively for parsing.
You can look into the javadocs of DateTimeFormatter and easily figure out that it implements ISO8601 which has T as the only valid separator between date and time, whereas RFC3339 also allows a space character.

If you don't believe me, try it out for yourself: https://onecompiler.com/jshell/42vrgfpy8

@cbornet
Copy link
Member

cbornet commented Oct 21, 2024

As per its ABNF, RFC3339 doesn't support space as a separator.
But it's true that there's a note that says applications may choose to use space... I wouldn't recommend to use it though...
So maybe we could be lenient on deser and accept it 🤔
Do people really use RFC3339 with space separator ?

@beikov
Copy link
Contributor Author

beikov commented Oct 21, 2024

I integrated an application which uses this format, so clearly, there is precedent for this need. I doubt that application will change the representation.
I can't speak for other users, but if OpenAPI claims that RFC3339 format is supported, then the OpenAPI generator should respect that.

Copy link
Contributor

@martin-mfg martin-mfg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @beikov, thanks for the PR! Apart from the small topics I commented on separately, this PR looks good to me. I tested the changes locally and can confirm that the changes enable the parsing of additional timestamp formats.

@@ -187,6 +187,7 @@ public class ApiClient {
{{/joda}}
objectMapper.registerModule(new JavaTimeModule());
{{#openApiNullable}}
objectMapper.registerModule(new RFC3339JavaTimeModule());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this new line shouldn't be inside the {{#openApiNullable}} block, right?

Function<FromDecimalArguments, T> fromNanoseconds,
BiFunction<T, ZoneId, T> adjust,
boolean replaceZeroOffsetAsZ) {
super(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This super constructor is deprecated (see here). Could you please switch to a different one? I guess you could simply use the one recommended in the above link. And for the additional parameters required there, you could use the default values. (DEFAULT_NORMALIZE_ZONE_ID=true, DEFAULT_ALWAYS_ALLOW_STRINGIFIED_DATE_TIMESTAMPS=false)

@@ -678,6 +678,8 @@ public void processOpts() {
additionalProperties.remove(SERIALIZATION_LIBRARY_GSON);
additionalProperties.remove(SERIALIZATION_LIBRARY_JSONB);
supportingFiles.add(new SupportingFile("RFC3339DateFormat.mustache", invokerFolder, "RFC3339DateFormat.java"));
supportingFiles.add(new SupportingFile("RFC3339InstantDeserializer.mustache", invokerFolder, "RFC3339InstantDeserializer.java"));
supportingFiles.add(new SupportingFile("RFC3339JavaTimeModule.mustache", invokerFolder, "RFC3339JavaTimeModule.java"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optional, not required for PR approval imo: Currently these supporting files are added even if the chosen library doesn't use them at all. You could introduce an additional if here to only add the files where they are actually supported.

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

Successfully merging this pull request may close these issues.

3 participants