Skip to content

Commit

Permalink
chore: fix package.json version
Browse files Browse the repository at this point in the history
  • Loading branch information
sebbi08 committed Jun 21, 2024
1 parent 226ad2d commit 29a86c4
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 29 deletions.
1 change: 1 addition & 0 deletions dist/metadata/metadataGenerator.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export interface UnionType extends Type {
export interface ReferenceType extends Type {
description: string;
properties: Array<Property>;
originalFileName: any;
}
export interface ObjectType extends Type {
properties: Array<Property>;
Expand Down
2 changes: 1 addition & 1 deletion dist/metadata/metadataGenerator.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/metadata/resolveType.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 32 additions & 10 deletions dist/metadata/resolveType.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/metadata/resolveType.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typescript-rest-swagger",
"version": "1.3.0",
"version": "1.3.4",
"description": "Generate Swagger files from a typescript-rest project",
"keywords": [
"typescript",
Expand Down
1 change: 1 addition & 0 deletions src/metadata/metadataGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ export interface UnionType extends Type {
export interface ReferenceType extends Type {
description: string;
properties: Array<Property>;
originalFileName: any;
// additionalProperties?: Array<Property>;
}

Expand Down
56 changes: 43 additions & 13 deletions src/metadata/resolveType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
UnionType,
} from "./metadataGenerator";

let timer = 0;

const syntaxKindMap: { [kind: number]: string } = {};
syntaxKindMap[ts.SyntaxKind.NumberKeyword] = "number";
syntaxKindMap[ts.SyntaxKind.StringKeyword] = "string";
Expand Down Expand Up @@ -109,22 +111,29 @@ export function resolveType(

let referenceType: ReferenceType;

let parent: any = typeNode;

while (!ts.isSourceFile(parent)) {
parent = parent.parent;
}
let sourceFile = parent as ts.SourceFile;
const sourceFile = getSourceFile(typeNode);

let tmpFileName = _.uniqueId("__tmp_") + ".ts";
const tmpFileName = _.uniqueId("__tmp_") + ".ts";

let fullTypeName = typeNode.getText();
const fullTypeName = typeNode.getText();

const fullRefTypeName = replaceNameText(fullTypeName);

const refType = MetadataGenerator.current.getReferenceType(fullRefTypeName);
if(refType){
return refType

const symbol =
MetadataGenerator.current.typeChecker.getSymbolAtLocation(typeNameNode);

let originalDeclarationFileName = sourceFile.fileName;
if (symbol) {
originalDeclarationFileName =
getOriginalSourceFile(symbol) ?? sourceFile.fileName;
}

if (refType && refType?.originalFileName !== originalDeclarationFileName) {
throw new Error(`reference type ${fullRefTypeName} with same name but different properties. Please use different names for different types.`)
} else if (refType) {
return refType;
}

const newTmpSourceFile = `
Expand All @@ -144,7 +153,9 @@ export function resolveType(
);

const statement = tmpSourceFile.getStatements().at(-2)!;

const originalStatement = tmpSourceFile.getStatements().at(-1)!;

const type = statement.getType();

if (type.isUnion()) {
Expand All @@ -165,9 +176,7 @@ export function resolveType(
}

if (!type.isObject()) {
const declaration =
MetadataGenerator.current.typeChecker.getSymbolAtLocation(typeNameNode)
?.declarations?.[0];
const declaration = symbol?.declarations?.[0];
if (!declaration) {
throw new Error("Could not resolve declaration of Object");
}
Expand Down Expand Up @@ -266,6 +275,7 @@ export function resolveType(
properties,
typeName: replaceNameText(fullTypeName),
simpleTypeName: typeName,
originalFileName: originalDeclarationFileName,
};

MetadataGenerator.current.morph.removeSourceFile(tmpSourceFile);
Expand Down Expand Up @@ -835,3 +845,23 @@ export function resolveImports<T>(node: T): T {
}
return node;
}

function getSourceFile(node: ts.Node): ts.SourceFile {
while (node.kind !== ts.SyntaxKind.SourceFile) {
node = node.parent;
}
return node as ts.SourceFile;
}

function getOriginalSourceFile(symbol: ts.Symbol) {
if (
symbol &&
symbol?.declarations?.[0].kind === ts.SyntaxKind.ImportSpecifier
) {
return MetadataGenerator.current.typeChecker
.getAliasedSymbol(symbol)
.getDeclarations()?.[0]
.getSourceFile().fileName;
}
return symbol?.declarations?.[0].getSourceFile().fileName;
}

0 comments on commit 29a86c4

Please sign in to comment.