timacdonald / json-api

A lightweight API resource for Laravel that helps you adhere to the JSON:API standard. Supports sparse fieldsets, compound documents, and more.
581 stars 38 forks source link

Using with non Model resources. #26

Closed juliomotol closed 1 year ago

juliomotol commented 1 year ago

We made a JsonApiResource for our settings class (from spatie/laravel-settings)

/** @property-read \Spatie\LaravelSettings\Settings $resource */
class SettingResource extends JsonApiResource
{
    public function toId(Request $request): string
    {
        return $this->resource::group();
    }

    public function toType(Request $request): string
    {
        return 'settings';
    }

    public function toAttributes(Request $request): array
    {
        return $this->resource->toArray();
    }
}

This used to work before, but when we updated to v1.0.0-beta.2 it broke. Here is the stack trace:

ErrorException: Undefined property: App\Settings\SiteSettings::$attributes in /home/halcyon-pc75/code/laravel/saas-ecommerce/vendor/spatie/laravel-settings/src/Settings.php:92
  Stack trace:
  #0 /home/halcyon-pc75/code/laravel/saas-ecommerce/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(268): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError()
  #1 /home/halcyon-pc75/code/laravel/saas-ecommerce/vendor/spatie/laravel-settings/src/Settings.php(92): Illuminate\Foundation\Bootstrap\HandleExceptions->Illuminate\Foundation\Bootstrap\{closure}()
  #2 /home/halcyon-pc75/code/laravel/saas-ecommerce/vendor/laravel/framework/src/Illuminate/Http/Resources/DelegatesToResource.php(139): Spatie\LaravelSettings\Settings->__get()
  #3 /home/halcyon-pc75/code/laravel/saas-ecommerce/vendor/timacdonald/json-api/src/Concerns/Attributes.php(73): Illuminate\Http\Resources\Json\JsonResource->__get()
  #4 /home/halcyon-pc75/code/laravel/saas-ecommerce/vendor/timacdonald/json-api/src/Concerns/Attributes.php(60): TiMacDonald\JsonApi\JsonApiResource->resolveAttributes()
  #5 /home/halcyon-pc75/code/laravel/saas-ecommerce/vendor/timacdonald/json-api/src/JsonApiResource.php(129): TiMacDonald\JsonApi\JsonApiResource->requestedAttributes()
  #6 /home/halcyon-pc75/code/laravel/saas-ecommerce/vendor/laravel/framework/src/Illuminate/Http/Resources/Json/JsonResource.php(95): TiMacDonald\JsonApi\JsonApiResource->toArray()
  #7 /home/halcyon-pc75/code/laravel/saas-ecommerce/vendor/laravel/framework/src/Illuminate/Http/Resources/Json/ResourceResponse.php(39): Illuminate\Http\Resources\Json\JsonResource->resolve()
  #8 /home/halcyon-pc75/code/laravel/saas-ecommerce/vendor/laravel/framework/src/Illuminate/Http/Resources/Json/JsonResource.php(231): Illuminate\Http\Resources\Json\ResourceResponse->toResponse()
  #9 /home/halcyon-pc75/code/laravel/saas-ecommerce/vendor/timacdonald/json-api/src/JsonApiResource.php(187): Illuminate\Http\Resources\Json\JsonResource->toResponse()

Its trying to get the $attribute of the settings class but that doesn't exists.

https://github.com/timacdonald/json-api/blob/c32663263545b1dc80a5708f92507a6fa2279a90/src/Concerns/Attributes.php#L69-L76

timacdonald commented 1 year ago

Thank you for reporting this.

Fixed in: https://github.com/timacdonald/json-api/commit/deb13fa2d3375f50a1aec8fb90c822161ce2f364

Available in the latest tag: https://github.com/timacdonald/json-api/releases/tag/v1.0.0-beta.3