Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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 src/Http/Requests/DestroyRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Lomkit\Rest\Http\Requests;

use Illuminate\Validation\Rule;
use Lomkit\Rest\Http\Resource;

class DestroyRequest extends RestRequest
Expand Down Expand Up @@ -32,10 +33,15 @@ public function rules()
*/
public function destroyRules(Resource $resource)
{
$model = $resource::newModel();

return [
'resources' => [
'required', 'array',
],
'resources.*' => [
Rule::exists($model->getTable(), $model->getKeyName()),
],
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

🧩 Analysis chain

422 vs 404 semantics — confirm product intent.

This change makes non‑existent IDs return 422 (validation). The linked issue mentions “e.g., 404”. For bulk DELETE via body, 422 is reasonable; just confirm this is the intended contract.


🏁 Script executed:

#!/bin/bash
# Find any docs/tests expecting 404 for non-existent deletes
rg -n -C2 -i 'delete.+(404|not found)|assertStatus\(\s*404\s*\)|assertNotFound' --type=php

Length of output: 336


422 vs 404 semantics — confirm product intent.

Tests assert 404 for non‑existent deletes, but this change causes a 422 validation response; reconcile contract.

  • Tests: tests/Feature/Controllers/ActionsOperationsTest.php:103 (assertStatus(404)).
  • Code: src/Http/Requests/DestroyRequest.php (lines 36–44) — 'resources.*' uses Rule::exists(...) causing validation failure -> 422.

Either restore 404 semantics (handle missing IDs and return 404) or update tests/docs to accept 422.

];
}
}
22 changes: 22 additions & 0 deletions tests/Feature/Controllers/DeleteOperationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,28 @@ public function test_deleting_a_non_authorized_model_with_an_authorized_one(): v
$this->assertDatabaseHas('models', $modelDeletable->only('id'));
}

public function test_deleting_a_not_existing_model(): void
{
$model = ModelFactory::new()->count(1)->createOne();

Gate::policy(Model::class, GreenPolicy::class);

$response = $this->delete(
'/api/models',
[
'resources' => [
999999,
$model->getKey(),
],
],
['Accept' => 'application/json']
);

$response->assertStatus(422);
$response->assertExactJsonStructure(['message', 'errors' => ['resources.0']]);
$this->assertDatabaseHas('models', $model->only('id'));
}

public function test_deleting_a_model(): void
{
$model = ModelFactory::new()->count(1)->createOne();
Expand Down