swagger-api / swagger-api/swagger-codegen

Issue with typescript-jquery generator returned promise type

Open
#8,383 2 comments 0 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

I was not able to use the typescript-jquery bindings in vscode or compile a simple application using them.

    someApi.someMethod().then( (result) => console.log(result.body));

The generated code looks like:

    let dfd = $.Deferred();
    $.ajax(requestOptions).then(
        (data: models.Operation, textStatus: string, jqXHR: JQueryXHR) =>
            dfd.resolve(jqXHR, data),
        (xhr: JQueryXHR, textStatus: string, errorThrown: string) =>
            dfd.reject(xhr, errorThrown)
    );
    return dfd.promise();

While my new-to-the-topic opinion is that it should look more like:

    let dfd = $.Deferred();
    $.ajax(requestOptions).then(
        (data: models.Operation, textStatus: string, jqXHR: JQueryXHR) =>
            dfd.resolve({ request: jqXHR, body: data }),
        (xhr: JQueryXHR, textStatus: string, errorThrown: string) =>
            dfd.reject({ request: xhr, body: errorThrown})
    );
    return dfd.promise();

Given that the signature of the generated method is:

public someMethod(): JQueryPromise<{ response: JQueryXHR; body: models.Operation;  }>

Not sure about the reject part, but both the compiler and intellisense are happier.

Swagger-codegen version

2.3.1 and 2.4.0-20180627.132127-273

Using jquery 3.1.1, with @types/jquery 3.3.4

Command line used for generation

swagger-codegen generate -l typescript-jquery -i ... -o ...

Note

It may very well be that I'm doing something wrong, in which case I'd suggest to maybe add some examples on how to call the generated code (either in a comment of the generated code, or in a separate example).

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 by reproducing the issue with the shown typescript-jquery generation command and inspect the generated method's JQueryPromise declaration and Deferred callbacks. Done means the generated promise's declared result matches its resolved value and the example compiles with the cited jQuery typings and provides correct editor assistance.

Written by the indexing model from the issue text.

Assessment

Tech stack
jquery, typescript
Domain
tooling
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.