Note that there are some explanatory texts on larger screens.

plurals
  1. POServiceStack support for conditionally omitting fields from a REST response on a per-call basis
    text
    copied!<p><strong><code>&lt;TL;DR&gt;</code></strong></p> <p>At a minimum, I'm looking for a way to conditionally exclude certain properties on the resource from being included in the response on a per-call basis (See <code>fields</code> below). </p> <p>Ideally, I'd like to implement a REST service with ServiceStack that supports all the major points below. </p> <p><strong>UPDATE</strong><br/> While I really like ServiceStack's approach in general and would prefer to use it if possible, if it isn't particularly well suited towards these ideas I'd rather not bend over backwards bastardizing it to make it work. If that's the case, can anyone point to another c# framework that might be more appropriate? I'm actively exploring other options myself, of course.</p> <p><strong><code>&lt;/TD;DR&gt;</code></strong></p> <p>In this <a href="http://www.stormpath.com/blog/designing-rest-json-apis" rel="nofollow">talk</a> entitled <em>Designing REST + JSON APIs</em>, the presenter describes his strategy for Resource References (via <code>href</code> property on resources) in JSON. In addition to this, he describes two query parameters (<code>fields</code> and <code>expand</code>) for controlling what data is included the response of a call to a REST service. I've been trying without success to dig into the ServiceStack framework to achieve support for <code>fields</code> in particular but have thus far been unsuccessful. Is this currently possible in ServiceStack? Ideally the solution would be format agnostic and would therefore work across all of ServiceStack's supported output formats. I would imagine <code>expand</code> would follow the same strategy. </p> <p>I'll describe these features here but I think the talk at the link does a better job of explaining them.</p> <p>Lets say we have an Profiles resource with the following properties: <code>givenName</code>, <code>surname</code>, <code>gender</code>, and <code>favColor</code>. The Profiles resource also includes a list of social networks the user belongs to in the <code>socialNetworks</code> property. </p> <p><strong><code>href</code></strong> - (42:22 in video) Every resource includes a full link to it on the REST service. A call to <code>GET /profiles/123</code> would return </p> <pre><code>{ "href":"https://host/profiles/123", "givenName":"Bob", "surname":"Smith", "gender":"male", "favColor":"red", "socialNetworks": { "href":"https://host/socialNetworkMemberships?profileId=123" } } </code></pre> <p>Notice that the <code>socialNetworks</code> property returns an object with just the href value populated. This keeps the response short and focused while also giving the end user enough information to make further requests if desired. The <code>href</code> property, used across the board in this manor, makes it easy (conceptually anyway) to reuse resource data structures as children of other resources.</p> <p><strong><code>fields</code></strong> - (55:44 in video) Query string parameter that instructs the server to only include the specified properties of the desired resource in the REST response. </p> <p>A normal response from <code>GET /profiles/123</code> would include all the properties of the resource as seen above. When the <code>fields</code> query param is included in the request, only the fields specified are returned. 'GET /propfiles/123?fields=surname,favColor' would return </p> <pre><code>{ "href":"https://host/profiles/123", "surname":"Smith", "favColor":"red" } </code></pre> <p><strong><code>expand</code></strong> - (45:53 in video) Query string parameter that instructs the server to flesh out the specified child resources in the result. Using our example, if you were to call <code>GET /profiles/123?expand=socialNetworks</code> you might receive something like</p> <pre><code>{ "href":"https://host/profiles/123", "givenName":"Bob", "surname":"Smith", "gender":"male", "favColor":"red", "socialNetworks": { "href":"https://host/socialNetworkMemberships?profileId=123", "items": [ { "href":"https://host/socialNetworkMemberships/abcde", "siteName":"Facebook", "profileUrl":"http://www.facebook.com/..." }, ... ] } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload