我们通过 gradle (即openapi-generator-gradle-plugin )使用 OpenAPI来生成 Jersey 资源。一般来说,这工作得很好。
生成的方法如下所示:
public Response getSomeFoo(@ApiParam(...) String someParam, ...) { ...
Run Code Online (Sandbox Code Playgroud)
但我们还希望支持使用异步服务器 API 的方法。
理论上,我们可以将所有生成的同步方法更改为异步方法,即调整模板文件api.mustache并apiService.mustache替换Responsebyvoid并添加@Suspended final AsyncResponse asyncResponse作为第一个参数(加上添加导入等)。
但将所有请求从同步处理转换为异步处理对我们来说是没有意义的。
所以我的问题是:我们如何实现某种“开关”来在输入文件中指示我们想要为每个方法生成(同步/异步)哪种类型的实现?
我正在考虑编写一个新的生成器,它从输入规范文件中读取例如标签,并将其放入在模板文件中评估的布尔变量中。这可行吗?有没有类似的问题已经解决了?或者你还有什么其他的想法要告诉我吗?
谢谢!
我最近尝试为我的 Sprint Boot 应用程序生成 OpenAPI 文档。我将以下几行添加到我的pom.xml
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.2.30</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
但是当我在本地主机上点击 swagger-ui URL 时,我得到了这个页面

我检查了 JSON,/v3/api-docs并得到了以下开头的响应:
"{\"openapi\":\"3.0.1\",\"info\":{\"title\":\"OpenAPI definition\",\"version\":\"v0\"},\"servers\":[{\"url\":\"https://localhost:8900/tds\",
Run Code Online (Sandbox Code Playgroud)
我可以看到正在指定该openapi字段,但看起来整个响应都是字符串化的,而不仅仅是 JSON。由于 OpenAPI 似乎没有任何配置,我认为它是从我的 Spring Boot 配置中的某些内容获取的,但我不知道在哪里。
我正在使用 Swagger Codegen 3.0.19,还尝试了 OpenAPI Generator 4.0.3。
Java环境:
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
Run Code Online (Sandbox Code Playgroud)
跑步者:
java -jar ./libs/openapi-generator-cli-4.3.0.jar generate \
-i pet.yaml \
-g spring \
-o ./OUTPUT/api/
Run Code Online (Sandbox Code Playgroud)
这是我的 OpenAPI 架构:
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
Run Code Online (Sandbox Code Playgroud)
输出是:
java -jar ./libs/openapi-generator-cli-4.3.0.jar generate \
-i pet.yaml \
-g spring \
-o ./OUTPUT/api/
Run Code Online (Sandbox Code Playgroud)
为什么代码生成器将 ON/OFF 枚举转换为 TRUE/FALSE?当我使用 Swagger Editor GUI 生成时,它不会执行此操作。
我使用以下 OpenAPI 命令来使用openapi-generator生成客户端:
npx @openapitools/openapi-generator-cli generate -i api.yaml -g cpp-restsdk -o %CD%
Run Code Online (Sandbox Code Playgroud)
运行命令后为什么会显示以下警告:
[main] WARN o.o.codegen.DefaultCodegen - The value (generator's option) must be either boolean or string. Default to `false`.
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 openapi 生成器生成 API,当我运行 mvncompile 时,似乎没有找到任何像 jacks httprequest 这样的依赖关系,因此,我添加了详细信息,然后添加了插件:
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<!-- RELEASE_VERSION -->
<version>4.3.1</version>
<!-- /RELEASE_VERSION -->
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/player.yml</inputSpec>
<generatorName>java</generatorName>
<configOptions>
<sourceFolder>src/gen/java/main</sourceFolder>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
使用此文件player.yml:
openapi: 3.0.1
info:
title: Swagger Petstore
description: 'This is a sample server Petstore server. You can find out more about Swagger
at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For
this sample, you can use the api key `special-key` to test the authorization filters.'
termsOfService: http://swagger.io/terms/
contact: …Run Code Online (Sandbox Code Playgroud) 描述 我有一个来自 spring boot 服务器的公开文件,我正在使用此脚本生成打字稿角度客户端
openapi-generator-cli 生成 -g typescript-angular -i ./src/app/data/open-api.json -o ./src/app/data/build/openapi
系统间存在重复的api名称
结构简单
控制器1:[获取,列表,删除,... ....] 控制器2:[获取,列表,删除,... ....] .....
生成的类看起来像
Controller1Service{
public get1 ....
public list1 .....
}
Controller2Service{
public get2 ....
public list2 .....
}
Run Code Online (Sandbox Code Playgroud)
但这些功能在同一控制器中是唯一的,并且生成器仍然向它们添加数字
使用 npm cli 的 openapi-generator 版本 4.3.1
OpenAPI 声明文件内容或 url 用于生成的命令行 openapi-generator-cligenerate -g typescript-angular -i ./src/app/data/open-api.json -o ./src/app/data/build/openapi
那么有什么办法可以让这些数字消失呢?
更新后端时,内部的数字可能会发生变化,这将导致手动代码重构
基本上我的所有 DTO 都是使用可空字段生成的,例如:id?: number;。如何为未标记为可为空的类型创建不可为空的类型。
我的 DTO 架构如下所示:
"UserDTO": {
"type": "object",
"properties": {
"firstName": {
"type": "string",
"nullable": true
},
"lastName": {
"type": "string",
"nullable": true
},
"id": {
"type": "integer",
"format": "int32"
}
}
Run Code Online (Sandbox Code Playgroud)
我生成这样的代码:
openapi-generator generate --additional-properties=prefixParameterInterfaces=true,typescriptThreePlus=true --remove-operation-id-prefix -i libs/services/defs/swagger.json -g typescript-fetch -o libs/services/src/api
在设计 API 时,我定义了一个UserGeo包含两个字段的模型 - domain(字符串数组) 和country(字符串数组)。
[]如果没有为 提供值,则应在请求正文中使用空列表domain。但是在将default属性定义为[]生成Spring代码时,不会分配空 ArrayList<> 又名[]。
附上代码示例:
招摇定义:
definitions:
UserGeo:
type: "object"
properties:
country:
type: "array"
items:
type: "string"
domain:
type: "array"
items:
type: "string"
default: []
Run Code Online (Sandbox Code Playgroud)
这不会将域值默认为空列表,请检查生成的 Spring/Java 代码:
.
.
public class Classification {
@JsonProperty("country")
@Valid
private List<String> country = null;
@JsonProperty("domain")
@Valid
private List<String> domain = null;
.
.
Run Code Online (Sandbox Code Playgroud)
然而,当我将域字段定义为required甚至没有使用默认值定义它时,生成的代码会分配一个空列表作为domain.
definitions:
UserGeo:
type: …Run Code Online (Sandbox Code Playgroud) 我们使用 openapi-generator 的 openapi-generator-maven-plugin 来自动与使用非int64数字数据类型的 swagger 集成。我们的代码库尝试围绕使用 Long 值进行标准化,但 openapi 生成使用 int 的工件。是否可以配置插件来生成使用 Long 而不是 Integer 的 POJO?
我们可以修改 swagger 定义来指定 int64 格式,但更喜欢通过 swagger 外部的配置来完成此操作。
我们使用 AddOpenApiDocument 得到了 Swagger.json 输出(见下文)。
下面的 swagger 片段显示它返回 application/json 的 200 响应类型。模式部分(我不太熟悉)显示“type”:“string”。
当我们从 NSwag.CodeGeneration.CSharp.CSharpClientGenerator 生成并使用客户端时,我们会收到以下错误:
SwaggerException: Could not deserialize the response body stream as System.String.
Status: 200
Response:
---> Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: P.
Path '', line 1, position 1.
Run Code Online (Sandbox Code Playgroud)
招摇的片段
"/api/infrastructure": {
"get": {
"tags": [
"Infrastructure"
],
"operationId": "Infrastructure_Ping",
"responses": {
"200": {
"description": "",
"content": {
"application/json": {
"schema": {
"type": "string"
}
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
生成客户端代码如下所示:
var status_ = (int)response_.StatusCode; …Run Code Online (Sandbox Code Playgroud) openapi ×5
java ×3
swagger ×2
typescript ×2
angular ×1
api ×1
asp.net-core ×1
jersey-2.0 ×1
maven ×1
nswag ×1
spring ×1
spring-boot ×1
yaml ×1