Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Nested nullable unions silently discard JSON payloads

未关闭
#2,565 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
76/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
go, openapi
领域
api, tooling

调研方向

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 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

oapi-codegen/oapi-codegen 的其他 Issue

查看 oapi-codegen/oapi-codegen 的全部 Issue

相似的 Issue

更多 Go Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。