Add support for expanding dot notation in Data objects and TypeScript transformer#1045
Open
menthol wants to merge 3 commits intospatie:mainfrom
Open
Add support for expanding dot notation in Data objects and TypeScript transformer#1045menthol wants to merge 3 commits intospatie:mainfrom
menthol wants to merge 3 commits intospatie:mainfrom
Conversation
c80deda to
42f0b31
Compare
Member
|
Like the idea, thanks for the PR! Few remarks:
If that's all resolved this one can me merged, note myself to check the docs and see how they fit in once merging. |
Author
|
I will update this PR as soon as possible |
594b7a2 to
208c2a8
Compare
Author
|
@rubenvanassche done |
- Introduced `expand_dot_notation` configuration in `data.php`, defaulting to `false`. - Updated `TransformedDataResolver` to handle dot notation expansion using `Arr::set()`. - Added documentation on nested data transformation with examples. - Implemented tests for both enabled and disabled dot notation expansion.
- Introduced `MapDotExpandedName` and `MapDotExpandedOutputName` attributes for handling nested data transformations. - Updated `MapName`, `MapOutputName`, and name resolver logic to support `expandDotNotation` functionality. - Modified tests and documentation to reflect new features.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR adds a new feature that allows dot notation in property names to be expanded into nested objects when using Data objects as resources and in TypeScript definitions. This is particularly useful when working with APIs that require or return nested JSON structures.
When using the
MapOutputNameorMapNameattributes with dot notation (e.g., 'user.name'), the package can now generate nested arrays in the JSON output and nested TypeScript interfaces that match the structure of your JSON.Configuration
The feature is controlled by a new configuration option in
config/data.php:Benefits
Examples
Basic Example
With
expand_dot_notationenabled, this generates the following JSON:{ "user": { "name": "John Doe", "profile": { "bio": "Software developer" } } }Instead of the flat structure:
{ "user.name": "John Doe", "user.profile.bio": "Software developer" }JSON:API Specification Example
When working with JSON:API specification, you often have relationships and attributes in a specific structure:
With
expand_dot_notationenabled, this generates a JSON structure that perfectly matches the JSON:API specification:{ "id": 1, "attributes": { "title": "My First Article", "content": "This is the content of my article", "published_at": "2023-01-15T10:00:00Z" }, "relationships": { "author": { "data": { "id": 1, "attributes": { "name": "John Doe" } } }, "comments": { "data": [ { "id": 1, "attributes": { "body": "Great article!" } } ] } } }