Skip to content

Commit 0fc17d4

Browse files
authored
fix(core): remove duplicate imports generated by swagger-typescript (#502)
1 parent 22b4159 commit 0fc17d4

File tree

4 files changed

+83
-19
lines changed

4 files changed

+83
-19
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`swagger-typescript generator should run successfully for OpenAPI 3.0: libs/generated-ts/src/index.ts 1`] = `
4+
"export * from './interfaces/company';
5+
export * from './interfaces/person';
6+
export * from './interfaces/temperature';
7+
export * from './interfaces/weather-forecast';
8+
"
9+
`;
10+
11+
exports[`swagger-typescript generator should run successfully for OpenAPI 3.0: libs/generated-ts/src/interfaces/company.ts 1`] = `
12+
"import { Person } from './person';
13+
14+
export interface Company {
15+
CEO?: Person
16+
employees?: Person[]
17+
}
18+
"
19+
`;
20+
21+
exports[`swagger-typescript generator should run successfully for OpenAPI 3.0: libs/generated-ts/src/interfaces/person.ts 1`] = `
22+
"import { Company } from './company';
23+
24+
export interface Person {
25+
employer?: Company
26+
}
27+
"
28+
`;
29+
30+
exports[`swagger-typescript generator should run successfully for OpenAPI 3.0: libs/generated-ts/src/interfaces/temperature.ts 1`] = `
31+
"
32+
export interface Temperature {
33+
temperatureC: number
34+
temperatureF: number
35+
}
36+
"
37+
`;
38+
39+
exports[`swagger-typescript generator should run successfully for OpenAPI 3.0: libs/generated-ts/src/interfaces/weather-forecast.ts 1`] = `
40+
"import { Temperature } from './temperature';
41+
import { Person } from './person';
42+
43+
export interface WeatherForecast {
44+
date: string
45+
temperature?: Temperature
46+
summary?: string
47+
forecaster?: Person
48+
}
49+
"
50+
`;

packages/core/src/generators/swagger-typescript/generator.spec.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ const MOCK_SWAGGER_JSON = `{
5656
"Company": {
5757
"type": "object",
5858
"properties": {
59+
"CEO": {
60+
"$ref": "#/components/schemas/Person"
61+
},
5962
"employees": {
6063
"type": "array",
6164
"items": {
@@ -129,24 +132,31 @@ describe('swagger-typescript generator', () => {
129132
outputProject: 'generated-ts',
130133
});
131134

135+
// tree
136+
// .listChanges()
137+
// .forEach((change) =>
138+
// console.log(change.path, change.content?.toString()),
139+
// );
140+
141+
expectFileToMatchSnapshot(
142+
'libs/generated-ts/src/interfaces/weather-forecast.ts',
143+
tree,
144+
);
132145
expectFileToMatchSnapshot(
133-
'libs/generated-ts/src/interfaces/weather-forecast',
146+
'libs/generated-ts/src/interfaces/temperature.ts',
134147
tree,
135148
);
136149
expectFileToMatchSnapshot(
137-
'libs/generated-ts/src/interfaces/weather-forecasts',
150+
'libs/generated-ts/src/interfaces/person.ts',
138151
tree,
139152
);
140153
expectFileToMatchSnapshot(
141-
'libs/generated-ts/src/interfaces/temperature',
154+
'libs/generated-ts/src/interfaces/company.ts',
142155
tree,
143156
);
144-
expectFileToMatchSnapshot('libs/generated-ts/src/interfaces/person', tree);
145-
expectFileToMatchSnapshot('libs/generated-ts/src/interfaces/company', tree);
146-
// const config = readProjectConfiguration(appTree, 'test');
147-
// expect(config).toBeDefined();
157+
expectFileToMatchSnapshot('libs/generated-ts/src/index.ts', tree);
148158
});
149159
});
150160

151161
const expectFileToMatchSnapshot = (file: string, tree: Tree) =>
152-
expect(tree.read(file)?.toString()).toMatchSnapshot;
162+
expect(tree.read(file)?.toString()).toMatchSnapshot(file);

packages/core/src/generators/swagger-typescript/generator.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,15 @@ function generateInterfaceFiles(
3333
? [tsInterface.elements]
3434
: []
3535
)?.reduce((necessary, next) => {
36-
if (next !== 'object' && !builtInTypes.has(next)) {
37-
necessary.push(names(next));
36+
if (
37+
next !== 'object' &&
38+
!builtInTypes.has(next) &&
39+
!necessary.has(next)
40+
) {
41+
necessary.set(next, names(next));
3842
}
3943
return necessary;
40-
}, [] as ReturnType<typeof names>[]);
44+
}, new Map<string, ReturnType<typeof names>>());
4145

4246
const templateOptions = {
4347
...options,
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
<% if (necessaryImports) for (const i of necessaryImports) { -%>
2-
import {<%=i.className%>} from './<%=i.fileName%>';
3-
<% } %>
1+
<% if (necessaryImports) for (const [, i] of necessaryImports) { -%>
2+
import { <%=i.className%> } from './<%=i.fileName%>';
3+
<% } -%>
44

5-
<% if (tsInterface.type === 'interface') { %>
5+
<% if (tsInterface.type === 'interface') { -%>
66
export interface <%= interfaceClassName %> {
7-
<% for (const property of tsInterface.properties) {-%>
7+
<%_ for (const property of tsInterface.properties) {-%>
88
<%= property.name %><% if (property.nullable) { %>?<%}%>: <%= property.type %>
9-
<% } %>
9+
<%_ } -%>
1010
}
11-
<% } else if (tsInterface.type === 'array') {%>
11+
<% } else if (tsInterface.type === 'array') { -%>
1212
export type <%= interfaceClassName %> = <%= tsInterface.elements %>[]
13-
<% } %>
13+
<% } -%>

0 commit comments

Comments
 (0)