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
2.81k stars 313 forks source link

Orval generates some enums that are used before they are declared #1511

Open markosandalj opened 2 months ago

markosandalj commented 2 months ago

What are the steps to reproduce this issue?

  1. Open codesandbox link here: https://codesandbox.io/p/devbox/slgr4l
  2. run pnpm run orval in terminal or start development from terminal GUI in

I already run that command so you can also look at the generated TS file. I added both "stripped" version of schema yaml and full one. Currently stripped one is used for generation and should be enough but if needed orval.config can be switched to use full version.

Here is a google drive link for schema files: https://drive.google.com/drive/folders/1CtwFX2peUVhFRpPqgDjZMqNY-5FS30b2?usp=sharing

What happens?

Backend is written in Django and Django rest framework so we use drf spectacular for generating open api schema. What happens is, Orval correctly generates all the types but for some reason few enums are used before they are generated. For example on line 1540: export const SubjectRequestIcon = { ...SubjectIconEnum, ...BlankEnum, ...NullEnum } as const; , both BlankEnum and NullEnum are used but they are declared much further in the file (line ~8449 and ~5053). These two are most prevalent since they are used on every model that uses Django's choice filed and can be null in database. There are two more enums that are used before they are declared if it helps: ProductIconEnum and LevelEnum.

What were you expecting to happen?

That enumas that are used to construct other enums are generated before them so we don't get TS errors.

Any logs, error output, etc?

There is no logs, errors or anything in CLI - everything runs fine but the generated file has typescript errors:

Any other comments?

I attached schema.yaml if it helps and here is orval output config:

output: {
    target: './src/features/api/orval/',
    indexFiles: true,
    client: 'fetch',
    mode: 'tags-split',
    clean: true,
    prettier: true,
    baseUrl: 'http://0.0.0.0:8081',
    urlEncodeParameters: true,
    headers: true,
    override: {
        useNamedParameters: true,
        suppressReadonlyModifier: true,
        useTypeOverInterfaces: true,
        contentType: {
            include: [
                'application/json',
                'text/plain',
                'application/x-www-form-urlencoded',
                'multipart/form-data',
            ],
        },
    },
},

What versions are you using?

Operating System: MacOS Package Version: 6.31.0 Browser Version: Newest Chrome

melloware commented 2 months ago

Do you have an OpenAPI JSON file to go with it?

markosandalj commented 2 months ago

Here is a link to google drive since Github doesn't allow uploading .yaml files https://drive.google.com/drive/folders/1CtwFX2peUVhFRpPqgDjZMqNY-5FS30b2?usp=sharing

I added it to description aswell.

melloware commented 2 weeks ago

This looks fetch client related right?

soartec-lab commented 1 week ago

I don't think so, since creating a schemas containing an enum is a different process than creating a client

soartec-lab commented 1 week ago

I don't have time to deal with this issue yet, so I'll remove the assignment to me.

rob-long commented 4 days ago

Also seeing a few of these typescript errors in my generated schema. Any suggested ways to workaround it?

Update: I am working around it by re-ordering my Open API 3.0 schemas in the json

melloware commented 4 days ago

Another thought was use "mode: split" which puts all models in another file?