Skip to content

Commit

Permalink
fix(core): remove duplicate imports generated by swagger-typescript (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder authored Aug 29, 2022
1 parent 22b4159 commit 0fc17d4
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`swagger-typescript generator should run successfully for OpenAPI 3.0: libs/generated-ts/src/index.ts 1`] = `
"export * from './interfaces/company';
export * from './interfaces/person';
export * from './interfaces/temperature';
export * from './interfaces/weather-forecast';
"
`;

exports[`swagger-typescript generator should run successfully for OpenAPI 3.0: libs/generated-ts/src/interfaces/company.ts 1`] = `
"import { Person } from './person';
export interface Company {
CEO?: Person
employees?: Person[]
}
"
`;

exports[`swagger-typescript generator should run successfully for OpenAPI 3.0: libs/generated-ts/src/interfaces/person.ts 1`] = `
"import { Company } from './company';
export interface Person {
employer?: Company
}
"
`;

exports[`swagger-typescript generator should run successfully for OpenAPI 3.0: libs/generated-ts/src/interfaces/temperature.ts 1`] = `
"
export interface Temperature {
temperatureC: number
temperatureF: number
}
"
`;

exports[`swagger-typescript generator should run successfully for OpenAPI 3.0: libs/generated-ts/src/interfaces/weather-forecast.ts 1`] = `
"import { Temperature } from './temperature';
import { Person } from './person';
export interface WeatherForecast {
date: string
temperature?: Temperature
summary?: string
forecaster?: Person
}
"
`;
26 changes: 18 additions & 8 deletions packages/core/src/generators/swagger-typescript/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ const MOCK_SWAGGER_JSON = `{
"Company": {
"type": "object",
"properties": {
"CEO": {
"$ref": "#/components/schemas/Person"
},
"employees": {
"type": "array",
"items": {
Expand Down Expand Up @@ -129,24 +132,31 @@ describe('swagger-typescript generator', () => {
outputProject: 'generated-ts',
});

// tree
// .listChanges()
// .forEach((change) =>
// console.log(change.path, change.content?.toString()),
// );

expectFileToMatchSnapshot(
'libs/generated-ts/src/interfaces/weather-forecast.ts',
tree,
);
expectFileToMatchSnapshot(
'libs/generated-ts/src/interfaces/weather-forecast',
'libs/generated-ts/src/interfaces/temperature.ts',
tree,
);
expectFileToMatchSnapshot(
'libs/generated-ts/src/interfaces/weather-forecasts',
'libs/generated-ts/src/interfaces/person.ts',
tree,
);
expectFileToMatchSnapshot(
'libs/generated-ts/src/interfaces/temperature',
'libs/generated-ts/src/interfaces/company.ts',
tree,
);
expectFileToMatchSnapshot('libs/generated-ts/src/interfaces/person', tree);
expectFileToMatchSnapshot('libs/generated-ts/src/interfaces/company', tree);
// const config = readProjectConfiguration(appTree, 'test');
// expect(config).toBeDefined();
expectFileToMatchSnapshot('libs/generated-ts/src/index.ts', tree);
});
});

const expectFileToMatchSnapshot = (file: string, tree: Tree) =>
expect(tree.read(file)?.toString()).toMatchSnapshot;
expect(tree.read(file)?.toString()).toMatchSnapshot(file);
10 changes: 7 additions & 3 deletions packages/core/src/generators/swagger-typescript/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ function generateInterfaceFiles(
? [tsInterface.elements]
: []
)?.reduce((necessary, next) => {
if (next !== 'object' && !builtInTypes.has(next)) {
necessary.push(names(next));
if (
next !== 'object' &&
!builtInTypes.has(next) &&
!necessary.has(next)
) {
necessary.set(next, names(next));
}
return necessary;
}, [] as ReturnType<typeof names>[]);
}, new Map<string, ReturnType<typeof names>>());

const templateOptions = {
...options,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<% if (necessaryImports) for (const i of necessaryImports) { -%>
import {<%=i.className%>} from './<%=i.fileName%>';
<% } %>
<% if (necessaryImports) for (const [, i] of necessaryImports) { -%>
import { <%=i.className%> } from './<%=i.fileName%>';
<% } -%>

<% if (tsInterface.type === 'interface') { %>
<% if (tsInterface.type === 'interface') { -%>
export interface <%= interfaceClassName %> {
<% for (const property of tsInterface.properties) {-%>
<%_ for (const property of tsInterface.properties) {-%>
<%= property.name %><% if (property.nullable) { %>?<%}%>: <%= property.type %>
<% } %>
<%_ } -%>
}
<% } else if (tsInterface.type === 'array') {%>
<% } else if (tsInterface.type === 'array') { -%>
export type <%= interfaceClassName %> = <%= tsInterface.elements %>[]
<% } %>
<% } -%>

0 comments on commit 0fc17d4

Please sign in to comment.