Skip to content

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

Open
@darkbasic

Description

@darkbasic

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingopenapi-tsRelevant to the openapi-typescript library

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions