OpenAPITools / OpenAPITools/openapi-generator

[BUG] REF_AS_PARENT_IN_ALLOF=true has no effect for simple inheritance without extra properties

Open
#21,222 3 comments 6 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

Using the openapiNormalizer option REF_AS_PARENT_IN_ALLOF has no effect for child schema that contains only the parent reference in the allOf.

With the example spec below, it is expected that the generated class FirstChild extends FirstParent, and the generate class SecondChild extends SecondParent. What actually happens is that class SecondChild extends SecondParent but class FirstChild only inherits FirstParent's properties, but does not get the inheritance.

openapi-generator version

7.13.0
I've verified behavior using openapi-generator-maven-plugin, openapi-generator-cli, and using spec v3.0.1 and v2.0.

The last version that this worke

OpenAPI declaration file content or url
# test.yaml
openapi: '3.0.1'
info:
  title: Example
  version: '1.0.0'

paths: {}

components:
  schemas:
    FirstParent:
      type: object
      properties:
        parentProp:
          type: string

    FirstChild:
      allOf:
        - $ref: '#/components/schemas/FirstParent'
    
    SecondParent:
      type: object
      properties:
        parentProp:
          type: string

    SecondChild:
      allOf:
        - $ref: '#/components/schemas/SecondParent'
        - properties:
            childProp:
              type: string
Generation Details

openapi-generator generate -i test.yaml -o out -g java --openapi-normalizer=REF_AS_PARENT_IN_ALLOF=true --global-property models

Steps to reproduce
  1. generate models using the command above.
Related issues/PRs

[BUG] REF_AS_PARENT_IN_ALLOF=true no longer works for allOf combined with properties #16389

Suggest a fix

The regression seems to be caused by the changes in #17426. Skipping an "allOf" list with only one item is causing the FirstParent class not to be marked as x-parent: true by the normalizer, even though the FirstChild includes it in its "allOf" list. SecondParent is being marked as x-parent: true because the extra properties constitute a second entry in the "allOf" list. removing the size() == 1 guard does not seem to cause a regression for #17425 (i.e. the tests still pass)

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 reproducing the issue with test.yaml using the documented openapi-generator command, then inspect the normalizer's allOf handling and the single-item guard implicated in #17426. Done means FirstChild extends FirstParent as SecondChild already does, with regression coverage confirming the related behavior remains intact.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.