]> BookStack Code Mirror - bookstack/commitdiff
Attachment API: Fixed error when name not provided in update
authorDan Brown <redacted>
Mon, 9 Dec 2024 11:32:15 +0000 (11:32 +0000)
committerDan Brown <redacted>
Mon, 9 Dec 2024 11:32:15 +0000 (11:32 +0000)
Fixes #5353

app/Uploads/AttachmentService.php
tests/Api/AttachmentsApiTest.php

index 033f2334104b44f008d0ce29ecdc84bc68a33117..dabd537292f4823eb7a93faada344f59cc2b2a93 100644 (file)
@@ -116,16 +116,18 @@ class AttachmentService
      */
     public function updateFile(Attachment $attachment, array $requestData): Attachment
     {
-        $attachment->name = $requestData['name'];
-        $link = trim($requestData['link'] ?? '');
+        if (isset($requestData['name'])) {
+            $attachment->name = $requestData['name'];
+        }
 
+        $link = trim($requestData['link'] ?? '');
         if (!empty($link)) {
             if (!$attachment->external) {
                 $this->deleteFileInStorage($attachment);
                 $attachment->external = true;
                 $attachment->extension = '';
             }
-            $attachment->path = $requestData['link'];
+            $attachment->path = $link;
         }
 
         $attachment->save();
index b03f280ac6784074ab692df20b3ded892d6c8f32..b234658792482b76bcd89717d1e250f51ba6fb68 100644 (file)
@@ -12,7 +12,7 @@ class AttachmentsApiTest extends TestCase
 {
     use TestsApi;
 
-    protected $baseEndpoint = '/api/attachments';
+    protected string $baseEndpoint = '/api/attachments';
 
     public function test_index_endpoint_returns_expected_book()
     {
@@ -302,6 +302,23 @@ class AttachmentsApiTest extends TestCase
     }
 
     public function test_update_file_attachment_to_link()
+    {
+        $this->actingAsApiAdmin();
+        $page = $this->entities->page();
+        $attachment = $this->createAttachmentForPage($page);
+
+        $resp = $this->putJson("{$this->baseEndpoint}/{$attachment->id}", [
+            'link' => 'https://example.com/donkey',
+        ]);
+
+        $resp->assertStatus(200);
+        $this->assertDatabaseHas('attachments', [
+            'id' => $attachment->id,
+            'path' => 'https://example.com/donkey',
+        ]);
+    }
+
+    public function test_update_does_not_require_name()
     {
         $this->actingAsApiAdmin();
         $page = $this->entities->page();
Morty Proxy This is a proxified and sanitized view of the page, visit original site.