错误的序列化LocalDateTime与OpenAPI Generator

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

Wrong serialized LocalDateTime with OpenAPI Generator

问题

以下是翻译好的内容:

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

{
  "startDate": [ 2019, 11, 13, 0, 0 ],
  "endDate": [ 2020, 12, 31, 0, 0 ]
}

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

{
  "startDate": "2019-11-13T00:00",
  "endDate": "2020-12-31T00:00"
}

我的 gradle 配置如下:

dependencies {
  compile 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.10.3'
  classpath 'org.openapitools:openapi-generator-gradle-plugin:4.2.3'
}

task generateClientDS(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
  inputSpec = "$rootDir/specifications/client-ds.yaml".toString()
  outputDir = "$rootDir".toString()
  generatorName = 'java'
  library = 'resttemplate'
  apiPackage = 'com.example.gen.clients.clientds.api'
  modelPackage = 'com.example.gen.clients.clientds.dto'
  modelNameSuffix = 'DTO'
  configOptions = [
          hideGenerationTimestamp: 'true',
          dateLibrary: 'java8'
  ]
  typeMappings = [
          OffsetDateTime: 'java.time.LocalDateTime'
  ]
}

application.properties

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

{
  "startDate": [ 2019, 11, 13, 0, 0 ],
  "endDate": [ 2020, 12, 31, 0, 0 ]
}

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

{
  "startDate": "2019-11-13T00:00",
  "endDate": "2020-12-31T00:00"
}

My gradle configuration is this:

dependencies {
  compile 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.10.3'
  classpath 'org.openapitools:openapi-generator-gradle-plugin:4.2.3'
}

task generateClientDS(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
  inputSpec = "$rootDir/specifications/client-ds.yaml".toString()
  outputDir = "$rootDir".toString()
  generatorName = 'java'
  library = 'resttemplate'
  apiPackage = 'com.example.gen.clients.clientds.api'
  modelPackage = 'com.example.gen.clients.clientds.dto'
  modelNameSuffix = 'DTO'
  configOptions = [
          hideGenerationTimestamp: 'true',
          dateLibrary: 'java8'
  ]
  typeMappings = [
          OffsetDateTime: 'java.time.LocalDateTime'
  ]
}

application.properties

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文档上。尝试进行更改。

startDate:
type: string
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.

startDate:
type: string
format: 'yyyy-mm-dd'

答案2

得分: 0

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

对我来说,解决方案是:

typeMappings = [
        LocalDate: "String"
]

我还在这个帖子中找到了关于这种行为的解释: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:

typeMappings = [
        LocalDate: "String"
]

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:

确定