我最近升级了我的项目以使用 spring-boot 3.0.0。所以我的javax.*项目中不再有模块了。但开放 API 生成器不断尝试导入javax模块。特别是,它用于项目中不再存在的注释javax.annotation.Generated。@Generated有没有办法以某种方式重新配置它?
请在我的合同 yaml 文件中找到架构:
Foo:
allOf:
- $ref: "#/components/schemas/Bar"
properties:
ancestors:
items:
$ref: "#/components/schemas/Bar"
type: array
description:
type: object
additionalProperties:
type: string
id:
description: id
type: string
type: object
Bar:
properties:
accessAllowed:
items:
type: string
type: array
catalog:
type: boolean
children:
items:
$ref: "#/components/schemas/Bar"
type: array
Run Code Online (Sandbox Code Playgroud)
使用 swagger 2 时,生成的类 Foo 扩展了 Bar。但是使用 openAPI 3 后,在使用allOf. 它所做的就是将 Bar 的所有属性复制到 Foo 类。
尽管现在 Foo 类将包含 Bar 的所有属性,但当我们查看 java 代码方面时,Foo 实际上并没有继承。使用 OpenAPI 3 时,有没有办法生成扩展 Bar 的类 Foo,因为在很多情况下需要生成继承父类的类。
我是 OpenApi 新手,想使用 api.yaml (OpenApi 版本 3.0.1)定义我的 api。我的问题是生成的枚举仅包含名称而不包含值。
这是我的代码中的枚举:
TEST1(1, "Test 1", "T1"),
TEST2(2, "Test 2", "T2"),
TEST3(3, "Test 3", "T2");
Run Code Online (Sandbox Code Playgroud)
这是使用 OpenApi 生成后的枚举:
TEST1("TEST1"),
TEST2("TEST2"),
TEST3("TEST3");
Run Code Online (Sandbox Code Playgroud)
枚举自动定义如下:
testenum:
type: string
description: desciption of the enum
enum:
- TEST1
- TEST2
- TEST3
Run Code Online (Sandbox Code Playgroud)
如何在 api.yaml 中定义枚举以使其看起来像第一个示例?
java enums openapi openapi-generator openapi-generator-maven-plugin
我想根据 Openapi 规范 3.0 yml 定义生成模型。在我的规范中,我有一个定义,用于allOf包含其他组件的字段。使用 生成模型时openapi-generator-maven-plugin。我看到以下警告allOf with multiple schemas defined. Using only the first one。
结果是仅allOf包含第一个定义的属性。虽然我希望所有字段都包含在内。https://editor.swagger.io/生成正确的架构。
为什么不是所有属性都定义在allOfinclude 中?
架构定义:
Dto:
title: Dto
type: object
properties:
created_by:
type: string
format: date-time
Foo:
title: Foo
type: object
allOf:
- $ref: '#/Dto'
properties:
fooProperty:
type: string
Bar:
title: Bar
type: object
allOf:
- $ref: '#/Foo'
properties:
barProperty:
type: string
Run Code Online (Sandbox Code Playgroud)
maven插件配置:
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>6.1.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration> …Run Code Online (Sandbox Code Playgroud) java maven-plugin openapi openapi-generator openapi-generator-maven-plugin
我已将 Web 服务定义编写为 OpenAPI 文档。我使用的 openapi-generator-maven-plugin 总是生成带有 poms 和 gradle 构建脚本的整个项目,但我只需要 pojos 以及可能生成的 API 客户端。它应该与 JaxB 或 JaxWS 代码生成器同等工作。
那么有没有办法告诉插件只生成Java代码呢?也许还有另一个插件可以完成这项工作?
这是我的配置:
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>5.1.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/my-api.yaml</inputSpec>
<modelPackage>com.my.path.to.api</modelPackage>
<generatorName>java</generatorName>
<generateApis>false</generateApis>
<generateModels>true</generateModels>
<generateModelDocumentation>false</generateModelDocumentation>
<generateModelTests>false</generateModelTests>
<library>vertx</library>
<configOptions>
<sourceFolder>src/main/java</sourceFolder>
<dateLibrary>java8</dateLibrary>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud) java maven-plugin maven openapi openapi-generator-maven-plugin
我在版本 5.3.1 中使用插件 openapi-generator-maven-plugin。
将 openapi 规范从 3.0.3 升级到 3.1.0 会导致生成阶段错误:
规格样本:
{
"openapi": "3.1.0",
"info": {
"title": "Spec sample",
"version": "1.0.0"
},
"paths": {
"/users": {
"get": {
"responses": {
"200": {}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
插件错误:
[INFO] Output directory ((removed)\target\generated-sources\openapi) does not exist, or is inaccessible. No file (.openapi-generator-igno
re) will be evaluated.
[WARNING] (removed).json [0:0]: unexpected error in Open-API generation
java.lang.RuntimeException: Issues with the OpenAPI input. Possible causes: invalid/missing spec, malformed JSON/YAML files, etc.
at org.openapitools.codegen.DefaultGenerator.generate …Run Code Online (Sandbox Code Playgroud) 我用来openapi-generator-maven-plugin从 openapi yml 文件生成类,但在其中一些文件中我收到以下错误:
Cannot find symbol symbol: method requiredMode() location: @interface io.swagger.v3.oas.annotations.media.Schema
我检查了一下io.swagger.v3.oas.annotations.media.Schema,发现 requiredMode() 方法确实丢失了。
在我的 pom 文件下面:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.maglione</groupId>
<artifactId>contactsapi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>contactsapi</name>
<description>Demo project </description>
<properties>
<java.version>17</java.version>
<openapi-generator.version>6.6.0</openapi-generator.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId> …Run Code Online (Sandbox Code Playgroud) 我正在为 spring-boot 应用程序编写一个规范,以使用 openapi-generator-maven-plugin 为客户端生成 API。我想导入一些模型,因此我尝试像往常一样使用 schemaMappings 属性导入它们,就像我使用 openapi-generator-gradle-plugin 一样。在 yaml 规范中,我创建了要导入的模型的空架构,并在 pom.xml 的插件设置中指定了所需的类型。我的插件设置:
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>6.4.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/openapi/some-api.yaml</inputSpec>
<generatorName>spring</generatorName>
<output>${project.build.directory}/generated-sources/openapi</output>
<generateApiTests>false</generateApiTests>
<generateModelTests>false</generateModelTests>
<generateModelTests>false</generateModelTests>
<generateModelDocumentation>false</generateModelDocumentation>
<importMappings>
<importMapping>MyDto1=com.some.project.metric.MyDto1</importMapping>
<importMapping>MyDto2=com.some.project.metric.MyDto2</importMapping>
</importMappings>
<configOptions>
<title>Some project</title>
<library>spring-boot</library>
<useTags>true</useTags>
<dateLibrary>java8</dateLibrary>
<basePackage>${default.package}</basePackage>
<apiPackage>${default.package}.api</apiPackage>
<modelPackage>${default.package}.model</modelPackage>
<performBeanValidation>true</performBeanValidation>
<interfaceOnly>true</interfaceOnly>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
但突然我发现,由于某种原因,openapi-generator-maven-plugin 与 openapi-generator-gradle-plugin 不同,不使用 importMappings 属性。它存在于插件的代码中,您可以在生成器的配置或 configOptions 中使用它,但最终它不适用于我自己的 DTO。又检查了一遍。在 Gradle 中一切正常,但在 Maven 中则不然。它没有生成 POJO,因为它们是自由格式的对象,只是用对象类型替换它们。我开始深入研究这个问题,一段时间后我发现这个问题已经存在很长时间了。至少从 5.3.1 版本开始。你可以在这里看到
我在 spring 项目中使用 OpenAPI java 生成器 [1] 和 library:resttemplate, dateLibrary:java8 从规范生成客户端。
对于规范中的属性:
targetDate:
type: string
format: date
Run Code Online (Sandbox Code Playgroud)
生成以下代码:
public static final String JSON_PROPERTY_TARGET_DATE = "targetDate";
private LocalDate targetDate;
@javax.annotation.Nonnull
@JsonProperty(JSON_PROPERTY_TARGET_DATE)
@JsonInclude(value = JsonInclude.Include.ALWAYS)
public LocalDate getTargetDate() {
return targetDate;
}
@JsonProperty(JSON_PROPERTY_TARGET_DATE)
@JsonInclude(value = JsonInclude.Include.ALWAYS)
public void setTargetDate(LocalDate targetDate) {
this.targetDate = targetDate;
}
Run Code Online (Sandbox Code Playgroud)
我希望该字段能够序列化为完整日期,例如规范所承诺的“2023-01-01”:https: //spec.openapis.org/oas/v3.0.0#data-types。然而它实际上被序列化为一个数组:[2023,1,1]。
同样的另一个属性
otherDate:
type: string
format: date-time
Run Code Online (Sandbox Code Playgroud)
被序列化为自纪元以来的秒数,而不是全时。(我认为这是生成器中的错误)
由于生成了代码,我无法添加任何注释。我怎样才能确保日期正确序列化?
[1] openapi-generator-maven-plugin 6.3.0
java openapi jackson-databind openapi-generator openapi-generator-maven-plugin
我希望 openapi-generator-maven-plugin 不生成${openapi-generator.base-package}/OpenApiGeneratorApplicationTests.java.
我正在尝试正确使用 .openapi-generator-ignore 功能。
openapi-generator-maven-plugin ×10
java ×9
openapi ×6
maven ×4
maven-plugin ×2
spring-boot ×2
enums ×1
import ×1
inheritance ×1