Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: generate literal types for booleans and numbers #1512

Merged
merged 1 commit into from
Jul 18, 2024
Merged

feat: generate literal types for booleans and numbers #1512

merged 1 commit into from
Jul 18, 2024

Conversation

RinseV
Copy link
Contributor

@RinseV RinseV commented Jul 9, 2024

Status

READY

Description

Allows for the generation of literal types for booleans and numbers. Fixes #1509.

Todos

  • Tests
  • Documentation
  • Changelog Entry (unreleased)

Steps to Test or Reproduce

Outline the steps to test or reproduce the PR here.

> git clone https://github.com/RinseV/orval.git
  1. Install dependencies & build the package
  2. Generate the default specification in the tests folder: yarn generate:default
  3. The generated models will have the correct literal types

Notes

I didn't want to break the current functionality from #1326 where not specifying a type will generate it as a string literal. So, having only const without a type will still generate as a string. The same is true for a type of string. But if the type is either number, integer or boolean, the type will be generated as the literal value of const. This can result in invalid code (like specifying a property with type: number and const: value which will generate as property: value).

Comparison

Schema:

components:
  schemas:
    StringConst:
      type: object
      required:
        - value
        - valueWithoutType
        - valueNullable
      properties:
        value:
          type: string
          const: string
        valueWithoutType:
          const: string
        valueNullable:
          type:
            - string
            - 'null'
          const: string

Before

export type StringConst = {
  value: string;
  valueWithoutType: "string";
  valueNullable: "string" | null;
};

Now

export type StringConst = {
  value: "string";
  valueWithoutType: "string";
  valueNullable: "string" | null;
};

@RinseV RinseV marked this pull request as ready for review July 9, 2024 18:05
@melloware melloware added the enhancement New feature or request label Jul 9, 2024
@melloware melloware added this to the 6.32.0 milestone Jul 9, 2024
@melloware melloware requested a review from anymaniax July 9, 2024 18:34
@melloware melloware merged commit e30c7fc into orval-labs:master Jul 18, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

OpenAPI: Support for const boolean & integer values
2 participants