Skip to content

Commit

Permalink
fix(enum-values): access optional prop child props
Browse files Browse the repository at this point in the history
  • Loading branch information
darkbasic committed Feb 5, 2025
1 parent 82e98b4 commit b11ceea
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
14 changes: 12 additions & 2 deletions packages/openapi-typescript/src/lib/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ export function addJSDocComment(schemaObject: AnnotatedSchemaObject, node: ts.Pr
}

/** Convert OpenAPI ref into TS indexed access node (ex: `components["schemas"]["Foo"]`) */
export function oapiRef(path: string): ts.TypeNode {
export function oapiRef(path: string, deepRequired = false): ts.TypeNode {
const { pointer } = parseRef(path);
if (pointer.length === 0) {
throw new Error(`Error parsing $ref: ${path}. Is this a valid $ref?`);
}
let t: ts.TypeReferenceNode | ts.IndexedAccessTypeNode = ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier(String(pointer[0])),
ts.factory.createIdentifier(deepRequired ? `DeepRequired<${String(pointer[0])}>` : String(pointer[0])),
);
if (pointer.length > 1) {
for (let i = 1; i < pointer.length; i++) {
Expand Down Expand Up @@ -251,6 +251,16 @@ export function tsArrayLiteralExpression(
let variableName = sanitizeMemberName(name);
variableName = `${variableName[0].toLowerCase()}${variableName.substring(1)}`;

if (
options?.injectFooter &&
!options.injectFooter.some((node) => ts.isTypeAliasDeclaration(node) && node?.name?.escapedText === "DeepRequired")
) {
const helper = stringToAST(
"type DeepRequired<T> = { [K in keyof T]: Required<DeepRequired<T[K]>> };",
)[0] as any;
options.injectFooter.push(helper);
}

const arrayType = options?.readonly
? tsReadonlyArray(elementType, options.injectFooter)
: ts.factory.createArrayTypeNode(elementType);
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-typescript/src/transform/schema-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export function transformSchemaObjectWithComposition(

const enumValuesArray = tsArrayLiteralExpression(
enumValuesVariableName,
oapiRef(options.path ?? ""),
oapiRef(options.path ?? "", true),
schemaObject.enum as (string | number)[],
{
export: true,
Expand Down

0 comments on commit b11ceea

Please sign in to comment.