Repository navigation
feat: encode and decode JSON through the runtime codec - #2665
Conversation
Generated code now goes through the process-wide JSON codec added in runtime v1.9.0, so runtime.SetJSONCodec applies to it. - Replace json.Marshal and json.Unmarshal in the templates with runtime.JSONMarshal and runtime.JSONUnmarshal. - Encode strict server responses with runtime.JSONMarshalWrite, keeping the intermediate buffer. - Leave the json.NewDecoder probe in the union UnmarshalText on encoding/json, since it depends on UseNumber. - Bump the runtime dependency of examples and internal/test to v1.9.0. - Regenerate the fixtures with Go 1.27. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
[High impact] The PR appears safe to merge; no new actionable issue was found. SummaryGenerated JSON calls now use the runtime codec. The latest changes add broader codec tests and update documentation.
Reviews (3) · Last reviewed commit: "docs: update generated-code samples and ..." · Reviewed by Greptile |
Add a round trip through the generated client and the strict stdhttp server with a counting codec installed. The existing tests only check the generated text, so they cannot tell the codec from a direct call to encoding/json. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
|
Thanks @CRaLFa, this follows the split exactly: every call site is switched, the strict response keeps its buffer, and the regenerated files contain nothing but the swap and the import changes. Two things I'd like addressed before merging. 1. The test doesn't cover enough
Please extend existing fixtures that already have the right types where you can, in the category that matches the feature, rather than adding new specs. Since the codec is process-wide, none of these tests may call 2. Documentation that is now wrong
The |
…a custom codec Run the remaining kinds of call site with a codec set through runtime.SetJSONCodec. - schemas/objects: MarshalJSON and UnmarshalJSON of an additionalProperties type. - aggregates/oneof: From*, As*, Merge*, MarshalJSON and UnmarshalJSON of a union, with and without additionalProperties. - parameters/roundtrip: JSON-valued path, query, header and cookie parameters, client and server, for every framework. - servers/strict/stdhttp: a codec implementing runtime.StreamingJSONCodec receives the request body and the response as streams. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
….9.0 - Show runtime.JSONMarshal and runtime.JSONUnmarshal in the additionalProperties, anyOf and oneOf samples of the README. - Raise the go directive in docs/stdhttp-server.md to 1.26, which runtime v1.9.0 requires. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
|
@mromaszewicz Thanks for the review. Both points are addressed. Tests (9ab50ad), extending existing fixtures, none of them parallel:
Docs (1f2d3e1): One question: the server table in the README lists |
|
Thanks for doing this, and thanks for being flexible in addressing changes. |
* chore: fix malformed Greptile config and duplicate rule number .greptile/config.json began with two opening braces, so it has not been valid JSON since it was last edited, and the strictness and *.gen.go settings in it could not be parsed. Remove the extra brace. .greptile/rules.md had two sections numbered 5. The schema-merging rule, which comes last, becomes section 8. Co-Authored-By: Claude <noreply@anthropic.com> * chore: add a Greptile rule that generated JSON uses the runtime codec Generated code is moving to the process-wide JSON codec in runtime v1.9.0 (#2665), so that runtime.SetJSONCodec applies to all of it. A template that calls encoding/json directly escapes the codec without any test noticing, because the default codec is encoding/json. Add a review rule that flags direct json.Marshal, json.Unmarshal, json.NewEncoder and json.NewDecoder calls in templates and in code that emits generated source, names the runtime function to use for each, and protects the buffer-before-WriteHeader ordering of the strict response. It lists what is exempt: json.RawMessage, json.Number, json.Valid, the UseNumber probe in the union template, JSON a web framework handles, and the generator's own use of encoding/json. Co-Authored-By: Claude <noreply@anthropic.com> --------- Co-authored-by: Claude <noreply@anthropic.com>
Generated code imports github.com/oapi-codegen/runtime, and with the JSON codec change (#2665) it needs runtime v1.9.0, which requires Go 1.26. The README still listed Go 1.24 or 1.25 per server and said the generated code has lower requirements than oapi-codegen itself. Set every row of the Supported Servers table to 1.26+, and reword the table's introduction and the note under Installing to say that the generated code has the same Go 1.26 floor and why. Co-authored-by: Claude <noreply@anthropic.com>
Follow-up to #2371, replacing it. Generated code now goes through the process-wide JSON codec added in runtime v1.9.0 (oapi-codegen/runtime#183), so
runtime.SetJSONCodecapplies to it.This follows the split described in #2371 (comment).
Changes
json.Marshalandjson.Unmarshalin the templates becomesruntime.JSONMarshalandruntime.JSONUnmarshal. That covers the model code (additional-properties.tmpl,union.tmpl,union-and-additional-properties.tmpl), client request bodies and JSON-valued parameters, client response parsing (template_helpers.go), and server JSON-valued parameters for every framework.strict/strict-interface.tmplencode withruntime.JSONMarshalWrite(&buf, ...). The intermediate buffer and the ordering aroundWriteHeaderare unchanged.encoding/json: thejson.NewDecoder(...).UseNumber()probe in the unionUnmarshalText, andjson.Valid,json.RawMessageandjson.Number.ctx.JSONin the echo, gin, fiber and iris strict servers.examplesandinternal/testis bumped to v1.9.0.TestContentTypesShortNameexpectedjson.Unmarshal(bodyBytes, &dest)in the generated code; it now expects theruntimecall.Fixtures
Regenerated with
make generateon Go 1.27. Every changed line in the generated files is one of the replaced calls, a droppedencoding/jsonimport or an addedruntimeimport. The embedded spec blobs are untouched.The import lists needed no change:
imports.tmplalready lists both packages, andimports.Processdrops the unused one.Tests
Each kind of call site runs with a codec set through
runtime.SetJSONCodec. The tests extend existing fixtures, and none of them is parallel, since the codec is process-wide.servers/strict/stdhttp: one JSON round trip through the generated client and the strict server reaches the codec at the client request body, the server request binding, the server response and the client response parsing. A codec implementingruntime.StreamingJSONCodecgets the request body throughUnmarshalReadand the response throughMarshalWrite, with its byte methods not called.schemas/objects:MarshalJSON/UnmarshalJSONof anadditionalPropertiestype, called through the codec so that they re-enter it.aggregates/oneof: a union'sFrom*/As*/Merge*, andMarshalJSON/UnmarshalJSONof a union withadditionalProperties.parameters/roundtrip: JSON-valued path, query, header and cookie parameters, one client marshal and one server unmarshal each, for every framework in the sharedtestImpl.Docs
docs/stdhttp-server.mdsays Go 1.26, which runtime v1.9.0 requires.additionalProperties,anyOfandoneOfsamples in the README showruntime.JSONMarshalandruntime.JSONUnmarshal.Verification
On Go 1.27:
make test,make lintandmake tidy-cipass.🤖 Generated with Claude Code