Skip to content

Commit 270e13e

Browse files
committed
fix: model with id required
1 parent e460a58 commit 270e13e

File tree

3 files changed

+63
-16
lines changed

3 files changed

+63
-16
lines changed

packages/cli/generators/controller/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,12 @@ module.exports = class ControllerGenerator extends ArtifactGenerator {
181181
when: this.artifactInfo.idType === undefined,
182182
default: 'number',
183183
},
184+
{
185+
type: 'confirm',
186+
name: 'idOmitted',
187+
message: 'Is the id omitted when creating a new instance?',
188+
default: true,
189+
},
184190
{
185191
type: 'input',
186192
name: 'httpPathName',

packages/cli/generators/controller/templates/src/controllers/controller-rest-template.ts.ejs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ export class <%= className %>Controller {
4040
'application/json': {
4141
schema: getModelSchemaRef(<%= modelName %>, {
4242
title: 'New<%= modelName %>',
43-
exclude: ['<%= id %>'],
43+
<%if (idOmitted) {%>exclude: ['<%= id %>'],<% } %>
4444
}),
4545
},
4646
},
4747
})
48-
<%= modelVariableName %>: Omit<<%= modelName %>, '<%= id %>'>,
48+
<%= modelVariableName %>: <% if (!idOmitted) { -%><%= modelName %><% } else { -%>Omit<<%= modelName %>, '<%= id %>'><% } -%>,
4949
): Promise<<%= modelName %>> {
5050
return this.<%= repositoryNameCamel %>.create(<%= modelVariableName %>);
5151
}

packages/cli/test/integration/generators/controller.integration.js

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ describe('lb4 controller', () => {
100100
restCLIInput,
101101
);
102102

103-
it('creates REST CRUD template with valid input', async () => {
103+
it('creates REST CRUD template with valid input - id omitted', async () => {
104104
await testUtils
105105
.executeGenerator(generator)
106106
.inDir(SANDBOX_PATH, () =>
@@ -111,7 +111,23 @@ describe('lb4 controller', () => {
111111
)
112112
.withPrompts(restCLIInputComplete);
113113

114-
checkRestCrudContents();
114+
checkRestCrudContents({idOmitted: true});
115+
});
116+
117+
it('creates REST CRUD template with valid input', async () => {
118+
await testUtils
119+
.executeGenerator(generator)
120+
.inDir(SANDBOX_PATH, () =>
121+
testUtils.givenLBProject(SANDBOX_PATH, {
122+
includeDummyModel: true,
123+
includeDummyRepository: true,
124+
}),
125+
)
126+
.withPrompts(
127+
Object.assign({}, restCLIInputComplete, {idOmitted: false}),
128+
);
129+
130+
checkRestCrudContents({idOmitted: false});
115131
});
116132

117133
describe('HTTP REST path', () => {
@@ -228,13 +244,44 @@ function checkBasicContents() {
228244
assert.fileContent(expectedFile, /constructor\(\) {}/);
229245
}
230246

247+
function checkCreateContentsWithIdOmitted() {
248+
const postCreateRegEx = [
249+
/\@post\('\/product-reviews', {/,
250+
/responses: {/,
251+
/'200': {/,
252+
/description: 'ProductReview model instance'/,
253+
/content: {'application\/json': {schema: getModelSchemaRef\(ProductReview\)}},\s{1,}},\s{1,}},\s{1,}}\)/,
254+
/async create\(\s+\@requestBody\({\s+content: {\s+'application\/json': {\s+schema: getModelSchemaRef\(ProductReview, {\s+title: 'NewProductReview',\s+exclude: \['productId'\],\s+}\),\s+},\s+},\s+}\)\s+productReview: Omit<ProductReview, 'productId'>,\s+\)/,
255+
];
256+
postCreateRegEx.forEach(regex => {
257+
assert.fileContent(expectedFile, regex);
258+
});
259+
}
260+
261+
/**
262+
* Check the contents for operation 'create' when id is not required.
263+
*/
264+
function checkCreateContents() {
265+
const postCreateRegEx = [
266+
/\@post\('\/product-reviews', {/,
267+
/responses: {/,
268+
/'200': {/,
269+
/description: 'ProductReview model instance'/,
270+
/content: {'application\/json': {schema: getModelSchemaRef\(ProductReview\)}},\s{1,}},\s{1,}},\s{1,}}\)/,
271+
/async create\(\s+\@requestBody\({\s+content: {\s+'application\/json': {\s+schema: getModelSchemaRef\(ProductReview, {\s+title: 'NewProductReview',\s+}\),\s+},\s+},\s+}\)\s+productReview: ProductReview,\s+\)/,
272+
];
273+
postCreateRegEx.forEach(regex => {
274+
assert.fileContent(expectedFile, regex);
275+
});
276+
}
277+
231278
/**
232279
* Assertions against the template to determine if it contains the
233280
* required signatures for a REST CRUD controller, specifically to ensure
234281
* that decorators are grouped correctly (for their corresponding
235282
* target functions)
236283
*/
237-
function checkRestCrudContents() {
284+
function checkRestCrudContents(options) {
238285
assert.fileContent(expectedFile, /class ProductReviewController/);
239286

240287
// Repository and injection
@@ -243,17 +290,11 @@ function checkRestCrudContents() {
243290

244291
// Assert that the decorators are present in the correct groupings!
245292
// @post - create
246-
const postCreateRegEx = [
247-
/\@post\('\/product-reviews', {/,
248-
/responses: {/,
249-
/'200': {/,
250-
/description: 'ProductReview model instance'/,
251-
/content: {'application\/json': {schema: getModelSchemaRef\(ProductReview\)}},\s{1,}},\s{1,}},\s{1,}}\)/,
252-
/async create\(\s+\@requestBody\({\s+content: {\s+'application\/json': {\s+schema: getModelSchemaRef\(ProductReview, {\s+title: 'NewProductReview',\s+exclude: \['productId'\],\s+}\),\s+},\s+},\s+}\)\s+productReview: Omit<ProductReview, 'productId'>,\s+\)/,
253-
];
254-
postCreateRegEx.forEach(regex => {
255-
assert.fileContent(expectedFile, regex);
256-
});
293+
if (options && options.idOmitted) {
294+
checkCreateContentsWithIdOmitted();
295+
} else {
296+
checkCreateContents();
297+
}
257298

258299
// @get - count
259300
const getCountRegEx = [

0 commit comments

Comments
 (0)