如果已经回答了这个问题,我深表歉意,但我找不到明确的答案。
目前的生成器支持认证吗?
我需要生成一个具有两种潜在身份验证方式的客户端:
api_token参数。如果有,我该如何使用它?
如果没有,您认为在生成客户端后添加身份验证的最佳方法是什么?
当将 OpenAPI 生成器与 Gradle 一起使用时,我希望将性别源发送到其他源生成器插件使用的标准目录。类似 Maven 生成源的东西。
\n\n到目前为止,我还无法做到这一点,特别是限制生成 Java 源类而不是整个 \xe2\x80\x9carchetype 项目\xe2\x80\x9d。
\n\n看来 OpenAPI Gradle 插件工作流程与 Maven 的工作流程并不相同。
\n\n是否有一个配置标志可以省略所有非java代码的生成,并在\xe2\x80\x9c generated resources\xe2\x80\x9d文件夹(例如/out/product/ generated/)中执行此操作?
\n我们通过 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 生成时,它不会执行此操作。
您如何为每个子类设置默认鉴别器?
例如,采用以下模式:
components:
schemas:
Pet:
type: object
required:
- petType
properties:
petType:
type: string
discriminator:
propertyName: petType
Cat:
allOf:
- $ref: '#/components/schemas/Pet'
- type: object
# all other properties specific to a `Cat`
properties:
name:
type: string
Dog:
allOf:
- $ref: '#/components/schemas/Pet'
- type: object
# all other properties specific to a `Dog`
properties:
bark:
type: string
Run Code Online (Sandbox Code Playgroud)
上述模式的代码生成器将创建一个客户端,其中的petType值必须由程序员显式设置。为什么不能默认设置Cat对象?petTypeCat
我尝试使用defaultvalue 让它工作。但是,生成的代码包含隐藏的属性(子级和父级上的相同属性)。
components:
schemas:
Pet:
type: object
required:
- petType
properties:
petType:
type: string …Run Code Online (Sandbox Code Playgroud) polymorphism swagger openapi swagger-codegen openapi-generator
我们使用 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) 我正在团队中开发一个新项目,我们正在遵循 API 优先方法来实现 API。我们使用openapi-generator-maven-pluginOpenAPI 3.0.3 格式的 yml 文件生成 API。为了生成 swagger 文件,我们使用 springfox 2.9.2。我面临的问题是当我尝试为请求增加安全性时。
components:
securitySchemes:
bearerAuth:
type: http
scheme: bearer
bearerFormat: JWT
security:
- bearerAuth: [ ]
Run Code Online (Sandbox Code Playgroud)
该Authorize按钮不会出现在 swagger 页面中,仅出现请求附近的锁,但它不会执行任何操作(见下图)。
我观察到,如果我打开/v2/api-docsswagger json 不包含安全定义部分。
我设法添加安全性的唯一方法是通过 Docket 对象中的代码添加安全部分,如下所示:
new Docket(DocumentationType.SWAGGER_2)
.securityContexts(Collections.singletonList(securityContext()))
.securitySchemes(Collections.singletonList(bearerJwtKey()))
.select()
.apis(RequestHandlerSelectors.basePackage("com.example"))
.paths(PathSelectors.any())
.build();
Run Code Online (Sandbox Code Playgroud)
这是为 Swagger UI 添加安全性的唯一方法还是我遗漏了什么?
我们使用 OpenAPI 3.0.0 架构构建 API,并生成用于应用程序前端部分的打字稿代码。我们定义了一个端点,它应该能够使用多个文件并将它们上传到后端。这是端点的 requestbody 的样子:
requestBody:
content:
multipart/form-data:
schema:
properties:
images:
type: array
items:
type: string
format: binary
required:
- images
Run Code Online (Sandbox Code Playgroud)
在有关文件上传的 OpenAPI 网页以及 Stackoverflow 上的多个帖子中也找到了类似的定义。
生成 API 时,images预计类型为Array<Blob>。当文件输入元素的输入在前端发生更改时,将调用端点。更具体地说,端点的调用方式如下:
const onInputChange = useCallback(() => {
const files = inputRef.current.files; // React.useRef that's passed in as ref for the html input-element
uploadImages(Array.from(files)); // uploadImages is the function that calls the endpoint
}, [inputRef, api]);
Run Code Online (Sandbox Code Playgroud)
在这种情况下,API 验证始终返回状态代码为 400 - 错误请求的 HTTP 响应。查看 OpenAPI 生成器生成的实现,-propertyimages …
multifile-uploader swagger typescript openapi openapi-generator
我正在尝试生成具有继承的 Java 模型(Open API 2.0)。问题与此类似:如何让maven swagger codegen插件使用继承生成Java类?
我想实现没有鉴别器属性的继承。为什么这个字段必须在父类中指向?此外根据文档:
鉴别器固定字段必须引用同一架构中的属性,并且它必须是必需的属性
我不想指出任何必填字段。我只是想获得遗产。还有其他方法可以做到这一点吗?我知道一些“解决方法”是改变属性级别:
"Child": {
"allOf": [
{"$ref": "#/components/schemas/Parent"},
],
"properties": {.......}
}
Run Code Online (Sandbox Code Playgroud)
但这相当不符合规范。最令人惊讶的部分是它适用于 openapi-generator-maven-plugin,但此时如果我们放入“必需”列表,则不会生成正确的代码。必填字段列表将被省略,但继承将正确生成。
有没有其他方法可以在没有歧视器的情况下生成正确的继承?
openapi ×6
swagger ×4
java ×3
spring-boot ×2
asp.net-core ×1
jersey-2.0 ×1
nswag ×1
polymorphism ×1
typescript ×1
yaml ×1