kubb-labs / kubb

OpenAPI to TypeScript, React-Query, Zod, Zodios, Faker.js, MSW and Axios.
https://kubb.dev
MIT License
568 stars 40 forks source link

Const types in union are declared as generic string type when specific variants are in the type definition #1060

Closed Fearghal-Des closed 1 week ago

Fearghal-Des commented 2 weeks ago

What version of kubb is running?

2.18.20

What platform is your computer?

Windows

What version of external packages are you using(@tanstack-query, MSW, React, Vue, ...)

"@tanstack/react-query": "^5.49.2", "react": "^18.3.1",

What steps can reproduce the bug?

Specify an open api specification with a union of const types. Here is an example:

"kind": {
                      "default": "open",
                      "anyOf": [
                        {
                          "const": "open",
                          "type": "string"
                        },
                        {
                          "const": "invite_only",
                          "type": "string"
                        },
                        {
                          "const": "request_to_join",
                          "type": "string"
                        },
                        {
                          "const": "invite_only_hidden",
                          "type": "string"
                        }
                      ]
                    },

After running kubb generate the type definition is outputted like:

kind : (string | string | string | string)

The code was ran with the following kubb config:

import { defineConfig } from '@kubb/core'
import { pluginOas } from '@kubb/plugin-oas'
import { pluginTanstackQuery } from '@kubb/swagger-tanstack-query'
import { pluginTs } from '@kubb/swagger-ts'
import { pluginZod } from '@kubb/swagger-zod'

export default defineConfig({
  input: {
    path: './documentation.json',
  },
  output: {
    path: './src/lib',
  },
  plugins: [
    pluginTs(),
    pluginOas(),
    pluginZod(),
    pluginTanstackQuery({
      output: {
        path: './hooks',
      },
      group: {
        type: 'tag',
        output: './hooks/{{tag}}Hooks'
      },
      framework: 'react',
      dataReturnType: 'full',
      mutate: {
        variablesType: 'hook',
        methods: [ 'post', 'put', 'delete' ],
      },
      infinite: {
        queryParam: 'next_page',
        initialPageParam: 0,
        cursorParam: 'nextCursor',
      },
      query: {
        methods: [ 'get' ],
        importPath: "@tanstack/react-query"
      },
      suspense: {},
      parser: 'zod',
    }),
  ],
})

How often does this bug happen?

Every time

What is the expected behavior?

Expected behaviour is for the type of kind to be declared like:

kind : "open" | "invite_only" | "request_to_join" | "invite_only_hidden"

Swagger/OpenAPI file?

An example of a relevant section of the OpenAPI file is here:

"kind": {
  "default": "open",
  "anyOf": [
    {
      "const": "open",
      "type": "string"
    },
    {
      "const": "invite_only",
      "type": "string"
    },
    {
      "const": "request_to_join",
      "type": "string"
    },
    {
      "const": "invite_only_hidden",
      "type": "string"
    }
  ]
},

Additional information

The OpenAPI specification was auto generated using the ElysiaJS Swagger plugin.

stijnvanhulle commented 2 weeks ago

Be aware that const is only a feature in OpenAPI 3.1.x so if your version is not set to 3.1.0 or higher it will just return 'string'.