Skip to content

Commit dd2eff6

Browse files
authored
Do not write HTTP headers with SkipResponseBodyEncodeDecode (#3695)
Make it possible for user code to write HTTP headers.
1 parent 91eb849 commit dd2eff6

4 files changed

Lines changed: 27 additions & 1 deletion

File tree

http/codegen/server_encode_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ func TestEncode(t *testing.T) {
8585
{"empty-server-response", testdata.EmptyServerResponseDSL, testdata.EmptyServerResponseEncodeCode},
8686
{"empty-server-response-with-tags", testdata.EmptyServerResponseWithTagsDSL, testdata.EmptyServerResponseWithTagsEncodeCode},
8787

88+
{"skip-response-body-encode-decode", testdata.ResponseEncoderSkipResponseBodyEncodeDecodeDSL, testdata.ResponseEncoderSkipResponseBodyEncodeDecodeCode},
89+
8890
{"result-with-custom-pkg-type", testdata.ResultWithCustomPkgTypeDSL, testdata.ResultWithCustomPkgTypeEncodeCode},
8991
{"result-with-embedded-custom-pkg-type", testdata.EmbeddedCustomPkgTypeDSL, testdata.ResultWithEmbeddedCustomPkgTypeEncodeCode},
9092
}

http/codegen/templates/response_encoder.go.tpl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ func {{ .ResponseEncoder }}(encoder func(context.Context, http.ResponseWriter) g
3333
{{- end }}
3434
{{- else }}
3535
{{- with (index .Result.Responses 0) }}
36-
w.WriteHeader({{ .StatusCode }})
36+
{{- if not $.Method.SkipResponseBodyEncodeDecode }}
37+
w.WriteHeader({{ .StatusCode }})
38+
{{- end }}
3739
return nil
3840
{{- end }}
3941
{{- end }}

http/codegen/testdata/result_encode_functions.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,16 @@ func EncodeMethodEmptyServerResponseWithTagsResponse(encoder func(context.Contex
10361036
}
10371037
`
10381038

1039+
var ResponseEncoderSkipResponseBodyEncodeDecodeCode = `// EncodeMethodResponseEncoderSkipResponse returns an encoder for responses
1040+
// returned by the ServiceResponseEncoderSkip MethodResponseEncoderSkip
1041+
// endpoint.
1042+
func EncodeMethodResponseEncoderSkipResponse(encoder func(context.Context, http.ResponseWriter) goahttp.Encoder) func(context.Context, http.ResponseWriter, any) error {
1043+
return func(ctx context.Context, w http.ResponseWriter, v any) error {
1044+
return nil
1045+
}
1046+
}
1047+
`
1048+
10391049
var ResultWithCustomPkgTypeEncodeCode = `// EncodeMethodResultWithCustomPkgTypeDSLResponse returns an encoder for
10401050
// responses returned by the ServiceResultWithCustomPkgTypeDSL
10411051
// MethodResultWithCustomPkgTypeDSL endpoint.

http/codegen/testdata/server_dsls.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,3 +294,15 @@ var ServerSkipResponseBodyEncodeDecodeDSL = func() {
294294
})
295295
})
296296
}
297+
298+
var ResponseEncoderSkipResponseBodyEncodeDecodeDSL = func() {
299+
Service("ServiceResponseEncoderSkip", func() {
300+
Method("MethodResponseEncoderSkip", func() {
301+
HTTP(func() {
302+
GET("/")
303+
SkipResponseBodyEncodeDecode()
304+
Response(StatusOK)
305+
})
306+
})
307+
})
308+
}

0 commit comments

Comments
 (0)