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] AnyType is not defined #6332

Closed
5 of 6 tasks
codeserk opened this issue May 16, 2020 · 21 comments
Closed
5 of 6 tasks

[bug][typescript] AnyType is not defined #6332

codeserk opened this issue May 16, 2020 · 21 comments

Comments

@codeserk
Copy link
Contributor

codeserk commented May 16, 2020

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

AnyType is not defined in typescript generated code.
In some situations, schemas return any, this is handled in other languages where that is translated into something the language understands, examples:

  • Java: Object
  • C#: `Object
  • Rust: serde_json:Value

However, this translation is not made for typescript, and the generators write AnyType in such situations, leading to type errors:

Cannot find name 'AnyType'
image

openapi-generator version

5.0.0-SNAPSHOT

OpenAPI declaration file content or url

The part that is causing troubles look like this:

          "cat": {
            "allOf": [
              {
                "$ref": "#/components/schemas/Cat"
              },
              {
                "description": "Cat information"
              }
            ]

This was generated using @nestjs/swagger and the problem is that the generator is unable to understand the second part and that one ends in _AnyType_

This is a full example:

openapi: 3.0.1
info:
  version: 1.0.0
  title: Example
  license:
    name: MIT
servers:
  - url: http://api.example.xyz/v1
paths:
  /person/display/{personId}:
    get:
      parameters:
        - name: personId
          in: path
          required: true
          description: The id of the person to retrieve
          schema:
            type: string
      operationId: list
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Person"
components:
  schemas:
    Person:
      type: object
      discriminator:
        propertyName: $_type
        mapping:
          a: '#/components/schemas/Adult'
          c: Child
      properties:
        $_type:
          type: string
        lastName:
          type: string
        firstName:
          type: string
    Adult:
      description: A representation of an adult
      allOf:
      - $ref: '#/components/schemas/Person'
      - type: object
        properties:
          children:
            type: array
            items:
              $ref: "#/components/schemas/Child"
          firstChild:
            allOf:
              - $ref: '#/components/schemas/Person'
              - description: First child
    Child:
      description: A representation of a child
      allOf:
      - type: object
        properties:
          age:
            type: integer
            format: int32
      - $ref: '#/components/schemas/Person'

output using typescript-axios

/**
 * A representation of an adult
 * @export
 * @interface Adult
 */
export interface Adult extends Person {
    /**
     * 
     * @type {Array<Child>}
     * @memberof Adult
     */
    children?: Array<Child>;
    /**
     * 
     * @type {Person & AnyType}
     * @memberof Adult
     */
    firstChild?: Person & AnyType;
}
/**

NOTE: AnyType is not defined anywhere.

Suggest a fix

The proposal is simple, adding a translation for AnyType -> object for all the typescript generators. This can be achieved adding one line to AbstractTypeScriptCodegen.java like the ones we have in other generators:

typeMapping.put("AnyType", "Object");
@auto-labeler
Copy link

auto-labeler bot commented May 16, 2020

👍 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.

@codeserk
Copy link
Contributor Author

FYI, I'm working on a fix for this, so I'll present a PR that fixes this situation

@macjohnny
Copy link
Member

fixed with #6335

dbanetto added a commit to dbanetto/kitsu-apiclient that referenced this issue Jun 28, 2020
@krajek
Copy link

krajek commented Sep 15, 2020

If the fix already present in the published version? I cannot see any versions after 4.3.1, which has the bug.

@codeserk
Copy link
Contributor Author

@krajek I think it was published only in v5, which I think it's still in beta!
image

@davidyee
Copy link
Contributor

@krajek @codeserk I ran into the same issue while using the latest stable 4.3.1 but updating to 5.0.0-beta2 resolved the issue. Thanks for the suggestion!

@GunnarAK
Copy link

If you're able to; try downgrading to 4.3.0
This resolved the issue for me while I came from 4.1.3. AnyType became Object again.

@Song2017
Copy link

openapitools/openapi-generator-cli:v4.2.3 works for me

@wing328
Copy link
Member

wing328 commented Oct 27, 2020

The latest master has this issue resolved. Please try the SNAPSHOT version (mentioned in the README) and let us know if you've any feedback before the official release of v5.0.0 in Dec 2020.

@Khaled-Slay
Copy link

please can someone explain the fix.i have many problems in openapi like:
-basepath="http://localhost".
-Anytype bug.

  • ModuleWithProvider bug

@davija
Copy link

davija commented Dec 14, 2020

@Khaled-Slay

please can someone explain the fix.i have many problems in openapi like:
-basepath="http://localhost".
-Anytype bug.

  • ModuleWithProvider bug

Sounds like you probably need to go and ask questions on stack overflow... This is a bug report that has been closed.

@codeserk
Copy link
Contributor Author

please can someone explain the fix.i have many problems in openapi like:
-basepath="http://localhost".
-Anytype bug.

  • ModuleWithProvider bug

Hello!
Not sure what you mean, but the problem that why tried to fix with this patch was related to the present of a symbol called AnyType which was never designed.
I think there were 2 changes involved

  • Ensuring any was used for AnyType (the correct symbol in TS).
  • Remove any in some situations where it was not needed.

@davija
Copy link

davija commented Dec 14, 2020

@codeserk Sorry, I was trying to reply to the previous post.

@codeserk
Copy link
Contributor Author

@davija yeah my comment is also an attempt to reply to @Khaled-Slay 😅

@Khaled-Slay
Copy link

Khaled-Slay commented Dec 15, 2020 via email

@codeserk
Copy link
Contributor Author

Hello @Khaled-Slay

I'm not sure about the problem with the basepath, but the problem you mention about AnyType was the same problem I found when I created this issue. It should be solved in version 5.x.x, but not in 4.x.x
can you check which version are you using? This problem shouldn't be present in the new version (which I believe it's still in beta phase):

https://hub.docker.com/layers/openapitools/openapi-generator-cli/v5.0.0-beta/images/sha256-fdea7c99b45273354b12e4c84b771af1374a35c69eedbe4fe2cc3da8e9018e0f?context=explore

Hope this helps :)

@Khaled-Slay
Copy link

Khaled-Slay commented Dec 15, 2020 via email

@networkdevCD
Copy link

Hello

I am still facing this issue when using the opengenerator version 5.0.0 beta 1-2-3 on the following yaml file : https://github.com/jdegre/5GC_APIs/blob/master/TS29510_Nnrf_NFManagement.yaml (or any) to create python-flask server . Upon running the python -m openapi_server I am greeted with following :

Traceback (most recent call last):

File "C:\Python39\lib\runpy.py", line 197, in _run_module_as_main

return _run_code(code, main_globals, None,

File "C:\Python39\lib\runpy.py", line 87, in _run_code

exec(code, run_globals)

File "C:\ \test\openapi_server_main_.py", line 5, in

from openapi_server import encoder

File "C:\ openapi_server\encoder.py", line 4, in

from openapi_server.models.base_model_ import Model

File "C:\ test\openapi_server\models_init_.py", line 15, in

from openapi_server.models.change_item import ChangeItem

File "C:\test\openapi_server\models\change_item.py", line 9, in

from openapi_server.models.any_type import AnyType

ModuleNotFoundError: No module named 'openapi_server.models.any_type'

I have tried changing the generator version to 4.3.1 as other thread suggested still nothing and tried all of the beta version but still get this error.

@codeserk
Copy link
Contributor Author

Hello @networkdevCD

Not sure if I'm missing something, but looks like you are using a different generator (python). This change only affected the generator for typescript-axios, but this change had no impact on the generators for python :(

@networkdevCD
Copy link

networkdevCD commented Dec 24, 2020

Hello @codeserk

Your correct apologies was get to end of my tether with it and found this bug report . It seems we also have the same issue in the python generator , was there any quick work around you did the with YAML ?

@mpp-doric
Copy link

Same deal using the gradle plugin id "org.openapi.generator" version "5.1.0" and generating Kotlin. Manually replacing with kotlins Any does the trick.

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