英文:
Create model classes with openapi generator which implements external interface
问题
我正在使用 openapi-generator 生成 Java 类。
我希望这些模型类实现一个外部接口,该接口未由 openapi-generator 生成。
是否可以在模型的 YAML 文件中定义某些内容,或者是否可以通过传递属性给 openapi-generator-maven-plugin 来实现此行为?
所需行为示例:
package com.example.model;
/**
* ExampleModel
*/
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
public class ExampleModel implements com.example.CustomInterface {
@JsonProperty("property1")
private String property1;
@JsonProperty("property2")
private String property2;
英文:
I am using openapi-generator to generate java classes.
I want the model classes to implement an external interface which has not been generated by openapi-generator.
Is there something that can be defined within the model yaml or a property that can be passed to the openapi-generator-maven-plugin which allows for this behaviour?
Example of required behaviour:
package com.example.model;
/**
* ExampleModel
*/
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
public class ExampleModel implements com.example.CustomInterface {
@JsonProperty("property1")
private String property1;
@JsonProperty("property2")
private String property2;
答案1
得分: 6
自从openapi-generator-maven-plugin版本6.0.0起,有一种更好的方式:x-implements
只需修改api.yml中的schema定义,生成的Java类将实现指定的接口
openapi: 3.0.0
components:
schemas:
MyObject:
type: object
description: 将实现接口的对象
x-implements: ['com.example.Interface']
properties:
data:
description: 一些数据
type: object
(此功能在早期版本中已存在,但存在错误,已进行修复:https://github.com/OpenAPITools/openapi-generator/issues/11636)
注意:您必须使用全限定接口名称,例如java.io.Serializable,而不仅仅是Serializable
英文:
Since openapi-generator-maven-plugin version 6.0.0 there is a better way: x-implements
Simply modify the schema-definition in your api.yml and the generated java-classes will implement the specified interfaces
openapi: 3.0.0
components:
schemas:
MyObject:
type: object
description: object that will implement interface
x-implements: ['com.example.Interface']
properties:
data:
description: some data
type: object
(The feature existed in earlier versions but was bugged, this has been fixed: https://github.com/OpenAPITools/openapi-generator/issues/11636)
NOTE: you must use the fully-qualified interface name, eg java.io.Serializable instead of just Serializable
答案2
得分: 3
如果您想以相同的方式修改所有类,我建议您更改模板。在您的情况下,最有可能的文件是:pojo.mustache
只需将其复制到您的 src/main/resources/
文件夹中(可以是名为 custom 的子文件夹),然后根据您的需求进行调整。
然后您还需要调整您的 pom.xml
文件:
<configuration>
<!-- 下面这行非常重要: -->
<templateDirectory>${project.basedir}/src/main/resources/custom</templateDirectory>
<!-- 其他配置在此处: -->
<inputSpec>${project.basedir}/src/main/resources/api.yaml</inputSpec>
<generatorName>java</generatorName>
<configOptions>
<sourceFolder>src/gen/java/main</sourceFolder>
</configOptions>
</configuration>
另外,您还可以查阅这份模板文档,以获取更多相关信息。
英文:
In case you want to modify all of the classes in the same way I would opt for changing the template. In your case that is most probably this file: pojo.mustache
Just copy it over to your src/main/resources/
folder (maybe in a subfolder named custom) and adapt it according to your needs.
Then you need to adapt your pom.xml
, too:
<configuration>
<!-- The following line is crucial: -->
<templateDirectory>${project.basedir}/src/main/resources/custom</templateDirectory>
<!-- Your other config goes here: -->
<inputSpec>${project.basedir}/src/main/resources/api.yaml</inputSpec>
<generatorName>java</generatorName>
<configOptions>
<sourceFolder>src/gen/java/main</sourceFolder>
</configOptions>
</configuration>
Also have a look at this templating documentation for more information on the subject.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论