Skip to content

Commit b351640

Browse files
authored
Merge pull request #338 from hristozov/topic/ghristozov/fix-no-module-export
Do not emit an empty module export in the index if no module is required Fixes #337
2 parents c85bb58 + 0caf7d0 commit b351640

File tree

5 files changed

+79
-1
lines changed

5 files changed

+79
-1
lines changed

templates/index.handlebars

+2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ export { {{configurationClass}} } from './{{{configurationFile}}}';
66
export { {{baseServiceClass}} } from './{{{baseServiceFile}}}';
77
export { {{requestBuilderClass}} } from './{{{requestBuilderFile}}}';
88
export { {{responseClass}} } from './{{{responseFile}}}';
9+
{{#if moduleClass}}
910
export { {{moduleClass}} } from './{{{moduleFile}}}';
11+
{{/if}}
1012
{{#modelIndex.imports}}export { {{{typeName}}}{{#useAlias}} as {{{qualifiedName}}}{{/useAlias}} } from './models{{{file}}}';
1113
{{/modelIndex.imports}}
1214
{{#services}}export { {{typeName}} } from './services/{{{fileName}}}';

test/all-types.spec.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { OpenAPIObject } from '@loopback/openapi-v3-types';
2-
import { ClassDeclaration, EnumDeclaration, InterfaceDeclaration, TypeAliasDeclaration, TypescriptParser } from 'typescript-parser';
2+
import { ClassDeclaration, EnumDeclaration, InterfaceDeclaration, NamedExport, TypeAliasDeclaration, TypescriptParser } from 'typescript-parser';
33
import { NgOpenApiGen } from '../lib/ng-openapi-gen';
44
import { Options } from '../lib/options';
55
import options from './all-types.config.json';
@@ -526,4 +526,19 @@ describe('Generation tests using all-types.json', () => {
526526
done();
527527
});
528528
});
529+
530+
it('index file', done => {
531+
const ref = gen.models.get('InlineObject');
532+
const ts = gen.templates.apply('index', ref);
533+
const parser = new TypescriptParser();
534+
parser.parseSource(ts).then(ast => {
535+
expect(ast.exports.length).withContext('Has the correct number of exports').toBe(5);
536+
expect(ast.exports.some((ex: NamedExport) => ex.from === './api-configuration')).withContext('Has an ApiConfiguration export').toBeDefined();
537+
expect(ast.exports.some((ex: NamedExport) => ex.from === './base-service')).withContext('Has a BaseService export').toBeDefined();
538+
expect(ast.exports.some((ex: NamedExport) => ex.from === './request-builder')).withContext('Has a RequestBuilder export').toBeDefined();
539+
expect(ast.exports.some((ex: NamedExport) => ex.from === './strict-http-response')).withContext('Has a StrictHttpResponse export').toBeDefined();
540+
expect(ast.exports.some((ex: NamedExport) => ex.from === './api.module')).withContext('Has an ApiModule export').toBeDefined();
541+
done();
542+
});
543+
});
529544
});

test/noModule.config.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"$schema": "../ng-openapi-gen-schema.json",
3+
"input": "test/noModule.json",
4+
"output": "out/noModule",
5+
"useTempDir": true,
6+
"module": false,
7+
"indexFile": true
8+
}

test/noModule.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"openapi": "3.0",
3+
"info": {
4+
"title": "Test with noModule",
5+
"version": "1.0"
6+
},
7+
"paths": {
8+
"/foo": {
9+
"get": {
10+
"responses": {
11+
"200": {
12+
"content": {
13+
"application/json": {
14+
"schema": {
15+
"type": "string"
16+
}
17+
},
18+
"text/plain": {}
19+
}
20+
}
21+
}
22+
}
23+
}
24+
}
25+
}

test/noModule.spec.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { OpenAPIObject } from 'openapi3-ts';
2+
import { NgOpenApiGen } from '../lib/ng-openapi-gen';
3+
import options from './noModule.config.json';
4+
import templatesSpec from './noModule.json';
5+
import { NamedExport, TypescriptParser } from 'typescript-parser';
6+
7+
describe('Generation tests with index and no ApiModule', () => {
8+
const gen = new NgOpenApiGen(templatesSpec as OpenAPIObject, options);
9+
10+
beforeAll(() => {
11+
gen.generate();
12+
});
13+
14+
it('index file', done => {
15+
const ref = gen.models.get('InlineObject');
16+
const ts = gen.templates.apply('index', ref);
17+
const parser = new TypescriptParser();
18+
parser.parseSource(ts).then(ast => {
19+
expect(ast.exports.length).withContext('Has the correct number of exports').toBe(4);
20+
expect(ast.exports.some((ex: NamedExport) => ex.from === './api-configuration')).withContext('Has an ApiConfiguration export').toBeDefined();
21+
expect(ast.exports.some((ex: NamedExport) => ex.from === './base-service')).withContext('Has a BaseService export').toBeDefined();
22+
expect(ast.exports.some((ex: NamedExport) => ex.from === './request-builder')).withContext('Has a RequestBuilder export').toBeDefined();
23+
expect(ast.exports.some((ex: NamedExport) => ex.from === './strict-http-response')).withContext('Has a StrictHttpResponse export').toBeDefined();
24+
done();
25+
});
26+
});
27+
28+
});

0 commit comments

Comments
 (0)