Skip to content

Commit

Permalink
fix(typescript): Use serialization layer to convert types to JSON str…
Browse files Browse the repository at this point in the history
…ings when enabled.

fix(typescript): Use serialization layer to convert types to JSON strings when enabled.
  • Loading branch information
Swimburger authored Jan 14, 2025
1 parent 3fd8599 commit 9cdb96c
Show file tree
Hide file tree
Showing 316 changed files with 79,722 additions and 722 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ export class DynamicSnippetsTestRunner {
headers: undefined,
requestBody: {
file: "Hello, world!",
fileList: ["First", "Second"]
file_list: ["First", "Second"]
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,9 @@ export class ExpressGenerator {
includeSerdeLayer: this.config.includeSerdeLayer,
retainOriginalCasing: this.config.retainOriginalCasing,
useBigInt: this.config.useBigInt,
enableInlineTypes: false
enableInlineTypes: false,
allowExtraFields: this.config.allowExtraFields,
omitUndefined: false
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ export declare namespace ExpressContextImpl {
retainOriginalCasing: boolean;
useBigInt: boolean;
enableInlineTypes: boolean;
allowExtraFields: boolean;
omitUndefined: boolean;
}
}

Expand Down Expand Up @@ -141,6 +143,8 @@ export class ExpressContextImpl implements ExpressContext {
includeSerdeLayer,
retainOriginalCasing,
enableInlineTypes,
allowExtraFields,
omitUndefined,
useBigInt
}: ExpressContextImpl.Init) {
this.logger = logger;
Expand Down Expand Up @@ -168,13 +172,15 @@ export class ExpressContextImpl implements ExpressContext {
retainOriginalCasing,
useBigInt,
enableInlineTypes,
allowExtraFields,
omitUndefined,
context: this
});
this.typeSchema = new TypeSchemaContextImpl({
sourceFile,
coreUtilities: this.coreUtilities,
importsManager,
typeResolver,
context: this,
typeSchemaDeclarationReferencer,
typeDeclarationReferencer,
typeGenerator,
Expand All @@ -183,7 +189,9 @@ export class ExpressContextImpl implements ExpressContext {
includeSerdeLayer,
retainOriginalCasing,
useBigInt,
enableInlineTypes
enableInlineTypes,
allowExtraFields,
omitUndefined
});

this.expressInlinedRequestBody = new ExpressInlinedRequestBodyContextImpl({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ImportsManager, Reference, TypeReferenceNode, Zurg } from "@fern-typescript/commons";
import { CoreUtilities } from "@fern-typescript/commons/src/core-utilities/CoreUtilities";
import { GeneratedTypeSchema, TypeSchemaContext } from "@fern-typescript/contexts";
import { BaseContext, GeneratedTypeSchema, TypeSchemaContext } from "@fern-typescript/contexts";
import { TypeResolver } from "@fern-typescript/resolvers";
import { TypeGenerator } from "@fern-typescript/type-generator";
import {
Expand All @@ -20,7 +20,7 @@ export declare namespace TypeSchemaContextImpl {
sourceFile: SourceFile;
coreUtilities: CoreUtilities;
importsManager: ImportsManager;
typeResolver: TypeResolver;
context: BaseContext;
typeDeclarationReferencer: TypeDeclarationReferencer;
typeSchemaDeclarationReferencer: TypeDeclarationReferencer;
typeGenerator: TypeGenerator;
Expand All @@ -30,6 +30,8 @@ export declare namespace TypeSchemaContextImpl {
retainOriginalCasing: boolean;
useBigInt: boolean;
enableInlineTypes: boolean;
allowExtraFields: boolean;
omitUndefined: boolean;
}
}

Expand All @@ -41,7 +43,7 @@ export class TypeSchemaContextImpl implements TypeSchemaContext {
private typeSchemaDeclarationReferencer: TypeDeclarationReferencer;
private typeReferenceToRawTypeNodeConverter: TypeReferenceToRawTypeNodeConverter;
private typeReferenceToSchemaConverter: TypeReferenceToSchemaConverter;
private typeResolver: TypeResolver;
private context: BaseContext;
private typeGenerator: TypeGenerator;
private typeSchemaGenerator: TypeSchemaGenerator;
private includeSerdeLayer: boolean;
Expand All @@ -51,7 +53,7 @@ export class TypeSchemaContextImpl implements TypeSchemaContext {
sourceFile,
coreUtilities,
importsManager,
typeResolver,
context,
typeDeclarationReferencer,
typeGenerator,
typeSchemaDeclarationReferencer,
Expand All @@ -60,40 +62,46 @@ export class TypeSchemaContextImpl implements TypeSchemaContext {
includeSerdeLayer,
retainOriginalCasing,
useBigInt,
enableInlineTypes
enableInlineTypes,
allowExtraFields,
omitUndefined
}: TypeSchemaContextImpl.Init) {
this.sourceFile = sourceFile;
this.coreUtilities = coreUtilities;
this.importsManager = importsManager;
this.typeReferenceToRawTypeNodeConverter = new TypeReferenceToRawTypeNodeConverter({
getReferenceToNamedType: (typeName) => this.getReferenceToRawNamedType(typeName).getEntityName(),
generateForInlineUnion: (typeName) => this.generateForInlineUnion(typeName),
typeResolver,
context,
treatUnknownAsAny,
includeSerdeLayer,
useBigInt,
enableInlineTypes
enableInlineTypes,
allowExtraFields,
omitUndefined
});
this.typeReferenceToSchemaConverter = new TypeReferenceToSchemaConverter({
getSchemaOfNamedType: (typeName) => this.getSchemaOfNamedType(typeName, { isGeneratingSchema: true }),
zurg: this.coreUtilities.zurg,
typeResolver,
context,
treatUnknownAsAny,
includeSerdeLayer,
useBigInt,
enableInlineTypes
enableInlineTypes,
allowExtraFields,
omitUndefined
});
this.typeDeclarationReferencer = typeDeclarationReferencer;
this.typeSchemaDeclarationReferencer = typeSchemaDeclarationReferencer;
this.typeResolver = typeResolver;
this.context = context;
this.typeGenerator = typeGenerator;
this.typeSchemaGenerator = typeSchemaGenerator;
this.includeSerdeLayer = includeSerdeLayer;
this.retainOriginalCasing = retainOriginalCasing;
}

public getGeneratedTypeSchema(typeName: DeclaredTypeName): GeneratedTypeSchema {
const typeDeclaration = this.typeResolver.getTypeDeclarationFromName(typeName);
const typeDeclaration = this.context.type.getTypeDeclaration(typeName);
const examples = typeDeclaration.userProvidedExamples;
if (examples.length === 0) {
examples.push(...typeDeclaration.autogeneratedExamples);
Expand Down Expand Up @@ -188,7 +196,7 @@ export class TypeSchemaContextImpl implements TypeSchemaContext {
}

private wrapSchemaWithLazy(schema: Zurg.Schema, typeName: DeclaredTypeName): Zurg.Schema {
const resolvedType = this.typeResolver.resolveTypeName(typeName);
const resolvedType = this.context.type.resolveTypeName(typeName);
return resolvedType.type === "named" && resolvedType.shape === ShapeType.Object
? this.coreUtilities.zurg.lazyObject(schema)
: this.coreUtilities.zurg.lazy(schema);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { ImportsManager, Reference, TypeReferenceNode } from "@fern-typescript/commons";
import { BaseContext, GeneratedType, GeneratedTypeReferenceExample, TypeContext } from "@fern-typescript/contexts";
import {
BaseContext,
GeneratedType,
GeneratedTypeReferenceExample,
TypeContext,
TypeSchemaContext
} from "@fern-typescript/contexts";
import { TypeResolver } from "@fern-typescript/resolvers";
import { TypeGenerator } from "@fern-typescript/type-generator";
import {
Expand All @@ -12,7 +18,6 @@ import { SourceFile, ts } from "ts-morph";
import {
DeclaredTypeName,
ExampleTypeReference,
ObjectProperty,
ResolvedTypeReference,
TypeDeclaration,
TypeReference
Expand All @@ -34,6 +39,8 @@ export declare namespace TypeContextImpl {
useBigInt: boolean;
context: BaseContext;
enableInlineTypes: boolean;
allowExtraFields: boolean;
omitUndefined: boolean;
}
}

Expand Down Expand Up @@ -62,6 +69,8 @@ export class TypeContextImpl implements TypeContext {
retainOriginalCasing,
useBigInt,
enableInlineTypes,
allowExtraFields,
omitUndefined,
context
}: TypeContextImpl.Init) {
this.sourceFile = sourceFile;
Expand All @@ -77,18 +86,22 @@ export class TypeContextImpl implements TypeContext {
this.typeReferenceToParsedTypeNodeConverter = new TypeReferenceToParsedTypeNodeConverter({
getReferenceToNamedType: (typeName) => this.getReferenceToNamedType(typeName).getEntityName(),
generateForInlineUnion: (typeName) => this.generateForInlineUnion(typeName),
typeResolver,
context,
treatUnknownAsAny,
includeSerdeLayer,
useBigInt,
enableInlineTypes
enableInlineTypes,
allowExtraFields,
omitUndefined
});
this.typeReferenceToStringExpressionConverter = new TypeReferenceToStringExpressionConverter({
typeResolver,
context,
treatUnknownAsAny,
includeSerdeLayer,
useBigInt,
enableInlineTypes
enableInlineTypes,
allowExtraFields,
omitUndefined
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@fern-fern/ir-sdk": "53.23.0",
"@fern-api/core-utils": "workspace:*",
"@fern-typescript/commons": "workspace:*",
"@fern-typescript/resolvers": "workspace:*",
"@fern-typescript/contexts": "workspace:*",
"ts-morph": "^15.1.0"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TypeReferenceNode } from "@fern-typescript/commons";
import { TypeResolver } from "@fern-typescript/resolvers";
import { BaseContext } from "@fern-typescript/contexts";
import { ts } from "ts-morph";

import {
Expand All @@ -15,11 +15,13 @@ import {

export declare namespace AbstractTypeReferenceConverter {
export interface Init {
typeResolver: TypeResolver;
context: BaseContext;
treatUnknownAsAny: boolean;
includeSerdeLayer: boolean;
useBigInt: boolean;
enableInlineTypes: boolean;
allowExtraFields: boolean;
omitUndefined: boolean;
}
}

Expand Down Expand Up @@ -86,24 +88,30 @@ export namespace ConvertTypeReferenceParams {
const genericIn = ConvertTypeReferenceParams.GenericIn;

export abstract class AbstractTypeReferenceConverter<T> {
protected typeResolver: TypeResolver;
protected context: BaseContext;
protected treatUnknownAsAny: boolean;
protected includeSerdeLayer: boolean;
protected useBigInt: boolean;
protected enableInlineTypes: boolean;
protected allowExtraFields: boolean;
protected omitUndefined: boolean;

constructor({
typeResolver,
context,
treatUnknownAsAny,
includeSerdeLayer,
useBigInt,
enableInlineTypes
enableInlineTypes,
allowExtraFields,
omitUndefined
}: AbstractTypeReferenceConverter.Init) {
this.typeResolver = typeResolver;
this.context = context;
this.treatUnknownAsAny = treatUnknownAsAny;
this.includeSerdeLayer = includeSerdeLayer;
this.useBigInt = useBigInt;
this.enableInlineTypes = enableInlineTypes;
this.allowExtraFields = allowExtraFields;
this.omitUndefined = omitUndefined;
}

public convert(params: ConvertTypeReferenceParams): T {
Expand Down Expand Up @@ -167,7 +175,7 @@ export abstract class AbstractTypeReferenceConverter<T> {
}

protected map(mapType: MapType, params: ConvertTypeReferenceParams): T {
const resolvdKeyType = this.typeResolver.resolveTypeReference(mapType.keyType);
const resolvdKeyType = this.context.type.resolveTypeReference(mapType.keyType);
if (resolvdKeyType.type === "named" && resolvdKeyType.shape === ShapeType.Enum) {
return this.mapWithEnumKeys(mapType, params);
} else {
Expand All @@ -179,7 +187,7 @@ export abstract class AbstractTypeReferenceConverter<T> {
protected abstract mapWithNonEnumKeys(mapType: MapType, params: ConvertTypeReferenceParams): T;

protected isTypeReferencePrimitive(typeReference: TypeReference): boolean {
const resolvedType = this.typeResolver.resolveTypeReference(typeReference);
const resolvedType = this.context.type.resolveTypeReference(typeReference);
if (resolvedType.type === "primitive") {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export abstract class AbstractTypeReferenceToTypeNodeConverter extends AbstractT
}

protected override named(typeName: DeclaredTypeName, params: ConvertTypeReferenceParams): TypeReferenceNode {
const resolvedType = this.typeResolver.resolveTypeName(typeName);
const resolvedType = this.context.type.resolveTypeName(typeName);
const isOptional = ResolvedTypeReference._visit<boolean>(resolvedType, {
container: (container) => this.container(container, params).isOptional,
primitive: (primitive) => this.primitive(primitive).isOptional,
Expand All @@ -45,7 +45,7 @@ export abstract class AbstractTypeReferenceToTypeNodeConverter extends AbstractT
});

let typeNodeWithoutUndefined: ts.TypeNode;
const typeDeclaration = this.typeResolver.getTypeDeclarationFromName(typeName);
const typeDeclaration = this.context.type.getTypeDeclaration(typeName);
if (this.enableInlineTypes && typeDeclaration.inline) {
if (ConvertTypeReferenceParams.isInlinePropertyParams(params)) {
typeNodeWithoutUndefined = this.createTypeRefenceForInlinePropertyNamedType(params);
Expand Down
Loading

0 comments on commit 9cdb96c

Please sign in to comment.