Skip to content

Commit 3c37286

Browse files
mschickervxobraymondfeng
authored andcommitted
fix(cli): set required: true in property decoration for openapi
Make sure generated model properties from openapi schemas have `required` flag
1 parent f09d866 commit 3c37286

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

packages/cli/generators/openapi/schema-helper.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,13 @@ function mapObjectType(schema, options) {
160160
);
161161
// The property name might have chars such as `-`
162162
const propName = escapePropertyOrMethodName(p);
163+
163164
let propDecoration = `@property({name: '${p}'})`;
165+
166+
if (required.includes(p)) {
167+
propDecoration = `@property({name: '${p}', required: true})`;
168+
}
169+
164170
if (propertyType.itemType && propertyType.itemType.kind === 'class') {
165171
// Use `@property.array` for array types
166172
propDecoration = `@property.array(${

packages/cli/test/integration/generators/openapi-petstore.integration.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ describe('openapi-generator specific files', function() {
7171
assert.file(newPetModel);
7272
assert.fileContent(newPetModel, `export class NewPet {`);
7373
assert.fileContent(newPetModel, `@model({name: 'NewPet'})`);
74-
assert.fileContent(newPetModel, `@property({name: 'name'})`);
74+
assert.fileContent(
75+
newPetModel,
76+
`@property({name: 'name', required: true})`,
77+
);
7578
assert.fileContent(newPetModel, `name: string;`);
7679
assert.fileContent(newPetModel, `@property({name: 'tag'})`);
7780
assert.fileContent(newPetModel, `tag?: string`);

packages/cli/test/integration/generators/swagger-petstore.integration.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ describe('openapi-generator specific files', () => {
6969
assert.fileContent(newPetModel, `export class NewPet {`);
7070
assert.fileContent(newPetModel, `constructor(data?: Partial<NewPet>) {`);
7171
assert.fileContent(newPetModel, `@model({name: 'NewPet'})`);
72-
assert.fileContent(newPetModel, `@property({name: 'name'})`);
72+
assert.fileContent(
73+
newPetModel,
74+
`@property({name: 'name', required: true})`,
75+
);
7376
assert.fileContent(newPetModel, `name: string;`);
7477
assert.fileContent(newPetModel, `@property({name: 'tag'})`);
7578
assert.fileContent(newPetModel, `tag?: string`);

packages/cli/test/unit/openapi/schema-model.unit.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ describe('schema to model', () => {
109109
{
110110
name: 'criteria',
111111
signature: "criteria: string = '*:*';",
112-
decoration: "@property({name: 'criteria'})",
112+
decoration: "@property({name: 'criteria', required: true})",
113113
description:
114114
'Uses Lucene Query Syntax in the format of propertyName:value, ' +
115115
'propertyName:[num1 TO num2] and date range format: ' +
@@ -188,7 +188,7 @@ describe('schema to model', () => {
188188
{
189189
name: 'name',
190190
signature: 'name: string;',
191-
decoration: "@property({name: 'name'})",
191+
decoration: "@property({name: 'name', required: true})",
192192
},
193193
{
194194
name: 'tag',
@@ -208,7 +208,7 @@ describe('schema to model', () => {
208208
{
209209
name: 'id',
210210
signature: 'id: number;',
211-
decoration: "@property({name: 'id'})",
211+
decoration: "@property({name: 'id', required: true})",
212212
},
213213
],
214214
signature: '{\n id: number;\n}',
@@ -228,7 +228,7 @@ describe('schema to model', () => {
228228
{
229229
name: 'name',
230230
signature: 'name: string;',
231-
decoration: "@property({name: 'name'})",
231+
decoration: "@property({name: 'name', required: true})",
232232
},
233233
{
234234
name: 'tag',
@@ -251,12 +251,12 @@ describe('schema to model', () => {
251251
{
252252
name: 'code',
253253
signature: 'code: number;',
254-
decoration: "@property({name: 'code'})",
254+
decoration: "@property({name: 'code', required: true})",
255255
},
256256
{
257257
name: 'message',
258258
signature: 'message: string;',
259-
decoration: "@property({name: 'message'})",
259+
decoration: "@property({name: 'message', required: true})",
260260
},
261261
],
262262
imports: [],
@@ -326,7 +326,7 @@ describe('schema to model', () => {
326326
{
327327
name: 'id',
328328
signature: 'id: number;',
329-
decoration: "@property({name: 'id'})",
329+
decoration: "@property({name: 'id', required: true})",
330330
},
331331
{
332332
name: 'first-name',

0 commit comments

Comments
 (0)