OpenApi Generator Golang – 如何修复将请求头添加到查询参数的问题?

huangapple go评论105阅读模式
英文:

OpenApi Generator Golang - Adding headers to request ending up in query params how to fix?

问题

我正在尝试生成一个Go客户端,但生成器无法识别头部,并且不允许我将其作为头部传递给服务器,而是将其作为查询参数发送。

以下是我正在使用的生成器:

# https://github.com/OpenAPITools/openapi-generator/blob/master/docs/generators/go.md
docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli generate \
  -i /local/spec.yaml \
  -g go \
  -o /local/internal/infrastructure/sdk \
  -p enumClassPrefix=true \
  -p generateInterfaces=true \
  -p isGoSubmodule=true \
  -p packageName=sdk

你会注意到它生成了以下代码行,将我传递的头部值添加到查询参数中:

parameterAddToQuery(localVarQueryParams, "X-Request-ID", r.xRequestID, "")

这是一个错误吗?我该怎么办?

英文:

I'm trying to generate a go client but the generator won't recognise a header and won't let me pass it to the server as a header - instead it's sent as a query param.

info:
  title: API
  version: "1.2"
servers:
  - url: https://example.com
paths:
  /ping:
    get:
      summary: Checks if the server is alive
      parameters:
        - in: header
          name: X-Request-ID
          schema:
            type: string
            format: uuid
          required: true
      responses:
        '200':
          description: Request has been successful
          content:
            application/json:
              schema:
                type: object
                properties:
                  returned_url:
                    type: string

And here is the generator I am using:

# https://github.com/OpenAPITools/openapi-generator/blob/master/docs/generators/go.md
docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli generate \
  -i /local/spec.yaml \
  -g go \
  -o /local/internal/infrastructure/sdk \
  -p enumClassPrefix=true \
  -p generateInterfaces=true \
  -p isGoSubmodule=true \
  -p packageName=sdk

You'll notice it generates this line which adds the value of the header I pass to the query params:

parameterAddToQuery(localVarQueryParams, "X-Request-ID", r.xRequestID, "")

Is this a bug? What can I do about this?

答案1

得分: 2

这看起来像是go模板中的一个错误:
https://github.com/OpenAPITools/openapi-generator/blob/4487042f0d4ef1f4686950f0c1ddac6fe6d44f98/modules/openapi-generator/src/main/resources/go/api.mustache#L243-L252

它应该与Java模板中的方式类似:
https://github.com/OpenAPITools/openapi-generator/blob/4487042f0d4ef1f4686950f0c1ddac6fe6d44f98/modules/openapi-generator/src/main/resources/Java/api.mustache#L89-L91

英文:

This looks like a bug in the go template:
https://github.com/OpenAPITools/openapi-generator/blob/4487042f0d4ef1f4686950f0c1ddac6fe6d44f98/modules/openapi-generator/src/main/resources/go/api.mustache#L243-L252

It should be similar to how its done in the Java template:
https://github.com/OpenAPITools/openapi-generator/blob/4487042f0d4ef1f4686950f0c1ddac6fe6d44f98/modules/openapi-generator/src/main/resources/Java/api.mustache#L89-L91

huangapple
  • 本文由 发表于 2022年12月5日 06:17:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/74681442.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定