Skip to content

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

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

Open
1 of 2 tasks
jollyjunkaih opened this issue Jan 26, 2025 · 0 comments · May be fixed by #2125
Assignees
Labels
bug Something isn't working openapi-metadata Relevant to openapi-metadata library

Comments

@jollyjunkaih
Copy link

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

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

Extra

@jollyjunkaih jollyjunkaih added bug Something isn't working openapi-ts Relevant to the openapi-typescript library labels Jan 26, 2025
@kerwanp kerwanp added openapi-metadata Relevant to openapi-metadata library and removed openapi-ts Relevant to the openapi-typescript library labels Jan 26, 2025
@kerwanp kerwanp self-assigned this Jan 26, 2025
@kerwanp kerwanp linked a pull request Jan 27, 2025 that will close this issue
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working openapi-metadata Relevant to openapi-metadata library
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants