Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/model with id required #3667

Merged
merged 1 commit into from
Sep 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/cli/generators/controller/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>);
}
Expand Down
69 changes: 55 additions & 14 deletions packages/cli/test/integration/generators/controller.integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, () =>
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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<ProductReview, 'productId'>,\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
Expand All @@ -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<ProductReview, 'productId'>,\s+\)/,
];
postCreateRegEx.forEach(regex => {
assert.fileContent(expectedFile, regex);
});
if (options && options.idOmitted) {
checkCreateContentsWithIdOmitted();
} else {
checkCreateContents();
}

// @get - count
const getCountRegEx = [
Expand Down