Creating a POST OData Object with the Extension

I am trying to instantiate an object and two child objects at the same time.

If I send the next JSON to the / user _objects resource, it will happily create the parent user_object and the associated user_object_attribute. The only problem is that I cannot extend the result set to include the new user_object_attribute attribute, so I really have the most uptodate user_object version, but I must then go and read from the OData service, that is, another server, get user_object_attributes attributes.

Upon returning, I have an identifier that will accurately transition into the child object user_object_attribute, but what if the child has another field on the server side, for example created_date? Then do I still need to click on the OData repository again?

Have I tried the correct $ expand query? $ expand = user_objects_attributes, and as long as the creation succeeds, I still do not return an extended result set.

Can anyone think if this is possible? Or is it because this is not because the only way to do this is to return all child user_object_attributes?

{ "annotator_id":1, "content_type":"content_type", "created_date":"2013-02-15T17:20:09.191", "guid":"GUID", "size_kb":100, "title":"Title", "updated_date":null, "url":"URL", "user_object_id":0, "user_objects_attributes":[ { "attribute_id":0, "name":"name191", "user_object_id":0, "value":"value191" } ] } 

Thanks Mark.

+6
source share
2 answers

Looking at the OData V3 specification here :

In version 1.0 and version 2.0, if the insertion is completed successfully, the server MUST return a response with a status code of 201 (created) and a response body that corresponds to the syntax specified in the InsertEntity Request (section 2.2.7.1.1). The response body MUST contain the values ​​of the inserted resource after the server has completed all of its server-side data processing rules (validation, etc.). the server MAY change the values ​​of the resource received from the client before the resource is inserted into the server.

In version 3.0, the response MAY have a status code of 204, as specified in [RFC2616], based on client preferences (see "Preferences" (section 2.2.5.9)) in the InsertEntity request.

It is not very clear what the server should do ... return only the created top-level object, or this object and all its extended links.

I am not surprised that $ expand will not affect POST (or any CUD requests). WCF DS will probably ignore it if it is not a request. And by specification, this is probably correct.

Honestly, I think that with the WCF DS server you can’t return anything else. In V3, you either get 201 with your entity (only), or without 204 content if you specify a heading saying you don't want content.

Whether it is compatible with OData or not ... not quite sure :-). Despite this, I do not think that you can get what you want in the WCF DS stack at the moment.

+3
source

I was able to return the navigation properties after POST to create a new object using the OData v4 $expand query option without problems.

Make sure your method is decorated with [EnableQuery] and you call Include with the name of the navigation property.

For instance:

 [EnableQuery] public IHttpActionResult Post(user_object user) { if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } this.db.user_objects.Add(user); this.db.SaveChanges(); user = this.db.user_objects.Include("user_objects_attributes").Single(x => x.user_object_id == user.user_object_id)); return this.Created(user); } 

The POST request line should include ?$expand=user_objects_attributes .

See also:
https://github.com/OData/WebApi/issues/356

0
source

All Articles