openapi-generator: [bug][typescript] `AnyType` is not defined
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:ObjectC#: `ObjectRust: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'

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");
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 2
- Comments: 21 (9 by maintainers)
Commits related to this issue
- Fix generation issues Related to https://github.com/OpenAPITools/openapi-generator/issues/6332 — committed to dbanetto/kitsu-apiclient by dbanetto 4 years ago
Yap that is it.thank you.much appreaciated
On Tue, 15 Dec 2020, 10:47 Jose Camara, notifications@github.com wrote:
Hello @Khaled-Slay
I’m not sure about the problem with the
basepath, but the problem you mention aboutAnyTypewas 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 😃
If you’re able to; try downgrading to
4.3.0This resolved the issue for me while I came from4.1.3.AnyTypebecameObjectagain.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 forpython😦please can someone explain the fix.i have many problems in openapi like: -basepath=“http://localhost”. -Anytype bug.
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.
@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!