This PR addresses an issue with setting JSON wrappers on resource collections or other general-purpose JSON resource classes where adjusting the wrapper for an AnonymousResourceCollection or other generic resource class would affect all instances in the same request or following tests. This PR introduces instance-specific JSON wrapping to JsonResource through a new $wrapper property, along with withWrapper() and withoutWrapper() methods. This change allows developers to set a wrapper for individual resources without impacting others globally.
Example of the Issue
Currently, modifying the wrapper for a resource collection applies globally within the same request or test:
// Setting a wrapper for a resource collection
AnonymousResourceCollection::wrap('posts');
// This affects ALL instances of PostResource in the same request
$postsCollection = PostResource::collection($posts); // Wrapped with 'posts'
$booksCollection = BookResource::collection($books); // Also wrapped with 'posts'
With this PR, the wrapper can be set on a per-instance basis:
// Per-instance wrapper configuration
$postsCollection = PostResource::collection($posts)->withWrapper('posts');
$booksCollection = BookResource::collection($books)->withWrapper('books');
// Result: Different wrappers for different instances
// $postsCollection is wrapped with 'posts'
// $booksCollection is wrapped with 'books'
Benefits
Fine-Grained Control: Customize JSON wrappers for each resource instance, preventing unintended global side effects.
More Flexible API Design: Easier to handle unique formatting requirements on a per-instance basis, particularly useful when working with collections.
Consistency in Tests: Ensures that modifications to one test case's wrapper configuration won't carry over to others, making tests more reliable and isolated.
Why This Change Doesn't Break Existing Features
The default behavior remains the same because the new instance-specific $wrapper property respects the global $wrap setting unless overridden. Existing resource configurations and behaviors are not altered unless explicitly changed.
How It Makes Building Web Applications Easier
Instance-Specific Adjustments: Developers can define how each resource or collection is wrapped, reducing complexity and avoiding the need for duplicate resource classes just to modify the wrapper behavior.
Better Test Coverage: Isolated control over wrappers simplifies testing, allowing for more focused test cases without unintended side effects.
Simpler Code: Reduces the need for workarounds or extra conditionals to handle resource-specific JSON wrapping, leading to cleaner and more maintainable code.
Overview
This PR addresses an issue with setting JSON wrappers on resource collections or other general-purpose JSON resource classes where adjusting the wrapper for an
AnonymousResourceCollection
or other generic resource class would affect all instances in the same request or following tests. This PR introduces instance-specific JSON wrapping toJsonResource
through a new$wrapper
property, along withwithWrapper()
andwithoutWrapper()
methods. This change allows developers to set a wrapper for individual resources without impacting others globally.Example of the Issue
Currently, modifying the wrapper for a resource collection applies globally within the same request or test:
With this PR, the wrapper can be set on a per-instance basis:
Benefits
Why This Change Doesn't Break Existing Features
The default behavior remains the same because the new instance-specific
$wrapper
property respects the global$wrap
setting unless overridden. Existing resource configurations and behaviors are not altered unless explicitly changed.How It Makes Building Web Applications Easier