dotarj/PartialResponse.AspNetCore.Mvc.Formatters.Json

Ability to ignore data wrappers and special responses

Opened this issue · 3 comments

The project really needs an configuration to decide whether to apply transformation or not and to what part of the data.
For example, Problem Details RFC https://tools.ietf.org/html/rfc7807 tells that all errors should be returned wrapped inside of Problem Details object.
Another example is data containers, if an endpoint returns Companies the data for collection the data will be likely wrapped inside some object:

{
    paging: {
    ....
    },
    items: [
    ....
    ],
    other_metadata: {
    }
}

This could be implemented by just adding a static prefix in the configuration, this prefix would then be added to every field before being matched:

Configuration: {
  "PartialResponse": {
    "Prefix": "items",
    "AlwaysInclude": [ "other_metadata", "paging"]
  }
  [...]
}

https://my.api.domain.tld/my/endpoint?fields=id,name

This would end up in including other_metadata,paging,items/id,items/name in the final result.

I feel like @dotarj was not really happy in adding new configuration fields (see #1 :) ), but thanks to the IFieldsParser, this could now be done without any modification to the package, by implementing a custom IFieldsParser.

What do you think?

Hi @robin-thoni, you are completely right that I'm careful with expanding the API of the project to prevent breaking changes in the future. This feature, however, seems like it could benefit a lot of people, so I would really like to add it to the project.

Thanks you for the configuration suggestion. I think it is a good suggestion, but I'm only concerned with the single prefix in the example. Would it be better to make it an array, so multiple prefixes can be added, or would that be overkill? I'm not sure. What do you think @robin-thoni and @zihotki?

I currently can't see any use case where we would need multiple prefixes (maybe for errors, but why would you like to discard error fields?).

Question is then, if you have multiple prefixes, how do you handle them? Do you do a cartesian product with all fields?

Configuration: {
  "PartialResponse": {
    "Prefixes": ["items", "other_metadata"],
    "AlwaysInclude": [ "paging"]
  }
  [...]
}

https://my.api.domain.tld/my/endpoint?fields=id,name

Which gives: paging,items/id,items/name,other_metadata/id,other_metadata/name

Seems weird, isn't it?