Hacktoberfest 2026: die Issues, die Maintainer für den Oktober markiert haben – offen und einsteigerfreundlich. Hacktoberfest-Issues durchsuchen

Nested nullable unions silently discard JSON payloads

Offen
#2,565 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Anfängerfreundlichkeit
76/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
go, openapi
Bereich
api, tooling

Rechercherichtung

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.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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.

Vorherrschende Sprache
Go
Sterne
8.6k
Forks
1.1k
Ø Merge
14 Std. 45 Min.
Gemergte PRs (30 T.)
29

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus oapi-codegen/oapi-codegen

Alle Issues in oapi-codegen/oapi-codegen

Ähnliche Issues

Weitere Issues zu Go

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.