cyclosproject / ng-openapi-gen

An OpenAPI 3.0 codegen for Angular
MIT License
397 stars 132 forks source link

Syntax error on generate services if operationId is not a valid identifier #26

Closed ThinkontrolSY closed 5 years ago

ThinkontrolSY commented 5 years ago
{
  "openapi": "3.0.0",
  "info": {
    "title": "LoopBack Application",
    "version": "1.0.0"
  },
  "paths": {
    "/login": {
      "post": {
        "x-controller-name": "LoginController",
        "x-operation-name": "login",
        "tags": [
          "LoginController"
        ],
        "responses": {
          "200": {
            "description": "Token",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "token": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        },
        "requestBody": {
          "description": "The input of login function",
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "username",
                  "password"
                ],
                "properties": {
                  "username": {
                    "type": "string"
                  },
                  "password": {
                    "type": "string"
                  }
                }
              }
            }
          }
        },
        "operationId": "LoginController.login"
      }
    }
  },
  "servers": [
    {
      "url": "http://10.0.0.126:3000"
    }
  ],
  "components": {
    "schemas": {
      "User": {
        "title": "User",
        "properties": {
          "id": {
            "type": "string"
          },
          "username": {
            "type": "string"
          },
          "password": {
            "type": "string"
          },
          "createdAt": {
            "type": "string",
            "format": "date-time"
          },
          "updatedAt": {
            "type": "string",
            "format": "date-time"
          }
        },
        "required": [
          "id",
          "username",
          "password"
        ]
      },
      "UserPartial": {
        "title": "UserPartial",
        "properties": {
          "id": {
            "type": "string"
          },
          "username": {
            "type": "string"
          },
          "password": {
            "type": "string"
          },
          "createdAt": {
            "type": "string",
            "format": "date-time"
          },
          "updatedAt": {
            "type": "string",
            "format": "date-time"
          }
        }
      }
    }
  }
}

Generate service:

/* tslint:disable */
import { Injectable } from '@angular/core';
import { HttpClient, HttpResponse } from '@angular/common/http';
import { BaseService } from '../base-service';
import { ApiConfiguration } from '../api-configuration';
import { StrictHttpResponse } from '../strict-http-response';
import { RequestBuilder } from '../request-builder';
import { Observable } from 'rxjs';
import { map, filter } from 'rxjs/operators';

@Injectable({
  providedIn: 'root',
})
export class LoginControllerService extends BaseService {
  constructor(
    config: ApiConfiguration,
    http: HttpClient
  ) {
    super(config, http);
  }

  /**
   * Path part for operation LoginController.login
   */
  static readonly LoginController.loginPath = '/login';

  /**
   * This method provides access to the full `HttpResponse`, allowing access to response headers.
   * To access only the response body, use `LoginController.login()` instead.
   *
   * This method sends `application/json` and handles response body of type `application/json`
   */
  LoginController.login$Response(params: {

  /**
   * The input of login function
   */
  body: { 'username': string, 'password': string }
  }): Observable<StrictHttpResponse<{ 'token': string }>> {

    const rb = new RequestBuilder(this.rootUrl, LoginControllerService.LoginController.loginPath, 'post');
    if (params) {

      rb.body(params.body, 'application/json');
    }
    return this.http.request(rb.build({
      responseType: 'json',
      accept: 'application/json'
    })).pipe(
      filter((r: any) => r instanceof HttpResponse),
      map((r: HttpResponse<any>) => {
        return r as StrictHttpResponse<{ 'token': string }>;
      })
    );
  }

  /**
   * This method provides access to only to the response body.
   * To access the full response (for headers, for example), `LoginController.login$Response()` instead.
   *
   * This method sends `application/json` and handles response body of type `application/json`
   */
  LoginController.login(params: {

  /**
   * The input of login function
   */
  body: { 'username': string, 'password': string }
  }): Observable<{ 'token': string }> {

    return this.LoginController.login$Response(params).pipe(
      map((r: StrictHttpResponse<{ 'token': string }>) => r.body as { 'token': string })
    );
  }

}

Get error:

src/app/api/services/login-controller.service.ts:26:34 - error TS1005: ';' expected.
26   static readonly LoginController.loginPath = '/login';

src/app/api/services/login-controller.service.ts:34:18 - error TS1005: ';' expected.                                                                                                                                                          
34   LoginController.login$Response(params: {
src/app/api/services/login-controller.service.ts:66:18 - error TS1005: ';' expected.                                                                                                                                                          
66   LoginController.login(params: {
luisfpg commented 5 years ago

The bug is because "operationId" is not a valid identifier. Will fix in the next release.