Commit 0aad7b8
fix(enum): apply enum_value custom strings on Go server & client JSON (#214)
* fix(enum): apply enum_value custom strings on Go server & client JSON
The OpenAPI generator (and TS/Python clients) honor (sebuf.http.enum_value)
and emit "low"/"medium"/"high", but the generated Go HTTP server emitted the
raw proto names ("RISK_LEVEL_LOW"). Both Go surfaces serialize at the message
level with protojson, which never invokes the Go enum type's MarshalJSON — so
the enum-type marshalers in *_enum_encoding.pb.go are dead code server-side.
Add a message-level marshaler (*_enum_field_encoding.pb.go) for any message
with a custom-enum field. It reuses the existing xToJSON/xFromJSON lookup maps
to rewrite enum fields between proto value names and custom strings, in both
directions (MarshalJSONSebuf emits "low"; UnmarshalJSONSebuf accepts "low"),
across all shapes: singular, proto3 optional, repeated, and map<_, enum>.
Because a Go type can own only one MarshalJSON, register enum_value in the
existing fail-fast conflict web (flatten, oneof) plus a new
checkEnumMarshalJSONConflict, so combining a custom enum with another
JSON-mapping annotation errors clearly instead of emitting duplicate methods.
Mirrored in clientgen for Go-client request parity. Adds examples/enum-encoding
with an end-to-end test proving the wire format. Also regenerates the
TimestampFormat OpenAPI golden, which was already stale on current deps from a
recent protobuf bump (unrelated to this change).
Follow-ups tracked in #213 (transitive nesting, generator composition,
dual-plugin file collision, cross-package enums).
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* chore(lint): suppress dupl on generated enum marshaler helpers
golangci-lint flags the message-level marshaler/unmarshaler generators as
duplicates of the flatten/timestamp equivalents. Add //nolint:dupl where dupl
actually fires (matching the existing bytes_encoding.go convention), placed
asymmetrically to avoid an unused-directive nolintlint error.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* fix(enum): handle UseProtoNames keys and fail loudly on cross-package enums
Addresses Codex review feedback on the enum_value marshaler:
1. UseProtoNames regression: the patcher keyed only on the camelCase JSON name,
so a server/client configured with protojson UseProtoNames (snake_case keys)
left enum fields unpatched and leaked raw proto names. Marshal/unmarshal now
patch both the JSON name and the proto name key.
2. Cross-package enums were silently skipped (silent wrong output). Generation
now fails loudly via validateEnumFieldEncoding when a custom-enum field
references an enum from another Go package, since the marshaler relies on that
package's private lookup maps. Full cross-package support remains tracked in #213.
Adds a UseProtoNames end-to-end assertion to examples/enum-encoding and updates
the consistency test for the two-key patch shape.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>1 parent 96cc343 commit 0aad7b8
36 files changed
Lines changed: 3431 additions & 10 deletions
File tree
- examples/enum-encoding
- api/proto/services
- proto/services
- internal
- clientgen
- testdata/golden
- httpgen
- testdata
- golden
- proto
- openapiv3/testdata/golden
- json
- yaml
- tsclientgen/testdata/golden
- tsservergen/testdata/golden
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
Lines changed: 304 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
0 commit comments