OpenAPITools / OpenAPITools/openapi-generator

[BUG] [GO] api.mustache template builds a client that does not correctly set header params

Open
#14,578 0 comments 0 reactions 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

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

Go's api.mustache template builds a Go client that adds header params as query params. The bug was introduced in this PR and still exists on the latest main branch.

openapi-generator version

This issue doesn't yet appear to be included in a release (the PR was merged on Nov 19, 2022, and the latest release was on Nov 1, 2022 [v6.2.1]).

That being said, the latest main branch includes this code. I'm using the openapi-generator-cli and docker without specifying a version and seem to be pulling in the changes from main.

OpenAPI declaration file content or url

Example yaml file with headers included:

swagger: "2.0"
info:
  version: 1.0.0
  title: Swagger Petstore
  description: >
    A sample API that uses a petstore as an example
    to demonstrate features in the swagger-2.0 specification
consumes:
  - application/json
produces:
  - application/json
paths:
  /pets:
    get:
      description: Returns all pets from the petstore
      parameters:
        - name: X-Region
          in: header
          type: string
          required: true
      responses:
        "200":
          description: pet response
          schema:
            type: array
            items:
              $ref: "#/definitions/pet"
        default:
          description: unexpected error
          schema:
            $ref: "#/definitions/errorModel"
definitions:
  pet:
    $ref: sample/pet.yaml
  pet-owner:
    $ref: sample/pet-owner.yaml
  errorModel:
    $ref: sample/error.json
Generation Details

I'm using the openapi-generator-cli and docker to generate my Go client.

Steps to reproduce
  1. Run openapi-generator-cli with docker on an OpenAPI spec yaml file that includes headers. Pull generated client code into a Go repo.
  2. Build requests using the above generated Golang client. Requests will not set header params at all (and instead it appears they will be added to query params).
Related issues/PRs
Suggest a fix

The fix should be minor - the api.mustache template needs a few lines updated to set headers instead of query params.

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

Inspect modules/openapi-generator/src/main/resources/go/api.mustache around lines 243-252, then generate a Go client from the header-containing OpenAPI YAML in the issue. Build a request with the generated client and verify that X-Region is sent as a header rather than appearing in query parameters; update the template and confirm the generated behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
api, tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.