错误的序列化LocalDateTime与OpenAPI Generator

huangapple go评论115阅读模式
英文:

Wrong serialized LocalDateTime with OpenAPI Generator

问题

以下是翻译好的内容:

我正在使用 OpenAPI Generator 创建一个客户端,但是当我尝试进行 POST 请求时,LocalDateTime 被序列化为整数数组,看起来像是这样:

  1. {
  2. "startDate": [ 2019, 11, 13, 0, 0 ],
  3. "endDate": [ 2020, 12, 31, 0, 0 ]
  4. }

我希望得到的结果是按照 ISO 字符串进行解析,像是这样:

  1. {
  2. "startDate": "2019-11-13T00:00",
  3. "endDate": "2020-12-31T00:00"
  4. }

我的 gradle 配置如下:

  1. dependencies {
  2. compile 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.10.3'
  3. classpath 'org.openapitools:openapi-generator-gradle-plugin:4.2.3'
  4. }
  5. task generateClientDS(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
  6. inputSpec = "$rootDir/specifications/client-ds.yaml".toString()
  7. outputDir = "$rootDir".toString()
  8. generatorName = 'java'
  9. library = 'resttemplate'
  10. apiPackage = 'com.example.gen.clients.clientds.api'
  11. modelPackage = 'com.example.gen.clients.clientds.dto'
  12. modelNameSuffix = 'DTO'
  13. configOptions = [
  14. hideGenerationTimestamp: 'true',
  15. dateLibrary: 'java8'
  16. ]
  17. typeMappings = [
  18. OffsetDateTime: 'java.time.LocalDateTime'
  19. ]
  20. }

application.properties

  1. spring.jackson.serialization.write-dates-as-timestamps=false

我甚至已经将它添加为属性,但它不起作用,有任何关于出错的想法吗?

英文:

I'm using OpenAPI Generator to create a client, but when I trying to make a POST request is serialized the LocalDateTime like a integer array, looks like

  1. {
  2. "startDate": [ 2019, 11, 13, 0, 0 ],
  3. "endDate": [ 2020, 12, 31, 0, 0 ]
  4. }

I'm waiting for this result, parse like a ISO string

  1. {
  2. "startDate": "2019-11-13T00:00",
  3. "endDate": "2020-12-31T00:00"
  4. }

My gradle configuration is this:

  1. dependencies {
  2. compile 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.10.3'
  3. classpath 'org.openapitools:openapi-generator-gradle-plugin:4.2.3'
  4. }
  5. task generateClientDS(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
  6. inputSpec = "$rootDir/specifications/client-ds.yaml".toString()
  7. outputDir = "$rootDir".toString()
  8. generatorName = 'java'
  9. library = 'resttemplate'
  10. apiPackage = 'com.example.gen.clients.clientds.api'
  11. modelPackage = 'com.example.gen.clients.clientds.dto'
  12. modelNameSuffix = 'DTO'
  13. configOptions = [
  14. hideGenerationTimestamp: 'true',
  15. dateLibrary: 'java8'
  16. ]
  17. typeMappings = [
  18. OffsetDateTime: 'java.time.LocalDateTime'
  19. ]
  20. }

application.properties

  1. spring.jackson.serialization.write-dates-as-timestamps=false

I've even added it as been but it doesn't work, any idea what is wrong?

答案1

得分: 1

我遇到了相同的错误。经过几个小时的查找,在这里找到了答案。问题出在我的API文档上。尝试进行更改。

  1. startDate:
  2. type: string
  3. format: 'yyyy-mm-dd'
英文:

I get the same error. After a few hours of looking found the answer here. The problem was in my API doc. Try to change it.

  1. startDate:
  2. type: string
  3. format: 'yyyy-mm-dd'

答案2

得分: 0

我正在寻找针对LocalDate相同问题的解决方案。

对我来说,解决方案是:

  1. typeMappings = [
  2. LocalDate: "String"
  3. ]

我还在这个帖子中找到了关于这种行为的解释:https://stackoverflow.com/a/75881743/8725920

> 这不是一个错误,而是开发人员的有意决定。你说你正在使用带有resttemplate库的java生成器。默认情况下,resttemplate库会生成一个名为RFC3339DateFormat的类,以及一个名为JavaTimeFormatter的类。

英文:

I was looking for a solution to the same problem for LocalDate

For me the solution was:

  1. typeMappings = [
  2. LocalDate: "String"
  3. ]

Also I found explanation of this behaviour in the tread: https://stackoverflow.com/a/75881743/8725920

> This is not a bug, but a conscious decision on the part of the developers. You said you are using the java generator with the resttemplate library. By default, the resttemplate library generates a class called RFC3339DateFormat, and a class called JavaTimeFormatter

huangapple
  • 本文由 发表于 2020年10月28日 01:35:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/64559945.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定