我的 openapi 合约中有以下模式定义来表示commission amount。
commissionAmount:
type: number
minimum: -99999.99
maximum: 99999.99
Run Code Online (Sandbox Code Playgroud)
生成的代码:
@Valid
@DecimalMin("-99999.99") @DecimalMax("99999.99")
public BigDecimal getCommissionAmount() {
return commAmt;
}
Run Code Online (Sandbox Code Playgroud)
生成的代码很好并且符合预期。我只是想知道这些-99999.99以及和99999.99的有效值。minimummaximum
提出这个问题的原因是它没有检查小数部分的限制。例如,我期望12345.678is invalid ,12345.67is valid 。但它将两者标记为有效。
I read@Digits用于检查整数和小数部分的位数限制。我如何告诉也openapi-generator-maven-plugin进行注释?Digits
预期生成的代码:
@Valid
@Digits(integer = 5, fraction = 2)
@DecimalMin("-99999.99") @DecimalMax("99999.99")
public BigDecimal getCommissionAmount() {
return commAmt;
}
Run Code Online (Sandbox Code Playgroud) 我已成功从 .yaml open-api 描述符文件生成接口,但是,如问题标题所示,我希望将这些接口的响应类型从 ResponseEntity 更改为我自己的类型。基本上而不是具有此签名的接口:
ResponseEntity<Void> clearCache();
Run Code Online (Sandbox Code Playgroud)
对于基本上以这种方式实现的方法:
public void clearCache(){ //do something}
Run Code Online (Sandbox Code Playgroud)
我希望生成的界面就像
void clearCache();
Run Code Online (Sandbox Code Playgroud)
对于我自己定义的类来说也是如此,而不是ResponseEntity<MyBook> getBook(String ISBN);我希望它只用作MyBook返回类型,所以它应该看起来像MyBook getBook(String ISBN);
我用于 openapi-generator 插件的当前设置是
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>4.3.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>my-own-service-be/src/main/resources/api-docs.yaml</inputSpec>
<generatorName>spring</generatorName>
<additionalProperties>
<additionalProperty>skipDefaultInterface=true</additionalProperty>
<additionalProperty>interfaceOnly=true</additionalProperty>
</additionalProperties>
<generateApis>true</generateApis>
<apiPackage>controller</apiPackage>
<supportingFilesToGenerate>false</supportingFilesToGenerate>
<modelPackage>dto</modelPackage>
<generateModelTests>false</generateModelTests>
<generateApiTests>false</generateApiTests>
</configuration>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud) 我有一个使用supabase/postgres(t) 的rest api,它生成它自己的OpenAPI/Swagger 定义。问题是这仅包含 id 字段,但不包含对其他表的实际引用。
因此,当使用 openapi-generator 生成客户端时,类仅包含外键,而不包含实际的对象引用。
有没有办法生成带有引用的 openapi 定义,以便我可以带有引用生成我的类?
{
"swagger": "2.0",
"info": {
"description": "standard public schema",
"title": "PostgREST API",
"version": "9.0.0.20220211 (pre-release) (dcf7ade)"
},
"host": "localhost:3000",
"basePath": "/",
"schemes": [
"http"
],
"consumes": [
"application/json",
"application/vnd.pgrst.object+json",
"text/csv"
],
"produces": [
"application/json",
"application/vnd.pgrst.object+json",
"text/csv"
],
"paths": {
"/": {
"get": {
"tags": [
"Introspection"
],
"summary": "OpenAPI description (this document)",
"produces": [
"application/openapi+json",
"application/json"
],
"responses": {
"200": {
"description": "OK"
}
}
}
},
"/hallo": …Run Code Online (Sandbox Code Playgroud) 我想构建一个 Maven 模块,使用 来openapi-generator-maven-plugin从两个 openapi 3 规范生成服务器代码和客户端代码。我希望服务器代码使用 Spring boot,因此我有以下设置:
<generatorName>spring</generatorName>
<library>spring-boot</library>
Run Code Online (Sandbox Code Playgroud)
这工作正常,我需要io.swagger.core.v3:swagger-annotationsOAS jakarta.validation:jakarta.validation-api3 注释和验证。
但是,对于客户端代码,我想使用WebClientSpring,我能找到的唯一设置是:
<generatorName>java</generatorName>
<library>webclient</library>
Run Code Online (Sandbox Code Playgroud)
客户端代码生成,但问题是,它需要较旧的io.swagger:swagger-annotations和javax.validation:validation-api.
我想避免拥有不同的库集。是否有使用同一组注释和验证库的服务器和客户端代码生成设置?最好同时使用io.swagger.core.v3:swagger-annotations和jakarta.validation:jakarta.validation-api。
我有两个 yaml 文件,customer.yaml 和 employee.yaml。如何在单个项目中从这两个 yaml 文件生成 java 代码。我正在使用 gradle,我知道单个 yaml 的任务规范,但是如何指定多个 yaml。我应该在单个 openApiGenerator 下指定多个 inputSpec 吗?如果是,那么这样做的确切语法是什么。下面是我在 build.gradle 文件中的 openApiGenerator 任务。
``
openApiGenerate {
generatorName = "spring"
apiPackage = "com.xxx.generated.controller"
modelPackage = "com.xxx.generated.model"
inputSpec = "$rootDir//schema/employee.yaml".toString()
outputDir = "$rootDir/generated".toString()
configOptions = [
dateLibrary: "java8"
]
systemProperties = [
invoker : "false",
generateSupportingFiles: "true"
]
additionalProperties = [
interfaceOnly : "true",
]
}
``
Run Code Online (Sandbox Code Playgroud)
我听说过 openApiGenerators 任务,它列出了通过 Open API Generators 可用的生成器,但找不到使用它的方法。
我有一个 Spring Boot 应用程序,我使用 openapi-generator-maven-plugin 来生成其余客户端。我想要一个在运行时更改 url 的选项。
其余服务器的 url 现在硬编码在以下 OpenAPI 定义片段中:
openapi: 3.0.1
info:
title: OpenAPI definition
version: v0
servers:
- url: 'http://localhost:8080'
description: Generated server url
Run Code Online (Sandbox Code Playgroud)
maven插件的配置:
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>4.3.1</version>
<execution>
<id>vydejClient</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>
${project.basedir}/src/main/resources/manualni_kodovani_vydej.yaml
</inputSpec>
<generatorName>java</generatorName>
<generateApiTests>false</generateApiTests>
<generateModelTests>false</generateModelTests>
<configOptions>
<dateLibrary>java8</dateLibrary>
</configOptions>
<library>resttemplate</library>
<typeMappings>
<typeMapping>File=org.springframework.core.io.Resource</typeMapping>
</typeMappings>
<apiPackage>client</apiPackage>
<modelPackage>client.model</modelPackage>
</configuration>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
这段代码是生成的
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", date = "2020-11-23T14:40:42.232315+01:00[Europe/Prague]")
@Component("ApiClient")
public class ApiClient {
...
private String basePath = "http://localhost:8080";
...
/**
* …Run Code Online (Sandbox Code Playgroud) 我有一个 ASP.NET Core 5 Web API,它使用 Swagger 生成 OpenAPI3 json 文件。在我的 Blazor WASM 客户端上使用 Visual Studo 2019 (16.8.2),我已向该 json 文件添加了服务引用。
所有这些都工作正常,除了由于某种原因由 Visual Studio 生成的 POCO(? NSWag 可能基于参考文献)都使用DateTimeOffset.
现在我想我需要useDateTimeOffset 基于此设置一个设置,但不知道在哪里或如何这样做。
编辑:看起来DateTimeOffset是通用日期的推荐数据类型。但我还是想知道如何更改选项!
如果是 OpenAPI3 json 的问题,targetDate应该只是日期,并且dateRaised应该是日期和时间:
"InitialRequestDTO": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int32"
},
"requesterName": {
"type": "string",
"nullable": true
},
"sponsorName": {
"type": "string",
"nullable": true
},
"requestTitle": {
"type": "string",
"nullable": true
},
"ownReference": …Run Code Online (Sandbox Code Playgroud) visual-studio swagger asp.net-core openapi-generator asp.net-core-5.0
我使用@openapitools/openapi-generator-cli (v2.1.7) 在客户端生成 API 库。
它工作得很好,除了我无法按照我的意愿格式化生成的代码。
我刚刚注意到有一个新选项可以配置示例中提到的空格("spaces": 2):
{
"$schema": "node_modules/@openapitools/openapi-generator-cli/config.schema.json",
"spaces": 2,
"generator-cli": {
"version": "4.3.1",
"storageDir": "~/my/custom/storage/dir", // optional
"generators": { // optional
"v2.0": { // any name you like (just printed to the console log)
"generatorName": "typescript-angular",
"output": "#{cwd}/output/v2.0/#{ext}/#{name}",
"glob": "examples/v2.0/{json,yaml}/*.{json,yaml}",
"additionalProperties": {
"ngVersion": "6.1.7",
"npmName": "restClient",
"supportsES6": "true",
"npmVersion": "6.9.0",
"withInterfaces": true
}
},
"v3.0": { // any name you like (just printed to the console log)
"generatorName": "typescript-fetch",
"output": "#{cwd}/output/v3.0/#{ext}/#{name}",
"glob": "examples/v3.0/petstore.{json,yaml}" …Run Code Online (Sandbox Code Playgroud) 我有一个使用 Java 11 的 Spring Boot (2.4.0) REST 服务。端点和对象是使用 openapi-generator-maven-plugin v5.0.1 从 OpenAPI 3.0.3 文件生成的
在 API 中,有一个 GET 请求需要两个查询参数。其中至少必须有一名在场。此示例提到使用 minProperties。尽管我已在 OpenAPI 文件中指定了minProperties,但自动生成的对象不会验证这一点。有办法让它发挥作用吗?
OpenAPI 3.3 代码片段:
parameters:
- in: query
name: id
required: true
style: form
explode: true
schema:
title: EntityId
type: object
properties:
businessId:
type: string
nonBusinessId:
type: string
minProperties: 1
additionalProperties: false
Run Code Online (Sandbox Code Playgroud)
openapi-generator-maven-plugin 配置文件:
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>5.0.1</version>
<executions>
<execution>
<id>generate-api</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.build.directory}/swagger/apis/api.yaml</inputSpec>
<generatorName>spring</generatorName>
<apiPackage>com.acme.api</apiPackage>
<modelPackage>com.acme.api.dto</modelPackage>
<configOptions>
<dateLibrary>java8</dateLibrary>
<interfaceOnly>true</interfaceOnly>
<java8>true</java8>
<useBeanValidation>true</useBeanValidation> …Run Code Online (Sandbox Code Playgroud) 假设我的 openapi.yml 中有这个定义(这只是一个虚构的示例来描述我遇到的问题):
components:
schemas:
Address:
type: object
properties:
name:
type: string
zip:
type: integer
format: int64
town:
type: string
Run Code Online (Sandbox Code Playgroud)
这将导致生成如下所示的模型代码:
public class Address {
@JsonProperty("name")
private String name = null;
@JsonProperty("zip")
private Long zip = null;
@JsonProperty("town")
private String town = null;
...
Run Code Online (Sandbox Code Playgroud)
我的问题是,我必须将这个 Pojo 保留在数据库中,并且不存在表Address(让我们假设它被调用Places),并且邮政编码的列被调用zipcode而不是zip.
所以我需要两件事:
Address=Places和zip=zipcode.@Table(name="Places")和。@Column(name="zipcode")重要提示:我无法更改 API,预计会坚持使用Address和zip。
这可以做到吗?我检查了规范以及 swagger-codegen …
swagger openapi swagger-codegen openapi-generator openapi-generator-cli
openapi ×7
java ×4
swagger ×4
spring ×2
spring-boot ×2
angular ×1
asp.net-core ×1
codegen ×1
gradle ×1
npm ×1
postgresql ×1
postgrest ×1
supabase ×1