Swagger-codegen: [typescript-angular] property of number type incorrectly typed as BigDecimal

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

Description

When a schema as some properties of number type, they are typed as BigDecimal instead of number in the exported model

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

The generated model is

import { BigDecimal } from './bigDecimal';

export interface Response { 
    propA?: BigDecimal;
    propB?: BigDecimal;
}

And it is invalid (bigDecimal is not defined). The generated model should be:

export interface Response { 
    propA?: number;
    propB?: number;
}

Most helpful comment

fixed in swagger-api/swagger-codegen-generators/pull/446

All 4 comments

I have not tested it yet, but I believe the fix for this would be to add
typeMapping.put("BigDecimal", "number");

in the file

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractTypeScriptClientCodegen.java

where it says:

        typeMapping = new HashMap<String, String>();
        typeMapping.put("Array", "Array");
        typeMapping.put("array", "Array");
        typeMapping.put("List", "Array");
        typeMapping.put("boolean", "boolean");
        typeMapping.put("string", "string");

Until a fix is officially released, a quick workaround could be to create a bigDecimal.ts file containing the type definition as alias of number:

export type BigDecimal = number;

Obviously you have to add the file each time you generate the code... annoying but working.

fixed in swagger-api/swagger-codegen-generators/pull/446

Same happened for golang server generation

Was this page helpful?
0 / 5 - 0 ratings