OpenAPI 规范:使用 getter 和 setter 声明 Java 接口

问题描述 投票:0回答:1

我面临的问题是关于 OpenAPI 规范的:

openapi: 3.0.3
components:
  schemas:
    Accounts:
      title: Accounts entity
      type: object
      allOf:
        - $ref: '#/components/schemas/PaginatedArray'
        - type: object
          properties:
            accounts:
              type: array
              items:
                $ref: '#/components/schemas/AccountInfo'

    AccountInfo:
      type: object
      description: Account info (either lightweight, or detailed)
      oneOf:
        - $ref: '#/components/schemas/AccountDescriptor'
        - $ref: '#/components/schemas/AccountWithDetails'

这个规范生成了由两个模型类实现的接口

AccoutInfo
,这部分还可以(感谢如何使用openapi-generator-maven-plugin生成模型的接口?的参与者):

@Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
public interface AccountInfo {}

public class AccountDescriptor implements AccountInfo {
// some properties
}

public class AccountWithDetails implements AccountInfo {
// some properties
}

现在我想在

AccountInfo
中添加一些 getter 和 setter 以支持客户端代码中的多态性,所以我尝试这样做,但没有任何运气:

AccountInfo:
  type: object
  description: Account info (either lightweight, or detailed)
  properties:
    depositAccount:
      $ref: '#/components/schemas/DepositAccount'
  oneOf:
    - $ref: '#/components/schemas/AccountDescriptor'
    - $ref: '#/components/schemas/AccountWithDetails'

有没有办法生成带有返回/接受特定类型的 getter 和 setter 的 Java 接口?

java swagger openapi openapi-generator
1个回答
0
投票

我已经使用

x-implements
属性解决了这个问题

AccountInfo:
  type: object
  description: Account info (either lightweight, or detailed)
  x-implements: [ 'com.my.model.AccountInfoProvider' ]
  oneOf:
    - $ref: '#/components/schemas/AccountDescriptor'
    - $ref: '#/components/schemas/AccountWithDetails'

结合我的自定义界面:

package com.my.model;

public interface AccountInfoProvider {

    default DepositAccount getDepositAccount() {
        return null;
    }

    default void setDepositAccount(DepositAccount depositAccount) {
    }
    // the rest of the accessors
}

这使我可以使用访问器从接口扩展生成的

AccountInfo
,多态性终于起作用了:

@Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
public interface AccountInfo extends com.my.model.AccountInfoProvider {
}
© www.soinside.com 2019 - 2024. All rights reserved.