Skip to content

Commit 31b46c5

Browse files
mromaszewiczclaude
andcommitted
fix: generate correct type for $ref text responses in strict server (oapi-codegen#2190)
When a response is defined as a $ref to a component response with text/plain content type (and no headers), the strict-server template generated a struct-embedding type whose Visit method tried to call []byte(response) on a struct, which failed to compile. The root cause was the revert of PR oapi-codegen#1132 (commit 891a067), which had originally fixed this by making all text responses string types. That PR was reverted because it broke text responses with headers (oapi-codegen#1676), which require the struct form with a Body field. The fix extends the existing multipart special case in Branch 1A of the strict-interface template to also cover text responses without headers. This generates a named type alias (e.g. `type GetTest401TextResponse UnauthorizedTextResponse`) instead of a struct embedding, so []byte(response) compiles and works correctly. Text responses with headers continue to go through Branch 1C (struct with Body + Headers fields), so oapi-codegen#1676 is unaffected — confirmed by verifying no diff in internal/test/issues/issue-1676/ping.gen.go. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent ac5f448 commit 31b46c5

File tree

3 files changed

+3
-3
lines changed

3 files changed

+3
-3
lines changed

internal/test/issues/issue-2190/issue2190.gen.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/test/issues/issue-2190/issue2190_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
// where the generated code tried to do []byte(response) on a struct type,
1515
// which does not compile.
1616
func TestGetTest401TextResponse(t *testing.T) {
17-
resp := GetTest401TextResponse{UnauthorizedTextResponse("Unauthorized")}
17+
resp := GetTest401TextResponse("Unauthorized")
1818
w := httptest.NewRecorder()
1919

2020
err := resp.VisitGetTestResponse(w)

pkg/codegen/templates/strict/strict-interface.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
{{range .Contents}}
4141
{{$receiverTypeName := printf "%s%s%s%s" $opid $statusCode .NameTagOrContentType "Response"}}
4242
{{if and $fixedStatusCode $isRef -}}
43-
{{ if and (not $hasHeaders) ($fixedStatusCode) (.IsSupported) (eq .NameTag "Multipart") -}}
43+
{{ if and (not $hasHeaders) ($fixedStatusCode) (.IsSupported) (or (eq .NameTag "Multipart") (eq .NameTag "Text")) -}}
4444
type {{$receiverTypeName}} {{$ref}}{{.NameTagOrContentType}}Response
4545
{{else if $isExternalRef -}}
4646
type {{$receiverTypeName}} struct { {{$ref}} }

0 commit comments

Comments
 (0)