orval-labs / orval

orval is able to generate client with appropriate type-signatures (TypeScript) from any valid OpenAPI v3 or Swagger v2 specification, either in yaml or json formats. 🍺
https://orval.dev
MIT License
3.21k stars 337 forks source link

Cannot create constant for `false` #1647

Closed RinseV closed 1 month ago

RinseV commented 1 month ago

What are the steps to reproduce this issue?

  1. Create an OpenAPI 3.1 schema with const: false in a property
  2. Generate Orval code

Example schema:

openapi: 3.1.0
info:
  version: 1.0.0
  title: Example
  license:
    name: MIT
    identifier: MIT
servers:
  - url: http://api.example.xyz/v1
paths:
  /somePath:
    get:
      operationId: getSomePath
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema: {}
components:
  schemas:
    BooleanConstFalse:
      type: object
      required:
        - value
      properties:
        value:
          type: boolean
          const: false

What happens?

The generated code will produce the field with type: boolean instead of type: false.

Generated code from above sample:

export type BooleanConstFalse = {
  value: boolean;
};

What were you expecting to happen?

The type should be generated with type: false.

Expected code from above sample:

export type BooleanConstFalse = {
  value: false;
};

Any other comments?

Problem is here, if const is false, then this if statement will evaluate to false and thus not set the value. We just need to change the check to if (itemWithConst.const !== undefined). The same probably happens for integers with const: 0.

What versions are you using?

System:
    OS: macOS 15.0
    CPU: (8) arm64 Apple M3
    Memory: 86.31 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  npmPackages:
    @tanstack/react-query: ^5.56.2 => 5.56.2
    orval: 7.1.1 => 7.1.1
    react: ^18.3.1 => 18.3.1
melloware commented 1 month ago

PR is welcome @RinseV !