From 91a8dae85db2996bcb190c73462d17588a913a3a Mon Sep 17 00:00:00 2001 From: Marcus Stade Date: Sun, 3 Nov 2024 23:16:27 +0100 Subject: [PATCH] fix(typescript): alias imported types in (de)serialization schemas This fixes an issue where a type imported in a (de)serialization schema has the same name as the exported schema, causing build errors. Without this fix, we'd have schemas generated looking like so: ```ts /** * This file was auto-generated by Fern from our API Definition. */ import * as serializers from "../../../index"; import * as Engine from "../../../../api/index"; import * as core from "../../../../core"; import { SecurityFinding } from "../../ocsf/resources/v110/resources/securityfinding/resources/classes/types/SecurityFinding"; export const SecurityFinding: core.serialization.ObjectSchema = SecurityFinding; export declare namespace SecurityFinding { type Raw = SecurityFinding.Raw; } ``` The imported `SecurityFinding` type shares the same name as the serialization schema, which then causes build errors that are hard and awkward to fix in post processing of the generated source code. After this fix, the generated code looks like so: ```ts /** * This file was auto-generated by Fern from our API Definition. */ import * as serializers from "../../../index"; import * as Engine from "../../../../api/index"; import * as core from "../../../../core"; import { SecurityFinding as SecurityFindingType } from "../../ocsf/resources/v110/resources/securityfinding/resources/classes/types/SecurityFinding"; export const SecurityFinding: core.serialization.ObjectSchema = SecurityFindingType; export declare namespace SecurityFinding { type Raw = SecurityFindingType.Raw; } ``` The added `Type` suffix ensures the names do not conflict, solving any build issues that would otherwise arise. --- .../src/contexts/type-schema/TypeSchemaContextImpl.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/generators/typescript/sdk/generator/src/contexts/type-schema/TypeSchemaContextImpl.ts b/generators/typescript/sdk/generator/src/contexts/type-schema/TypeSchemaContextImpl.ts index 8222e014e66..4974f87b6b0 100644 --- a/generators/typescript/sdk/generator/src/contexts/type-schema/TypeSchemaContextImpl.ts +++ b/generators/typescript/sdk/generator/src/contexts/type-schema/TypeSchemaContextImpl.ts @@ -146,7 +146,7 @@ export class TypeSchemaContextImpl implements TypeSchemaContext { useDynamicImport: false, namespaceImport: "serializers" } - : { type: "direct" }, + : { type: "direct", alias: `${typeName.name.originalName}Type` }, // TODO this should not be hardcoded here subImport: ["Raw"], importsManager: this.importsManager, @@ -175,7 +175,7 @@ export class TypeSchemaContextImpl implements TypeSchemaContext { return { type: "fromRoot", useDynamicImport: false, namespaceImport: "serializers" }; } else if (isGeneratingSchema) { // Return default import strategy or another strategy based on your logic - return { type: "direct" }; + return { type: "direct", alias: `${typeName.name.originalName}Type` }; } else { // We don't really know when or if this case is actually used return getSchemaImportStrategy({ useDynamicImport: false });