Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG][typescript-axios] Using allOf with a $ref and an object, along with withSeparateModelsAndApi generates broken imports #9163

Open
5 of 6 tasks
olivierbeaulieu opened this issue Apr 2, 2021 · 13 comments

Comments

@olivierbeaulieu
Copy link

olivierbeaulieu commented Apr 2, 2021

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 allOf with a $ref and an object, along with withSeparateModelsAndApi generates broken imports

openapi-generator version

5.1.0

OpenAPI declaration file content or url
openapi: 3.0.2
info:
  title: hello
  version: 1.0.0
components:
  schemas:
    SomeSchema:
      type: object
      properties:
        some_prop:
          type: integer
paths:
  /foobar:
    get:
      responses:
        200:
          description: ok
          content:
            application/json:
              schema:
                allOf:
                  - $ref: '#/components/schemas/SomeSchema'
                  - type: object
                    properties:
                      foobar: 
                        type: string
Generation Details
openapi-generator-cli generate -i http://localhost:8000/swagger.yml -g typescript-axios -o foo -p "withSeparateModelsAndApi=true,modelPackage=models,apiPackage=api"
Steps to reproduce
  1. Run the above command with the provided yaml file
  2. Look at the generated default-api.ts

On line 24, it creates an invalid import statement: import { SomeSchema & object } from '../models';

@auto-labeler
Copy link

auto-labeler bot commented Apr 2, 2021

👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

@heyhippari
Copy link

heyhippari commented Apr 14, 2021

We (Jellyfin) are also encountering this issue.

openapi.json
generated client suffering from the bug

Removing withSeparateModelsAndApi fixed it. Removing withSeparateModelsAndApi still causes the issue when building the packge with npm run build.

ferferga added a commit to jellyfin-archive/jellyfin-client-axios that referenced this issue Apr 14, 2021
ferferga added a commit to jellyfin-archive/jellyfin-client-axios that referenced this issue Apr 14, 2021
@fastlorenzo
Copy link

Same here.

Without withSeparateModelsAndApi, the rendered code is correct, but when I try to use that flag, it renders the following (incorrect) code in the api definition:

// ...SNIPPED FOR BREVITY
// @ts-ignore
import { Agency } from '../models';
// @ts-ignore
import { Array<Agency> } from '../models';
// @ts-ignore
import { CreateManyAgencyDto } from '../models';
// @ts-ignore
import { GetManyAgencyResponseDto } from '../models';
// SNIPPED FOR BREVITY...

Notice the attempt to import an Array of Agency (which was already imported in the statement before), as well as the html entities encoding that is wrong (&lt; should be <, etc.).

I think it is related to this definition in one of the model export:

import { Agency } from './agency';

/**
 *
 * @export
 * @interface CreateManyAgencyDto
 */
export interface CreateManyAgencyDto {
  /**
   *
   * @type {Array<Agency>}
   * @memberof CreateManyAgencyDto
   */
  bulk: Array<Agency>;
}

And this is the related part of the code in openapi.yaml file:

    CreateManyAgencyDto:
      type: object
      properties:
        bulk:
          type: array
          items:
            $ref: '#/components/schemas/Agency'
      required:
        - bulk

@rainmanhhh
Copy link
Contributor

typescript-rxjs also has this problem. btw, withSeparateModelsAndApi only separate models, but apis are still in one file

@ThibaultNocchi
Copy link

Is there some news for this?

@sarumont
Copy link
Contributor

sarumont commented Feb 24, 2022

Starting in 5.4.0, this affects typescript-node, too (with the defaults - withSeparateModelsAndApi option is typescript-axios specific).

It appears as though 6430aaf introduced this into 5.4.0, as that changed how imports are handled, skipping AbstractTypeScriptClientCodegen's override for certain cases.

@sarumont
Copy link
Contributor

I have a PR open (#11719) which should fix this. Can any of you using the other TS generators give that a whirl and chime in?

@funkeyfreak
Copy link

@sarumont, do we expect this code change to resolve #9031? If so, I'll give this a whirl on the typescript-angular generator.

@sarumont
Copy link
Contributor

@funkeyfreak Not sure... #9031 seems unrelated. I'm a bit crunched for time right now, but I'll try to get some tests into my PR soon to get it merged. :)

@miroljub1995
Copy link

any update on this issue?

@markus-ebner
Copy link

This problem worked fine with the 6.6 version but reoccured in the major version 7. Does someone noticed the same?

I am using the typescript-axios generator with withSeparateModelsAndApi and something like

  PersonWithNicknames:
    allOf:
      - type: object
        properties:
          nicknames:
            type: array
            items:
              type: string
      - $ref: '#/components/schemas/Person'

It had generate the type export type PersonWithNicknames = Person & PersonWithNicknamesAllOf but now only the type export type PersonWithNicknames = Person is generated.

@Mettbrot
Copy link

Mettbrot commented Jan 18, 2024

This problem worked fine with the 6.6 version but reoccured in the major version 7. Does someone noticed the same?

I am using the typescript-axios generator with withSeparateModelsAndApi and something like

  PersonWithNicknames:
    allOf:
      - type: object
        properties:
          nicknames:
            type: array
            items:
              type: string
      - $ref: '#/components/schemas/Person'

It had generate the type export type PersonWithNicknames = Person & PersonWithNicknamesAllOf but now only the type export type PersonWithNicknames = Person is generated.

I am still facing this exact issue. It works correctly with two refs and allOf. But not with one plus properties.

Relevant: #16150

@AndriiGera
Copy link

AndriiGera commented Nov 7, 2024

This problem worked fine with the 6.6 version but reoccured in the major version 7. Does someone noticed the same?

I am using the typescript-axios generator with withSeparateModelsAndApi and something like

  PersonWithNicknames:
    allOf:
      - type: object
        properties:
          nicknames:
            type: array
            items:
              type: string
      - $ref: '#/components/schemas/Person'

It had generate the type export type PersonWithNicknames = Person & PersonWithNicknamesAllOf but now only the type export type PersonWithNicknames = Person is generated.

I'm facing the same issue with TS-Axios + OpenApi generator 7.9.0. Did you manage to deal with this problem @markus-ebner ?

Edit: #16150 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests