ASP.NET MVC model binding precedence vs. RESTful resources - asp.net-mvc

The order of precedence that ASP.NET MVC uses when it binds models for incoming requests is bothering me in the context of REST. Essentially, MVC binds models using values in the following order of precedence:
POST body
Route (URL)
Query string values
What bothers me is how that trumps a resource's Uri with values from the body of the message.
For example, I could have a RESTful resource exposed as follows:
/dogs/
... returning:
[{
'name': 'Fido',
'color': 'brown',
'_links': {
'self': { 'href': '/dogs/7' }
}
},
{
'name': 'Spot',
'color': 'spotted',
'_links': {
'self': { 'href': '/dogs/5' }
}
}]
Note that with the "self" link (HAL style), I have everything I need to modify the dogs by PUTting those resources back to the server without needing their "id" values.
PUT /dogs/7
{
'name': 'Super Fido',
'color': 'rainbow'
}
The server has everything needed to update the dog, with no confusion. MVC will model-bind everything nicely into my model, including the id (sourced from the route).
However, some API styles I have seen include the 'id' in the message body, so that it looks like this:
GET /dogs/7
{
'id': 7,
'name': 'Super Fido',
'color': 'rainbow'
}
But fundamentally what bothers me the most is that if I have a route defined like "{controller}/{id}", and the client does the following:
PUT /dogs/7
{
'id': 5,
'name': 'Snowy',
'color': 'white'
}
... then MVC will model bind the 'id' to 5 (from the message body), and not 7 (from the route), yet based on the resource URI that my client provided, they should have been updating Fido's information. This would be akin to trying to save a document to your hard drive in a certain location, and because of something in the document, having it be automatically saved somewhere else.
How would one go about changing the out-of-the-box model binding precedence in ASP.NET MVC to prioritize the route values over the message body (when conflicts exist), and would this be a good thing to do in this context?

Related

How to return friendly field names for zapier trigger (zapier developers)

I am working on a Zapier integration for an online form builder. Each unique form for our users has lots of long, auto-generated field names.
We have a trigger called “New form entries”, which polls our server for form entries, and comes back like this:
[
{
"id": "6209aee326baa600224d822c",
"email_907058157108782": "test#test.com",
"phone_589083232390193": "12345",
},
{
"id": "61fd629f19408200225e1893",
"email_907058157108782": "test#test2.com",
"phone_589083232390193": "54321",
},
]
However, this results in end users seeing these really long, gross field names in the Zapier interface:
My question: how do I get Zapier to display friendly labels to the user, whilst using the unique field IDs behind the scenes?
I’m thinking of returning something like the following (each object represents a form entry, , but I need to know how to actually use “friendlyFieldName” and “value” in Zapier!-
[
{
// the id for the entry
"id": "62179ec5ab9daa0022df7d1d",
// the id for the first field entry
"text_576692390099896": {
// a friendly name and value for zapier
"friendlyFieldName": "What is your favourite colour?",
"value": "Blue"
}
}
]
Thank you :)
You can define output fields labels in outputFields. Here are the reference document that you can follow: Output Fields

Create a documentSet using graph API - configuration

I implemented with Graph API several calls to create a document set.
I followed the answer posted here concerning the possibility of creating a DocumentSet in SharePoint here : Is it possible to create a project documentset using graph API?
For this i followed those steps :
1. Getting the library driveId :
`GET https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${listId}?$expand=drive`
2. Creating the folder:
POST https://graph.microsoft.com/v1.0/drives/${driveId}/root/children
I have to pass an object:
{
"name": ${nameOfTheFolder},
"folder": {},
}
3. Getting the Sharepoint itemId:
4. Updating the document library:
`PATCH https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${listId}/items/${sharepointIds.listItemId}`
and passing a body:
{
"contentType": {
"id": "content-type-id-of-the-document-set"
},
"fields": {
//whatever fields you want to set
}
}
I have questions concerning the folder creation and the updating:
What is expected in the folder object ?
{
"name": ${nameOfTheFolder},
"folder": {},
}
Concerning the path step:
{
"contentType": {
"id": "content-type-id-of-the-document-set"
},
"fields": {
//whatever fields you want to set
}
}
I have several questions :
Let's consider i have a document type called invoices. Which id is expected for document type id ?
finally how do i pass the fields ? let's say i want to pass 3 fields : invoiceId, claimId, clientId.
Graph API is great but some more information would be helpful. thanks !
I have questions concerning the folder creation and the updating: What is expected in the folder object ?
The folder object (sent as {}) is there to tell graph API that you are creating a folder and not a file. It is a property of the drive item
Let's consider i have a document type called invoices. Which id is expected for document type id ?
This is the id contentType subfield of the list item you are patching
ally how do i pass the fields ? let's say i want to pass 3 fields : invoiceId, claimId, clientId.
You just pass them with repective values like below. See Update listItem
{
"invoiceId": "value",
"claimId": "value"
...
}
One point I didn't express correctly was to know what id is expected here :
{
"contentType": {
"id": "content-type-id-of-the-document-set"
},
"fields": {
//whatever fields you want to set
}
}
I retrieved the different content types of my site by calling this kind of URL and check if the content type exists.
https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${listId}/contentTypes
From the result i retrieve in a Value object the id.
The id looks like this :
0x0120D5200082903AB771604546844DB2AC483D905B00E58445A7D..........
In modern SharePoint, you can also get the Content Type ID from the UI by browsing to SharePoint Site > Site Settings > Site content types > <ContentTypeName> > Content Type ID.
Content Type ID
Not sure if this is easier than via graph, but it's another option at least.

Pre-setting values to Docusign template

I am new to Docusign api and I am trying to post values from my form into a template. To be honest, I am not sure if I even created the custom fields properly or if there is some special way to set them into the form other than just creating a text field with a name.
I have read through the docs and recipes and about a dozen or more stack posts.
I am using rails and my fields post just fine but it's my tabs that do not. I read somewhere that I am supposed to use tabs and not custom_fields. Not sure if that's totally correct but that's how I've interpreted it.
Here is my current code:
body: {
"emailSubject": "DocuSign API call - Request Signature - Boom",
"templateId": "e1d5bce1-9757-4ffe-881b-054aa9139f2f",
"templateRoles": [{
"email": "#{renter.email}",
"name": "#{renter.background.legal_name}",
"roleName": "Lessee"
},{
"email": "#{#manager.email}",
"name": "#{#manager.name}",
"roleName": "Lessor",
"tabs": {
"texttabs": [{
"tabLabel": "Rent",
"value": "#{#lease.rent}"
},{
"tabLabel": "Address",
"value": "987 apple lane"
}]
}
}],
"status": "sent"
}.to_json
baseUrl that I am sending to:
"https://demo.docusign.net/restapi/v2/accounts/my_id/envelopes"
In your texttabs section, you should be passing in the following parameters at a minimum per tab: tablabel & value.
tablabel is the name of the tab that you have defined on the template. So from what I can tell, you have a text box on your template called Address. So you should put "tablabel":"Address".
value is what you would like to pre-populate in the tab. Looks like you have that correct.
You do not want to use tabID as that is not a valid parameter in this flow. The API documentation details what parameters you can use: https://www.docusign.com/p/RESTAPIGuide/RESTAPIGuide.htm#REST%20API%20References/Tabs/Text%20Tab.htm?Highlight=data%20tab
I also see an extraneous parameter of "Rent" under templateRoles section. That value will be ignored since it is not a valid parameter.
Turns out the problem was not with the code but with the setup within docusign. Make sure that you are setting up your tabs correctly and if you want to replicate a field multiple times make sure that they all share the same exact name.

Eve - Is it possible to unset a key from a document?

In a schema with optional values such as code in the example:
'code': {
'type': 'string',
},
'name': {
'type': 'string',
'required': True,
},
'email': {
'type': 'string',
'required': True
}
Let's say there's an inserted document with a value for code. Can I unset the code key like mongodb $unset does, using Eve somehow?
One way to achieve this is to setup a default projection for the endpoint.
Limiting the Fieldset Exposed by the API Endpoint
By default API responses to GET requests will include all fields defined by the corresponding resource schema. The projection setting of the datasource resource keyword allows you to redefine the fields.
people = {
'datasource': {
'projection': {'username': 1}
}
}
The above setting will expose only the username field to GET requests, no matter the schema defined for the resource.
Another option is to leverage MongoDB Aggregation Framework itself. Just set the endpoint so that a aggregation is performed before data is returned to the client. The following should work (see the docs for details):
posts = {
'datasource': {
'aggregation': {
'pipeline': [{"$unset": "code"}]
}
}
}
You need Eve v0.7 for aggregation support.
I doubt you can do it with a PATCH request, but a PUT request should do.
import requests
# get original doc
resp = requests.get(document_url)
# prepare a new document
doc = resp.json()
new_doc = {k: v for k, v in doc.items() if not k.startswith('_')}
del new_doc['code']
# overwrite the complete document
resp = requests.put(document_url, json=new_doc, headers={'If-Match': doc['_etag']}

Syntax for submitting a mutation to a graphql-relay mutationWithClientMutationId

I defined a GraphQL Mutation using graphql-relay but am having issues figuring out how to submit a mutation to it.
Here is the relevant schema:
const userType = new GraphQLObjectType({
name: 'User',
description: 'user',
fields: () => ({
id: {
type: new GraphQLNonNull(GraphQLString),
description: 'The UUID for the user.',
resolve(user) {
return user.uuid;
},
},
})
});
const registerUser = mutationWithClientMutationId({
name: 'registerUser',
inputFields: {
},
outputFields: {
user: {
type: userType,
resolve: (payload) => {
models.user.findById(payload.userId);
}
},
},
mutateAndGetPayload: (args) => {
var newUser = models.user.build().save();
return {
userId: newUser.id,
};
}
});
const rootMutation = new GraphQLObjectType({
name: 'RootMutationType',
fields: {
registerUser: registerUser,
},
});
const schema = new GraphQLSchema({
query: rootQuery,
mutation: rootMutation,
});
What should an HTTP call look like to register a new user and get back the userId?
Thanks!
I want to point out that I see that you're saying that your mutation requires no parameters - how does it know what the new user's details are? You'll probably need some parameters on that mutation, eventually. They would be available to your mutateAndGetPayload on that first function parameter. (I'm not saying every mutation needs parameters, but this one probably does)
If you're using Relay, there is some pretty good information on the official document as to how to use your mutations from Relay. Particularly down at the bottom where it shows the various mutator configs. If you're using connections, you may want to use RANGE_ADD to add this new account to the Relay store manually, otherwise if you'd like to perform a more broad refetch you can use FIELDS_CHANGE. You said you need the new user id after the mutation finishes. If you're using Relay, you may need to look into REQUIRED_CHILDREN to specify that regardless of the computed query that Relay will build, you always want that id to be queried.
The output of your mutation is a userType, so you'd be able to access it with a fragment on the payload type, which would probably be RegisterUserPayload, that might look something like ...
fragment on RegisterUserPayload {
user {
id
}
}
Now, that's assuming you're using Relay. If you'd like to try this out manually via GraphiQL, then you can use the examples of how to do mutations through there on the GraphQL Mutation docs. There's a direct example of how you'd query your mutation.
Last, since you asked how to do this at a low level of issuing the HTTP request yourself, for that you can look at express-graphql documentation, which explains how to query it.
I figured out a mutation format that worked:
mutation RootMutationType {
registerUser(input:{clientMutationId:"123"}){
clientMutationId, user { id }
}
}

Resources