Skip to content
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

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

Open
vlle opened this issue Oct 19, 2024 · 0 comments
Labels
enhancement New feature or request openapi-features OpenAPI features support issues

Comments

@vlle
Copy link

vlle commented Oct 19, 2024

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
@vlle vlle added enhancement New feature or request openapi-features OpenAPI features support issues labels Oct 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request openapi-features OpenAPI features support issues
Projects
None yet
Development

No branches or pull requests

2 participants
@vlle and others