swagger-api / swagger-codegen-generators

Apache License 2.0
284 stars 424 forks source link

[typescript-angular] "type": "number" references BigDecimal, which is never generated #336

Closed wtho closed 4 years ago

wtho commented 5 years ago

Issue

When generating (v3.0.5) using

java -jar \
  -DsupportsES6=true \
  swagger-codegen.jar generate \
  -l typescript-angular \
  -i schema.json \
  -o src/app/generated/

and the schema (swagger: 2.0) contains a field

{
  "someField": {
    "type": "number"
  }
}

it generates this model:

import { BigDecimal } from './bigDecimal';

export interface AbonnementTypVO { 
  someField: BigDecimal;
}

The problem is, that the file bigDecimal.ts is never created.

If the schema contains

{
  "someOtherField": {
    "type" : "integer",
    "format" : "int64"
  }
}

it renders someOtherField: number;

wtho commented 5 years ago

The current workaround from my side was to create bigDecimal.ts with

export type BigDecimal = number;

which is quite annoying to do every time.

wtho commented 5 years ago

So in JS/TS there are no native solutions for arbitrary-precision decimal arithmetic, but there are e. g. these libraries to achieve it:

(At least for big.js) There are also corresponding typings in DefinitelyTyped/types.

If you don't like to include any of them or implement BigDecimal in JS/TS on swagger's side, it does not make much sense to use some sort of BigDecimal in JS/TS. In that case number should work just fine.

Are there swagger guidelines on type projections to other languages?

If there is a consent how to handle this and someone points me on the right code locations I'm happy to create a PR. With the current setup and the missing BigDecimal this generator is not usable.

HugoMario commented 4 years ago

hi @wtho, this issue has been fixed, for latest releases, mapping BigDecimal to number. going to close issue, but please let me know if you still get the issue.

itbrandonsilva commented 4 years ago

I've run into this issue myself. A generated model attempts to import BigDecimal in a way similar to @wtho initial example does, but the file is missing. In my case, I'm using the generic javascript generator, not typescript-angular, so let me know if it's preferred for me to create a new issue.

Using the latest build available to me at the moment: https://oss.sonatype.org/content/repositories/snapshots/io/swagger/codegen/v3/swagger-codegen-cli/3.0.17-SNAPSHOT/swagger-codegen-cli-3.0.17-20200123.095202-4-javadoc.jar.

Edit: I'm fairly new to these tools, and I've realized that this isn't as issue for the swagger-codegen repo, so my report may be misplaced. I can see here that the type mappings defined in this lib don't correlate with my experience.

AmazingTurtle commented 4 years ago

@HugoMario is there any difference between swagger-codegen-cli and swagger-codegen-cli-v3? I tried latest, 3.0.18 and unstable on swagger-codegen-cli-v3 and BigDecimal is still poppin' up. Also I am using the javascript generator.

More specifically when I'm using swagger-codegen-cli:latest instead, I get this one missing swagger input or config! edit: I get the above error because swagger-codegen-cli uses v2 by default and that doesn't support OpenAPI v3 (which I am using)

HugoMario commented 4 years ago

@itbrandonsilva @AmazingTurtle going to check this issue, thanks for letting me know

HugoMario commented 4 years ago

hey @itbrandonsilva @AmazingTurtle

Can you please try with 3.0.20 version

AmazingTurtle commented 4 years ago

@HugoMario I can confirm, BigDecimal is gone by now with the 3.0.20 version. Thanks for the fix