生成的 OpenAPI golang 客户端似乎无法正确处理日期?

我正在开发一个提供交易列表的 API。我正在用 Java/Kotlin Spring 编写它,但更喜欢将 golang 用于 CLI,因此我正在为其生成一个 golang 客户端。该 API 在 Swagger UI 中运行良好。


科特林 API:


@GetMapping

fun listTransactions() : ResponseEntity<List<Transaction>> {

    val transactions = ArrayList<Transaction>()

    transactionRepository.findAll().mapTo(transactions) { fromEntity(it) }

    return ResponseEntity.ok(transactions)

}

科特林对象:


data class Transaction(

        val id: Long,

        val transactionDate: Date, // Java SQL date

        val postedDate: Date?, // Java SQL date

        val amount: BigDecimal,

        val category: Category,

        val merchant: Merchant,

        val merchantDescription: String?

)

Golang 客户端对象:


type Transaction struct {

   Id *int64 `json:"id,omitempty"`

   TransactionDate *time.Time `json:"transactionDate,omitempty"`

   PostedDate *time.Time `json:"postedDate,omitempty"`

   Amount *float32 `json:"amount,omitempty"`

   Category *Category `json:"category,omitempty"`

   Merchant *Merchant `json:"merchant,omitempty"`

   MerchantDescription *string `json:"merchantDescription,omitempty"`

}

所有这一切似乎都是正确的。但是,当我使用 OpenAPI 客户端时,反序列化似乎无法正常工作:


Error when calling `TransactionRouterApi.ListTransactions``: parsing time "\"2022-10-28\"" as "\"2006-01-02T15:04:05Z07:00\"": cannot parse "\"" as "T"

Response from `TransactionRouterApi.ListTransactions`: [{0x140000aa218 0001-01-01 00:00:00 +0000 UTC <nil> <nil> <nil> <nil> <nil>}]

我是否做错了什么导致反序列化失败?或者这是一个客户端错误(似乎值得怀疑,但谁知道呢)。


我查看了我使用的生成参数和端点上可用的模式,它们看起来都是正确的。


执行的脚本:openapi-generator-cli generate -g go -i http://localhost:8080/v3/api-docs


侃侃无极
浏览 120回答 1
1回答

海绵宝宝撒

两种选择:创建自定义类型并实现Unmarshaler日期字段的接口。从 API 返回有效的 RFC 3339 日期/时间。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go