jsonschema2pojo icon indicating copy to clipboard operation
jsonschema2pojo copied to clipboard

formatDateTimes in gradle is private and thus cannot be set to 'true'

Open attebjorner opened this issue 1 year ago • 8 comments

We are currently migrating our project from maven to gradle and noticed that the "formatDateTimes" property cannot be set in the gradle because it is private. We are using the latest 1.2.2 version. The property is in the README.md and is described as changeable.

attebjorner avatar Nov 01 '24 20:11 attebjorner

Hi

Can you provide an example ?


Could not reproduce with following input:

test.json:

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

build.gradle:

jsonSchema2Pojo {
    setSource(files("$projectDir/src/main/resources/json"))
    formatDateTimes = true
}

generates class:

...
@Generated("jsonschema2pojo")
public class Test {

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

whereas with formatDateTimes = true in build.gradle generates:

@Generated("jsonschema2pojo")
public class Test {

    @JsonProperty("defaultFormat")
    private Date defaultFormat;

unkish avatar Nov 02 '24 10:11 unkish

Hi!

Unfortunatelly I am unable to provide a screenshot due to some restrictions on my work pc, the configuration looks like this and I get this error only for the formatDateTimes property. If I remove it I get no errors.

I also tried to stop all gradle daemons, clean gradle caches, invalidate idea's caches.

The error message is: Cannot access 'formatDateTimes': it is private in 'JsonSchemaExtension'

And I cannot provide the json schema because it contains sensitive info. But it works fine with maven.

plugins {
    ...
    id("org.jsonschema2pojo") version "1.2.2"
}

jsonSchema2Pojo {
    sourceFiles = files("/example")
    targetPackage = "org.example"
    includeHashcodeAndEquals = false
    includeToString = false
    includeAdditionalProperties = false
    formatDateTimes = true   <--- here I get an error
    formatDates = true
    dateType = "java.time.LocalDate"
    dateTimeType = "java.time.OffsetDateTime"
    useLongIntegers = true
    useDoubleNumbers = false
}

attebjorner avatar Nov 02 '24 16:11 attebjorner

Some questions:

  • are you using gradle kotlin dsl (*.kts) or groovy dsl
  • which gradle version is being used

unkish avatar Nov 02 '24 21:11 unkish

I am using kotlin dsl, the gradle version is 7.4

attebjorner avatar Nov 03 '24 09:11 attebjorner

It seems that gradle kotlin dsl behaves differently than groovy dsl. For now as a workaround you could use

setFormatDateTimes(true)

instead of

formatDateTimes = true

unkish avatar Nov 03 '24 09:11 unkish

thanks for the option with the setter, but I have already tried it and it cannot resolve the setter also, as the property is "private" :( I've also just noticed the customDateTimePattern option in the docs, maybe it will do the trick without formatDateTimes = true

attebjorner avatar Nov 03 '24 10:11 attebjorner

thanks for the option with the setter, but I have already tried it and it cannot resolve the setter also, as the property is "private" :( I've also just noticed the customDateTimePattern option in the docs, maybe it will do the trick without formatDateTimes = true

That's weird - checked with following combinations and it worked fine

jsonSchema2Pojo {
    sourceFiles = files("/src/main/resources/schema")
    targetPackage = "my.pkg"
    generateBuilders = false
    setFormatDateTimes(true)
}
jsonSchema2Pojo {
    sourceFiles = files("/src/main/resources/schema")
    targetPackage = "my.pkg"
    generateBuilders = false
    isFormatDateTimes = true
}

Would be good to get a SSCCE (all company/work code could be stripped/replaced/reduced to bare minimum)

unkish avatar Nov 03 '24 17:11 unkish

Just hit this exact issue myself. Also using kotlin dsl, also unable to show code :) I'll try to find the time to build a minimal demo later.

ilkkapoutanen-61n avatar Sep 02 '25 14:09 ilkkapoutanen-61n