diff --git a/packages/cli/generators/controller/index.js b/packages/cli/generators/controller/index.js index 95514964d8db..55d2aad3f0bf 100644 --- a/packages/cli/generators/controller/index.js +++ b/packages/cli/generators/controller/index.js @@ -181,6 +181,12 @@ module.exports = class ControllerGenerator extends ArtifactGenerator { when: this.artifactInfo.idType === undefined, default: 'number', }, + { + type: 'confirm', + name: 'idOmitted', + message: 'Is the id omitted when creating a new instance?', + default: true, + }, { type: 'input', name: 'httpPathName', diff --git a/packages/cli/generators/controller/templates/src/controllers/controller-rest-template.ts.ejs b/packages/cli/generators/controller/templates/src/controllers/controller-rest-template.ts.ejs index 5ab592f5fc9b..908e72d8539b 100644 --- a/packages/cli/generators/controller/templates/src/controllers/controller-rest-template.ts.ejs +++ b/packages/cli/generators/controller/templates/src/controllers/controller-rest-template.ts.ejs @@ -40,12 +40,12 @@ export class <%= className %>Controller { 'application/json': { schema: getModelSchemaRef(<%= modelName %>, { title: 'New<%= modelName %>', - exclude: ['<%= id %>'], + <%if (idOmitted) {%>exclude: ['<%= id %>'],<% } %> }), }, }, }) - <%= modelVariableName %>: Omit<<%= modelName %>, '<%= id %>'>, + <%= modelVariableName %>: <% if (!idOmitted) { -%><%= modelName %><% } else { -%>Omit<<%= modelName %>, '<%= id %>'><% } -%>, ): Promise<<%= modelName %>> { return this.<%= repositoryNameCamel %>.create(<%= modelVariableName %>); } diff --git a/packages/cli/test/integration/generators/controller.integration.js b/packages/cli/test/integration/generators/controller.integration.js index 58ae3c5ea711..1b26d6ba9e1f 100644 --- a/packages/cli/test/integration/generators/controller.integration.js +++ b/packages/cli/test/integration/generators/controller.integration.js @@ -100,7 +100,7 @@ describe('lb4 controller', () => { restCLIInput, ); - it('creates REST CRUD template with valid input', async () => { + it('creates REST CRUD template with valid input - id omitted', async () => { await testUtils .executeGenerator(generator) .inDir(SANDBOX_PATH, () => @@ -111,7 +111,23 @@ describe('lb4 controller', () => { ) .withPrompts(restCLIInputComplete); - checkRestCrudContents(); + checkRestCrudContents({idOmitted: true}); + }); + + it('creates REST CRUD template with valid input', async () => { + await testUtils + .executeGenerator(generator) + .inDir(SANDBOX_PATH, () => + testUtils.givenLBProject(SANDBOX_PATH, { + includeDummyModel: true, + includeDummyRepository: true, + }), + ) + .withPrompts( + Object.assign({}, restCLIInputComplete, {idOmitted: false}), + ); + + checkRestCrudContents({idOmitted: false}); }); describe('HTTP REST path', () => { @@ -228,13 +244,44 @@ function checkBasicContents() { assert.fileContent(expectedFile, /constructor\(\) {}/); } +function checkCreateContentsWithIdOmitted() { + const postCreateRegEx = [ + /\@post\('\/product-reviews', {/, + /responses: {/, + /'200': {/, + /description: 'ProductReview model instance'/, + /content: {'application\/json': {schema: getModelSchemaRef\(ProductReview\)}},\s{1,}},\s{1,}},\s{1,}}\)/, + /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,\s+\)/, + ]; + postCreateRegEx.forEach(regex => { + assert.fileContent(expectedFile, regex); + }); +} + +/** + * Check the contents for operation 'create' when id is not required. + */ +function checkCreateContents() { + const postCreateRegEx = [ + /\@post\('\/product-reviews', {/, + /responses: {/, + /'200': {/, + /description: 'ProductReview model instance'/, + /content: {'application\/json': {schema: getModelSchemaRef\(ProductReview\)}},\s{1,}},\s{1,}},\s{1,}}\)/, + /async create\(\s+\@requestBody\({\s+content: {\s+'application\/json': {\s+schema: getModelSchemaRef\(ProductReview, {\s+title: 'NewProductReview',\s+}\),\s+},\s+},\s+}\)\s+productReview: ProductReview,\s+\)/, + ]; + postCreateRegEx.forEach(regex => { + assert.fileContent(expectedFile, regex); + }); +} + /** * Assertions against the template to determine if it contains the * required signatures for a REST CRUD controller, specifically to ensure * that decorators are grouped correctly (for their corresponding * target functions) */ -function checkRestCrudContents() { +function checkRestCrudContents(options) { assert.fileContent(expectedFile, /class ProductReviewController/); // Repository and injection @@ -243,17 +290,11 @@ function checkRestCrudContents() { // Assert that the decorators are present in the correct groupings! // @post - create - const postCreateRegEx = [ - /\@post\('\/product-reviews', {/, - /responses: {/, - /'200': {/, - /description: 'ProductReview model instance'/, - /content: {'application\/json': {schema: getModelSchemaRef\(ProductReview\)}},\s{1,}},\s{1,}},\s{1,}}\)/, - /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,\s+\)/, - ]; - postCreateRegEx.forEach(regex => { - assert.fileContent(expectedFile, regex); - }); + if (options && options.idOmitted) { + checkCreateContentsWithIdOmitted(); + } else { + checkCreateContents(); + } // @get - count const getCountRegEx = [