Skip to content

Commit 2310556

Browse files
authored
fix(core): codegen should not import self and default to required reference types (#761)
1 parent cdc3654 commit 2310556

File tree

4 files changed

+140
-107
lines changed

4 files changed

+140
-107
lines changed

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,34 @@ exports[`swagger-typescript generator should run successfully for OpenAPI 3.0: l
55
export * from './interfaces/person';
66
export * from './interfaces/temperature';
77
export * from './interfaces/weather-forecast';
8+
export * from './interfaces/my-type';
89
"
910
`;
1011

1112
exports[`swagger-typescript generator should run successfully for OpenAPI 3.0: libs/generated-ts/src/interfaces/company.ts 1`] = `
1213
"import { Person } from './person';
1314
1415
export interface Company {
15-
CEO?: Person
16+
CEO: Person
1617
employees?: Person[]
1718
}
1819
"
1920
`;
2021

22+
exports[`swagger-typescript generator should run successfully for OpenAPI 3.0: libs/generated-ts/src/interfaces/my-type.ts 1`] = `
23+
"
24+
export interface MyType {
25+
id: string
26+
children: MyType
27+
}
28+
"
29+
`;
30+
2131
exports[`swagger-typescript generator should run successfully for OpenAPI 3.0: libs/generated-ts/src/interfaces/person.ts 1`] = `
2232
"import { Company } from './company';
2333
2434
export interface Person {
25-
employer?: Company
35+
employer: Company
2636
}
2737
"
2838
`;
@@ -42,9 +52,9 @@ import { Person } from './person';
4252
4353
export interface WeatherForecast {
4454
date: string
45-
temperature?: Temperature
55+
temperature: Temperature
4656
summary?: string
47-
forecaster?: Person
57+
forecaster: Person
4858
}
4959
"
5060
`;

packages/core/src/generators/swagger-typescript/build-typescript-representation/parse-schema-object.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ export function parsePropertyDefintion(
4242

4343
return {
4444
type: reference,
45-
nullable: true,
45+
nullable:
46+
'nullable' in propertyDefinition
47+
? !!propertyDefinition.nullable
48+
: false,
4649
};
4750
} else if (
4851
propertyDefinition.type &&

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

Lines changed: 120 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -4,118 +4,132 @@ import { libraryGenerator } from '@nrwl/js';
44

55
import generator from './generator';
66

7-
const MOCK_SWAGGER_JSON = `{
8-
"openapi": "3.0.1",
9-
"info": {
10-
"title": "NxDotnet.Test.Webapi, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null",
11-
"version": "1.0"
12-
},
13-
"paths": {
14-
"/WeatherForecast": {
15-
"get": {
16-
"tags": [
17-
"WeatherForecast"
18-
],
19-
"operationId": "GetWeatherForecast",
20-
"responses": {
21-
"200": {
22-
"description": "Success",
23-
"content": {
24-
"text/plain": {
25-
"schema": {
26-
"type": "array",
27-
"items": {
28-
"$ref": "#/components/schemas/WeatherForecast"
29-
}
30-
}
31-
},
32-
"application/json": {
33-
"schema": {
34-
"type": "array",
35-
"items": {
36-
"$ref": "#/components/schemas/WeatherForecast"
37-
}
38-
}
7+
const MOCK_SWAGGER_JSON = JSON.stringify(
8+
{
9+
openapi: '3.0.1',
10+
info: {
11+
title:
12+
'NxDotnet.Test.Webapi, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null',
13+
version: '1.0',
14+
},
15+
paths: {
16+
'/WeatherForecast': {
17+
get: {
18+
tags: ['WeatherForecast'],
19+
operationId: 'GetWeatherForecast',
20+
responses: {
21+
'200': {
22+
description: 'Success',
23+
content: {
24+
'text/plain': {
25+
schema: {
26+
type: 'array',
27+
items: {
28+
$ref: '#/components/schemas/WeatherForecast',
29+
},
30+
},
31+
},
32+
'application/json': {
33+
schema: {
34+
type: 'array',
35+
items: {
36+
$ref: '#/components/schemas/WeatherForecast',
37+
},
38+
},
39+
},
40+
'text/json': {
41+
schema: {
42+
type: 'array',
43+
items: {
44+
$ref: '#/components/schemas/WeatherForecast',
45+
},
46+
},
47+
},
3948
},
40-
"text/json": {
41-
"schema": {
42-
"type": "array",
43-
"items": {
44-
"$ref": "#/components/schemas/WeatherForecast"
45-
}
46-
}
47-
}
48-
}
49-
}
50-
}
51-
}
52-
}
53-
},
54-
"components": {
55-
"schemas": {
56-
"Company": {
57-
"type": "object",
58-
"properties": {
59-
"CEO": {
60-
"$ref": "#/components/schemas/Person"
61-
},
62-
"employees": {
63-
"type": "array",
64-
"items": {
65-
"$ref": "#/components/schemas/Person"
6649
},
67-
"nullable": true
68-
}
50+
},
6951
},
70-
"additionalProperties": false
7152
},
72-
"Person": {
73-
"type": "object",
74-
"properties": {
75-
"employer": {
76-
"$ref": "#/components/schemas/Company"
77-
}
53+
},
54+
components: {
55+
schemas: {
56+
Company: {
57+
type: 'object',
58+
properties: {
59+
CEO: {
60+
$ref: '#/components/schemas/Person',
61+
},
62+
employees: {
63+
type: 'array',
64+
items: {
65+
$ref: '#/components/schemas/Person',
66+
},
67+
nullable: true,
68+
},
69+
},
70+
additionalProperties: false,
7871
},
79-
"additionalProperties": false
80-
},
81-
"Temperature": {
82-
"type": "object",
83-
"properties": {
84-
"temperatureC": {
85-
"type": "integer",
86-
"format": "int32"
72+
Person: {
73+
type: 'object',
74+
properties: {
75+
employer: {
76+
$ref: '#/components/schemas/Company',
77+
},
8778
},
88-
"temperatureF": {
89-
"type": "integer",
90-
"format": "int32",
91-
"readOnly": true
92-
}
79+
additionalProperties: false,
9380
},
94-
"additionalProperties": false
95-
},
96-
"WeatherForecast": {
97-
"type": "object",
98-
"properties": {
99-
"date": {
100-
"type": "string",
101-
"format": "date-time"
81+
Temperature: {
82+
type: 'object',
83+
properties: {
84+
temperatureC: {
85+
type: 'integer',
86+
format: 'int32',
87+
},
88+
temperatureF: {
89+
type: 'integer',
90+
format: 'int32',
91+
readOnly: true,
92+
},
10293
},
103-
"temperature": {
104-
"$ref": "#/components/schemas/Temperature"
94+
additionalProperties: false,
95+
},
96+
WeatherForecast: {
97+
type: 'object',
98+
properties: {
99+
date: {
100+
type: 'string',
101+
format: 'date-time',
102+
},
103+
temperature: {
104+
$ref: '#/components/schemas/Temperature',
105+
},
106+
summary: {
107+
type: 'string',
108+
nullable: true,
109+
},
110+
forecaster: {
111+
$ref: '#/components/schemas/Person',
112+
},
105113
},
106-
"summary": {
107-
"type": "string",
108-
"nullable": true
114+
additionalProperties: false,
115+
},
116+
MyType: {
117+
type: 'object',
118+
properties: {
119+
id: {
120+
type: 'string',
121+
},
122+
children: {
123+
$ref: '#/components/schemas/MyType',
124+
},
109125
},
110-
"forecaster": {
111-
"$ref": "#/components/schemas/Person"
112-
}
113126
},
114-
"additionalProperties": false
115-
}
116-
}
117-
}
118-
}`;
127+
},
128+
},
129+
},
130+
null,
131+
2,
132+
);
119133

120134
describe('swagger-typescript generator', () => {
121135
let tree: Tree;
@@ -154,6 +168,11 @@ describe('swagger-typescript generator', () => {
154168
'libs/generated-ts/src/interfaces/company.ts',
155169
tree,
156170
);
171+
expectFileToMatchSnapshot(
172+
'libs/generated-ts/src/interfaces/my-type.ts',
173+
tree,
174+
);
175+
157176
expectFileToMatchSnapshot('libs/generated-ts/src/index.ts', tree);
158177
});
159178
});

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ function generateInterfaceFiles(
3636
if (
3737
next !== 'object' &&
3838
!builtInTypes.has(next) &&
39-
!necessary.has(next)
39+
!necessary.has(next) &&
40+
!(next === tsInterface.name)
4041
) {
4142
necessary.set(next, names(next));
4243
}

0 commit comments

Comments
 (0)