Skip to content

Content-Encoding: Add option for setting an content-encoding OR reworking a application/gzip response  #1331

Open
@vlle

Description

@vlle

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.

References:

  1. OpenAPI Specification v3.1.0 Extensions
  2. Mozilla: Accept-Encoding Header
  3. OpenAPI GitHub Issue #738
  4. Mozilla: Content-Encoding Header

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions