Stringify non-string text/plain responses in strict servers#2464
Conversation
Greptile SummaryThis PR fixes a code generation bug where
Confidence Score: 5/5Safe to merge — the fix is a targeted one-line change per template, all fixtures have been regenerated consistently, and runtime tests cover the previously-broken integer and boolean paths on three backends. The template change replaces an expression that either failed to compile or silently produced wrong output with one that is correct for all primitive types and byte-identical for strings. Every strict-server *.gen.go fixture was regenerated. New regression tests with explicit body-content assertions were added for stdhttp, fiber, and iris. No configuration structs, public API signatures, or serialization wire formats were altered. No files require special attention.
|
| Filename | Overview |
|---|---|
| pkg/codegen/templates/strict/strict-interface.tmpl | Core template fix: replaces []byte(response) with []byte(fmt.Sprint(response)) for text/plain responses on the net/http path, correctly handling non-string schemas |
| pkg/codegen/templates/strict/strict-fiber-interface.tmpl | Same fix applied to fiber (v2) template: string(response) → fmt.Sprint(response) in ctx.WriteString call |
| pkg/codegen/templates/strict/strict-fiber-v3-interface.tmpl | Same fix applied to fiber v3 template, consistent with v2 change |
| pkg/codegen/templates/strict/strict-iris-interface.tmpl | Same fix applied to iris template: string(response) → fmt.Sprint(response) in ctx.WriteString call |
| internal/test/bodies/content_types/text_non_string/text_non_string_test.go | New regression tests covering integer ("201"), boolean ("true"), and string ("pong") text/plain responses on the std-http path |
| internal/test/bodies/content_types/text_non_string/fiber/fiber_test.go | Fiber (v2) path regression test asserting integer 201 serializes as "201" not a Unicode code point |
| internal/test/bodies/content_types/text_non_string/iris/iris_test.go | Iris path regression test asserting integer 201 serializes as "201" not a Unicode code point |
| internal/test/bodies/content_types/text_non_string/spec.yaml | New OpenAPI spec with three /ping responses covering string, integer, and boolean text/plain schemas; correctly placed under bodies/content_types/ |
| internal/test/servers/strict/fiber/server.gen.go | Regenerated fixture updated consistently: four text/plain Visit* methods now use fmt.Sprint; changes are scoped to string-kinded responses so runtime behavior is unchanged |
| internal/test/bodies/responses/headers/headers.gen.go | Regenerated: response.Body (a string) now wrapped in fmt.Sprint — semantically identical for string types, no behavioral change |
Reviews (2): Last reviewed commit: "Stringify non-string text/plain response..." | Re-trigger Greptile
Closes: oapi-codegen#1897 For a `text/plain` response whose schema is not a string, the strict server generated an invalid body conversion in the response `Visit` method. On the net/http path it emitted `w.Write([]byte(response))`, which fails to compile for a non-string underlying type (integer, boolean, `openapi_types.File`). On the fiber/iris/fiber-v3 path it emitted `ctx.WriteString(string(response))`, which compiles for an integer but silently produces the Unicode code point for that value (e.g. `string(201)` yields "É") and trips a `go vet` warning. Only a plain `string` schema worked. Wrap the response value in `fmt.Sprint` so any primitive is rendered as its natural text form: strings pass through unchanged (byte-identical output), integers/booleans/floats render as their literal representation. This mirrors how response headers are already stringified. `goimports` adds the `fmt` import as needed. Adds internal/test/bodies/content_types/text_non_string exercising integer, boolean, and string text/plain responses with runtime assertions that the integer response writes "201" and the boolean writes "true"; regenerates the strict-server fixtures, whose string text responses are unaffected at runtime. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
677a8fa to
330ecaf
Compare
Closes: #1897
For a
text/plainresponse whose schema is not a string, the strict server generated an invalid body conversion in the responseVisitmethod. On the net/http path it emittedw.Write([]byte(response)), which fails to compile for a non-string underlying type (integer, boolean,openapi_types.File). On the fiber/iris/fiber-v3 path it emittedctx.WriteString(string(response)), which compiles for an integer but silently produces the Unicode code point for that value (e.g.string(201)yields "É") and trips ago vetwarning. Only a plainstringschema worked.Wrap the response value in
fmt.Sprintso any primitive is rendered as its natural text form: strings pass through unchanged (byte-identical output), integers/booleans/floats render as their literal representation. This mirrors how response headers are already stringified.goimportsadds thefmtimport as needed.Adds internal/test/bodies/content_types/text_non_string exercising integer, boolean, and string text/plain responses with runtime assertions that the integer response writes "201" and the boolean writes "true"; regenerates the strict-server fixtures, whose string text responses are unaffected at runtime.