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

huangapple go评论80阅读模式
英文:

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:

&lt;configuration&gt;

    &lt;!-- The following line is crucial: --&gt;
    &lt;templateDirectory&gt;${project.basedir}/src/main/resources/custom&lt;/templateDirectory&gt;

    &lt;!-- Your other config goes here: --&gt;
    &lt;inputSpec&gt;${project.basedir}/src/main/resources/api.yaml&lt;/inputSpec&gt;
    &lt;generatorName&gt;java&lt;/generatorName&gt;
    &lt;configOptions&gt;
        &lt;sourceFolder&gt;src/gen/java/main&lt;/sourceFolder&gt;
    &lt;/configOptions&gt;
&lt;/configuration&gt;

Also have a look at this templating documentation for more information on the subject.

huangapple
  • 本文由 发表于 2020年9月22日 16:30:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/64005905.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定