Skip to content

Commit

Permalink
fix(#132): wip
Browse files Browse the repository at this point in the history
  • Loading branch information
astahmer committed May 2, 2023
1 parent 3933722 commit 40f6958
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lib/src/openApiToTypescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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<SchemaObject["type"], any[] | undefined>;
Expand Down

0 comments on commit 40f6958

Please sign in to comment.