Nested nullable unions silently discard JSON payloads
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
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
- Kein Dockerfile und keine Docker-Compose-Datei
- Keine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus oapi-codegen/oapi-codegen
-
client
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 70/100
oapi-codegen/oapi-codegen#2483 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 65/100
oapi-codegen/oapi-codegen#2408 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 70/100
oapi-codegen/oapi-codegen#2397 · 3 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
documentation
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 65/100
oapi-codegen/oapi-codegen#2168 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 70/100
oapi-codegen/oapi-codegen#1996 · 1 Kommentar · 1 Reaktion ·
Maintainer antworten meist innerhalb von 1 Tag
Alle Issues in oapi-codegen/oapi-codegen
Ähnliche Issues
-
[Docs] - Document minimum Terraform/OpenTofu version (>= 1.11) required by write-only argumentsOffen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 92/100
MagaluCloud/terraform-provider-mgc#323 ·
Maintainer antworten meist innerhalb von 11 Tagen
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
rossoctl/context-guru#366 ·
Maintainer antworten meist innerhalb von 1 Tag
-
stage-fail
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
siyuan-note/bazaar#2293 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 84/100
piraeusdatastore/piraeus-operator#1070 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
Maintainer antworten meist innerhalb von 1 Tag