OpenAPITools / OpenAPITools/openapi-generator

[BUG][typescript-axios] `anyOf` creates same interface as `allOf`

Open
#8,894 4 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Feature: Composition / Inheritance 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

When using anyOf to describe an object, the created interface is incorrect. It contains properties from all subtypes, i.e. the result is what I would expect from allOf.

openapi-generator version

5.0.1 with openapi-generator-cli 2.1.26

OpenAPI declaration file content or url
Simplified Schema:
Pet:
    anyOf:
      - $ref: '#/components/schemas/Cat'
      - $ref: '#/components/schemas/Dog'
Cat:
    type: object
    properties:
        petType:
            type: string
            enum: ["cat"]
        huntingSkill:
            type: string
    required:
        - petType
        - huntingSkill
Dog:
    properties:
        petType:
            type: string
            enum: ["dog"]
        packSize:
            type: integer
    required:
        - petType
Actual Output:
/**
 * 
 * @export
 * @interface Pet
 */
export interface Pet {
    /**
     * 
     * @type {string}
     * @memberof Pet
     */
    petType: PetPetTypeEnum;
    /**
     * 
     * @type {string}
     * @memberof Pet
     */
    huntingSkill: string;
    /**
     * 
     * @type {number}
     * @memberof Pet
     */
    packSize?: number;
}

/**
    * @export
    * @enum {string}
    */
export enum PetPetTypeEnum {
    Dog = 'dog'
}
Expected Output
/**
 * A representation of a cat.
 * @export
 * @interface Cat
 */
export interface Cat {
    /**
     * 
     * @type {string}
     * @memberof Cat
     */
    petType: CatPetTypeEnum;
    /**
     * 
     * @type {string}
     * @memberof Cat
     */
    huntingSkill: string;
}

/**
  * @export
  * @enum {string}
  */
export enum CatPetTypeEnum {
    Cat = 'cat'
}

/**
 * A representation of a dog.
 * @export
 * @interface Dog
 */
export interface Dog {
    /**
     * 
     * @type {string}
     * @memberof Dog
     */
    petType: DogPetTypeEnum;
    /**
     * 
     * @type {number}
     * @memberof Dog
     */
    packSize?: number;
}

/**
  * @export
  * @enum {string}
  */
export enum DogPetTypeEnum {
    Dog = 'dog'
}

/**
 * @type Pet
 * @export
 */
export type Pet = Cat | Dog;
Generation Details

Generator Name: typescript-axios

Steps to reproduce

README

Related issues/PRs
Suggest a fix

Using oneOf, the expected interface is generated. It looks like anyOf is incorrectly using the same behaviour as allOf.
Ideally, oneOf and anyOf should not produce the same code, as there is a semantic difference (see https://github.com/OpenAPITools/openapi-generator/issues/6376 linked above), but it would be much better already to model both anyOf and oneOf as a type union.

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 typescript-axios generator and reproduce the issue using the linked openapi.yaml and project README. Trace how anyOf is processed compared with allOf and oneOf, then verify that the generated Pet, Cat, and Dog declarations match the expected union behavior. No source file or test path is named in the issue.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.