Skip to content

Commit 4f25d14

Browse files
Merge pull request #79 from dmitry-urenev/simple-type-aliases
Simple type aliases
2 parents a1bac4a + 32e1f04 commit 4f25d14

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

src/metadata/keywordKinds.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { SyntaxKind } from 'typescript';
2+
3+
export const keywords = [
4+
SyntaxKind.AnyKeyword,
5+
SyntaxKind.UnknownKeyword,
6+
SyntaxKind.NumberKeyword,
7+
SyntaxKind.BigIntKeyword,
8+
SyntaxKind.ObjectKeyword,
9+
SyntaxKind.BooleanKeyword,
10+
SyntaxKind.StringKeyword,
11+
SyntaxKind.SymbolKeyword,
12+
SyntaxKind.ThisKeyword,
13+
SyntaxKind.VoidKeyword,
14+
SyntaxKind.UndefinedKeyword,
15+
SyntaxKind.NullKeyword,
16+
SyntaxKind.NeverKeyword
17+
];

src/metadata/resolveType.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as _ from 'lodash';
22
import * as ts from 'typescript';
33
import { getDecoratorName } from '../utils/decoratorUtils';
44
import { getFirstMatchingJSDocTagName } from '../utils/jsDocUtils';
5+
import { keywords } from './keywordKinds';
56
import { ArrayType, EnumerateType, MetadataGenerator, ObjectType, Property, ReferenceType, Type } from './metadataGenerator';
67

78
const syntaxKindMap: { [kind: number]: string } = {};
@@ -460,7 +461,11 @@ function getModelTypeProperties(node: any, genericTypes?: Array<ts.TypeNode>): A
460461
}
461462

462463
if (node.kind === ts.SyntaxKind.TypeAliasDeclaration) {
463-
return getModelTypeProperties(node.type as any, genericTypes);
464+
const typeAlias = node as ts.TypeAliasDeclaration;
465+
466+
return !keywords.includes(typeAlias.type.kind)
467+
? getModelTypeProperties(typeAlias.type, genericTypes)
468+
: [];
464469
}
465470

466471
const classDeclaration = node as ts.ClassDeclaration;

test/data/apis.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,8 @@ export class DerivedEndpoint2 {
233233
}
234234
}
235235

236-
export interface SimpleHelloType {
236+
// tslint:disable-next-line: interface-over-type-literal
237+
export type SimpleHelloType = {
237238
/**
238239
* Description for greeting property
239240
*/
@@ -251,13 +252,16 @@ export interface SimpleHelloType {
251252
};
252253

253254
comparePassword: (candidatePassword: string, cb: (err: any, isMatch: any) => {}) => void;
254-
}
255+
};
255256

256257
export interface Something {
258+
id: UUID;
257259
someone: string;
258260
kind: string;
259261
}
260262

263+
export type UUID = string;
264+
261265
@Path('type')
262266
export class TypeEndpoint {
263267

test/unit/definitions.spec.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,15 @@ describe('Definition generation', () => {
189189
describe('TypeEndpoint', () => {
190190
it('should generate definitions for type aliases', () => {
191191
expect(spec.paths).to.have.property('/type/{param}');
192-
const expression = jsonata('definitions.SimpleHelloType.properties.greeting.description');
192+
let expression = jsonata('definitions.SimpleHelloType.properties.greeting.description');
193193
expect(expression.evaluate(spec)).to.eq('Description for greeting property');
194+
195+
expression = jsonata('definitions.UUID');
196+
expect(expression.evaluate(spec)).to.eql({
197+
description: '',
198+
properties: {},
199+
type: 'object',
200+
});
194201
});
195202

196203
it('should generate nested object types in definitions', () => {

0 commit comments

Comments
 (0)