Hi silverbullet2
Discriminators would generally not be used where you want to allow the consumer describe the shape of the response representation. Think of them more as a shortcut to possibly allow a consumer make a choice from a selection of possible pre-defined schemas. This does not really fit into the example you describe as you just want to reduce the representation of a single schema.
There is no definitive right way to achieve your goal, but a common technique is to have a query parameter (named something like ‘fields’ or ‘properties’ or ‘attributes’ or ‘select’) to allow a consumer specify a subset of properties they would like to be returned with a successful response, rather than the full representation of the resource.
It’s up to you as the API provider to decide what query parameter naming to choose, and to decide if you limit to root level properties or also enable the consumer to select subset of properties within nested objects or arrays that may be returned in the representation. If you’re going to allow selection to subsets within nested objects or arrays, then it would be common to apply a sub-selector expression (generally this would be based on XPath syntax or object.member dot notation)
Some examples:
http://someapiurl.com?fields=color,name –> return all pets and only the color and name properties
http://someapiurl.com?fields=color,name,location/city OR http://someapiurl.com?fields=color,name,location.city –> return all pets and only the color and name root level properties as well as the city property from the location object.
http://someapiurl.com?fields=color,name,hobbies(id, name) –> return all pets and only return the color, name properties, and just the id and name properties for items in the hobbies array
It’s also relatively common that providers leverage bits and pieces from specifications like OData, SCIM or syntaxes like Lucene for advanced filtering/querying (these approaches can increase complexity however).
Things to keep in mind:
- URLs have limits so work to ensure that consumers can not hit URL limits (good practice is to ensure that any combination of params will not result in length greater than ~2000 characters)
- You will need to validate request to ensure that the selector parameter is valid and return the appropriate 400 series HTTP Status code if it contains an error or is otherwise invalid
If you wanted to go full query orientated, then you could consider GraphQL which has it’s own pro’s an con’s.
In general, I would always promote good design where the minimal representation to address the business goal is returned. Separate your APIs into separate resources (or sub-resources), giving your consumer the ability to make more concise calls. Progressively enhance the consumer experience with options to expand or embed additional resources when appropriate.
Ensure you leverage HTTP caching when/where possible to ensure that consumers can cache representations while its safe to do so.
I hope this helps you achieve what you need to do with your API.