openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] [TypeScript-Axios] invalid code generation of anyOf

Open simllll opened this issue 2 years ago • 6 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [x] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] What's the actual output vs expected output?
Description

With the latest master branch, the following spec does generate the wrong code.

upgradeTo:
                                    anyOf: [{properties: {purchasedPackageId: {$ref: '#/components/schemas/APITypeObjectId_APIObjectType.PurchasedPackage_'}, mode: {type: string, enum: [purchasedPackage], nullable: false}}, required: [purchasedPackageId, mode], type: object}, {properties: {packageId: {$ref: '#/components/schemas/APITypeObjectId_APIObjectType.BuyablePackage_'}, mode: {type: string, enum: [package], nullable: false}}, required: [packageId, mode], type: object}]
                                

What it should be (this is the original defiinitoin on our side) image

But what it does: image

so it's not purchasedPackageId OR packageId.. it is an AND.

It's the same if we use it without the "mode" paramter, then it's just merged into one (so also purchasedPackageId and packageId is required, instead of an OR).

openapi-generator version

6.0.0 - latest master snapshot did work with 5.x

OpenAPI declaration file content or url

simllll avatar May 23 '22 07:05 simllll

Update: it "works" in earlier versions only because it's generated to a generic "object": image

simllll avatar May 23 '22 08:05 simllll

What generator are you using?

spacether avatar Jun 16 '22 02:06 spacether

axios-typescript

That's the command line:

openapi-generator-cli generate -t openapi-template -p apiPackage=apipkg -p modelPackage=modelPkg -p withSeparateModelsAndApi=true -p modelPropertyNaming=original -p supportsES6=true -i ../../services/admin-api/dist/swagger/swagger.yaml -g typescript-axios -o src/api-client

simllll avatar Jun 16 '22 09:06 simllll

Can this issue be renamed to avoid confusion? There are multiple clients with this issue and I belive all of them should be fixed individually.

This can be renamed to

[BUG] [TypeScript-Axios] Invalid code generation of "anyOf"

(most used in the issues page) or

[BUG] [TypeScript] [Axios] Invalid code generation of "anyOf"

(kind of easier to find for axios users, but harder for TypeScript users)

ikopysov avatar Sep 07 '22 23:09 ikopysov

I am pretty sure that this issue affects all typescript generators.

123FLO321 avatar Sep 08 '22 06:09 123FLO321

I also face this issue. The generated code is invalid with anyOf

bintoll avatar Sep 21 '22 06:09 bintoll

I have the same issue with this generator. Any of generates this file:



// May contain unused imports in some cases
// @ts-ignore
import { TIncludedItemAnyOf } from './tincluded-item-any-of';
// May contain unused imports in some cases
// @ts-ignore
import { TIncludedItemAnyOf1 } from './tincluded-item-any-of1';
// May contain unused imports in some cases
// @ts-ignore
import { TIncludedItemAnyOf2 } from './tincluded-item-any-of2';
// May contain unused imports in some cases
// @ts-ignore
import { TIncludedItemAnyOf3 } from './tincluded-item-any-of3';
// May contain unused imports in some cases
// @ts-ignore
import { TIncludedItemAnyOf3Attributes } from './tincluded-item-any-of3-attributes';

/**
 * 
 * @export
 * @interface TIncludedItem
 */
export interface TIncludedItem {
    /**
     * 
     * @type {string}
     * @memberof TIncludedItem
     */
    'type': TIncludedItemTypeEnum;
    /**
     * 
     * @type {string}
     * @memberof TIncludedItem
     */
    'uid'?: string;
    /**
     * 
     * @type {TIncludedItemAnyOf3Attributes}
     * @memberof TIncludedItem
     */
    'attributes'?: TIncludedItemAnyOf3Attributes;
}

export const TIncludedItemTypeEnum = {
    Set: 'set'
} as const;

export type TIncludedItemTypeEnum = typeof TIncludedItemTypeEnum[keyof typeof TIncludedItemTypeEnum];

You can try to use oneOf as a workaround. It will work in the same way, but slightly slower.

vpishuk avatar Dec 11 '22 12:12 vpishuk

I mad an MR to fix this issue. https://github.com/OpenAPITools/openapi-generator/pull/14241

vpishuk avatar Dec 11 '22 19:12 vpishuk

I noticed that anyOf generator fails in 2 different ways:

Component:
  anyOf:
    - $ref: "#/components/schemas/Component1"
    - $ref: "#/components/schemas/Component2"

generates empty interface:

export interface Component {
}
Component:
  type: object
  additionalProperties:
    anyOf:
      - $ref: "#/components/schemas/Component1"
      - $ref: "#/components/schemas/Component2"

combines all properties to single interface as described in original issue.

Naktibalda avatar Jul 24 '23 12:07 Naktibalda

I changed all anyOf keywords tooneOf in my specification and openapi-generator produced expected types.

export type Component = Component1 | Component2;

Naktibalda avatar Jul 25 '23 17:07 Naktibalda

I changed all anyOf keywords tooneOf in my specification and openapi-generator produced expected types.

export type Component = Component1 | Component2;

I have the same issue: anyOf generates an empty interface, while oneOf correctly generates a Union

Not sure if it's safe to change all anyOf to oneOf

binhtran04 avatar Sep 26 '23 08:09 binhtran04