swagger-api / swagger-api/swagger-codegen

[Java] [typescript-angular] Bug swagger-codegen Invalid output for methods with URL Query parameters

Open
#8,207 3 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

Swagger Codegen produces incorrect output for typescript-angular for get methods that receive multiple query parameters (?a=1&b=2). But produces the correct output for get methods that receive path parameters (/{a}/{b} /1/2 )

/**
     * getDocumentTypes
     * 
     * @param documentContext documentContext
     * @param lifecycleStage lifecycleStage
     * @param processType processType
     * @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
     * @param reportProgress flag to report request and response progress.
     */
    public getDocumentTypesUsingGET(documentContext?: string, lifecycleStage?: string, processType?: string, observe?: 'body', reportProgress?: boolean): Observable<ResponseGenericListDocumentSubType>;
    public getDocumentTypesUsingGET(documentContext?: string, lifecycleStage?: string, processType?: string, observe?: 'response', reportProgress?: boolean): Observable<HttpResponse<ResponseGenericListDocumentSubType>>;
    public getDocumentTypesUsingGET(documentContext?: string, lifecycleStage?: string, processType?: string, observe?: 'events', reportProgress?: boolean): Observable<HttpEvent<ResponseGenericListDocumentSubType>>;
    public getDocumentTypesUsingGET(documentContext?: string, lifecycleStage?: string, processType?: string, observe: any = 'body', reportProgress: boolean = false ): Observable<any> {
    
        let queryParameters = new HttpParams({encoder: new CustomHttpUrlEncodingCodec()});
        if (documentContext !== undefined && documentContext !== null) {
            queryParameters = queryParameters.set('documentContext', <any>documentContext);
        }
        if (lifecycleStage !== undefined && lifecycleStage !== null) {
            queryParameters = queryParameters.set('lifecycleStage', <any>lifecycleStage);
        }
        if (processType !== undefined && processType !== null) {
            queryParameters = queryParameters.set('processType', <any>processType);
        }

        let headers = this.defaultHeaders;

        // to determine the Accept header
        let httpHeaderAccepts: string[] = [
            '*/*'
        ];
        const httpHeaderAcceptSelected: string | undefined = this.configuration.selectHeaderAccept(httpHeaderAccepts);
        if (httpHeaderAcceptSelected != undefined) {
            headers = headers.set('Accept', httpHeaderAcceptSelected);
        }

        // to determine the Content-Type header
        const consumes: string[] = [
            'application/json'
        ];

// ERROR ON THIS LINE
        return this.httpClient.get<ResponseGenericListDocumentSubType>(`${this.basePath}/adr/refData/documentTypes${encodeURIComponent(String(documentContextlifecycleStageprocessType))}`,
            {
                params: queryParameters,
                withCredentials: this.configuration.withCredentials,
                headers: headers,
                observe: observe,
                reportProgress: reportProgress
            }
        );
    }

The url query parameters are incorrectly appended and encoded at the end of the url path

return this.httpClient.get<ResponseGenericListDocumentSubType>
(`${this.basePath}/adr/refData/documentTypes${encodeURIComponent(String(documentContextlifecycleStageprocessType))}`,
Swagger-codegen version

swagger-codegen-cli-2.3.1

Swagger declaration file content or url
"/adr/refData/documentTypes{?documentContext,lifecycleStage,processType}": {
            "get": {
                "tags": [
                    "adr-reference-data-controller"
                ],
                "summary": "getDocumentTypes",
                "operationId": "getDocumentTypesUsingGET",
                "consumes": [
                    "application/json"
                ],
                "produces": [
                    "*/*"
                ],
                "parameters": [
                    {
                        "name": "documentContext",
                        "in": "query",
                        "description": "documentContext",
                        "required": false,
                        "type": "string"
                    },
                    {
                        "name": "lifecycleStage",
                        "in": "query",
                        "description": "lifecycleStage",
                        "required": false,
                        "type": "string"
                    },
                    {
                        "name": "processType",
                        "in": "query",
                        "description": "processType",
                        "required": false,
                        "type": "string"
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK",
                        "schema": {
                            "$ref": "#/definitions/ResponseGeneric«List«DocumentSubType»»"
                        }
                    },
                    "401": {
                        "description": "Unauthorized"
                    },
                    "403": {
                        "description": "Forbidden"
                    },
                    "404": {
                        "description": "Not Found"
                    }
                }
            }
        },
Command line used for generation

java -jar tools/swagger-codegen-cli-2.3.1.jar generate -i http://localhost:8082/ngs/swagger.json -l typescript-angular -t tools/templates -o src/app/api -c swagger-codegen.config.json

Steps to reproduce
Related issues/PRs
Suggest a fix/enhancement

I would guess the error resides somewhere in since the {{{path}}} is output as in Mustache templates

https://github.com/swagger-api/swagger-codegen/blob/v2.3.1/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngularClientCodegen.java

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 TypeScriptAngularClientCodegen.java and the Mustache template usage referenced by {{{path}}}; reproduce generation with the supplied swagger-codegen command and OpenAPI path. Trace why query parameters are included in the encoded path, then verify generated typescript-angular methods keep them in the HttpParams request options rather than appending them to the URL.

Written by the indexing model from the issue text.

Assessment

Tech stack
angular, java, typescript
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.