OpenApi Generator 在 YAML 文件规范中引用外部 POJO

gha*_*sen 5 swagger-codegen openapi-generator

我正在使用OpenApi v3.3.4(以前称为Swagger CodeGen)maven 插件通过文件生成我的其余控制器api.yml,其中我描述了我想要公开的所有操作。

在我的用例中,我想公开一个方法POST: handleNotification(@RequestBody SignatureNotification notification),其请求正文的类型是通过/targer文件夹中的另一个 maven-plugin 生成的。

实际上我SignatureNotificationComponents.yml 文件的一部分中定义:

...
requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/SignatureNotification'
...
Run Code Online (Sandbox Code Playgroud)

它由 OpenApi 插件生成,然后我将其映射到SignatureNotification已经存在且具有相同属性的插件。

我对这个解决方案不太满意,所以我想知道是否有办法告诉 OpenApi Generator 使用外部对象作为引用?

bil*_*lak 9

如果我正确理解您的需求,您只想告诉生成器不要再次生成现有的类。如果上述正确,那么您可以importMappings像这样配置插件:

<plugin>
  <groupId>org.openapitools</groupId>
  <artifactId>openapi-generator-maven-plugin</artifactId>
  <version>${openapi-generator-maven-plugin.version}</version>
  <configuration>
      ... excluded for simplicity
      <importMappings>
          <importMapping>SignatureNotification=path.to.your.SignatureNotification</importMapping>        
      </importMappings>
  </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

使用此配置,openapi 生成器不会根据SignatureNotification定义生成类,而是使用现有的类。

  • 太感谢了 !这很好用,我只想提一下,在“.yml”文件中,我们需要在组件区域中定义“SignatureNotification”,并将其设置为“type: object” (3认同)