|
| 1 | +// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +// Package json_tag_fidelity witnesses that the emitted property set matches what |
| 5 | +// `encoding/json` actually puts on the wire, for every shape of the `json:"-"` |
| 6 | +// tag. |
| 7 | +// |
| 8 | +// This corpus has an ORACLE, which is unusual: the correct answer is not a design |
| 9 | +// choice, it is whatever encoding/json does. `wire_test.go` marshals each type |
| 10 | +// here and captures the resulting key set as `wire.golden.json`; the integration |
| 11 | +// test scans the same types and asserts the property sets agree. Neither side |
| 12 | +// hard-codes an expectation. |
| 13 | +// |
| 14 | +// Values are chosen non-zero so `omitempty` never hides a key that the wire would |
| 15 | +// otherwise carry. |
| 16 | +package json_tag_fidelity |
| 17 | + |
| 18 | +// Base is the shared embedded type. Every promoted name below comes from here. |
| 19 | +type Base struct { |
| 20 | + // ID is a plain promoted field. |
| 21 | + ID int64 `json:"id"` |
| 22 | + |
| 23 | + // Name is a plain promoted field. |
| 24 | + Name string `json:"name"` |
| 25 | + |
| 26 | + // Age is the field the outer structs re-declare. |
| 27 | + Age int32 `json:"age"` |
| 28 | +} |
| 29 | + |
| 30 | +// IgnoreShadow re-declares a promoted field with `json:"-"`. |
| 31 | +// |
| 32 | +// encoding/json ignores a `-` field ENTIRELY: it never enters the name set, so it |
| 33 | +// does not shadow the promoted `age`, which Go still marshals. An author writing |
| 34 | +// this usually means "drop it" — `swagger:omit` on the embed is the honest way to |
| 35 | +// say that, and the scan raises a Hint pointing there. |
| 36 | +// |
| 37 | +// swagger:model IgnoreShadow |
| 38 | +type IgnoreShadow struct { |
| 39 | + Base |
| 40 | + |
| 41 | + Age int32 `json:"-"` |
| 42 | +} |
| 43 | + |
| 44 | +// RenameShadow re-declares a promoted field under a real name — the control. |
| 45 | +// Here Go's depth rule DOES apply and the outer declaration wins. |
| 46 | +// |
| 47 | +// swagger:model RenameShadow |
| 48 | +type RenameShadow struct { |
| 49 | + Base |
| 50 | + |
| 51 | + Age int32 `json:"age"` |
| 52 | +} |
| 53 | + |
| 54 | +// PlainIgnore carries `json:"-"` with nothing to shadow — the control for the |
| 55 | +// common case, where dropping the property is correct. |
| 56 | +// |
| 57 | +// swagger:model PlainIgnore |
| 58 | +type PlainIgnore struct { |
| 59 | + // Keep stays on the wire. |
| 60 | + Keep string `json:"keep"` |
| 61 | + |
| 62 | + // Drop is ignored entirely by encoding/json. |
| 63 | + Drop string `json:"-"` |
| 64 | +} |
| 65 | + |
| 66 | +// DashName uses the `json:"-,"` escape, which names the field literally `-` |
| 67 | +// rather than ignoring it. The trailing comma is the whole difference. |
| 68 | +// |
| 69 | +// swagger:model DashName |
| 70 | +type DashName struct { |
| 71 | + // Weird is emitted under the name "-". |
| 72 | + Weird string `json:"-,"` |
| 73 | +} |
| 74 | + |
| 75 | +// DashNameOmitEmpty is the `-,omitempty` variant, which the historic corpus |
| 76 | +// already contains (classification/models/nomodel.go). Non-zero here, so the key |
| 77 | +// is present on the wire. |
| 78 | +// |
| 79 | +// swagger:model DashNameOmitEmpty |
| 80 | +type DashNameOmitEmpty struct { |
| 81 | + // Weird is emitted under the name "-" when non-empty. |
| 82 | + Weird string `json:"-,omitempty"` |
| 83 | +} |
| 84 | + |
| 85 | +// EmbedIgnored tags the EMBED itself `json:"-"`, which does drop the whole embed |
| 86 | +// — the control showing that `-` on an embed and `-` on a re-declaration are |
| 87 | +// different acts. |
| 88 | +// |
| 89 | +// swagger:model EmbedIgnored |
| 90 | +type EmbedIgnored struct { |
| 91 | + Base `json:"-"` |
| 92 | + |
| 93 | + // Extra is the only field that survives. |
| 94 | + Extra string `json:"extra"` |
| 95 | +} |
0 commit comments