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".
Related
In Rails 6 and FullCalendar v5.11, when passing form parameters in FullCalendar's addEventSource() method via extraParams, array parameters are added to the url query string in a single parameter rather than repeated per value as expected by Rails.
E.G. with extraParams: { "staff_id[]": ["123", "139"] },
FullCalendar adds it to the url in the form:
staff_id[]=123,139
but Rails requires it in the query string as:
staff_id[]=123&staff_id[]=139
Any suggestions on how to get FullCalendar to send arrays in the query string format expected by Rails?
There are multiple filters so it is awkward to wrestle the strings from Rails params back into arrays in the controller (in this example the controller Parameters receive "staff_id"=>["123,139"] instead of "staff_id"=>["123", "139"])
Update: jQuery.get() generates url parameters in the form key[]=1&key[]=2, as required by Rails controllers, for arrays in its data argument. Can FullCalendar be made do the same?
Update: FullCalendar's encodeParams() function is responsible for this:
function encodeParams(params) {
var parts = [];
for (var key in params) {
parts.push(encodeURIComponent(key) + "=" + encodeURIComponent(params[key]));
}
return parts.join('&');
}
Overwriting this code locally with jQuery.param(params) works.
We are trying to achieve a scenario on the Swagger UI in the Body section. In the Requests section, can we have an Example Value JSON hiding one or more fields but the Model would still show those fields?
We are basically trying to reduce the number of fields in the request body but have all the fields visible in the Model.
For example, we would like to hide the name in the example here:
but still display the name in the Model here:
To hide fields from auto-generated model examples in Swagger UI, you'll need to add a custom example for that model that includes only the fields you need.
definitions:
Pet:
type: object
properties:
...
# Override model example that will be displayed in Swagger UI
example:
id: foo
status: available
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.
Collections return a maximum of 100 records per page, and by default return 100 records per page. How can I change this default value?
You can set this on a per request basis by passing e.g. per_page=50 in the request parameters. You iterate the collection by incrementing the page attribute, e.g. page=3. Collections also include links in the response body for easier navigation, generally they are on this structure:
{
"users": [ ... ],
"count": 1234,
"next_page": "https://account.zendesk.com/api/v2/users.json?page=2&per_page=50",
"previous_page": null
}
Stop paging when the next_page attribute is null.
I have two Dictionary<string, byte> properties in my model that should validate properly with from 0 to 5 items. For example the property skill (string dropDownListLabel, byte years).
Because I need to support non-javascript clients, I render all 5 input pairs to the browser, only binding existing dictionary items, and life is great. This gives 5 empty input pairs for a new plain HTML form, each with unique input names, which I also want.
Here's the serialization (input names) I use:
skill[0].Key = "", skill[0].Value = ""
... three more pairs ...
skill[4].Key = "", skill[4].Value = ""
But on POST, for Key/Value pairs with neither Key nor Value specified, DefaultModelBinder validation errors result on Value.
Is there a type and serialization I can use that will validate in DefaultModelBinder when both or neither Key and Value are POSTed, so MVC does as much work for me as possible, only adding pairs into a collection when they have content?
Thanks,
Shannon
You cannot bind an empty string to a value type (byte) so the default model binder marks your model as invalid. If you want to allow empty values you should use a nullable byte:
Dictionary<string, byte?>
UPDATE:
Another possibility is to use a collection of a custom type containing two properties each representing respectively the key and the value. Then use strongly typed helpers and editor templates in your view so that you don't have to worry about the wire format and finally you will need to ensure that the keys are unique so you will need a custom validator. Personally I use FluentValidation.NET for this part but if you are using data annotations you will need a custom attribute which will be used to decorate the collection property in the view model.