亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

生成的 OpenAPI golang 客戶端似乎無法正確處理日期?

生成的 OpenAPI golang 客戶端似乎無法正確處理日期?

Go
侃侃無極 2023-03-07 17:10:58
我正在開發一個提供交易列表的 API。我正在用 Java/Kotlin Spring 編寫它,但更喜歡將 golang 用于 CLI,因此我正在為其生成一個 golang 客戶端。該 API 在 Swagger UI 中運行良好。科特林 API:@GetMappingfun 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
查看完整描述

1 回答

?
海綿寶寶撒

TA貢獻1809條經驗 獲得超8個贊

兩種選擇:

  1. 創建自定義類型并實現Unmarshaler日期字段的接口。

  2. 從 API 返回有效的 RFC 3339 日期/時間。


查看完整回答
反對 回復 2023-03-07
  • 1 回答
  • 0 關注
  • 114 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號