OpenAPITools / OpenAPITools/openapi-generator

[BUG][typescript-angular] interface generated for `anyOf` contains properties of all sub schemas

Open
#14,803 3 comments 5 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

For a anyOf type the typescript-angular generator creates an interface that contains all properties from all sub-schemas listed. That is the same output as for allOf.
I'd expect to se a union type, it does not match the meaning of anyOf exactly but is good approximation.

Current Output:

export interface Bar { 
    prop1: number;
    prop2: string;
    prop3: string;
}

export interface Foo { 
    active?: boolean;
}

export interface FooBar { 
    active?: boolean;
    prop1: number;
    prop2: string;
    prop3: string;
}

export interface BarFoo { 
    active?: boolean;
    prop1: number;
    prop2: string;
    prop3: string;
}

Expected Output:

export interface Bar { 
    prop1: number;
    prop2: string;
    prop3: string;
}

export interface Foo { 
    active?: boolean;
}

export type FooBar = Foo | Bar;

export interface BarFoo { 
    active?: boolean;
    prop1: number;
    prop2: string;
    prop3: string;
}
openapi-generator version

6.3.0

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: anyOf issue
  version: 1.0.0
servers:
  - url: http://localhost:8080
components:
  schemas:
    Foo:
      type: object
      properties:
        active:
          type: boolean
    Bar:
      type: object
      required:
        - prop1
        - prop2
        - prop3
      properties:
        prop1:
          type: number
        prop2:
          type: string
        prop3:
          type: string

    FooBar:
      anyOf:
        - $ref: "#/components/schemas/Foo"
        - $ref: "#/components/schemas/Bar"
    BarFoo:
      allOf:
        - $ref: "#/components/schemas/Foo"
        - $ref: "#/components/schemas/Bar"

paths:
  /:
    get:
      summary: Create a new planning session
      operationId: getFooBar
      responses:
        "200":
          description: Return a Foo and/or Bar
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/FooBar"
Generation Details

openapi-generator-cli generate -i anyOf_issue.yaml -g typescript-angular -o anyOf_Issue

Steps to reproduce
  1. Save provided spec as anyOf_issue.yaml
  2. run code generation openapi-generator-cli generate -i anyOf_issue.yaml -g typescript-angular -o anyOf_Issue
  3. Observe content of file anyOf_Issue/model/fooBar.ts
Related issues/PRs

There is an issue reporting an empty interface:
https://github.com/OpenAPITools/openapi-generator/issues/13428

Suggest a fix

Create a union type instead of an interface

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

Run the provided openapi-generator-cli command with the example specification, then inspect anyOf_Issue/model/fooBar.ts and compare it with the allOf-generated model. Trace the typescript-angular generation path responsible for anyOf models; done means FooBar is emitted as a union while BarFoo retains its combined interface properties.

Written by the indexing model from the issue text.

Assessment

Tech stack
angular, typescript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.