Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not emit an empty module export in the index if no module is required #338

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions templates/index.handlebars
Original file line number Diff line number Diff line change
@@ -6,7 +6,9 @@ export { {{configurationClass}} } from './{{{configurationFile}}}';
export { {{baseServiceClass}} } from './{{{baseServiceFile}}}';
export { {{requestBuilderClass}} } from './{{{requestBuilderFile}}}';
export { {{responseClass}} } from './{{{responseFile}}}';
{{#if moduleClass}}
export { {{moduleClass}} } from './{{{moduleFile}}}';
{{/if}}
{{#modelIndex.imports}}export { {{{typeName}}}{{#useAlias}} as {{{qualifiedName}}}{{/useAlias}} } from './models{{{file}}}';
{{/modelIndex.imports}}
{{#services}}export { {{typeName}} } from './services/{{{fileName}}}';
17 changes: 16 additions & 1 deletion test/all-types.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OpenAPIObject } from '@loopback/openapi-v3-types';
import { ClassDeclaration, EnumDeclaration, InterfaceDeclaration, TypeAliasDeclaration, TypescriptParser } from 'typescript-parser';
import { ClassDeclaration, EnumDeclaration, InterfaceDeclaration, NamedExport, TypeAliasDeclaration, TypescriptParser } from 'typescript-parser';
import { NgOpenApiGen } from '../lib/ng-openapi-gen';
import { Options } from '../lib/options';
import options from './all-types.config.json';
@@ -526,4 +526,19 @@ describe('Generation tests using all-types.json', () => {
done();
});
});

it('index file', done => {
const ref = gen.models.get('InlineObject');
const ts = gen.templates.apply('index', ref);
const parser = new TypescriptParser();
parser.parseSource(ts).then(ast => {
expect(ast.exports.length).withContext('Has the correct number of exports').toBe(5);
expect(ast.exports.some((ex: NamedExport) => ex.from === './api-configuration')).withContext('Has an ApiConfiguration export').toBeDefined();
expect(ast.exports.some((ex: NamedExport) => ex.from === './base-service')).withContext('Has a BaseService export').toBeDefined();
expect(ast.exports.some((ex: NamedExport) => ex.from === './request-builder')).withContext('Has a RequestBuilder export').toBeDefined();
expect(ast.exports.some((ex: NamedExport) => ex.from === './strict-http-response')).withContext('Has a StrictHttpResponse export').toBeDefined();
expect(ast.exports.some((ex: NamedExport) => ex.from === './api.module')).withContext('Has an ApiModule export').toBeDefined();
done();
});
});
});
8 changes: 8 additions & 0 deletions test/noModule.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"$schema": "../ng-openapi-gen-schema.json",
"input": "test/noModule.json",
"output": "out/noModule",
"useTempDir": true,
"module": false,
"indexFile": true
}
25 changes: 25 additions & 0 deletions test/noModule.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"openapi": "3.0",
"info": {
"title": "Test with noModule",
"version": "1.0"
},
"paths": {
"/foo": {
"get": {
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"type": "string"
}
},
"text/plain": {}
}
}
}
}
}
}
}
28 changes: 28 additions & 0 deletions test/noModule.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { OpenAPIObject } from 'openapi3-ts';
import { NgOpenApiGen } from '../lib/ng-openapi-gen';
import options from './noModule.config.json';
import templatesSpec from './noModule.json';
import { NamedExport, TypescriptParser } from 'typescript-parser';

describe('Generation tests with index and no ApiModule', () => {
const gen = new NgOpenApiGen(templatesSpec as OpenAPIObject, options);

beforeAll(() => {
gen.generate();
});

it('index file', done => {
const ref = gen.models.get('InlineObject');
const ts = gen.templates.apply('index', ref);
const parser = new TypescriptParser();
parser.parseSource(ts).then(ast => {
expect(ast.exports.length).withContext('Has the correct number of exports').toBe(4);
expect(ast.exports.some((ex: NamedExport) => ex.from === './api-configuration')).withContext('Has an ApiConfiguration export').toBeDefined();
expect(ast.exports.some((ex: NamedExport) => ex.from === './base-service')).withContext('Has a BaseService export').toBeDefined();
expect(ast.exports.some((ex: NamedExport) => ex.from === './request-builder')).withContext('Has a RequestBuilder export').toBeDefined();
expect(ast.exports.some((ex: NamedExport) => ex.from === './strict-http-response')).withContext('Has a StrictHttpResponse export').toBeDefined();
done();
});
});

});