英文:
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
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论