繁体   English   中英

使用实现外部接口的 openapi 生成器创建模型类

[英]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;

如果您想以相同的方式修改所有类,我会选择更改模板。 在你的情况下,这很可能是这个文件: pojo.mustache

只需将其复制到您的src/main/resources/文件夹(可能在名为 custom 的子文件夹中)并根据您的需要进行调整。

然后你也需要调整你的pom.xml

<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>

另请查看此模板文档以获取有关该主题的更多信息。

由于 openapi-generator-maven-plugin 版本 6.0.0 有一个更好的方法: x-implements

只需修改 api.yml 中的模式定义,生成的 java-classes 将实现

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

(该功能在早期版本中存在,但被窃听,已修复: https ://github.com/OpenAPITools/openapi-generator/issues/11636)

注意:您必须使用完全限定的接口名称,例如 java.io.Serializable 而不仅仅是 Serializable

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM