belgif / rest-guide

REST Guidelines of Belgian government institutions
https://www.belgif.be/specification/rest/api-guide/
Apache License 2.0
24 stars 4 forks source link

Recommended use of discriminator, oneOf, inheritance #181

Open pvdbosch opened 4 months ago

pvdbosch commented 4 months ago

We're regularly encountering errors when oneOf or discriminators are being used.

pvdbosch commented 4 months ago

Confirmed that oneOf with simple types gives bad code generation results, for instance:

  BelgianRemittanceInformationOneOfTwoStrings:
      oneOf:
        - description: Unstructured Remittance Information as supported by Belgian banks
          type: string
          maxLength: 140
        - description: Structured Remittance Information in the Belgian OGM-VCS format. Often formatted for display as +++ 3 digits / 4 digits / 5 digits / +++.
          type: string
          pattern: "^\\d{12}$"
          example: "010806817183"

generates:

public interface BelgianRemittanceInformationOneOfTwoStrings {
}

without any subclasses of this empty interface. Tested using openapi-generator 7.5.0, same behavior when using referenced subschemas instead of inline ones.

pvdbosch commented 4 months ago

Tested oneOf with object-type subschemas without discriminator in Java. It doesn't seem to work as desired:

    BelgianRemittanceInformationOneOfTwoObjects:
      type: object
      properties:
        variant:
          type: string
          enum:
            - variantA
            - variantB
      oneOf:
        - $ref: "#/components/schemas/BelgianRemittanceInformationUnstructuredObject"
        - $ref: "#/components/schemas/BelgianRemittanceInformationStructuredObject"
    BelgianRemittanceInformationStructuredObject:
      type: object
      properties:
        structured:
          $ref: "#/components/schemas/BelgianRemittanceInformationStructured"
    BelgianRemittanceInformationUnstructuredObject:
      type: object
      properties:
        unstructured:
          $ref: "#/components/schemas/BelgianRemittanceInformationUnstructured"

warning: 'oneOf' is intended to include only the additional optional OAS extension discriminator object. For more details, see https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.9.2.1.3 and the OAS section on 'Composition and Inheritance'.

generated Java code: