openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] [Java client] oneOf is not handling inheritance properly

Open zslajosfalvi opened this issue 7 years ago • 5 comments

If I'm using this component defintions:

    AccountReference:
      type: object
      properties:
        currency:
          type: string

    AccountReferenceIban:
      description: IBAN of an account
      allOf:
        - $ref: '#/components/schemas/AccountReference'
        - type: object
          properties:
            iban:
              type: string
          required:
            - iban

    AccountReferenceBban:
      description: Basic Bank Account Number (BBAN) Identifier
      allOf:
        - $ref: '#/components/schemas/AccountReference'
        - type: object
          properties:
            bban:
              type: string
          required:
            - bban

    SinglePayment:
      type: object
      properties:
        endToEndIdentification:
          type: string
        debtorAccount:
          oneOf:
            - $ref: "#/components/schemas/AccountReferenceIban"
            - $ref: "#/components/schemas/AccountReferenceBban"

I have multiple problems with the generated code:

1. AccountReferenceIban and AccountReferenceBban doesn't extend AccountReference.

public class AccountReferenceBban  {
  
  @ApiModelProperty(value = "")
  private String currency;
...

2. SinglePayment has a

private OneOfAccountReferenceIbanAccountReferenceBban debtorAccount = null;

that refers a non existing class. Insted it should use the common parent AccountReference.

zslajosfalvi avatar Feb 05 '19 06:02 zslajosfalvi

AccountReferenceIban and AccountReferenceBban doesn't extend AccountReference.

You need to use the discriminator field for polymorphism

Ref: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#composition-and-inheritance-polymorphism

wing328 avatar Feb 05 '19 07:02 wing328

Unfortunatly I don't have a discriminator field. I know normally there should be one in such cases, but the data type defintion is not done by me, instead I get from somewhere else. To handle properly the inheritance I use a custom non generated class instead of the generated AccountReference.

@JsonDeserialize(using = AccountReferenceDeserializer.class)
@JsonSerialize(using = AccountReferenceSerializer.class)
public class AccountReference  {

zslajosfalvi avatar Feb 05 '19 07:02 zslajosfalvi

I know normally there should be one in such cases, but the data type defintion is not done by me, instead I get from somewhere else.

You can download the spec and modify it locally instead.

wing328 avatar Feb 05 '19 07:02 wing328

Requirement says: This

{
	"endToEndIdentification" : "123",
	"currency" : "EUR",
	"iban" : "DE89370400440532013000"
}

or this

{
	"endToEndIdentification" : "456",
	"currency" : "HUF",
	"bban" : "123456781234567812345678"
}

should be in the request and no extra fields. The concept behind this is to use exactly one of the account reference types. Sorry but this is not my idea nor my definition. I'm just doing the implementation. I can't change this.

zslajosfalvi avatar Feb 05 '19 07:02 zslajosfalvi

oneOf:
  - $ref: "#/components/schemas/AccountReferenceIban"
  - $ref: "#/components/schemas/AccountReferenceBban"

Looks like it's the correct use of `oneOf based on the requirement you provided.

(oneOf is not yet supported in the Java client and we welcome contribution to add that)

wing328 avatar Feb 05 '19 09:02 wing328