Skip to content

Comments

feat:data shaping#196

Merged
fvanderflier merged 7 commits intomainfrom
feat/data-shaping
Dec 4, 2025
Merged

feat:data shaping#196
fvanderflier merged 7 commits intomainfrom
feat/data-shaping

Conversation

@DavidPavlovski
Copy link
Collaborator

@DavidPavlovski DavidPavlovski commented Nov 26, 2025

Implemented data shaping trough query params.
The fields parameter is optional. If left empty we will get all of the properties of the requested resource.

Example:

/api/Users/e3171502-622c-4a05-beb8-0c4c37f48401
The response will be:

{
   "id": "e3171502-622c-4a05-beb8-0c4c37f48401"
   "userName": "exampleUser",
   "email": "email@example.com"
}

but if we add the fields parameter:
/api/Users/e3171502-622c-4a05-beb8-0c4c37f48401?fields=userName

The response will only contain the requested properties

{
   "userName": "exampleUser"
}

Also we can separate field names with comma to select multiple properties:

/api/Users/e3171502-622c-4a05-beb8-0c4c37f48401?fields=id,userName

The response will only contain the requested properties

{
   "id": "e3171502-622c-4a05-beb8-0c4c37f48401",
   "userName": "exampleUser"
}

closes #197

{
public static class IEnumerableExtensions
{
public static IEnumerable<ExpandoObject> ShapeData<TSource>(this IEnumerable<TSource> source, string? fields)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I follow the logic behind having almost identical lines of code in both extensions? Can't we just re-use the generic or object extension here with using smth like source.Select(o => o.ShapeObject)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can do that, but the main reason behind why its written like this initially is optimization purposes. In the IEnumerable extension the propertyInfo list is cached since it is an expensive operation and doing it for every single element in the list would be very performance heavy. It can be changed, but this is the main reason why its made this way.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This issue has been resolved. Now instead of using extension methods, we have a DataShaper service, and we are caching the propertyInfos inside a dictionary field. And we only have one implementation of the data shaping logic instead of the two as we had before


namespace Dappi.HeadlessCms.Extensions
{
public static class ObjectExtensions
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a suggestion: I would call this something like DataShaper for example. First of all because it doesn't really extend object but it is a generic.

@fvanderflier
Copy link
Collaborator

fvanderflier commented Nov 26, 2025

Like the idea overall. Would suggest some unit tests to find some hidden and nasty edge-cases if they exist.

@DavidPavlovski
Copy link
Collaborator Author

Like the idea overall. Would suggest some unit tests to find some hidden and nasty edge-cases if they exist.

Unit tests are implemented, we have a lot of coverage for this part. We will see if any bugs arise in the future.

@fvanderflier fvanderflier merged commit 6c7cd51 into main Dec 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Data Shaping

2 participants