Conditonally return different REST API response object for same endpoint

Viewed 1559

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.

1 Answers

you can create a Person resource with all the three fields and when the IsAdmin is false or without claim , you could set adminNotes to null.And to not return that property you can use @JsonInclude(Include.NON_NULL) (its for java, something like this might be available in asp.net) as well.So you don't need to create two separate entities or DTO's.

Related