Add support for expanding dot notation in Data objects and TypeScript transformer#1045
Add support for expanding dot notation in Data objects and TypeScript transformer#1045menthol wants to merge 3 commits intospatie:mainfrom
Conversation
c80deda to
42f0b31
Compare
|
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. |
|
I will update this PR as soon as possible |
594b7a2 to
208c2a8
Compare
|
@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.
|
Hi @menthol, we're really sorry for leaving this PR hanging for so long and then not merging it, that's not fair to you and the effort you put into this. In the meantime we've released v3 of typescript-transformer and are phasing out v2 support. The v3 architecture no longer uses Data* classes for building the TypeScript output but works with plain PHP instead, which makes it difficult to fit this in as-is. The data transformation side (expanding dot notation in output) is a nice feature though. You're welcome to give it another shot targeting v3 of the typescript-transformer, the changes would need to go in the A few pointers if you do:
Going to close this for now. Thanks for the effort, and again sorry for the wait! |
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!" } } ] } } }