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

format/parsing date-time fields #1453

Closed antoba closed 1 year ago

antoba commented 1 year ago

Given this json schema definition :

"Timestamp": {
  "type": "string",
  "format": "date-time"

}

I notice the the generated class is :

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:sssZ", timezone = "UTC") @JsonProperty("ts") @NotNull @Nonnull private Date ts;

in the java docs 's' (lowercase) means seconds (so should eventually be 'ss') milliseconds is "S" (uppercase) the final 'sss' in the pattern above is wrong I suppose. Shouldn't be like the following ?

yyyy-MM-dd'T'HH:mm:ss:SSSZ

thank you

unkish commented 1 year ago

Given Example.json schema:

{
    "type": "object",
    "properties": {
        "timestamp": {
            "type": "string",
            "format": "date-time"
        }
    }
}

Tried out with ver. 1.1.2: > jsonschema2pojo -fdt -s Example.json -t .

Output was as follows:

...
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "timestamp"
})
@Generated("jsonschema2pojo")
public class Example {

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

JsonFormat.pattern value was set through: https://github.com/joelittlejohn/jsonschema2pojo/blob/78ec2954c02ec18cfc886aa97c648153dfdca3be/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/Jackson2Annotator.java#L212 Which is defined as: https://github.com/joelittlejohn/jsonschema2pojo/blob/78ec2954c02ec18cfc886aa97c648153dfdca3be/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/FormatRule.java#L50

antoba commented 1 year ago

Thank you, we are using maven plugin with the following configuration :

<plugin>
    <groupId>org.jsonschema2pojo</groupId>
    <artifactId>jsonschema2pojo-maven-plugin</artifactId>
    <version>1.1.2</version>
    <configuration>
        <formatDateTimes>true</formatDateTimes>
        <targetPackage>eu.test.types</targetPackage>
        <generateBuilders>true</generateBuilders>
        <includeSetters>true</includeSetters>
        <includeGetters>true</includeGetters>
        <constructorsRequiredPropertiesOnly>true</constructorsRequiredPropertiesOnly>
        <includeConstructors>true</includeConstructors>
        <useTitleAsClassname>true</useTitleAsClassname>
        <useBigDecimals>true</useBigDecimals>
        <includeJsr303Annotations>true</includeJsr303Annotations>
        <includeJsr305Annotations>true</includeJsr305Annotations>
    </configuration>
</plugin>
antoba commented 1 year ago

I'm really amazed, now trying again it doesn't happen anymore but I'm sure it happened (I cut & paste the code...). Now produces the correct result. Thanks for now, I'll do further checks and eventually let you know.

antoba commented 1 year ago

Hello, I am very sorry to have wasted your time for a non-existent problem : I noticed that there was an incorrect "customDateTimePattern" in the json schema. I wanted to make the milliseconds part optional, but I haven't found a way to do this.... (assuming there is)

thanks you for your support.