swagger-api / swagger-api/swagger-codegen

[Angular2 TypeScript] Query parameters: The left-hand side of an 'instanceof' expression must be of type 'any', an object type or a type parameter.

Open
#5,482 10 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

Hi. I've started writing an API for a school project recently, and am using codegen to generate a client for my team-mate who is developping it using Angular2.

One of our first route uses number parameters. When compiling the whole thing, an error pops up:

error TS2358: The left-hand side of an 'instanceof' expression must be of type 'any', an object type or a type parameter.

After reading the code codegen generated, it seems that this part is causing it:

public getV1ClientPostsWithHttpInfo(start?: number, nb?: number, from?: number, to?: number, extraHttpRequestParams?: any): Observable<Response> {
    const path = this.basePath + `/v1/client/posts`;

    let queryParameters = new URLSearchParams();
    let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
    if (start !== undefined) {
        if(start instanceof Date) {
            queryParameters.set('start', <any>start.d.toISOString());
        } else {
            queryParameters.set('start', <any>start);
        }
    }

I didn't include it in my paste, but the three remaining parameters get the same check.

The fails occurs at the if(start instanceof Date) part (and frankly I don't get why this check is performed, as there's no way a number type can be instance of Date).

Edit: After further research, it seems that only query params get this check (as described here).

Swagger-codegen version

Dunno if that's a regression as it's my first time using codegen. The version I'm using is 2.2.2.

Swagger declaration file content or url

Here's the route that's causing trouble:
https://gist.github.com/babolivier/2afd8207502c5ef498c0f7ca902d165a#file-swagger-json-L76-L126

This JSON file was generated by hapi-swagger

Command line used for generation
java -jar swagger-codegen-cli.jar generate -i http://127.0.0.1:3000/swagger.json -l typescript-angular2
Steps to reproduce
Related issues

Searched for one, came empty-handed.

Suggest a Fix

I don't know codegen's codebase enough to think of a fix. However, this instanceof is clearly unecessary and should be removed as it's causing the code not to compile.

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/typescript-angular2/api.mustache, especially the query-parameter section linked in the issue, and reproduce the generated client with the supplied command and Swagger JSON. Confirm that generated Angular2 TypeScript clients with numeric query parameters compile without the reported TS2358 error.

Written by the indexing model from the issue text.

Assessment

Tech stack
angular, typescript
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.