swagger-api / swagger-api/swagger-codegen

[JavaScript][ES6] TypeError: Cannot call a class as a function

Open
#7,640 2 comments 8 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

Composition of model definitions does not work with es6 javascript, leading into a TypeError: Cannot call a class as a function when sources are compiled with babel, which is according to ES6 Spec 9.2.1 (Point 2) appropriate bahaviour.

This is the related line in the mustache template:

{{#interfaceModels}}{{classname}}.call(this{{#vendorExtensions.x-all-required}}, {{name}}{{/vendorExtensions.x-all-required}});{{/interfaceModels}}

For example in the Cat.js of the petstore sample this compiles to:

import Animal from './Animal';

export default class Cat {

    constructor(className) {
        Animal.call(this, className);  // <--- Throws Error
    }
    …
}

Animal.call(this, className) throws error, because es6 class constructors cannot be called as normal functions, like it is done here with Function.prototype.call, when for my case the sources are transpiled by babel.

Babel uses an internal function to check if a class is called as function and then throws:

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

Here ist the related REPL.

Swagger-codegen version

swagger-codegen-cli-2.3.1

Swagger declaration file content or url

Refering the perstore example.

Command line used for generation

config.json

{
    "projectName":  "...",
    "projectDescription":  "...",
    "projectVersion":  "0.0.0",
    "useES6": true
}

shell command

java -jar swagger-codegen-cli-2.3.1.jar generate -i config.json -l javascript
Steps to reproduce

Using a model definition which implements another model definition and generating a javascript client library which uses es6 standard.

Suggest a fix/enhancement

For now I am using my own partial_model_generic.mustache template commenting out the related code line. But I am not sure, what would be the appropriate javascript fix, since es6 does not support interfaces. So I will leave this question open for discussion and I will be open to file a PR at appropriate time.

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 modules/swagger-codegen/src/main/resources/Javascript/es6/partial_model_generic.mustache and compare its generated output with samples/client/petstore/javascript-es6/src/model/Cat.js. Reproduce the issue using swagger-codegen-cli-2.3.1 with the provided config and command, then verify that a generated model composing another model no longer throws the reported Babel TypeError.

Written by the indexing model from the issue text.

Assessment

Tech stack
babel, javascript
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.