joelittlejohn / jsonschema2pojo

Generate Java types from JSON or JSON Schema and annotate those types for data-binding with Jackson, Gson, etc
http://www.jsonschema2pojo.org
Apache License 2.0
6.24k stars 1.66k forks source link

when using customDateTimePattern @JsonFormat with timezone=UTC generated #1183

Open oarkhipov opened 3 years ago

oarkhipov commented 3 years ago

Using customDateTimePattern=yyyy-MM-dd'T'HH:mm:ss.SSSXXX and dateTimeType=java.time.Instant give us: @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX", timezone = "UTC") That's incorrect The string 2020-12-09T14:09:35.573+03:00 becomes an Instant of 2020-12-09T14:09:35.573Z (actual time zone ignored) After removing timezone=UTC all work perfectly.

We need an option to explicit define default timeZone.

Now there is no way to get rid of timezone = "UTC" without modifying source json-schema. As workaround, i use customTimeZone="##default" in schema.. that works too. But that's not always possible to modify source schema.

Regards, Oleg

Jayaprabahar commented 3 years ago

Seems the solution is not yet made.

ben-manes commented 2 years ago

I think that I may have run into this one too.

Take for example the json,

"appointment_start_date_time_utc": "2022-04-01T15:00:00",

I then define the json schema as,

"appointment_start_date_time_utc": {
  "customDateTimePattern" : "yyyy-MM-dd'T'HH:mm:ss",
  "customTimezone": "UTC",
  "format" : "date-time",
  "type": "string"
},

That produces the java object with

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss", timezone = "UTC")
@JsonProperty("appointment_start_date_time_utc")
private OffsetDateTime appointmentStartDateTimeUtc;

Unfortunately it fails with the error below.

``` java.time.DateTimeException: Unable to obtain ZoneOffset from TemporalAccessor: {InstantSeconds=1648825200},ISO,UTC resolved to 2022-04-01T15:00 of type java.time.format.Parsed at java.base/java.time.ZoneOffset.from(ZoneOffset.java:349) at java.base/java.time.OffsetDateTime.from(OffsetDateTime.java:360) ... 21 common frames omitted Wrapped by: java.time.DateTimeException: Unable to obtain OffsetDateTime from TemporalAccessor: {InstantSeconds=1648825200},ISO,UTC resolved to 2022-04-01T15:00 of type java.time.format.Parsed at java.base/java.time.OffsetDateTime.from(OffsetDateTime.java:371) at com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer._fromString(InstantDeserializer.java:302) ... 20 common frames omitted Wrapped by: com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.OffsetDateTime` from String "2022-04-01T15:00:00": Failed to deserialize java.time.OffsetDateTime: (java.time.DateTimeException) Unable to obtain OffsetDateTime from TemporalAccessor: {InstantSeconds=1648825200},ISO,UTC resolved to 2022-04-01T15:00 of type java.time.format.Parsed at [Source: (okhttp3.ResponseBody$BomAwareReader); line: 1, column: 92] (through reference chain: co.loaddocs.service.generated.json.entity.action.integration.chamberlain.AppointmentResponse["appointment_start_date_time_utc"]) at com.fasterxml.jackson.databind.exc.InvalidFormatException.from(InvalidFormatException.java:67) at com.fasterxml.jackson.databind.DeserializationContext.weirdStringException(DeserializationContext.java:1991) at com.fasterxml.jackson.databind.DeserializationContext.handleWeirdStringValue(DeserializationContext.java:1219) at com.fasterxml.jackson.datatype.jsr310.deser.JSR310DeserializerBase._handleDateTimeException(JSR310DeserializerBase.java:176) at com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer._fromString(InstantDeserializer.java:307) at com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer.deserialize(InstantDeserializer.java:217) at com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer.deserialize(InstantDeserializer.java:51) at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:129) at com.fasterxml.jackson.module.blackbird.deser.SuperSonicBeanDeserializer.deserialize(SuperSonicBeanDeserializer.java:159) at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:322) at com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:2051) at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1459) ```

The formatter parses it to a java.time.format.Parsed with the value {InstantSeconds=1648825200},ISO,UTC resolved to 2022-04-01T15:00

Jackson calls OffsetDateTime.from(temporal) which fails by not knowing the ZoneOffset. This should have been UTC, so I'm not sure why that was lost.