Description
Description
Implementing content-encoding and compression via the current functionality feels unnecessarily complex. Currently, I need to rely on boilerplate middleware for this. It would be much more efficient if content encoding could be handled directly through a generator extension.
Current structure:
responses:
'200':
description: ...
content:
application/gzip:
Desired structure (examples):
responses:
'200':
description: ...
content:
application/json:
x-content-encoding: gzip
or
responses:
'200':
description: ...
content:
application/json:
x-content-encoding: zstd
Alternatively, the current structure should generate correct content encoding when specifying application/gzip
:
responses:
'200':
description: ...
content:
application/gzip:
At the moment, it generates the following Go code:
func encodeExampleResponse(response Example, w http.ResponseWriter, span trace.Span) error {
w.Header().Set("Content-Type", "application/gzip")
w.WriteHeader(200)
span.SetStatus(codes.Ok, http.StatusText(200))
writer := w
if _, err := io.Copy(writer, response); err != nil {
return errors.Wrap(err, "write")
}
return nil
}
But the expected code that would reduce my reliance on middleware should look more like this:
func encodeExampleResponse(response Example, w http.ResponseWriter, span trace.Span) error {
w.Header().Set("Content-Type", "application/gzip")
w.WriteHeader(200)
w.Header().Set("Content-Encoding", "gzip")
span.SetStatus(codes.Ok, http.StatusText(200))
gzipWriter := gzip.NewWriter(w)
defer gzipWriter.Close()
if _, err := io.Copy(gzipWriter, response); err != nil {
return errors.Wrap(err, "write")
}
return nil
}
This would automatically handle content encoding and compression more efficiently, improving both developer experience and code generation.
Let me know what you think, it idea overall is likeable I could work on PR. I like idea of correcting application/gzip response, not implementing generator extension instead.