首页 > 解决方案 > OpenApi Spring 生成器 - 缺少模型文件

问题描述

我可以使用 gradle、OpenApi 生成器插件 ( "org.openapi.generator" version "4.2.1") 成功生成 Spring API 和模型文件。

我有一个api.yml文件:

openapi: 3.0.2
info:
  title: API Documentation
  version: 1.0.0
servers:
  - url: http://localhost:8080/
tags:
  - name: Users
paths:
  /users:
    post:
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Parent'
      responses:
        200:
          description: New user created

components:
  schemas:

    Parent:
      required:
        - status
      properties:
        status:
          type: string
      discriminator:
        propertyName: status
        mapping:
          CHILD_ONE: '#/components/schemas/ChildOne'
          CHILD_TWO: '#/components/schemas/ChildTwo'

    ChildOne:
      allOf:
        - $ref: '#/components/schemas/Parent'
        - type: object

    ChildTwo:
      allOf:
        - $ref: '#/components/schemas/Parent'
        - type: object

代码生成生成 Java Spring 接口文件和 3 个模型文件:Parent.javaChildOne.java- 2 个子类按预期ChildTwo.java扩展类。Parent

但是,当我分成api.yml2 个文件(见下文)ChildOne.java并且ChildTwo.java完全丢失(仅Parent.java生成文件)时:

api.yml文件:

openapi: 3.0.2
info:
  title: API Documentation
  version: 1.0.0
servers:
  - url: http://localhost:8080/
paths:
  /users:
    $ref: './api-sub.yml#/test'

api-sub.yml 文件:

test:
   post:
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Parent'
      responses:
        200:
          description: New user created

components:
  schemas:
    Parent:

      required:
        - status
      properties:
        status:
          type: string
      discriminator:
        propertyName: status
        mapping:
          CHILD_ONE: '#/components/schemas/ChildOne'
          CHILD_TWO: '#/components/schemas/ChildTwo'

    ChildOne:
      allOf:
        - $ref: '#/components/schemas/Parent'
        - type: object

    ChildTwo:
      allOf:
        - $ref: '#/components/schemas/Parent'
        - type: object

生成没有错误,但只生成 1 个类 -Parent.java生成:

// [...]
// Imports etc....
// [...]

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "status", visible = true)
@JsonSubTypes({
  @JsonSubTypes.Type(value = ChildOne.class, name = "CHILD_ONE"),
  @JsonSubTypes.Type(value = ChildTwo.class, name = "CHILD_TWO"),
})

public class Parent   {
  @JsonProperty("status")
  private String status;

// [...]
// Getters, setters etc....
// [...]

为什么还有 2 个其他类ChildOne.javaChildTwo.java没有生成?

请注意,生成器在使用它们的名称和映射时会看到它们Parent.java

我做了一些调查,我注意到当我将模式定义从api-sub.yml这样api.yml移动时:

api.yml文件:

openapi: 3.0.2
info:
  title: API Documentation
  version: 1.0.0
servers:
  - url: http://localhost:8080/
paths:
  /users:
    $ref: './api-sub.yml#/test'

components:
  schemas:

    Parent:
      required:
        - status
      properties:
        status:
          type: string
      discriminator:
        propertyName: status
        mapping:
          CHILD_ONE: '#/components/schemas/ChildOne'
          CHILD_TWO: '#/components/schemas/ChildTwo'

    ChildOne:
      allOf:
        - $ref: '#/components/schemas/Parent'
        - type: object

    ChildTwo:
      allOf:
        - $ref: '#/components/schemas/Parent'
        - type: object

api-sub.yml文件:

test:
   post:
      requestBody:
        content:
          application/json:
            schema:
              $ref: 'api.yml#/components/schemas/Parent'
      responses:
        200:
          description: New user created

然后,生成 4 个文件:Parent.javaChildOne.javaChildTwo.java(surprise) Parent2.java

Parent2.java在语义上与 相同Parent.java,只是名称不同。

知道为什么会这样吗?OpenApi 生成器中是否存在错误?

编辑:

还有一个观察:

当我在任何端点的某个地方使用一个孩子时,例如(仅用于测试)作为响应,然后生成这个特定的孩子(但不是另一个):

test:
   post:
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Parent'
      responses:
        200:
          description: New user created
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ChildOne'

### [...]
### rest of api-sub.yml with schemas
### [...]

是否需要使用子实体才能生成?我在继承案例之外的其他地方注意到了这种行为。

标签: openapiopenapi-generator

解决方案


推荐阅读