From 40f695825f2e593f008547511f377066d1d5b576 Mon Sep 17 00:00:00 2001 From: Alexandre Stahmer Date: Tue, 2 May 2023 15:24:45 +0200 Subject: [PATCH] fix(#132): wip --- lib/src/openApiToTypescript.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/src/openApiToTypescript.ts b/lib/src/openApiToTypescript.ts index 35b08ba5..15e1997d 100644 --- a/lib/src/openApiToTypescript.ts +++ b/lib/src/openApiToTypescript.ts @@ -25,8 +25,9 @@ export const getTypescriptFromOpenApi = ({ ctx, }: // eslint-disable-next-line sonarjs/cognitive-complexity TsConversionArgs): ts.Node | TypeDefinitionObject | string => { + let name = inheritedMeta?.name; const meta = {} as TsConversionArgs["meta"]; - const isInline = !inheritedMeta?.name; + const isInline = !name; if (ctx?.visitedsRefs && inheritedMeta?.$ref) { ctx.rootRef = inheritedMeta.$ref; @@ -203,14 +204,14 @@ TsConversionArgs): ts.Node | TypeDefinitionObject | string => { return isPartial ? t.reference("Partial", [objectType]) : objectType; } - if (!inheritedMeta?.name) { + if (!name) { throw new Error("Name is required to convert an object schema to a type reference"); } - const base = t.type(inheritedMeta.name, objectType); + const base = t.type(name, objectType); if (!isPartial) return base; - return t.type(inheritedMeta.name, t.reference("Partial", [objectType])); + return t.type(name, t.reference("Partial", [objectType])); } if (!schemaType) return t.unknown(); @@ -220,9 +221,12 @@ TsConversionArgs): ts.Node | TypeDefinitionObject | string => { }; const tsResult = getTs(); - return canBeWrapped - ? wrapTypeIfInline({ isInline, name: inheritedMeta?.name, typeDef: tsResult as TypeDefinition }) - : tsResult; + if (typeof tsResult === "string") { + canBeWrapped = true; + name = tsResult; + } + + return canBeWrapped ? wrapTypeIfInline({ isInline: !name, name, typeDef: tsResult as TypeDefinition }) : tsResult; }; type SingleType = Exclude;