#scrambledrop: Scramble 0.11.12

#scrambledrop: Scramble 0.11.12

September 4, 2024

Enhancing JSON API resource documentation with improved type inference and added support for other resource's features.

Hey!

Scramble 0.11.12 now accurately documents model’s method calls in JSON API resources. This release introduces support for withResponse, parent::toArray(), and array_merge, enhancing the accuracy of your API documentation.

Improved type inference of method calls of underlying models in JSON API resources

Laravel’s JSON API resources are a great way to prepare payload for responses. You’ve probably written code like this in resource’s toArray method:

return [
'active_accounts' => $this->resource->getActiveAccountsCount(),
];

Or like this:

return [
'active_accounts' => $this->getActiveAccountsCount(),
];

Both calls to getActiveAccountsCount gets proxied to the underlying model instance.

Due to model’s type inference being tricky, previously Scramble would silently discard the calls to the models and document these attributes as string.

New version brings the improvement for model’s method calls type inference. Now all these cases will be correctly handled and documented.

Added withResponse support for JSON API resources

Using withResponse method in a resource, you can customize the resulting response.

From this version Scramble will analyze and document the response properly.

public function withResponse($request, JsonResponse $response)
{
$response->setStatusCode(419);
}

Added parent::toArray() support for JSON API resources

Previously, you had to return an array node from toArray method of the resource. Now, Scramble understand parent::toArray() expression in that method. Returning it will result in an array identical to the model’s toArray.

Added array_merge inference support

Combining with the parent::toArray() support, array_merge is a nice way to add some extra attributes to the JSON API resources. So the following will work as expected:

return array_merge(parent::toArray($request), [
'foo' => $this->getFoo(),
]);

Fixes

  • Array unpacking support in JSON API resource’s toArray
  • some validation rule documentation improvements

Thanks!

Hope you like it! Feel free to leave some stars on GitHub!

Thanks!

Scramble PRO
Comprehensive API documentation generation for Spatie’s Laravel Data and Laravel Query Builder.