Skip to content

Commit

Permalink
fix(typescript): alias imported types in (de)serialization schemas
Browse files Browse the repository at this point in the history
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<serializers.SecurityFinding.Raw, Engine.SecurityFinding> = 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<serializers.SecurityFinding.Raw, Engine.SecurityFinding> = 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.
  • Loading branch information
mstade committed Nov 3, 2024
1 parent 105d6da commit 91a8dae
Showing 1 changed file with 2 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 });
Expand Down

0 comments on commit 91a8dae

Please sign in to comment.