Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
AadilC opened this issue May 18, 2018 · 3 comments

Comments

@AadilC
Copy link

AadilC commented May 18, 2018

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

@hypery2k
Copy link

I've had the same issue and found out that my controller was missing "@RequestBody" Annotation.

Because looking at your YAML you could not use optional path params, so I think you also missed @RequestBody on the backend side

@HugoMario
Copy link
Contributor

thanks for reporting this. if someone is willing to propose a PR for this, please ping me and i can help with merging.

@cmalard
Copy link

cmalard commented Dec 23, 2019

ping for Codegen 3 @HugoMario 😉 ☝️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants