我有以下开放API规范描述:
openapi: 3.0.3
...
/foo:
get:
operationId: foo
parameters:
- name: bar
in: query
required: true
schema:
type: string
responses:
200:
description: description
content:
application/json:
schema:
type: array
items:
type: string
headers:
myheader1:
description: myheader1 desc
schema:
type: string
myheader2:
description: myheader2 desc
schema:
type: integer
401:
description: "Unauthorized"
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
生成的代码是:
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2024-01-24T14:16:01.056664+03:00[Europe/London]")
@Validated
@Tag(name = "Default", description = "the Default API")
@RequestMapping("${openapi.directoryServiceManager.base-path:/api/v1}")
public interface DefaultApi {
/**
* GET /foo
*
* @param bar (required)
* @return description (status code 200)
* or Unauthorized (status code 401)
*/
@Operation(
operationId = "foo",
responses = {
@ApiResponse(responseCode = "200", description = "description", content = {
@Content(mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = String.class)))
}),
@ApiResponse(responseCode = "401", description = "Unauthorized", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = ErrorDto.class))
})
}
)
@RequestMapping(
method = RequestMethod.GET,
value = "/foo",
produces = { "application/json" }
)
ResponseEntity<List<String>> foo(
@NotNull
@Parameter(name = "bar", description = "", required = true, in = ParameterIn.QUERY) @Valid @RequestParam(value = "bar", required = true) String bar
);
}
如您所见,没有有关响应标头的信息(
myheader1
和myheader2
)
我希望如此
ApiResponse 应该添加 headers 参数
有办法解决吗?
更多环境信息:
buildscript {
dependencies {
classpath("org.openapitools:openapi-generator-gradle-plugin:5.0.0")
}
}
plugins {
java
kotlin("jvm")
kotlin("plugin.spring") version "1.8.21"
id("org.springframework.boot") version "3.1.4"
id("org.openapi.generator") version "6.3.0"
}
根据评论中的建议,我尝试更新版本:
buildscript {
dependencies {
classpath("org.openapitools:openapi-generator-gradle-plugin:7.2.0")
}
}
plugins {
java
kotlin("jvm")
kotlin("plugin.spring") version "1.8.21"
id("io.spring.dependency-management") version "1.1.0"
id("org.springframework.boot") version "3.1.4"
id("org.openapi.generator") version "6.5.0"
id("org.jlleitschuh.gradle.ktlint") version "11.5.0"
jacoco
}
生成的代码仍然不包含有关响应标头的任何信息:
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2024-01-24T16:42:30.088851100+03:00[Europe/Moscow]")
@Validated
@Tag(name = "Default", description = "the Default API")
@RequestMapping("${openapi.directoryServiceManager.base-path:/api/v1}")
public interface DefaultApi {
/**
* GET /foo
*
* @param bar (required)
* @return description (status code 200)
* or Unauthorized (status code 401)
*/
@Operation(
operationId = "foo",
responses = {
@ApiResponse(responseCode = "200", description = "description", content = {
@Content(mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = String.class)))
}),
@ApiResponse(responseCode = "401", description = "Unauthorized", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = ErrorDto.class))
})
}
)
@RequestMapping(
method = RequestMethod.GET,
value = "/foo",
produces = { "application/json" }
)
ResponseEntity<List<String>> foo(
@NotNull
@Parameter(name = "bar", description = "", required = true, in = ParameterIn.QUERY) @Valid @RequestParam(value = "bar", required = true) String bar
);
}
嗯,看来您面临的问题到目前为止还没有任何简单的解决方案。正如它来自这个问题的答案:openapi-generator does not generated response headers for Java client using Spring Webclient
基于 spring 的 openapi-generator 模板未提及响应 标题
这是
JavaSpring
的模板:JavaSpring/api.mustache
这个是为
scala-akka-client
准备的:scala-akka-client/api.mustache
如您所见,与第二个不同,第一个没有声明任何对
{{#responseHeaders}}
的引用。
此外,还有一个问题提交,描述了类似的问题:https://github.com/OpenAPITools/openapi-generator/issues/11061并且据我所知,它尚未修复。
理论上,您可以尝试使用自定义模板进行代码生成,但由于它来自上述响应,因此所有响应都将具有相同的标头,这与您的架构相矛盾(自定义标头仅应用于
200
).