Nested nullable unions silently discard JSON payloads
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
研究方向
Start with the provided spec.yaml, config.yaml, and repro_test.go, then run the listed oapi-codegen command and go test to reproduce the dropped payload. Trace the nullable-union collapse path, focusing on the reported UnionElements and Discriminator data, and add a regression test where the JSON round trip preserves the nested union payload.
由索引模型根據 Issue 內容生成。
描述
An OpenAPI 3.1 nullable wrapper around an inline oneOf or anyOf generates a union struct without its JSON methods. Generation succeeds, but unmarshalling and then marshalling silently drops the union payload.
Reproduced on main at 833a968f84b4b0ec7c2f3fb5bb119e3c449f7a4f, using Go 1.27.1 on darwin/arm64, with compatibility.schema-merging-behavior set to v1, v2, or v3.
Reproduction
Save these three files in a new directory. The reproduction is provided under Apache-2.0.
spec.yaml:
openapi: 3.1.0
info:
title: Nested nullable union reproduction
version: 1.0.0
paths: {}
components:
schemas:
Envelope:
type: object
required: [pet]
properties:
pet:
anyOf:
- oneOf:
- $ref: '#/components/schemas/Cat'
- $ref: '#/components/schemas/Dog'
- type: 'null'
Cat:
type: object
required: [meow]
properties:
meow: {type: string}
Dog:
type: object
required: [bark]
properties:
bark: {type: string}
config.yaml:
package: repro
output: types.gen.go
generate:
models: true
output-options:
skip-prune: true
repro_test.go:
package repro
import (
"encoding/json"
"testing"
)
func TestNestedNullableUnionRoundTrip(t *testing.T) {
const input = `{"pet":{"meow":"purr"}}`
var value Envelope
if err := json.Unmarshal([]byte(input), &value); err != nil {
t.Fatal(err)
}
output, err := json.Marshal(value)
if err != nil {
t.Fatal(err)
}
if string(output) != input {
t.Fatalf("got %s, want %s", output, input)
}
}
Run:
go mod init example.com/nested-nullable-repro
go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen@833a968f84b4b0ec7c2f3fb5bb119e3c449f7a4f --config=config.yaml spec.yaml
go get github.com/oapi-codegen/[email protected]
go test .
Actual result:
got {"pet":{}}, want {"pet":{"meow":"purr"}}
Expected: the complete payload survives the JSON round trip.
Cause
The nullable-union collapse path drops UnionElements and Discriminator, so the union's JSON methods and accessors are not generated.
Related to #2218. This reproduces after the OpenAPI 3.1 support added in #2336.
- 主要語言
- Go
- 星號
- 8.6k
- 分支
- 1.1k
- 平均合併
- 14 小時 45 分鐘
- 30 天內合併 PR
- 29
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 沒有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
oapi-codegen/oapi-codegen 的其他 Issue
-
area:*of bug
難度 2/5 1-3 小時 新手友好度 78/100
oapi-codegen/oapi-codegen#2593 ·
維護者通常 1 天內回覆
-
client
難度 2/5 1-3 小時 新手友好度 70/100
oapi-codegen/oapi-codegen#2483 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 65/100
oapi-codegen/oapi-codegen#2408 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 70/100
oapi-codegen/oapi-codegen#2397 · 3 則留言 ·
維護者通常 1 天內回覆
-
documentation
難度 2/5 1-3 小時 新手友好度 65/100
oapi-codegen/oapi-codegen#2168 · 1 則留言 ·
維護者通常 1 天內回覆
查看 oapi-codegen/oapi-codegen 的全部 Issue
相似的 Issue
-
automation models
難度 2/5 1-3 小時 新手友好度 78/100
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 72/100
txn2/mcp-data-platform#1984 ·
維護者通常 1 天內回覆
-
agentic-workflows
難度 2/5 1-3 小時 新手友好度 68/100
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 88/100
維護者通常 1 天內回覆
-
kind/docs prio/P2
難度 1/5 1 小時以內 新手友好度 95/100
agent-substrate/substrate#1986 ·
維護者通常 1 天內回覆