Wildcard properties in Swagger? - swagger

I have a JSON response which may have any number of nodes, and the names may vary too. For example:
{
thing: "this a thing"
other: "this is another thing"
another: "and yet another thing"
}
Is there a way to indicate it in Swagger that the object may contain any number of properties, each with any name?
type: object
properties:
*:
type: string
repeats: infinitely

Yes, it's possible. The additionalProperties flag means exactly that an unknown number of optional fields are to follow. They can have types like regular properties.
type: object
additionalProperties:
type: string
Thank you.

Related

Should string object property representing a number be a string or number

As stated in the title, my team always sends a response of which properties which represent float/int/bigInteger as strings, should the swagger type of those properties be number or a string?
The data type in your OpenAPI definition must indicate the actual data type used in the payload.
If the response is
{
"id": "12345"
}
then id is a type: string property.
You can use format and pattern (regex pattern) to clarify the value format. For example, if id strings contains non-negative integer numbers, you can define id as:
type: string
pattern: "^\\d+$"

Count distinct pairs of attributes in Rails

I have a class Restaurant:
Restaurant(id: integer, name: string, url: string, address: string
I would like to get all the different combinations of url and name and count them. How can I do that?
I've tried
Restaurant.select(:url, :name).distinct.count
but I get:
No function matches the given name and argument types. You might need to add explicit type casts.
Did you try?
Restaurant.pluck(:url, :name).uniq.count
More here http://api.rubyonrails.org/classes/ActiveRecord/Calculations.html#method-i-pluck
And also check this question Rails: select unique values from a column

Swagger: take one or more values from enum

I am writing an OpenAPI (Swagger) definition where a query parameter can take none, or N values, like this:
/path?sort=field1,field2
How can I write this in OpenAPI YAML?
I tried the following, but it does not produce the expected result:
- name: sort
in: query
schema:
type: string
enum: [field1,field2,field3]
allowEmptyValue: true
required: false
description: Sort the results by attributes. (See http://jsonapi.org/format/1.1/#fetching-sorting)
A query parameter containing a comma-separated list of values is defined as an array. If the values are predefined, then it's an array of enum.
By default, an array may have any number of items, which matches your "none or more" requirement. If needed, you can restrict the number of items using minItems and maxItems, and optionally enforce uniqueItems: true.
OpenAPI 2.0
The parameter definition would look as follows. collectionFormat: csv indicates that the values are comma-separated, but this is the default format so it can be omitted.
parameters:
- name: sort
in: query
type: array # <-----
items:
type: string
enum: [field1, field2, field3]
collectionFormat: csv # <-----
required: false
description: Sort the results by attributes. (See http://jsonapi.org/format/1.1/#fetching-sorting)
OpenAPI 3.x
collectionFormat: csv from OpenAPI 2.0 has been replaced with style: form + explode: false. style: form is the default style for query parameters, so it can be omitted.
parameters:
- name: sort
in: query
schema:
type: array # <-----
items:
type: string
enum: [field1, field2, field3]
required: false
description: Sort the results by attributes. (See http://jsonapi.org/format/1.1/#fetching-sorting)
explode: false # <-----
I think there's no need for allowEmptyValue, because an empty array will be effectively an empty value in this scenario. Moreover, allowEmptyValue is not recommended for use since OpenAPI 3.0.2 "as it will be removed in a future version."

Realm not query string field with whitespace

I have such function in my data source:
func getAllFood(by type: String) -> [UserFoodInformation] {
var findedFood = [UserFoodInformation]()
findedFood.append(contentsOf: baseUserFoodDataSource.getAllFood(by: type))
let predicate = NSPredicate(format: "foodType == %#", type)
let resultOfSearching = dataBase.objects(AddedUserFood.self).filter(predicate).sorted(byKeyPath: "name")
for searchedFood in resultOfSearching {
findedFood.append(searchedFood)
}
return findedFood
}
When I try to query with string that consist whitespace, I have no result, but if I query with simple one-word parameter, everything goes fine. Why is that? Can I have a string field in Realm that consists multiple words?
The predicate you're using is looking for objects whose foodType property is equal to the type string that is passed in. Only those objects whose property is exactly equal to that string will match. If you want to perform some other form of matching you'll need to use something other than the equality operator. BEGINSWITH, CONTAINS, ENDSWITH and LIKE are the comparison operators that Realm supports on string fields.
Can I have a string field in Realm that consists multiple words?
String fields can contain any string values. The supported comparison operators don't have the concept of a "word", though, so if you want to do filtering using that concept you'll likely need to do further work on your part. Depending on your use case, I can see a couple of ways to go about it:
Use CONTAINS to find any objects whose foodType properties contains the given type string.
Parse the string into structured data that you store in your model. For instance, it may make sense to store a List<FoodType> rather than a String for the foodType property.
There are likely other options, but they depend on details of what it is you're trying to achieve that you've not shared.

I want to know can i give name to enum declared in Swagger?

I have an Enum like below
enum:
- Male
- Female
but internally i want this as F and M? like if i print Male it should print M not male
Since in your comments you speak about the frontend, I guess you mean that when you show this values you want to show "Female" and "Male", instead of F and M, that part goes beyond what swagger does.
What you need to put now is the values the API expects to receive which are, as you stated, M and F.
properties:
gender:
type: string
description: Gender of the person
enum:
- M
- F
Considering the data types listed in the spec your best choice seems to be string.
How you show the data in the frontend is a completely different matter and you should implement something to display the enum values however you decide is best.
There are a couple of issues open in the swagger spec about enums that have a bit to do with what you want: here and here.

Resources