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

--enum-values tries to access optional prop child props in order to define the array type #2138

Open
1 of 2 tasks
darkbasic opened this issue Feb 5, 2025 · 1 comment · May be fixed by #2139
Open
1 of 2 tasks

--enum-values tries to access optional prop child props in order to define the array type #2138

darkbasic opened this issue Feb 5, 2025 · 1 comment · May be fixed by #2139
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library

Comments

@darkbasic
Copy link

darkbasic commented Feb 5, 2025

openapi-typescript version

7.6.1

Node.js version

v20.17.0

OS + version

Arch Linux

Description

Image

Image

export interface operations {
    "mapping.configurations.index": {
        parameters: {
            query?: {
                sort_key?: "created_at" | "updated_at" | null;
            };
            header?: never;
            path?: never;
            cookie?: never;
        };
        requestBody?: never;
    };
}

type ReadonlyArray<T> = [
    Exclude<T, undefined>
] extends [
    any[]
] ? Readonly<Exclude<T, undefined>> : Readonly<Exclude<T, undefined>[]>;

export const pathsMappingConfigurationsGetParametersQuerySort_keyValues: ReadonlyArray<paths["/mapping/configurations"]["get"]["parameters"]["query"]["sort_key"]> = ["created_at", "updated_at"];

Since query is optional you won't be able to access sort_key on paths["/mapping/configurations"]["get"]["parameters"]["query"] (Property 'sort_key' does not exist on type).

Reproduction

{
    "openapi": "3.1.0",
    "info": {
        "title": "API Portal",
        "version": "1.0.0",
        "description": "This is the **mapping API** description"
    },
    "paths": {
        "\/mapping\/configurations": {
            "get": {
                "operationId": "mapping.configurations.index",
                "summary": "Display a listing of the resource",
                "tags": [
                    "MappingConfiguration"
                ],
                "parameters": [
                    {
                        "name": "sort_key",
                        "in": "query",
                        "schema": {
                            "type": [
                                "string",
                                "null"
                            ],
                            "enum": [
                                "created_at",
                                "updated_at"
                            ]
                        }
                    }
                ],
                "responses": {}
            },
            "post": {}
        },

Expected result

We could easily brute force our way into the array type using DeepRequired:

type DeepRequired<T> = {
    [K in keyof T]: Required<DeepRequired<T[K]>>
  }

export const pathsMappingConfigurationsGetParametersQuerySort_keyValues: ReadonlyArray<DeepRequired<paths>["/mapping/configurations"]["get"]["parameters"]["query"]["sort_key"]> = ["created_at", "updated_at"];

I guess we could modify oapiRef() to prefix the base type with DeepRequired<> and use stringToAST to inject the helper type to the footer.
I would open open a PR but I'm not familiar enough with the Typescript compiler API to do so quickly, so unless no one else volunteers to do so I would gladly pass.

Required

  • My OpenAPI schema is valid and passes the Redocly validator (npx @redocly/cli@latest lint)

Extra

@darkbasic
Copy link
Author

I gave it a try anyway: #2139

I've also found another issue with enum-values: I will file a separate bug report.

darkbasic added a commit to darkbasic/openapi-typescript that referenced this issue Feb 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant