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

Open API throws Class constructor User cannot be invoked without 'new' when trying to use .map() in adonis @computed field in model

Open jollyjunkaih opened this issue 10 months ago • 0 comments

openapi-typescript version

12.1.3

Node.js version

23.6.0

OS + version

macOS 15.2

Description

I ran into an issue while working when using the open api lib

@ApiProperty({ type: [String] }) @computed() public get permissions(): string[] { return this.permissionRelation.map((p) => p.name) }

I am getting this error

"message": "Class constructor User cannot be invoked without 'new'",
"name": "TypeError",
"status": 500,
"frames": [
    {
        "file": "Users/junkaihwong/Projects/kkip-app/node_modules/.pnpm/[email protected][email protected]/node_modules/openapi-metadata/dist/loaders/type.js",
        "filePath": "/Users/junkaihwong/Projects/kkip-app/node_modules/.pnpm/[email protected][email protected]/node_modules/openapi-metadata/dist/loaders/type.js",
        "line": 104,
        "callee": "loadType",
        "calleeShort": "loadType",

I've pinpointed the error to the .map function but when i tried swapping .map with any other array iterator but it still gives me the same error. My code runs fine tho. Wondering if it has anything to do with anything the screenshot and is there a quick fix / workaround? Appreciate any help 🙏

Reproduction

Reproduce by doing the following:

  1. Initialising adonis (pnpm create adonisjs@latest hello-world -- --kit=api --auth-guard=access_tokens --db=postgres)
  2. Installing dependency (node ace add @foadonis/openapi )
  3. Setting up routes and adding controllers and updating the model to use .map() in the User Model @ApiProperty() @computed() public get name(): string[] { return ['hwllo'].map((q) => q) }

You will be greeted by this error:

"message": "Class constructor User cannot be invoked without 'new'", "name": "TypeError", "status": 500,

At point 3, the code looks like this: https://github.com/jollyjunkaih/adonis-openapi-bug

Expected result

It's not supposed to fail

Required

  • [x] My OpenAPI schema is valid and passes the Redocly validator (npx @redocly/cli@latest lint)

Extra

jollyjunkaih avatar Jan 26 '25 14:01 jollyjunkaih