-
-
Notifications
You must be signed in to change notification settings - Fork 614
Closed as duplicate of#2138
Labels
bugSomething isn't workingSomething isn't workingopenapi-tsRelevant to the openapi-typescript libraryRelevant to the openapi-typescript library
Description
openapi-typescript version
7.10.1
Node.js version
24.13.0
OS + version
macOS 26.2
Description
When an OpenAPI operation has all optional query parameters, the generated TypeScript types mark the query object itself as optional (query?:). However, the generated enum value constants don't account for this and try to index directly into the potentially-undefined query object.
Therefore, TypeScript complains:
output.ts:64:116 - error TS2339: Property 'status' does not exist on type '{ status?: "pending" | "active" | "completed"; } | undefined'.
export const pathsItemsGetParametersQueryStatusValues: ReadonlyArray<paths["/items"]["get"]["parameters"]["query"]["status"]> = ["pending", "active", "completed"];
Reproduction
Reproduction repository here: https://github.com/frankie567/openapi-ts-enum-repro-bug
- Given this OpenAPI schema:
openapi: 3.1.0
info:
title: Minimal Reproduction
version: 1.0.0
license:
name: MIT
identifier: MIT
servers:
- url: https://api.example.org
description: API server
security: []
paths:
/items:
get:
operationId: listItems
summary: List items with optional filters
security: []
parameters:
- name: status
in: query
required: false
schema:
type: string
enum:
- pending
- active
- completed
responses:
"200":
description: Success
content:
application/json:
schema:
type: array
items:
type: object
"400":
description: Bad Request- Generate types with
openapi-typescript:
openapi-typescript ./input.yml --enum-values -o ./output.ts- Run TypeScript on the output:
tsc --noEmitExpected result
The generator should wrap the query accessor with NonNullable:
export const pathsItemsGetParametersQueryStatusValues: ReadonlyArray<
NonNullable<paths["/items"]["get"]["parameters"]["query"]>["status"]
> = ["pending", "active", "completed"];Allowing TypeScript to pass.
Required
- My OpenAPI schema is valid and passes the Redocly validator (
npx @redocly/cli@latest lint)
Extra
- I’m willing to open a PR (see CONTRIBUTING.md)
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't workingopenapi-tsRelevant to the openapi-typescript libraryRelevant to the openapi-typescript library