pjadanowski / scramble-extras
Spatie Laravel Data + Query Builder integration for dedoc/scramble. A free, open replacement for scramble-pro covering output/input Data schemas, all Spatie validation attributes, query-builder filters/sorts/includes, jsonPaginate, and persistent schema caching.
Requires
- php: ^8.2
- dedoc/scramble: ^0.13.0
- illuminate/console: ^12.0 || ^13.0
- illuminate/database: ^12.0 || ^13.0
- illuminate/support: ^12.0 || ^13.0
- nikic/php-parser: ^4.0 || ^5.0
- spatie/laravel-data: ^4.0
Requires (Dev)
- orchestra/testbench: ^10.4 || ^11.0
- phpunit/phpunit: ^11.5 || ^12.0
- spatie/laravel-query-builder: ^6.0 || ^7.0
Suggests
- spatie/laravel-query-builder: Required to document `allowedFilters/Sorts/Includes/Fields`, `defaultSort`, and exact-enum filter detection.
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-23 07:53:50 UTC
README
A free, open Laravel package that adds full Spatie ecosystem support to dedoc/scramble — covering everything scramble-pro does for the integrations most projects actually need.
Features
- Spatie Laravel Data — output and input (request body) schemas auto-generated from your
Dataclasses. HonorsMapName/MapInputName/MapOutputNameanddata.name_mapping_strategy(names come from laravel-data's own metadata),Optional,Lazy,#[Computed],#[Hidden],#[DataCollectionOf], reads@example/@defaultfrom property phpdoc, and adds the constraints of a staticrules()method to request schemas. - All ~80 Spatie validation attributes → OpenAPI fields —
#[Email]→format: email,#[Min(3)]→minLength(minItemson arrays),#[GreaterThan(0)]→exclusiveMinimum,#[In(...)]/#[Enum(SomeEnum::class)]→enum,#[Regex]→pattern,#[Image]→contentMediaType: image/*, plus description notes for#[Same],#[Confirmed],#[Exists],#[RequiredIf], etc. PaginatedDataCollection<T>,CursorPaginatedDataCollection<T>,DataCollection<T>— the response envelopes laravel-data actually returns (data+links+meta, for length-aware and cursor pagination) / plain array, plus thepage/cursorquery parameter they read.- Spatie Laravel Query Builder —
allowedFilters/allowedSorts/allowedIncludes/allowedFields/defaultSort/jsonPaginate()parsed via AST — in the route method, the methods it calls andQueryBuildersubclasses it uses, with lists from local variables — includingAllowedFilter::*/AllowedSort::*/AllowedInclude::*builders with chained modifiers and the implicit{relation}Count/{relation}Existsincludes.AllowedFilter::exact($column)on an enum-cast Eloquent column ($castsorcasts()) emitsenum: [...]automatically. - Persistent schema cache — file-based, invalidated per class when its source (or a parent class, trait or attribute-referenced class) changes.
php artisan scramble-extras:cache:clearto wipe. - 422 for
Data-typed actions — a controller action type-hinted with aDataclass is validated the same way aFormRequestwould be, so it now documents a422response the same way core Scramble already does forFormRequest. whereInroute constraints —Route::whereIn('kind', ['a', 'b'])(including backed enum cases) documents the path parameter asenum: [a, b], keeping its type and default.PATCHdocumented alongsidePUT— Scramble's default route-methods resolver only documents the first HTTP method a route responds to, soRoute::apiResource()'s update action (registered for bothPUTandPATCH) only ever got aputoperation. Opt out withSCRAMBLE_EXTRAS_EXPAND_ROUTE_METHODS=false. Requires adedoc/scrambleversion withGeneratorConfig::resolveOperationMethodsUsing()(added after this package's 0.13.0 floor); on older installs this feature is a no-op rather than an error.
Requirements
- PHP 8.2+
- Laravel 12 or 13
dedoc/scramble^0.13.0spatie/laravel-data^4.0- optionally
spatie/laravel-query-builder(tested with 6.x and 7.x)
Install
composer require pjadanowski/scramble-extras
The service provider auto-registers all extensions via Laravel's package discovery — no config touching needed. Install dedoc/scramble itself if you haven't already, and you're done: your Data classes start showing up in the generated docs.
To document allowedFilters/Sorts/Includes/Fields you also need spatie/laravel-query-builder (the query-builder extension self-registers only when that package is present):
composer require spatie/laravel-query-builder
Pairs well with Atlas
Atlas is a drop-in renderer for Scramble: fast, fully static, three-column API docs with search, a try-it console and code samples in 12 clients. scramble-extras makes the generated OpenAPI document complete (Data schemas, validation constraints, query-builder parameters), and Atlas renders it.
composer require pjadanowski/atlas
Then set 'renderer' => 'atlas' in config/scramble.php. See the Atlas README for details.
Configuration
There's nothing to configure to get started. If you want to tune the cache, publish the config:
php artisan vendor:publish --tag=scramble-extras-config
// config/scramble-extras.php return [ 'cache' => [ 'enabled' => env('SCRAMBLE_EXTRAS_CACHE', true), 'path' => env('SCRAMBLE_EXTRAS_CACHE_PATH'), // null = storage/framework/cache/scramble-extras/schemas.php ], ];
Or just set the env vars — e.g. disable the cache while iterating on Data classes locally:
SCRAMBLE_EXTRAS_CACHE=false
How it plugs in
The package boots in two ways:
- Calls
Scramble::registerExtensions(...)in its service provider, contributing TypeToSchema, OperationExtension, and InferExtension classes that Scramble picks up automatically. - Calls
Scramble::configure()->withParametersExtractors(...)to prepend a Data-aware parameter extractor for request bodies typed withDatasubclasses.
You don't have to add anything to config/scramble.php.
Cache
The package transparently caches the response (output) schemas it builds from Data classes in storage/framework/cache/scramble-extras/schemas.php. An entry is rebuilt when the Data class's file changes, or the file of a parent class, a trait, or a class named in one of its attributes (e.g. #[Enum(StatusEnum::class)]). The cache file also carries a signature derived from the installed versions of Scramble, spatie/laravel-data and this package, so it self-invalidates after an upgrade — no manual clear required. Request body schemas are always built fresh.
Writes are deferred: a full export builds every schema in memory and persists the cache once on shutdown, rather than rewriting the file per class.
To wipe manually:
php artisan scramble-extras:cache:clear
Request bodies vs responses
A Data class is often used as both a response and a request body, and the two projections differ — #[Computed] is output-only, #[Hidden] is input-only, and Optional / #[MapInputName] change required-ness and field names. To keep both faithful, request body schemas are inlined (rendered directly on the operation) instead of $ref-ing the shared output component, and so are Data classes nested inside them, each with its own input projection. Response schemas remain reusable $ref components. (A Data class that contains itself, directly or indirectly, references its component at the point where it recurses.)
A Data class with UploadedFile properties is documented as a multipart/form-data body: files are binary fields, and nested values use the bracket keys PHP parses back into arrays (address[city], tags[]).
On GET, DELETE and HEAD routes, where laravel-data reads the query string, a Data argument is documented as one query parameter per property.
What's NOT covered (yet)
Long-tail features still on the roadmap:
- Sparse fieldsets rendered as proper enum array params (currently emitted as
stringwith allowed values in the description) - Polymorphic
Datawith discriminator schemas
Testing
composer install
composer test
The suite covers the attribute → OpenAPI translation, the query-builder AST extraction, the schema cache, the return-type inference, and a full end-to-end Scramble generation pass (request bodies, response $refs, paginated envelopes, and query parameters) booted with orchestra/testbench.
License
MIT
