OpenAPITools / OpenAPITools/openapi-generator

[BUG] Imports missing in nested models for `anyOf` properties

Open
#11,867 1 comment 2 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

https://github.com/OpenAPITools/openapi-generator/commit/6430aaf3b1b2a6bafc67d5342f9e3703ada1a671 introduced deeply nested imports. More specifically the method that recursively collects imports handles allOf and oneOf composed schemas but seems to ignore anyOf schemas.

Having the following Open API schemas, the generated model AnyFooBar does not include the required Foo and Bar imports

edit: Removed TypeScript from title as from what I can tell, looking at where the issue lies, this is likely affecting all languages.

openapi-generator version

This is in 5.4.0 as well as latest of master as far as I can tell.

OpenAPI declaration file content or url
components:
  schemas:
    Foo:
      type: object
      properties:
        name:
          type: string
        foo:
          type: string
    Bar:
      type: object
      properties:
        name:
          type: string
        bar:
          type: string
    AnyFooBar:
      type: object
      properties:
        name:
          type: string
        fooOrBar:
          anyOf:
            - $ref: '#/components/schemas/Foo'
            - $ref: '#/components/schemas/Bar'
Generation Details
java -jar openapi-generator-cli.jar generate -g typescript -i api.yml -o generated/
import { HttpFile } from '../http/http';

export class AnyFooBar {
    'name'?: string;
    'fooOrBar'?: Foo | Bar;

    static readonly discriminator: string | undefined = undefined;

    static readonly attributeTypeMap: Array<{name: string, baseName: string, type: string, format: string}> = [
        {
            "name": "name",
            "baseName": "name",
            "type": "string",
            "format": ""
        },
        {
            "name": "fooOrBar",
            "baseName": "fooOrBar",
            "type": "Foo | Bar",
            "format": ""
        }    ];

    static getAttributeTypeMap() {
        return AnyFooBar.attributeTypeMap;
    }

    public constructor() {
    }
}

Related issues/PRs

The following issue does look somewhat similar though language and models do differ.

Suggest a fix

Opened a pull request for candidate fix: #11879

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 with the recursive import-collection method introduced by commit 6430aaf3b1b2a6bafc67d5342f9e3703ada1a671 and reproduce the issue using the YAML schemas and TypeScript generation command in the report. Compare the generated AnyFooBar model with the expected imports for Foo and Bar, and review candidate fix PR #11879; done means nested anyOf references receive their required imports.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.