-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#19921] Add RFC 3339 compatible Jackson module for java.time types
- Loading branch information
Showing
172 changed files
with
5,788 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
74 changes: 74 additions & 0 deletions
74
modules/openapi-generator/src/main/resources/Java/RFC3339InstantDeserializer.mustache
This file contains 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,74 @@ | ||
{{>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.deser.InstantDeserializer; | ||
|
||
{{>generatedAnnotation}} | ||
public class RFC3339InstantDeserializer<T extends Temporal> extends InstantDeserializer<T> { | ||
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 | ||
); | ||
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 | ||
); | ||
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 | ||
); | ||
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) { | ||
super( | ||
supportedType, | ||
formatter, | ||
parsedToValue, | ||
fromMilliseconds, | ||
fromNanoseconds, | ||
adjust, | ||
replaceZeroOffsetAsZ | ||
); | ||
} | ||
|
||
@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 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 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 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 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 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 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 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 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 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 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
85 changes: 85 additions & 0 deletions
85
...a/apache-httpclient/src/main/java/org/openapitools/client/RFC3339InstantDeserializer.java
This file contains 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,85 @@ | ||
/* | ||
* Echo Server API | ||
* Echo Server API | ||
* | ||
* The version of the OpenAPI document: 0.1.0 | ||
* Contact: [email protected] | ||
* | ||
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). | ||
* https://openapi-generator.tech | ||
* Do not edit the class manually. | ||
*/ | ||
|
||
package org.openapitools.client; | ||
|
||
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.deser.InstantDeserializer; | ||
|
||
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.10.0-SNAPSHOT") | ||
public class RFC3339InstantDeserializer<T extends Temporal> extends InstantDeserializer<T> { | ||
|
||
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 | ||
); | ||
|
||
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 | ||
); | ||
|
||
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 | ||
); | ||
|
||
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) { | ||
super( | ||
supportedType, | ||
formatter, | ||
parsedToValue, | ||
fromMilliseconds, | ||
fromNanoseconds, | ||
adjust, | ||
replaceZeroOffsetAsZ | ||
); | ||
} | ||
|
||
@Override | ||
protected T _fromString(JsonParser p, DeserializationContext ctxt, String string0) throws IOException { | ||
return super._fromString(p, ctxt, string0.replace( ' ', 'T' )); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...i/java/apache-httpclient/src/main/java/org/openapitools/client/RFC3339JavaTimeModule.java
This file contains 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,31 @@ | ||
/* | ||
* Echo Server API | ||
* Echo Server API | ||
* | ||
* The version of the OpenAPI document: 0.1.0 | ||
* Contact: [email protected] | ||
* | ||
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). | ||
* https://openapi-generator.tech | ||
* Do not edit the class manually. | ||
*/ | ||
|
||
package org.openapitools.client; | ||
|
||
import java.time.Instant; | ||
import java.time.OffsetDateTime; | ||
import java.time.ZonedDateTime; | ||
|
||
import com.fasterxml.jackson.databind.module.SimpleModule; | ||
|
||
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.10.0-SNAPSHOT") | ||
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 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 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.