OData PUT to replace collection property - odata

Using the OData standard is it possible to replace collection by sending a new collection?
Scenario:
The person object contains a list Address object. I would want to replace the Address collection with a new collection.
PUT Persons(1)/Addresses
[{"city": "X", "country": "US"}, {"city": "Y", "country": "US"}]

This is not possible out of the box (at least for ODATAv3), as the default routing template does not expect segments after the key portion.
But you should be able to add an ODATA Action that would do what you want to achieve. Your action definition could then look similar to this:
var action = builder.Entity<Person>()
.Action("Addresses")
.Returns<bool>();
action.Parameter<Collection<CityCountryPair>>("data");
The type CityCountryPair would be a regular DTO containing your properties you want to change. Make sure this type is also registered as an EntitySet in Odata or use a plain map/dictionary with only primitive types.
The actual call to the ODATA action would then look similar to this:
POST http://www.example.com/api/YourEndpoint/Persons(42)/Addresses
Content-Type: application/json
{
"data" :
[
{ "city" : "Berne" , "country": "CH" },
{ "city" : "Y" , "country": "CH" }
]
}
If you want to send more complex data types you can still resort to a customer JSON Deserialiser and override the default one or use a custom model binder after all.

Related

Create SharePoint list item with lookup field with Microsoft Graph

I am trying to create list items with Microsoft Graph's new SharePoint endpoint. My URL is as follows:
https://graph.microsoft.com/beta/sites/{site-id}/lists/{list-id}/items
Calling this URL with POST, and a body like this:
{
"fields": {
"Title": "test",
}
}
.. works! But if I include a lookup field, the lookup field is always empty. I have tried with
"{columnName}": "id",
"{columnName}": "id;#value",
"{columnName}": {
"#odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedReference", //and others like this
"Id": "id",
"Value": "value",
}
"{columnName}": "value",
"{columnName}Id": "id",
None of these seems to work. Some give an error like "The request is malformed or incorrect.", others go through, but doesn't include the lookup field.
The documentation for this is scarce (if any), and I have found very little information on google (apart from someone asking the same question with no answers). Anyone that got this to work yet? Is it even possible?
I have got this to work with people fields where the multiple selection option was DISABLED. So if you have a person field which allows one person only, the following works.
"{columnName}LookupId": id
Additionally, the id still needs to be the SP ID and not the graph user GUID. This ID changes from site to site.
You need to add a lookup column in the following format
"Office": "London",
"OfficeLookupId", "16"
Where Office is the name of the column

Get data into MVC view

I have to populate a javascript function with some information from my DB, is the best way to access via the controller and pass it through(I'm not sure how to pass an object) or is it easier to have the view to retrieve the data?
i have to create an object in javascript something like this
new FleetSelector([{ "OwnerId": "2df3893d-c406-48fe-8443-1622ddc51af2", "DisplayName": "Navman Dev" }, { "OwnerId": "7A402CD7-5EEA-4AF5-80A3-22EED0610C9D", "DisplayName": "CORSA TRANSPORTES" }]);
}
so is there a way to pass an list of objects that i can loop through in my view to be able to make that javascript?

Define Nested Form Data As Model In Swagger 2.0

I have an endpoint that accepts a multipart form. The form has an array of fields that are grouped together as objects:
metadata[type][] = 'car'
metadata[id][] = 1
metadata[metadata][][something] = foo
metadata[metadata][][something-else] = bar
There can be multiple metadatas in each request; I'd like to define this endpoint in my spec as one model called "metadata". However, that doesn't seem possible with swagger 2.0. My type should be array then I can declare an items offset, but it can only be comprised of "string", "number", "integer", "boolean", or "array".

Assign value from api response to specific model object in RAILS

I have a Contact model that has three attributes:
name, email, id
name and email are both present, but id is null.
I am using name and email to pass in through an API Post call, and this is the response I get back:
{"count": 1, "next": null, "previous": null, "results": [
{"id": 8067950, "name": "Bill","email": "test#test.com"}]}
My loop looks like this:
contacts_array.each do |contact|
api_call(contact.name, contact.email)
end
contacts_array is an array of ruby objects, namely Contact models. How can I, within that loop, assign the response id I get back to the contact used in the api_call?
I didn't really understand the difference between your Person and Contact model, but I'll assume they're the same model.
Generally you should know that it's a bad idea to handle the field called id on your own in rails. If you insist on storing the id which is returned from the call, then you should store it in another field, like api_id for example.
Below is a code that should work fine if api_call returns a Ruby Hash like the one in your question.
contacts_array.each do |contact|
contact.api_id = api_call(contact.name, contact.email)['results'].first['id']
end

Why does MVC 4 Web API match action parameters from a GET request body?

I have a API method that returns a user object by specifying a user id in a request url the restful way, like this:
http://www.myapi.com/users/1
My route looks like this:
routes.MapHttpRoute(
"GetUserByUserId",
"users/{id}",
defaults: new { Controller = "User", Action = "GetUserByUserId" },
constraints: new { httpMethod = new HttpMethodConstraint(new string[] { "GET" })
});
The controller is named UserController and the method is named GetUserByUserId, and looks like this:
[HttpGet]
public User GetUserByUserId(int id)
{
... returns a User object ....
}
If I call the method it responds with the user object, as expected. The id "1" in the url is automatically matched as the GetUserByUserId attribute "id" by the route.
But(!!), if I accidently also provide a JSON serialized object of any kind in the request body, like this:
{"Id":6,"PermissionId":0,"UserId":3}
MVC is automatically mapping the "Id" property in the request body object to my "id" attribute of the GetUserByUserId method, ignoring the "1" id in my url!
To me, this looks very strange. I know Web API automatically maps the request url and the request body to appropriate attributes in the routed method, but the "Id" in the request body in my example is NOT a simple stand-alone integer, it is a property in a complex type and should not be interpretated as the method attribute "id".
Is this a bug, or am I missing something?
asp.net mvc uses ValueProviderFactories to extract values from request for model binding. You can see configured providers by inspecting ValueProviderFactories.Factories Property. With default configuration, this collection consists of
ChildActionValueProviderFactory
FormValueProviderFactory
JsonValueProviderFactory
RouteDataValueProviderFactory
QueryStringValueProviderFactory
HttpFileCollectionValueProviderFactory
This collection is sorted, and as JsonValueProviderFactory appears higher in list, it is used as value provider for id, rather than RouteDataValueProviderFactory.

Resources