Skip to content

Commit

Permalink
fix: use correct schema value when importing type
Browse files Browse the repository at this point in the history
  • Loading branch information
DJankauskas committed Jul 14, 2023
1 parent 6a70631 commit 4873796
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
4 changes: 2 additions & 2 deletions demo/stl-api-gen/api/posts/create.ts
@@ -1,6 +1,6 @@
import { z } from "stainless";
import { PostType as __symbol_PostType } from "./models";
import { PostType as __symbol_PostType } from "./create";
export const Query: z.ZodTypeAny = z.object({ include: z.includes(z.lazy(() => __symbol_PostType), 3).optional() });
export const Body: z.ZodTypeAny = z.object({ body: z.string() });
export const PostType: z.ZodTypeAny = z.any();
export const __postu32$api$posts: any = { query: z.lazy(() => Query), body: z.lazy(() => Body), response: z.lazy(() => PostType) };
export const __postu32$api$posts: any = { query: z.lazy(() => Query), body: z.lazy(() => Body), response: z.lazy(() => __symbol_PostType) };
4 changes: 2 additions & 2 deletions demo/stl-api-gen/api/posts/retrieve.ts
@@ -1,7 +1,7 @@
import { z } from "stainless";
import { PostType as __symbol_PostType } from "./models";
import { PostType as __symbol_PostType } from "./retrieve";
import * as __u46u46$u46u46$u46u46$libs$prismadbu46ts from "../../../libs/prismadb";
export const Query: z.ZodTypeAny = z.object({ include: z.includes(z.lazy(() => __symbol_PostType), 3).optional(), select: z.selects(z.lazy(() => __symbol_PostType), 3).optional() });
export const Path: z.ZodTypeAny = z.object({ post: z.string().prismaModelLoader(__u46u46$u46u46$u46u46$libs$prismadbu46ts.prisma.post) });
export const PostType: z.ZodTypeAny = z.any();
export const __getu32$api$posts$u123postu125: any = { query: z.lazy(() => Query), path: z.lazy(() => Path), response: z.lazy(() => PostType) };
export const __getu32$api$posts$u123postu125: any = { query: z.lazy(() => Query), path: z.lazy(() => Path), response: z.lazy(() => __symbol_PostType) };
25 changes: 15 additions & 10 deletions packages/ts-to-zod/src/convertType.ts
Expand Up @@ -165,13 +165,23 @@ export class ConvertTypeContext extends SchemaGenContext {
currentFilePath: string;

isSymbolImported(symbol: tm.Symbol): boolean {
const symbolFileName: string = getDeclarationOrThrow(symbol)
.getSourceFile()
.getFilePath();
return this.currentFilePath !== symbolFileName;
const declaration = getDeclarationOrThrow(symbol);
const symbolFileName: string = declaration.getSourceFile().getFilePath();
return (
this.currentFilePath !== symbolFileName ||
isDeclarationImported(declaration)
);
}
}

function isDeclarationImported(declaration: tm.Node): boolean {
return (
declaration instanceof tm.ImportSpecifier ||
declaration instanceof tm.ImportClause ||
declaration instanceof tm.ImportDeclaration
);
}

export interface ImportInfo {
as?: string;
/**
Expand Down Expand Up @@ -381,12 +391,7 @@ function convertTypeofNode(

if (
!declaration ||
!(
isDeclarationExported(declaration) ||
declaration instanceof tm.ImportSpecifier ||
declaration instanceof tm.ImportClause ||
declaration instanceof tm.ImportDeclaration
)
!(isDeclarationExported(declaration) || isDeclarationImported(declaration))
) {
console.dir(declaration);
ctx.addError(
Expand Down

0 comments on commit 4873796

Please sign in to comment.