-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
[#19921] Add RFC 3339 compatible Jackson module for java.time types #19925
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
Closed
beikov
wants to merge
4
commits into
OpenAPITools:master
from
beikov:issues/19921-jackson-java-time-rfc-3339
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
462e7d4
[#19921] Add RFC 3339 compatible Jackson module for java.time types
beikov 2798635
Regenerate samples and docs for 7.11-SNAPSHOT
beikov 1805fdd
Revert "Regenerate samples and docs for 7.11-SNAPSHOT"
wing328 40fa66e
Reapply "Regenerate samples and docs for 7.11-SNAPSHOT"
wing328 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
The diff you're trying to view is too large. We only load the first 3000 changed files.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
modules/openapi-generator/src/main/resources/Java/RFC3339InstantDeserializer.mustache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| {{>licenseInfo}} | ||
| package {{invokerPackage}}; | ||
|
|
||
| import java.io.IOException; | ||
| import java.time.Instant; | ||
| import java.time.OffsetDateTime; | ||
| import java.time.ZoneId; | ||
| import java.time.ZonedDateTime; | ||
| import java.time.format.DateTimeFormatter; | ||
| import java.time.temporal.Temporal; | ||
| import java.time.temporal.TemporalAccessor; | ||
| import java.util.function.BiFunction; | ||
| import java.util.function.Function; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonParser; | ||
| import com.fasterxml.jackson.databind.DeserializationContext; | ||
| import com.fasterxml.jackson.datatype.jsr310.JavaTimeFeature; | ||
| import com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer; | ||
|
|
||
| {{>generatedAnnotation}} | ||
| public class RFC3339InstantDeserializer<T extends Temporal> extends InstantDeserializer<T> { | ||
|
|
||
| private final static boolean DEFAULT_NORMALIZE_ZONE_ID = JavaTimeFeature.NORMALIZE_DESERIALIZED_ZONE_ID.enabledByDefault(); | ||
| private final static boolean DEFAULT_ALWAYS_ALLOW_STRINGIFIED_DATE_TIMESTAMPS | ||
| = JavaTimeFeature.ALWAYS_ALLOW_STRINGIFIED_DATE_TIMESTAMPS.enabledByDefault(); | ||
|
|
||
| public static final RFC3339InstantDeserializer<Instant> INSTANT = new RFC3339InstantDeserializer<>( | ||
| Instant.class, DateTimeFormatter.ISO_INSTANT, | ||
| Instant::from, | ||
| a -> Instant.ofEpochMilli( a.value ), | ||
| a -> Instant.ofEpochSecond( a.integer, a.fraction ), | ||
| null, | ||
| true, // yes, replace zero offset with Z | ||
| DEFAULT_NORMALIZE_ZONE_ID, | ||
| DEFAULT_ALWAYS_ALLOW_STRINGIFIED_DATE_TIMESTAMPS | ||
| ); | ||
|
|
||
| public static final RFC3339InstantDeserializer<OffsetDateTime> OFFSET_DATE_TIME = new RFC3339InstantDeserializer<>( | ||
| OffsetDateTime.class, DateTimeFormatter.ISO_OFFSET_DATE_TIME, | ||
| OffsetDateTime::from, | ||
| a -> OffsetDateTime.ofInstant( Instant.ofEpochMilli( a.value ), a.zoneId ), | ||
| a -> OffsetDateTime.ofInstant( Instant.ofEpochSecond( a.integer, a.fraction ), a.zoneId ), | ||
| (d, z) -> ( d.isEqual( OffsetDateTime.MIN ) || d.isEqual( OffsetDateTime.MAX ) ? | ||
| d : | ||
| d.withOffsetSameInstant( z.getRules().getOffset( d.toLocalDateTime() ) ) ), | ||
| true, // yes, replace zero offset with Z | ||
| DEFAULT_NORMALIZE_ZONE_ID, | ||
| DEFAULT_ALWAYS_ALLOW_STRINGIFIED_DATE_TIMESTAMPS | ||
| ); | ||
|
|
||
| public static final RFC3339InstantDeserializer<ZonedDateTime> ZONED_DATE_TIME = new RFC3339InstantDeserializer<>( | ||
| ZonedDateTime.class, DateTimeFormatter.ISO_ZONED_DATE_TIME, | ||
| ZonedDateTime::from, | ||
| a -> ZonedDateTime.ofInstant( Instant.ofEpochMilli( a.value ), a.zoneId ), | ||
| a -> ZonedDateTime.ofInstant( Instant.ofEpochSecond( a.integer, a.fraction ), a.zoneId ), | ||
| ZonedDateTime::withZoneSameInstant, | ||
| false, // keep zero offset and Z separate since zones explicitly supported | ||
| DEFAULT_NORMALIZE_ZONE_ID, | ||
| DEFAULT_ALWAYS_ALLOW_STRINGIFIED_DATE_TIMESTAMPS | ||
| ); | ||
|
|
||
| protected RFC3339InstantDeserializer( | ||
| Class<T> supportedType, | ||
| DateTimeFormatter formatter, | ||
| Function<TemporalAccessor, T> parsedToValue, | ||
| Function<FromIntegerArguments, T> fromMilliseconds, | ||
| Function<FromDecimalArguments, T> fromNanoseconds, | ||
| BiFunction<T, ZoneId, T> adjust, | ||
| boolean replaceZeroOffsetAsZ, | ||
| boolean normalizeZoneId, | ||
| boolean readNumericStringsAsTimestamp) { | ||
| super( | ||
beikov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| supportedType, | ||
| formatter, | ||
| parsedToValue, | ||
| fromMilliseconds, | ||
| fromNanoseconds, | ||
| adjust, | ||
| replaceZeroOffsetAsZ, | ||
| normalizeZoneId, | ||
| readNumericStringsAsTimestamp | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| protected T _fromString(JsonParser p, DeserializationContext ctxt, String string0) throws IOException { | ||
| return super._fromString(p, ctxt, string0.replace( ' ', 'T' )); | ||
| } | ||
| } | ||
20 changes: 20 additions & 0 deletions
20
modules/openapi-generator/src/main/resources/Java/RFC3339JavaTimeModule.mustache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| {{>licenseInfo}} | ||
| package {{invokerPackage}}; | ||
|
|
||
| import java.time.Instant; | ||
| import java.time.OffsetDateTime; | ||
| import java.time.ZonedDateTime; | ||
|
|
||
| import com.fasterxml.jackson.databind.module.SimpleModule; | ||
|
|
||
| {{>generatedAnnotation}} | ||
| public class RFC3339JavaTimeModule extends SimpleModule { | ||
|
|
||
| public RFC3339JavaTimeModule() { | ||
| super("RFC3339JavaTimeModule"); | ||
|
|
||
| addDeserializer(Instant.class, RFC3339InstantDeserializer.INSTANT); | ||
| addDeserializer(OffsetDateTime.class, RFC3339InstantDeserializer.OFFSET_DATE_TIME); | ||
| addDeserializer(ZonedDateTime.class, RFC3339InstantDeserializer.ZONED_DATE_TIME); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -186,6 +186,7 @@ public class ApiClient { | |
| objectMapper.registerModule(new JodaModule()); | ||
| {{/joda}} | ||
| objectMapper.registerModule(new JavaTimeModule()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The JavaTimeModule can already deserialize RFC3339 datetimes |
||
| objectMapper.registerModule(new RFC3339JavaTimeModule()); | ||
beikov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| {{#openApiNullable}} | ||
| JsonNullableModule jnm = new JsonNullableModule(); | ||
| objectMapper.registerModule(jnm); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
samples/client/echo_api/csharp-restsharp/.openapi-generator/VERSION
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| 7.11.0-SNAPSHOT | ||
| 7.10.0-SNAPSHOT |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
samples/client/echo_api/go-external-refs/.openapi-generator/VERSION
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| 7.11.0-SNAPSHOT | ||
| 7.10.0-SNAPSHOT |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| 7.11.0-SNAPSHOT | ||
| 7.10.0-SNAPSHOT |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
samples/client/echo_api/java/apache-httpclient/.openapi-generator/VERSION
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| 7.11.0-SNAPSHOT | ||
| 7.10.0-SNAPSHOT |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.