feat(openapi-v3): Preserve JSON Schema const values as TypeScript literal types#332
Open
sandovalrr wants to merge 1 commit intofabien0102:mainfrom
Open
feat(openapi-v3): Preserve JSON Schema const values as TypeScript literal types#332sandovalrr wants to merge 1 commit intofabien0102:mainfrom
sandovalrr wants to merge 1 commit intofabien0102:mainfrom
Conversation
- Introduced `SchemaObjectWithConst` type to support const values in schemas. - Added tests to verify generation of literal types for string, number, boolean, and null const values. - Updated `getType` and `getConstType` functions to handle const values correctly. - Ensured const literals are preserved in adjacent schemas during type generation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR preserves JSON Schema / OpenAPI
constconstraints as TypeScript literal types in generated output.Before this change, schemas such as
{ type: "string", const: "READY" }were widened tostringbecause the shared schema emission path did not recognizeconst. As a result, generated unions and discriminator-like object branches lost useful literal information.With this change, supported scalar
constvalues are emitted as their corresponding TypeScript literal types:"VALUE"123true/falsenullThis applies consistently anywhere schemas are emitted through the existing generator flow, including nested properties and composed schemas.
Root Cause
The shared generator logic in
schemaToTypeAliasDeclaration.getType()handled:$refoneOf/anyOf/allOfenumbut it never handled
const.That meant a schema with
constfell through to the normal primitive branches and was widened tostring,number, orbooleaninstead of preserving the literal value.Implementation
The fix adds explicit scalar-
consthandling in the generator’s shared schema-to-TypeScript emission path.Key points:
constis handled centrally inschemaToTypeAliasDeclaration.tsnullable: truecontinues to work for non-nullconstvaluesconstin branchesenum,$ref, and non-constbehavior is unchangedThis keeps the change small and aligned with the current architecture.
Tests
Added focused coverage for:
constconstoneOf/anyOfbranches withconstallOfwith compatibleconstoneOfbranches withconstdiscriminator-like properties and mixed value typesconstemits literal types while adjacent non-constschemas still generate as beforeScope / Tradeoffs
This PR intentionally supports scalar
constvalues only. Object and arrayconstvalues are left out of scope to keep the change narrow, maintainable, and reviewer-friendly.