OpenAPITools / OpenAPITools/openapi-generator

[BUG] [javascript-flowtyped] Add Error to language primitives

Open
#6,012 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: JavaScript/Node.js Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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?
Description

When there is a type named Error in the specification, the generator generates code that does not typecheck.

openapi-generator version

4.3.0

OpenAPI declaration file content or url
openapi: '3.0.0'
info:
  version: 0.0.0
  title: Bug
paths:
  /bug:
    get:
      responses:
        default:
          $ref: '#/components/responses/UnexpectedError'
components:
  responses:
    UnexpectedError:
      description: unexpected error
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
  schemas:
    Error:
      properties:
        code:
          type: integer
          format: int32
          example: 500
Command line used for generation

openapi-generator generate -i bug.yaml -g javascript-flowtyped -o bug/

Steps to reproduce
  • Generate code with the above command line
  • Check the generated code with flow check bug
  • You get
Cannot reference type Error [1] from a value position.

     62│  * @class RequiredError
     63│  * @extends {Error}
     64│  */
     65│ export class RequiredError extends Error {
     66│     name:string = "RequiredError"
     67│     constructor(field: string, msg?: string) {
     68│         super(msg);
       :
 [1] 76│ export type Error = {

because JavaScript's built-in Error type conflicts with the generated type alias for the Error record defined in the OpenAPI specification.

Related issues/PRs

I found none.

Suggest a fix

Reject the specification if it contains a type named Error.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the openapi-generator generate -i bug.yaml -g javascript-flowtyped -o bug/ entry point and reproduce the issue using the specification in the report. Run flow check bug and inspect the javascript-flowtyped generation path; done means a schema named Error no longer causes the generated output to fail type checking, or is rejected as specified.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.