openapi-typescript-codegen icon indicating copy to clipboard operation
openapi-typescript-codegen copied to clipboard

Default values of 0 are not generated

Open seriouslag opened this issue 2 years ago • 2 comments

If a default value of 0 is specified then it is not generated. I traced it to this line but I do not yet know .hbs if someone wants to take a stab at correcting this.

A workaround is provided below.

Lines I believe with logic error: https://github.com/ferdikoomen/openapi-typescript-codegen/blob/a0807fa78d7c19ae90985e63c32caaf1e04f2581/src/templates/partials/parameters.hbs#L5

and

https://github.com/ferdikoomen/openapi-typescript-codegen/blob/a0807fa78d7c19ae90985e63c32caaf1e04f2581/src/templates/partials/parameters.hbs#L25

if !default then there is no default value. This is incorrect as 0 should be able to be a default value. I could argue that null could be a default value as well but beyond the scope here I think.

example:

limit:
  name: limit
  in: query
  required: false
  schema:
    type: integer
    format: int32
    default: 25
page:
  name: page
  in: query
  required: false
  schema:
    type: integer
    format: int32
    default: 0
  • Generates *
    public getMyStuff({
        page,
        limit = 25,
    }: {
    ...
    }

'limit' would be generated with a default value of 25 but 'page' would not have a default value.

the workaround would look like this

limit:
  name: limit
  in: query
  required: false
  schema:
    type: integer
    format: int32
    default: '25'
page:
  name: page
  in: query
  required: false
  schema:
    type: integer
    format: int32
    default: '0'
  • Generates *
    public getMyStuff({
        page = 0,
        limit = 25,
    }: {
    ...
    }

seriouslag avatar May 23 '23 15:05 seriouslag

This is fixed in our fork of the repository @hey-api/openapi-ts

jordanshatford avatar Mar 31 '24 01:03 jordanshatford

This is fixed in our fork of the repository @hey-api/openapi-ts

Funny enough I have a PR open moving to your package, https://github.com/7nohe/openapi-react-query-codegen/pull/50

seriouslag avatar Mar 31 '24 04:03 seriouslag