OpenAPITools / OpenAPITools/openapi-generator

[BUG][Spring] Inheritance being generated incorrectly - v6.3.0

Open
#14,747 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Hello, could someone explain to me the correct way to achieve an inheritance relationship with two classes? In my example I have the following schemas:

openapi: 3.0.3
#...
paths:
  /categories:
    get:
      summary: List categories
      description: Lists categories taking into account pagination information, if provided.
      operationId: listCategories
      tags:
        - Categories
      parameters:
        - $ref: "#/components/parameters/orderBy"
        - $ref: "#/components/parameters/limit"
        - $ref: "#/components/parameters/page"
      responses:
        200:
          description: OK
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/CategoryDtoPagination"
        422:
          $ref: "#/components/responses/UnprocessableEntity"

  /transactions:
    get:
      summary: List transactions.
      description: Lists transactions taking into account paging information, if provided.
      operationId: listTransactions
      tags:
        - Transactions
      parameters:
        - $ref: "#/components/parameters/orderBy"
        - $ref: "#/components/parameters/limit"
        - $ref: "#/components/parameters/page"
      responses:
        200:
          description: OK
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/TransactionDtoPagination"
        422:
          $ref: "#/components/responses/UnprocessableEntity"

components:
  schemas:
    CategoryDtoPagination:
      description: Uses the PaginationData base object.
      allOf:
        - $ref: "#/components/schemas/PaginationData"
      properties:
        elements:
          type: array
          items:
            $ref: "#/components/schemas/CategoryDto"

    TransactionDtoPagination:
      description: Uses the PaginationData base object.
      allOf:
        - $ref: "#/components/schemas/PaginationData"
      properties:
        elements:
          type: array
          items:
            $ref: "#/components/schemas/TransactionDto"

    PaginationData:
      type: object
      properties:
        pageNumber:
          type: integer
          format: int32
          description: Current page number.
        pageSize:
          type: integer
          format: int32
          description: Represents the maximum size of the page being returned.
        totalPages:
          type: integer
          format: int32
          description: Total pages
        totalElements:
          type: integer
          format: int64
          description: Total elements
      discriminator:
        propertyName: PaginationData
#...

As I don't have a type or id property in the schema PaginationData I put the name of the schema itself for this test. With the discriminator property added to the schema, classes are generated perfectly with extends, as in the example below:

public class CategoryDtoPagination extends PaginationData {
//...
}

When I remove the discriminator property from the PaginationData schema, all other schemas that are defined with allOf and that should inherit from PaginationData do not have the extends. Ex:

public class CategoryDtoPagination {
//...
}

I've already analyzed some open issues, but I still couldn't understand clearly how the inheritance process should be generated. Excuse my ignorance, but I don't see the need to include a type or id property in the PaginationData schema so that the other classes inherit from PaginationData. Does anyone have a different view? Or if this would be a bug?

openapi-generator version

v6.3.0

Steps to reproduce

Carry out the generation process with one or more schemas using the allOf property referencing another schema.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the Spring generator with the supplied OpenAPI YAML on version 6.3.0, comparing output with and without the PaginationData discriminator. Trace the generator's allOf and discriminator handling for CategoryDtoPagination and TransactionDtoPagination. Done means the intended inheritance behavior is established and the generated Java classes consistently reflect it, with regression coverage if the behavior is confirmed as a bug.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, spring
Domain
backend-api-design, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.