Skip to content

Do not write HTTP headers with SkipResponseBodyEncodeDecode #3695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions http/codegen/server_encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ func TestEncode(t *testing.T) {
{"empty-server-response", testdata.EmptyServerResponseDSL, testdata.EmptyServerResponseEncodeCode},
{"empty-server-response-with-tags", testdata.EmptyServerResponseWithTagsDSL, testdata.EmptyServerResponseWithTagsEncodeCode},

{"skip-response-body-encode-decode", testdata.ResponseEncoderSkipResponseBodyEncodeDecodeDSL, testdata.ResponseEncoderSkipResponseBodyEncodeDecodeCode},

{"result-with-custom-pkg-type", testdata.ResultWithCustomPkgTypeDSL, testdata.ResultWithCustomPkgTypeEncodeCode},
{"result-with-embedded-custom-pkg-type", testdata.EmbeddedCustomPkgTypeDSL, testdata.ResultWithEmbeddedCustomPkgTypeEncodeCode},
}
Expand Down
4 changes: 3 additions & 1 deletion http/codegen/templates/response_encoder.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ func {{ .ResponseEncoder }}(encoder func(context.Context, http.ResponseWriter) g
{{- end }}
{{- else }}
{{- with (index .Result.Responses 0) }}
w.WriteHeader({{ .StatusCode }})
{{- if not $.Method.SkipResponseBodyEncodeDecode }}
w.WriteHeader({{ .StatusCode }})
{{- end }}
return nil
{{- end }}
{{- end }}
Expand Down
10 changes: 10 additions & 0 deletions http/codegen/testdata/result_encode_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,16 @@ func EncodeMethodEmptyServerResponseWithTagsResponse(encoder func(context.Contex
}
`

var ResponseEncoderSkipResponseBodyEncodeDecodeCode = `// EncodeMethodResponseEncoderSkipResponse returns an encoder for responses
// returned by the ServiceResponseEncoderSkip MethodResponseEncoderSkip
// endpoint.
func EncodeMethodResponseEncoderSkipResponse(encoder func(context.Context, http.ResponseWriter) goahttp.Encoder) func(context.Context, http.ResponseWriter, any) error {
return func(ctx context.Context, w http.ResponseWriter, v any) error {
return nil
}
}
`

var ResultWithCustomPkgTypeEncodeCode = `// EncodeMethodResultWithCustomPkgTypeDSLResponse returns an encoder for
// responses returned by the ServiceResultWithCustomPkgTypeDSL
// MethodResultWithCustomPkgTypeDSL endpoint.
Expand Down
12 changes: 12 additions & 0 deletions http/codegen/testdata/server_dsls.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,15 @@ var ServerSkipResponseBodyEncodeDecodeDSL = func() {
})
})
}

var ResponseEncoderSkipResponseBodyEncodeDecodeDSL = func() {
Service("ServiceResponseEncoderSkip", func() {
Method("MethodResponseEncoderSkip", func() {
HTTP(func() {
GET("/")
SkipResponseBodyEncodeDecode()
Response(StatusOK)
})
})
})
}
Loading