The following scenario does not follow RESTful standards and would be keen to know how best to structure my API to achieve the same goal.
For a given GET request against a resource, e.g. GET /api/person/1
, if the principle contains a claim I would like to return additional properties.
E.g.
GET /api/person/1 (Without IsAdmin claim)
{
name: "Buck Rogers",
dateOfBirth: 2000-01-01T00:00:00.000
}
GET /api/person/1 (With IsAdmin claim)
{
name: "Buck Rogers",
dateOfBirth: 2000-01-01T00:00:00.000,
adminNote: "Something private"
}
So I'd conditionally be returning two different DTOs for the same resource request, which isn't allowed.
How can I achieve this in a RESTful way?
Update:
It was suggested I could define the adminNote
property and NULL it based on the condition. How would I deal with the case where there might be multiple conditionals that determine which properties are included? E.g.
GET /api/person/1 (With IsModerator claim)
{
name: "Buck Rogers",
dateOfBirth: 2000-01-01T00:00:00.000,
moderatorNote: "Something else private"
}
I would be keen to avoid adding extra properties that will only ever not be null in one particular case.
adminNote
as "optional, valued only for admins" or something along those lines. – WilseranyOf
for instance, see swagger.io/docs/specification/data-models/oneof-anyof-allof-not. Whether you use two distinct DTOs or a single one with optional fields though is a very technical concern IMHO, I'm not sure REST goes that deep in the implementation details. I still see a single resource personally. I get your point though, interested to see other opinions about that :) – Wilser/api/person/1
suits both cases, meaning we're actually representing the same resource. If this really bothers you, you could still have a query paramview
that would takeminimal
ordetailed
for instance, but then it's more job for the callers, plus again, it's a very "technical param" (maybe more the job of a header, e.g.Accept
?). I wouldn't bother too much though personally, a well written documentation should do the job :) – Wilser