0

I am using fabric8 maven plugin to autogenerate Java classes for a CRD from it's yaml file. One of the fields is:

updated_time:
  format: date-time
  type: string

It's getting converted to a java.time.zoneddatetime field:

@com.fasterxml.jackson.annotation.JsonFormat(timezone = "UTC", pattern = "yyyy-MM-dd'T'HH:mm:ssX")
private java.time.ZonedDateTime updated_time;

I want to change the pattern above to yyyy-MM-dd'T'HH:mm:ssXXX , a timestamp with offset, so it can parse timestamps like 2023-08-07T06:43:19+00:00. How can I achieve this? On java-generator-maven-plugin v6.2.0, open to upgrade.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
xerocool
  • 113
  • 2
  • 10
  • 1
    A zone is more than just an offset, so you may want to switch to `java.time.OffsetDateTime`. – deHaar Aug 08 '23 at 07:38
  • How do I do that? The Java code is autogenerated. – xerocool Aug 08 '23 at 08:01
  • That's a good question, I have no idea… Hopefully, it is in the docs of fabric8. – deHaar Aug 08 '23 at 08:41
  • 1
    This is not supported at the moment, the field format is hard-coded. https://github.com/fabric8io/kubernetes-client/blob/a7c630bbd368a976d6b0e72287dcf4f7348c54bd/java-generator/core/src/main/java/io/fabric8/java/generator/nodes/JObject.java#L221 – Marc Nuri Aug 09 '23 at 13:17

2 Answers2

1

This is not possible at the moment in the fabric8 java-generator. A date-time field should be RFC 3339 according to the OpenAPI spec.

Would you mind elaborating on the use-case/project that is emitting the incorrect date format?

Anyhow, the viable workaround would be to comment out format: date-time in the CRD before the generation, so that you can receive a String.

Note on the version: I do believe that you are using java-generator with version > 6.8 as native support for date-time has only been recently released.

andreaTP
  • 106
  • 1
  • The date I mentioned in the question(2023-08-07T06:43:19+00:00) seems to fit the RFC 3339 standard: https://utcc.utoronto.ca/~cks/space/blog/unix/GNUDateAndRFC3339#:~:text=RFC%203339%20is%20an%20RFC,some%20Prometheus%20tools%20use%20it. The project emitting the date format is in another language and out of my hand. Changing the field type to string is not ideal, as I would like to stick to the original CRD yaml. – xerocool Aug 09 '23 at 14:51
  • Sadly the proposed date doesn't conform to the specification, it should be something like: `2023-08-07T06:42:19Z` ( [reference](https://datatracker.ietf.org/doc/html/rfc3339#section-5.6) ). I'm afraid this is a bug in the project producing it ... if it's open-source we can file an issue, if not, please report it somehow. You can automate the workaround with something like `kustomize`, but I'm not sure how to help any further ... – andreaTP Aug 10 '23 at 10:20
  • I step back :-) I'm reading again the specification and using the time offset is indeed allowed by the spec. Let's follow up on this upstream issue: https://github.com/fabric8io/kubernetes-client/issues/5382 We will ship a fix soon, thanks for reporting. – andreaTP Aug 11 '23 at 09:23
  • Thanks. For now, I'll just comment out the format. – xerocool Aug 11 '23 at 10:58
  • I provided the relevant fix [here](https://github.com/fabric8io/kubernetes-client/pull/5384), feel free to review the changes. – andreaTP Aug 11 '23 at 13:01
1

Version 6.8.1 of the fabric8 java-generator has been released and includes the relevant fix for this issue.

andreaTP
  • 106
  • 1