-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Conversation
@@ -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), |
There was a problem hiding this comment.
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),
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, indeed.
In LB3, users can do |
@@ -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), |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
.
bed8a75
to
edd77d7
Compare
@virkt25 @raymondfeng good to land? |
Any idea why coverage/coveralls would be affected by this? |
There was a problem hiding this 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.
I'm not sure but been wondering the same thing since I've noticed the coverage fluctuating on just |
@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. |
9129ed0
to
0c7bfaa
Compare
docs/site/Model.md
Outdated
cart: ShoppingCart; | ||
@property() email: string; | ||
@property() isMember: boolean; | ||
@property() cart: ShoppingCart; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
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); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
Made the previous change suggested by @bajtos. Anything else, anyone? |
name: 'Green Pencil', | ||
}); | ||
|
||
await expect(pencilRepo.deleteById(10000)).to.be.fulfilledWith(false); |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
).
@bajtos good to land? |
There was a problem hiding this 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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks 👍
Send 404 for delete requests to non-existent resource by default. Addresses issues like #1472
Checklist
npm test
passes on your machinepackages/cli
were updatedexamples/*
were updated