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
-
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 天内回复
-
难度 2/5 1-3 小时 新手友好度 70/100
oapi-codegen/oapi-codegen#1996 · 1 条评论 · 1 个 reaction ·
维护者通常 1 天内回复
查看 oapi-codegen/oapi-codegen 的全部 Issue
相似的 Issue
-
agent-butler-finding chore
难度 1/5 1 小时以内 新手友好度 88/100
jordansmall/spindrift#4146 ·
维护者通常 1 天内回复
-
security
难度 2/5 1-3 小时 新手友好度 68/100
IBM/ibmcloud-volume-file-vpc#119 ·
-
security
难度 2/5 1-3 小时 新手友好度 66/100
IBM/networking-go-sdk#339 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 88/100
kubernetes-sigs/mcp-lifecycle-operator#439 ·
维护者通常 1 天内回复
-
area: global bug dx priority: low
难度 2/5 1-3 小时 新手友好度 88/100
维护者通常 1 天内回复