OpenAPITools / OpenAPITools/openapi-generator

[Question] Go-server response: Content-Type text/html

Open
#12,853 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Hello,

I have a huge problem with the specification of the response type in openapi. We want to deploy a html web page if the user access the root path /.

The other paths can be encoded as json, but for this case we need the content-type: text/html or application/xml encoding.
However, in any case how I define the response in the .yaml, I get always a json response.

My reponse is described as that:

responses:
        200:
          description: success and returns some html text
          content:
            text/html:
              schema:
                type: string

Anyways, the content-type of this response is always application/json. No matter, if I use the openapi 3.0 specification or swagger 2.0.

I also tried to use the additional argument withXml=True and addResponseHeaders=True

I saw, that the get functions always execute the EncodeJSONResponse function, which is the following:

func EncodeJSONResponse(i interface{}, status *int, headers map[string][]string, w http.ResponseWriter) error {
	wHeader := w.Header()
	if headers != nil {
		for key, values := range headers {
			for _, value := range values {
				wHeader.Add(key, value)
			}
		}
	}
	wHeader.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)
}

It can be seen, that even if you set Content-Type: text/html in the additional response header, it will be overwritten by wHeader.Set("Content-Type", "application/json; charset=UTF-8").

It is really important for us, to return a html page for the user which is using the api.
Otherwise I have to discard my whole project with openapi...

So my question is, if there is one way to return a html response with the go-server.

Hopefully, someone of you can help me.

Best Regards

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the generated Go server's get functions and the EncodeJSONResponse entry point shown in the issue. Trace how the OpenAPI response content type reaches the response writer, then verify that a text/html response is preserved without being overwritten by application/json; exercise the behavior with a generated server example.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.