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

[BUG] go-server doesn't honour the provided headers within EncodeJSONResponse #9795

Closed
5 of 6 tasks
mgoltzsche opened this issue Jun 16, 2021 · 1 comment · Fixed by #9814
Closed
5 of 6 tasks

[BUG] go-server doesn't honour the provided headers within EncodeJSONResponse #9795

mgoltzsche opened this issue Jun 16, 2021 · 1 comment · Fixed by #9814

Comments

@mgoltzsche
Copy link
Contributor

mgoltzsche commented Jun 16, 2021

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When the option addResponseHeaders is enabled for the go-server generator it generates a ResponseWithHeaders method that calls an EncodeJSONResponse method which however ignores the headers that are provided to it.
The generated method looks as follows:

func EncodeJSONResponse(i interface{}, status *int, headers map[string][]string, w http.ResponseWriter) error {
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
	if status != nil {
		w.WriteHeader(*status)
	} else {
		w.WriteHeader(http.StatusOK)
	}

	return json.NewEncoder(w).Encode(i)
}

This stops me from specifying response headers within my controller implementation.

I would expect this method to apply the headers I provided.

openapi-generator version

openapi-generator 5.1.1

OpenAPI declaration file content or url

This is the example OpenAPI schema example-openapi.yaml - though you could use any other to reproduce the bug that is described here:

openapi: "3.0.3"

info:
  title: "Example API"
  version: "1.0.0"

paths:
  "/path/{myparam}/myresource":
    post:
      operationId: createResource
      parameters:
      - name: myparam
        in: path
        required: true
        schema:
          type: string
      requestBody:
        content:
          "application/json":
            schema:
              type: object
              properties:
                name:
                  type: string
      responses:
        '201':
          description: response with headers
          headers:
            myheader:
              schema:
                type: string
Generation Details
java -jar ./openapi-generator-5.1.1.jar generate -i ./example-openapi.yaml -g go-server -o ./generated --api-name-suffix API --package-name generated -p addResponseHeaders=true
Steps to reproduce

see generation details

Related issues/PRs
Suggest a fix

Generate the EncodeJSONResponse method so that it applies the provided headers to the ResponseWriter:

func EncodeJSONResponse(i interface{}, status *int, headers map[string][]string, w http.ResponseWriter) error {
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
	if headers != nil {
		for k, v := range headers {
			w.Header()[k] = v
		}
	}
	if status != nil {
		w.WriteHeader(*status)
	} else {
		w.WriteHeader(http.StatusOK)
	}

	return json.NewEncoder(w).Encode(i)
}
@mgoltzsche
Copy link
Contributor Author

mgoltzsche commented Jun 16, 2021

Apparently this is caused by a typo within the mustache template here where the condition refers to addResponseHeader instead of addResponseHeaders as the documentation says and as it is the case other parts of the template.

Fortunately this also means that there's a workaround already by specifying both addResponseHeaders=true and addResponseHeader=true ;)

mgoltzsche added a commit to mgoltzsche/openapi-generator that referenced this issue Jun 20, 2021
According to the [documentation](https://openapi-generator.tech/docs/generators/go-server) the go-server generator should support an `addResponseHeaders` option but a template uses an `addResponseHeader` option in some places which this PR fixes.

Closes OpenAPITools#9795

Signed-off-by: Max Goltzsche <max.goltzsche@gmail.com>
wing328 pushed a commit that referenced this issue Jun 21, 2021
According to the [documentation](https://openapi-generator.tech/docs/generators/go-server) the go-server generator should support an `addResponseHeaders` option but a template uses an `addResponseHeader` option in some places which this PR fixes.

Closes #9795

Signed-off-by: Max Goltzsche <max.goltzsche@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant