我正在尝试使用 OpenAPI Generator 4.0.0-SNAPSHOT(我们的经理出于某种原因要求我们使用该版本)生成 Typescript 客户端,该客户端读取 OpenAPI 3.0 规范。我们当前的所有端点要么接受查询字符串中的数据,要么接受表单正文中的数据,并且它们都工作得很好。我有一个新端点,它将在 POST 正文中读取 JSON 数据(其他端点最终也会被转换)。它将接受如下所示的对象:
{email: "something@domain.com"}
我正在尝试在 YAML 中记录端点,如下所示:
/users/send-password-reminder:
post:
[...]
requestBody:
content:
application/json:
schema:
type: object
properties:
email:
type: string
responses:
[...]
Run Code Online (Sandbox Code Playgroud)
但是,当我生成 Typescript 客户端时,它会生成一个SendPasswordReminderRequest对象,该对象包装一个自动生成的InlineObject1对象,该对象包装我实际的email.
这导致我像这样使用它:
const req: SendPasswordReminderRequest = {
inlineObject1:{
email: "..."
}
};
APIClient.user.sendPasswordReminder(req, ...)
Run Code Online (Sandbox Code Playgroud)
InlineObject1我想要的是完全摆脱它并SendPasswordReminderRequest直接包装email属性,并能够将其用作:
const req: SendPasswordReminderRequest = {
email: "..."
};
APIClient.user.sendPasswordReminder(req, ...)
Run Code Online (Sandbox Code Playgroud)
components/requestBodies我尝试在和 using中定义主体$ref,它仍然包装实际主体,即使它使用我的请求主体类型的名称。
我怎样才能摆脱这个包装?
我正在尝试使用anyOf和allOf属性创建OpenAPI自动生成的PHP客户端。
目标是能够返回其中具有多态性的数组:不同类型的对象。
这些对象也具有一个公共的基础对象。
在我的示例模式中,Items是一个数组,其中各项可以是类型ItemOne或ItemTwo。
这两种类型的项目有一个自己的财产(itemOneProperty和itemTwoProperty,分别地),和一个共同的属性baseItemProperty(从继承BaseItem与allOf关键字)。
这里有API规范yaml:
openapi: 3.0.0
info:
title: Test API
version: 1.0.0
servers:
- url: https://api.myjson.com/bins
paths:
/roxgd:
get:
operationId: getItems
responses:
'200':
description: Success
content:
application/json:
schema:
$ref: '#/components/schemas/Items'
components:
schemas:
Items:
type: array
items:
anyOf:
- $ref: '#/components/schemas/ItemOne'
- $ref: '#/components/schemas/ItemTwo'
BaseItem:
type: object
properties:
baseItemProperty:
type: string
ItemOne:
allOf:
- $ref: '#/components/schemas/BaseItem'
- type: object …Run Code Online (Sandbox Code Playgroud) 我正在尝试为 OpenApi 3.0.2 规范生成生成的服务器端 Spring MVC 代码。
这就是“路径”之一的样子:-
paths:
/api/v1/int/integrations/{some-path-variable}/some-action:
get:
summary: Summary
description: How to change the generated Api/Controller class name?
operationId: methodName
tags:
- inventory
parameters:
- name: Authorization
other details....
Run Code Online (Sandbox Code Playgroud)
服务器端代码使用 Maven 插件生成,配置为:-
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>4.1.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/open-api/myapi.yaml</inputSpec>
<generatorName>spring</generatorName>
<library>spring-boot</library>
<output>${project.build.directory}/generated-openapi/spring</output>
<generateApis>true</generateApis>
<addCompileSourceRoot>true</addCompileSourceRoot>
<artifactVersion>${project.version}</artifactVersion>
<groupId>com.company.division</groupId>
<artifactId>myapi-api</artifactId>
<generateApiTests>true</generateApiTests>
<modelPackage>com.company.division.myapi.generated.model</modelPackage>
<apiPackage>com.company.division.myapi.generated.api</apiPackage>
<generateApiDocumentation>true</generateApiDocumentation>
<configOptions>
<dateLibrary>java8</dateLibrary>
<java8>true</java8>
<interfaceOnly>true</interfaceOnly>
<reactive>false</reactive>
<useBeanValidation>true</useBeanValidation>
<performBeanValidation>true</performBeanValidation>
<useOptional>false</useOptional>
<serviceInterface>true</serviceInterface>
<serviceImplementation>false</serviceImplementation>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
从插件配置中可以看出,我只对生成模型类和 Spring 控制器接口/API 接口感兴趣。
问题
对于提到的 …
当尝试从 openapi 为 ktor 生成服务器端存根时,对我来说输出看起来并不是很实用。
我在 Github 上设置了一个示例项目,可以在其中查看设置。因为我需要在我的实际项目中使用 maven,所以我在这里也使用了它,但我想这对其他生成方法应该没有影响。
POM 的相关部分是这样的:
<build>
...
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>resources</id>
<phase>compile</phase>
<goals><goal>copy-resources</goal></goals>
<configuration>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)
将生成的内容如下:
Paths.kt包含 ktor-locations 格式的路径路径的对象io.ktor.routing.Route来自 SpringBoot 和 Swagger,我有点困惑,因为如果您想创建一个实现真正业务逻辑的实际应用程序,这组文件并不是很有用。我本来希望一些默认实现包含我只需要实现/覆盖的单个方法。我意识到 ktor 没有内置 DI,所以我也希望必须以routing某种方式扩展我的应用程序中的存根。
但是,有了这组生成的代码,我真的只能使用数据模型和Paths.kt对象。我将拥有自己的应用程序和默认实现,我无法覆盖但需要自己完全重写。
所以我的问题是,如何设置一个合适的 ktor 应用程序实现 openapi 规范。我是否错过了什么,或者我真的只得到模型并Paths.kt与之合作?
编辑
为了更清楚:特别是对于我的应用程序的这些部分,我不高兴没有工具的支持:
fun Application.module(testing: Boolean = false) { …Run Code Online (Sandbox Code Playgroud) 我们正在使用具有许多可选功能的大型第三方 API。有 27 个端点,我们只需要 Maven 中的几个。我们正在使用服务器端生成。
如果您感兴趣,API 位于: https: //github.com/mjeffrey/psd2 理想情况下,我们只会生成并公开我们支持的 API。
是否可以只生成 API 端点列表或排除我们不想支持的端点?
我发现有可能只生成某些模型,但这不是我们需要的。-D apis 参数似乎在源代码中被视为布尔值。
https://github.com/OpenAPITools/openapi-generator#3---用法 https://github.com/OpenAPITools/openapi-generator/blob/master/docs/customization.md#selective- Generation
我还在考虑使用预处理器,这样我们就不需要手动编辑 yaml 文件(定期更新)。对于预处理 yaml 文件有什么建议吗?
我们使用 openapi 生成器来生成 REST 客户端。工作得非常好,直到我们开始在UPERCASE_UNDERSCORE格式中使用枚举。它去掉了下划线。这样做的主要问题是,由于属性名称与定义不匹配,因此在 Typescript 中转换此枚举变得非常痛苦。
这是一个 YAML 示例:
properties:
boostId:
type: string
maxLength: 50
type:
type: string
enum:
- BOOST_UNIQUE_ALERT_TYPE_COUNTER
- BOOST_UNIQUE_SOURCE_SYSTEM_COUNTER
Run Code Online (Sandbox Code Playgroud)
生产:
var BoostConfiguration;
(function (BoostConfiguration) {
/**
* @export
* @enum {string}
*/
var TypeEnum;
(function (TypeEnum) {
TypeEnum["UNIQUEALERTTYPECOUNTER"] = "BOOST_UNIQUE_ALERT_TYPE_COUNTER";
TypeEnum["UNIQUESOURCESYSTEMCOUNTER"] = "BOOST_UNIQUE_SOURCE_SYSTEM_COUNTER";
})(TypeEnum = BoostConfiguration.TypeEnum || (BoostConfiguration.TypeEnum = {}));
})(BoostConfiguration = exports.BoostConfiguration || (exports.BoostConfiguration = {}));
Run Code Online (Sandbox Code Playgroud)
由于某种原因,它删除了“BOOST”,并删除了下划线。我已经尝试过enumPropertyNaming=original和modelPropertyNaming=original,似乎都没有改变任何东西(在枚举中)。这是我们的生成器脚本:
openapi-generator generate -i api/socosApi.yaml -g typescript-axios -o generated-sources/api --additional-properties=enumPropertyNaming=original,modelPropertyNaming=original
Run Code Online (Sandbox Code Playgroud) 我有一个 REST 服务,它接受一个 id 和两个字符串作为 json,并返回 id 和连接为 json 的两个字符串。如果出现错误,它可以返回状态代码 400、404 和 500,并在正文中包含 json 错误响应。
swagger.json 文件如下所示:
{
"swagger": "2.0",
"info": {
"description": "---",
"version": "---",
"title": "---"
},
"basePath": "/rest",
"tags": [
{
"name": "Concatenation resource"
}
],
"schemes": [
"http",
"https"
],
"paths": {
"/concatenation/{id}/concat": {
"post": {
"tags": [
"Concatenation resource"
],
"summary": "Concatenates two strings",
"description": "",
"operationId": "concatenate",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"parameters": [
{
"name": "id",
"in": "path",
"required": true, …Run Code Online (Sandbox Code Playgroud) java openapi spring-webflux openapi-generator spring-webclient
我有一个 Spring Boot 应用程序,其中包含通过 gradle 插件生成的 java 客户端:
openApiGenerate {
generatorName = "java"
inputSpec = specsYml
outputDir = "$buildDir/generated".toString()
apiPackage = "com.customapi.api"
invokerPackage = "com.customapi.invoker"
modelPackage = "com.customapi.model"
configOptions = [
dateLibrary: "java8",
library : "resttemplate"
]
}
Run Code Online (Sandbox Code Playgroud)
我选择了"java8"as,dateLibrary因为这似乎是 java 1.8 项目的首选。
使用生成的客户端,我正在执行一个请求,该请求返回一个包含时间戳的对象。我收到以下错误:
java.lang.IllegalStateException: Failed to execute CommandLineRunner
...
Caused by: org.springframework.web.client.RestClientException: Error while extracting response for type [class com.customapi.model.Info] and content type [application/json];
...
Caused by: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type …Run Code Online (Sandbox Code Playgroud) 我对各种 python HTTP 库了解如下:
我知道的另一个主要 HTTP 请求库是urllib3。这是OpenAPI Generator在生成 Python 客户端库时默认使用的内容。
我的问题是:
urrlib3 可以配置为发出 HTTP/2 请求吗?
我在文档中找不到任何有关http2支持的信息,并且通过我对生成的OpenAPI客户端的测试,所有请求都是HTTP/1.1。如果目前答案是否定的,维护者是否正在计划 HTTP/2 支持?我在项目的未解决问题中找不到任何证据。
我正在使用 openapi 生成器(5.4.0)和 gradle(7.3.3)和 SpringBoot(2.6.6)。
我的 OpenApi 配置:
ext.openApiConfigOptions = [
library : "spring-boot",
dateLibrary : "java8",
delegatePattern : "true",
useTags : "true",
openApiNullable : "false",
additionalModelTypeAnnotations: "@lombok.Builder @lombok.NoArgsConstructor @lombok.AllArgsConstructor",
booleanGetterPrefix : "is"
]
Run Code Online (Sandbox Code Playgroud)
我的目标是在该字段上添加 @lombok.Builder.Default 注释。
根据此:链接到示例, 我尝试在我的规范中使用 x-field-extra-annotation: ,但它在我的情况下不起作用。
就我而言,它是这样工作的;
类似的未回答问题:链接
openapi ×4
java ×3
swagger ×2
typescript ×2
http2 ×1
jackson ×1
kotlin ×1
ktor ×1
maven ×1
php ×1
polymorphism ×1
python ×1
spring ×1
spring-boot ×1
spring-mvc ×1
urllib3 ×1