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

feat: default 404 for delete request to non-existent resource #1621

Merged
merged 1 commit into from
Aug 27, 2018

Conversation

hacksparrow
Copy link
Contributor

@hacksparrow hacksparrow commented Aug 20, 2018

Send 404 for delete requests to non-existent resource by default. Addresses issues like #1472

Checklist

  • npm test passes on your machine
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • API Documentation in code was updated
  • Documentation in /docs/site was updated
  • Affected artifact templates in packages/cli were updated
  • Affected example projects in examples/* were updated

@hacksparrow hacksparrow self-assigned this Aug 20, 2018
@hacksparrow hacksparrow changed the title fix: fix DELETE with invalid ID is successful #1472 [WIP] fix: fix DELETE with invalid ID is successful #1472 Aug 20, 2018
@@ -128,7 +128,7 @@ export class DefaultCrudRepository<T extends Entity, ID>
this.modelClass = dataSource.createModel<juggler.PersistedModelClass>(
definition.name,
properties,
Object.assign({strict: true}, definition.settings),
Object.assign({strict: true}, {strictDelete: true}, definition.settings),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's simpler to have Object.assign({strict: true, strictDelete: true}, definition.settings),.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, indeed.

@hacksparrow
Copy link
Contributor Author

hacksparrow commented Aug 20, 2018

In LB3, users can do <Model>.settings.strictDelete = false to override the default. Not sure how we would do this in LB4. Besides, we don't have the equivalent/corresponding page for Model definition JSON file, yet.

@@ -128,7 +128,7 @@ export class DefaultCrudRepository<T extends Entity, ID>
this.modelClass = dataSource.createModel<juggler.PersistedModelClass>(
definition.name,
properties,
Object.assign({strict: true}, definition.settings),
Object.assign({strict: true, strictDelete: true}, definition.settings),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm ... Based on the discussion we had in the issue I'm thinking it's fine to leave the code change out and just document how to set the strictDelete property on a model and what it means. A quick and simple change to the docs in this section (similar to the strict property): https://loopback.io/doc/en/lb4/Model.html#using-legacy-juggler should be enough imo. Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I am fine with that too. How do you do <Model>.settings.strictDelete = false in LB4, though?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same way you do it for strict: false in LoopBack 4.

Here's an example:

// Use strictDelete to return 404 if a delete fails.
@model({settings: {strictDelete: true}})
class Todo extends Entity { ... }

And now we can leave strictDelete as default instead of making it true.

@hacksparrow hacksparrow force-pushed the fix/#1472 branch 3 times, most recently from bed8a75 to edd77d7 Compare August 22, 2018 15:04
@hacksparrow hacksparrow changed the title [WIP] fix: fix DELETE with invalid ID is successful #1472 docs: document the behavior reported in #1472 Aug 22, 2018
@hacksparrow
Copy link
Contributor Author

@virkt25 @raymondfeng good to land?

@hacksparrow
Copy link
Contributor Author

Any idea why coverage/coveralls would be affected by this?

Copy link
Member

@bajtos bajtos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. As I wrote in #1472 (comment), I think it may be a good idea to change the default of strictDelete to true now that we have a chance to make breaking changes.

@virkt25
Copy link
Contributor

virkt25 commented Aug 24, 2018

Any idea why coverage/coveralls would be affected by this?

I'm not sure but been wondering the same thing since I've noticed the coverage fluctuating on just .md file changes ... even though coveralls is showing no decrease in a file when I look at the report.

@virkt25
Copy link
Contributor

virkt25 commented Aug 24, 2018

@hacksparrow Can you update this PR to make strictDelete the default and them the documentation should be updated to show users how to turn it off. Thanks. And just ping everyone for another review after.

@bajtos bajtos changed the title docs: document the behavior reported in #1472 docs: document the behavior of delete operations on non-existent resources Aug 24, 2018
@hacksparrow hacksparrow changed the title docs: document the behavior of delete operations on non-existent resources feat: default 404 for request to non-existent resource Aug 24, 2018
@hacksparrow hacksparrow force-pushed the fix/#1472 branch 2 times, most recently from 9129ed0 to 0c7bfaa Compare August 24, 2018 13:25
cart: ShoppingCart;
@property() email: string;
@property() isMember: boolean;
@property() cart: ShoppingCart;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VSC has applied these code formatting automatically. Is it OK, or should I exclude these changes?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, we should preserve the current style and revert these changes.

Could you please check that you have the latest version of the Prettier plugin for VS Code and that the plugin is configured to use the prettier version from the project? You may want to run npm run lerna:clean && npm install to ensure you have the latest version of prettier installed.

@hacksparrow
Copy link
Contributor Author

Time for another round of review.

it('enables strict delete by default', async () => {
const p = await repo.create({slug: 'pencil'});
p.name = 'Red Pencil';
await repo.save(p);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

repo.create has already created a persisted instance. No need to call repo.save. Please remove L76 and L77.


await pencilRepo.create({
name: 'Green Pencil',
} as AnyObject);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it necessary to explicitly cast the value to AnyObject? In the test above, you called repo.create({slug: 'pencil'}) with no type casts.

@hacksparrow
Copy link
Contributor Author

Made the previous change suggested by @bajtos. Anything else, anyone?

name: 'Green Pencil',
});

await expect(pencilRepo.deleteById(10000)).to.be.fulfilledWith(false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it fulfilledWith(false) ... shouldn't it be true?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is in strictDelete: false mode, and it cannot find id 10000; instead of rejecting it resolves with false.

Copy link
Contributor Author

@hacksparrow hacksparrow Aug 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's why default strictDelete: false is confusing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is in strictDelete: false mode, and it cannot find id 10000; instead of rejecting it resolves with false.

Could you please capture this information in a code comment?

I am expecting more readers to be confused about .fulfilledWith(false).

@hacksparrow hacksparrow changed the title feat: default 404 for request to non-existent resource feat: default 404 for delete request to non-existent resource Aug 27, 2018
@hacksparrow
Copy link
Contributor Author

@bajtos good to land?

Copy link
Member

@bajtos bajtos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

name: 'Green Pencil',
});

await expect(pencilRepo.deleteById(10000)).to.be.fulfilledWith(false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is in strictDelete: false mode, and it cannot find id 10000; instead of rejecting it resolves with false.

Could you please capture this information in a code comment?

I am expecting more readers to be confused about .fulfilledWith(false).

send 404 for requests to non-existent resoure by default.
Copy link
Member

@bajtos bajtos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants