laravel-notion-api icon indicating copy to clipboard operation
laravel-notion-api copied to clipboard

Updating Page fails with validation error from Notion

Open peterfox opened this issue 2 years ago • 0 comments

Describe the bug When trying to update a page, it fails with a client exception due to the incorrect JSON format sent to Notion.

To Reproduce

The following code:

$page = NotionFacade::pages()->find($id);

$page->set('Name', \FiveamCode\LaravelNotionApi\Entities\Properties\Title::value('Test here'));

NotionFacade::pages()->update($page);

Expected behaviour The code should save the page's properties.

Exceptions

1) Tests\Feature\NotionTest::test_example2
FiveamCode\LaravelNotionApi\Exceptions\NotionException: Bad Request: (validation_error) (body failed validation. Fix one:
body.properties.Name.id should be defined, instead was `undefined`.
body.properties.Name.headers should be not present, instead was `undefined`.
body.properties.Name.statusCode should be not present, instead was `400`.
body.properties.Name.body should be not present, instead was `{"object":"error","status":400,"code":"validation_error...`.
body.properties.Name.start should be defined, instead was `undefined`.)

/Users/peterfox/Code/Experiments/sushi-and-notion/vendor/laravel/framework/src/Illuminate/Http/Client/Response.php:272
/Users/peterfox/Code/Experiments/sushi-and-notion/vendor/fiveam-code/laravel-notion-api/src/Exceptions/NotionException.php:61
/Users/peterfox/Code/Experiments/sushi-and-notion/vendor/fiveam-code/laravel-notion-api/src/Endpoints/Endpoint.php:138
/Users/peterfox/Code/Experiments/sushi-and-notion/vendor/fiveam-code/laravel-notion-api/src/Endpoints/Pages.php:105
/Users/peterfox/Code/Experiments/sushi-and-notion/app/Models/Item.php:61
/Users/peterfox/Code/Experiments/sushi-and-notion/tests/Feature/NotionTest.php:27
/Users/peterfox/Code/Experiments/sushi-and-notion/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:174

Caused by
Illuminate\Http\Client\RequestException: HTTP request returned status code 400:
{"object":"error","status":400,"code":"validation_error","message":"body failed validation. Fix one:\nbody.properties.Na (truncated...)


/Users/peterfox/Code/Experiments/sushi-and-notion/vendor/laravel/framework/src/Illuminate/Http/Client/Response.php:272
/Users/peterfox/Code/Experiments/sushi-and-notion/vendor/fiveam-code/laravel-notion-api/src/Exceptions/NotionException.php:61
/Users/peterfox/Code/Experiments/sushi-and-notion/vendor/fiveam-code/laravel-notion-api/src/Endpoints/Endpoint.php:138
/Users/peterfox/Code/Experiments/sushi-and-notion/vendor/fiveam-code/laravel-notion-api/src/Endpoints/Pages.php:105
/Users/peterfox/Code/Experiments/sushi-and-notion/app/Models/Item.php:61
/Users/peterfox/Code/Experiments/sushi-and-notion/tests/Feature/NotionTest.php:27
/Users/peterfox/Code/Experiments/sushi-and-notion/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:174

Additional context

I've played around with the properties, and the rawContent structure needs to be corrected. When using the following classes, the page is updated as expected.

<?php

namespace App\Notion;

class Title extends \FiveamCode\LaravelNotionApi\Entities\Properties\Title
{
    public static function value($text): \FiveamCode\LaravelNotionApi\Entities\Properties\Title
    {
        $titleProperty = parent::value($text);

        $titleProperty->rawContent = [
            0 => [
                'text' => [
                    'content' => $titleProperty->getRichText()->getPlainText(),
                ],
            ],
        ];

        return $titleProperty;
    }
}
<?php

namespace App\Notion;

class Url extends \FiveamCode\LaravelNotionApi\Entities\Properties\Url
{
    public static function value(string $url): \FiveamCode\LaravelNotionApi\Entities\Properties\Url
    {
        $urlProperty = parent::value($url);

        $urlProperty->rawContent = $url;

        return $urlProperty;
    }
}

My guess is Notion has changed the format of the API and the package doesn't take account of that when it comes to updating.

peterfox avatar Aug 19 '23 19:08 peterfox