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
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
8 changes: 8 additions & 0 deletions docs/site/Model.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ class MyFlexibleModel extends Entity {
}
```

The default response for a delete request to a non-existent resource is a `404`.
You can change this behavior to `200` by setting `strictDelete` to `false`.

```ts
@model({settings: {strictDelete: false}})
class Todo extends Entity { ... }
```

### Model Decorator

The model decorator can be used without any additional parameters, or can be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,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.

);
this.modelClass.attachTo(dataSource);
}
Expand Down
30 changes: 30 additions & 0 deletions packages/repository/test/acceptance/repository.acceptance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,36 @@ describe('Repository in Thinking in LoopBack', () => {
expect(stored).to.containDeep({extra: 'additional data'});
});

it('enables strict delete by default', async () => {
await repo.create({slug: 'pencil'});
await expect(repo.deleteById(10000)).to.be.rejectedWith(
/No instance with id/,
);
});

it('disables strict delete via configuration', async () => {
@model({settings: {strictDelete: false}})
class Pencil extends Entity {
@property({id: true})
id: number;
@property({type: 'string'})
name: string;
}

const pencilRepo = new DefaultCrudRepository<
Pencil,
typeof Pencil.prototype.id
>(Pencil, new DataSource({connector: 'memory'}));

await pencilRepo.create({
name: 'Green Pencil',
});

// When `strictDelete` is set to `false`, `deleteById()` on a non-existing
// resource is resolved with `false`, instead of being rejected.
await expect(pencilRepo.deleteById(10000)).to.be.fulfilledWith(false);
});

function givenProductRepository() {
const db = new DataSource({
connector: 'memory',
Expand Down