Skip to content

bug: [golang] oepnAPI generate function does not write http.ResponseWriter correctly #3982

@chengkai-c

Description

@chengkai-c

Hi All, I would like to double check if this is a bug or just Im miss using the function.

I am trying to generate a Response with oneOf:

  responses:
    200:
      description: Entity objects.
      content:
        application/json:
          schema:
            $ref: "schemas.yml#/Response"
Response:
  oneOf:
    - $ref: '#/A'
    - $ref: '#/B'

Where you can think of A and B are two different Object

and the generate response is as following

type Response struct {
	union json.RawMessage
}

type RetrieveAnEntity200JSONResponse Response

func (response RetrieveAnEntity200JSONResponse) VisitResponseResponse(w http.ResponseWriter) error {
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(200)

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

The issue I found is, the VisitResponseResponse did not Encode the response correctly to http.ResponseWriter(meaning that, the http.responseWriter will have empty body).

I tried to debug and found out that the potential reason is because union is in lowercase. This means that union becomes invisible, and thus the Encode function can't access it, resulting in an empty body.

When I modify api.gen.go and change the union to upper case Union, then this problem is resolved.(I know I should not modify this file but this is only for debug purpose)

Does any one have any thought's about it? or did I do something wrong here.

Thank!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions