Swagger: take one or more values from enum - swagger

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."

Related

Wildcard properties in 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.

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

How do I search within an JSON array of hashes by hash values?

I am using Postgres' JSON data type to store some information.
For example I have model User with a field locations that holds a json document(array of objects containing pairs of keys and values) in the following format:
[{"name": "Location 1", kind: "house"},
{"name": "Location 2", kind: "house"},
{"name": "Location 3", kind: "office"},
...
{"name": "Location X", kind: "house"}
]
I want to query with .where on the JSON data type.
I want to query for users that have at least one location with kind = office.
Thanks!
I want to query for users that have locations with kind office
# if locations is jsonb type
User.where('locations #> ?', { kind: 'office' }.to_json)
# if locations is json type
User.where("locations->>'kind' = 'office'")
# if locations is array of hashes
User.where('locations #> ?', [{ kind: 'office' }].to_json)
I have used a mix of above answers. This is my working code:
User.where("locations::jsonb #> ?", [{kind: 'office'}].to_json)
As a note, locations is a JSON column of User table (not JSONB datatype)
Filters based on the values of objects inside array JSONB
If you want to make filters based on the values of the objects (jsonb) inside the arrays in Rails with postgres you can use something like example below.
Assuming you have a Product model:
Product.select("*").from(
Product.select("*, jsonb_array_elements(registers) as register")
).where("(register ->> 'price')::numeric >= 1.50")
In above example we select all products with price greater than or equal 1.50.
For this we use:
jsonb_array_elements() from postgres;
an auxiliary column and alias;
and methods from ActiveRecord from Rails.
I applied ident in sub-query just for best readability.
After you can put this in a scope or use other best practice.
Source: https://www.postgresql.org/docs/current/static/functions-json.html
User.where("name::jsonb -> 'location' ->> 'kind' = ?", 'office')
Just to give my two cents:
I have a model with a JSON with a key and an array inside, so it looks like this:
ModelObj: attribute: { key: [val0, val1, val2, val3] }
I needed to find all objects with, for example, attributes where the key has val3 in the array.
I changed my attribute type to jsonb and this was how I find it:
Model.where('attribute #> ?', {key: ['val3']}.to_json)
Maybe it's useful to someone out there.

How to set up parameters of a stored procedure in JMeter using a JDBC Request

I am planning to load the database using a stored procedure callable statement. The test plan I am creating in JMeter looks like below:
- Test plan
- Thread Group
- JDBC Connection Configuration
- JDBC Request
- View results tree
- Summary Report
JDBC Connection Configuration is based on tests that already work.
The question is with my JDBC Request:
Variable name: is the database name same as in JDBC Connection configuration
Query Type: Callable Statement
Query: {call schema.dbpk_utilities.get_user_id(?,?,?,?,?,?,?)}
Parameter values: S12345, HR, OUT, NULL,NULL,NULL, NULL
Parameter Types: VARCHAR, VARCHAR, INTEGER, VARCHAR, VARCHAR, VARCHAR, VARCHAR
Variable names: username,hr, id, four, five, six, seven
The error I get is:
Response message: java.lang.NumberFormatException: For input string:
"OUT"
Can anyone tell me why the response message? Is it possible to call Stored procedures in JMeter? I am struggling to OK this request!
Following the 'no space rule' from the previous post I found out why the request was responding with java.lang.NumberFormatException: For input string: "OUT" message. See below
Variable Name: Oracle
SQL Query: Callable Statament
Query: {call nspishr.dbpk_user_utilities.get_user_details(?,?,?,?,?,?,?)}
Parameter Values: S12345,DMS,OUT,NULL,NULL,NULL,NULL
Par Types: VARCHAR,VARCHAR,OUT INTEGER,VARCHAR,VARCHAR,VARCHAR,VARCHAR
Variable names: username,dms,id,four,five,six,seven
Notice how the parameters are registered in this call. In my procedure param 1 and param 2 are IN parameters. These are made IN parameters implicitly by giving them a parameter value S12345 and HR in my case.
From param 3 all the way to param 7 are OUT parameters. Param 4, 5, 6 and 7 have NULL values. Therefore no need to register them as OUT parameters. Param 3 (the id), however, is the OUT parameter and it has to be registered as such by explicitly saying OUT in the parameter value and then saying OUT INTEGER in the parameter types section. The thing to remember is that you need to specify OUT in both parameter value and parameter type. Not to forget the datatype of the parameter in the parameter types field. ie OUT INTEGER, OUT VARCHAR ...
Hope this helps
Whenever I have to provide JMeter with a comma-separated list, I make sure there are no spaces before/after the commas. I call this the 'no spaces' rule.
The poster sqeeky has also discovered this trick here, where sqeeky said:
do NOT have any whitespace after the comma separating the parm names so instead of '${appid_1}, ${appid_2}' (where you can see there is a space separating the ParameterName values, specify ${appid_1},${appid_2} instead as the values in the ParameterName list - otherwise in this example ${appid_2} won't be included.
All three of your comma-separated lists (Parameter values, Parameter Types, Variable names) violate the 'no spaces' rule. Try taking out all spaces in all three lists and trying again.
I checkout out the doc and unfortunately, I saw no references to the 'no spaces' rule, or perhaps the wording didn't reach out and grab me.

Resources