Swagger-codegen: [typescript-angular] Missing curly brace in the generated service

Created on 25 Oct 2018  路  3Comments  路  Source: swagger-api/swagger-codegen

Description

The generated classes for services are invalid, because a curly brace is missing at the end

Swagger-codegen version

3.0.2

Swagger declaration file content or url
openapi: 3.0.0
info:
  title: TestApi
  version: 1.0.0
paths:
  /test:
    get:
      summary: Test
      operationId: testApi
      responses:
        "200":
          description: Ok
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Response"
components:
  schemas:
    Response:
      type: object
      properties:
        propA:
          type: number
        propB:
          type: number
Command line used for generation

docker run --rm -v ${PWD}:/local swaggerapi/swagger-codegen-cli-v3:3.0.2 generate -i /local/swagger.yaml -l typescript-angular -o /local/ts-angular

Suggest a fix/enhancement

Generated typescript class:

import { Inject, Injectable, Optional }                      from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams,
         HttpResponse, HttpEvent }                           from '@angular/common/http';
import { CustomHttpUrlEncodingCodec }                        from '../encoder';

import { Observable }                                        from 'rxjs/Observable';

import { Response } from '../model/response';

import { BASE_PATH, COLLECTION_FORMATS }                     from '../variables';
import { Configuration }                                     from '../configuration';


@Injectable()
export class DefaultService {

    protected basePath = '/';
    public defaultHeaders = new HttpHeaders();
    public configuration = new Configuration();

    constructor(protected httpClient: HttpClient, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) {
        if (basePath) {
            this.basePath = basePath;
        }
        if (configuration) {
            this.configuration = configuration;
            this.basePath = basePath || configuration.basePath || this.basePath;
        }
    }

    /**
     * @param consumes string[] mime-types
     * @return true: consumes contains 'multipart/form-data', false: otherwise
     */
    private canConsumeForm(consumes: string[]): boolean {
        const form = 'multipart/form-data';
        for (const consume of consumes) {
            if (form === consume) {
                return true;
            }
        }
        return false;
    }


    /**
     * Test
     * 
     * @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 testApi(observe?: 'body', reportProgress?: boolean): Observable<Response>;
    public testApi(observe?: 'response', reportProgress?: boolean): Observable<HttpResponse<Response>>;
    public testApi(observe?: 'events', reportProgress?: boolean): Observable<HttpEvent<Response>>;
    public testApi(observe: any = 'body', reportProgress: boolean = false ): Observable<any> {

        let headers = this.defaultHeaders;

        // to determine the Accept header
        let httpHeaderAccepts: string[] = [
            'application/json'
        ];
        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[] = [
        ];

        return this.httpClient.get(`${this.basePath}/test`,
            {
                withCredentials: this.configuration.withCredentials,
                headers: headers,
                observe: observe,
                reportProgress: reportProgress
            }
        );
    }

// <-- MISSING CLOSED CURLY BRACE HERE

Most helpful comment

:+1: I am also affected by this.

All 3 comments

:+1: I am also affected by this.

Weird. When running java -jar modules\swagger-codegen-cli\target\swagger-codegen-cli.jar generate -i http://petstore.swagger.io/v2/swagger.json -l php -o c:\temp\php_api_client from master, the last curly brace is missing, but when you look in the repo (e.g. pet.service.ts), the last curly brace is there.

Looks like this is the culprit: the file api.service.mustache has the following lines:

{{/useHttpClient}}
    }

{{/operation}}} --> note the additional curly brace here!!
{{/operations}}

that curly brace should be on a new line, as follows:

{{/useHttpClient}}
    }
{{/operation}}
}
{{/operations}}

I've created a PR to put the curly brace on a new line, which seems to solve the issue. To test the PR, you can run the following commands:

git clone https://github.com/dennisameling/swagger-codegen.git
git checkout issue-8857
cd swagger-codegen
mvn clean package
java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate \
   -i http://petstore.swagger.io/v2/swagger.json \
   -l typescript-angular \
   -o /var/tmp/angular_api_client

For Windows, replace the last command with

java -jar modules\swagger-codegen-cli\target\swagger-codegen-cli.jar generate -i http://petstore.swagger.io/v2/swagger.json -l typescript-angular -o c:\temp\angular_api_client

This should resolve the issue.

Was this page helpful?
0 / 5 - 0 ratings