How to set OpenAPI query parameter as array - openapi

I use openapi v2.
I read this document https://swagger.io/docs/specification/serialization/ .
I want to define path that take array and 1 value query parameter like below.(userIds and groupId)
/users{?userIds}{groupId}
But I call this url like below, it's return 404.
const url = '/user?userIds=10,11&groupId=1'
How to define it?

Related

How to insert jsonb[] data into column using pg-promise

Given a table with a column of type jsonb[], how do I insert a json array into the column?
Using the provided formatters :array, :json won't work in this instance - unless I am missing the correct combination or something.
const links = [
{
title: 'IMDB',
url: 'https://www.imdb.com/title/tt0076759'
},
{
title: 'Rotten Tomatoes',
url: 'https://www.rottentomatoes.com/m/star_wars'
}
];
const result = await db.none(`INSERT INTO tests (links) VALUES ($1:json)`, [links]);
You do not need the library's :json filter in this case, as you need an array of JSON objects, and not a JSON with an array of JSON objects.
The former is formatted correctly by default, which then only needs ::json[] type casting:
await db.none(`INSERT INTO tests(links) VALUES($1::json[])`, [links]);
Other Notes
Use pg-monitor or event query to output queries being executed, for easier diagnostics.
Method none can only resolve with null, no point storing the result in a variable.
Library pg-promise does not have any :array filter, see supported filters.

get route only with specified parameter

I am new to MongoDB and CRUD APIs.
I have created my first database and inserted some data. I can do get, post and delete requests.
Now I want to request a 'get' by adding a parameter, so I do the following:
router.get('/:story_name', async function (req, res, next) {
const selectedStory = await loadStoryToRead()
res.send(await selectedStory.find({}).toArray())
})
say that story_name is S1C1,
I can do http://localhost:3000/api/whatever/s1c1 to get the data.
I would have expected to retrieve the data ONLY by using the specified parameter, however I can use the ID or the date or any other parameter found in the json file to get the data.
for example I can do
http://localhost:3000/api/whatever/5d692b6b21d5fdac2... // the ID
or
http://localhost:3000/api/whatever/2019-08-30T13:58:03.035Z ... // the created_at date
and obtain the same result.
Why is that?
How can I make sure that if I use router.get('/:story_name' ... I can retrieve the data only if I use the 'story_name' parameter?
Thanks!
* UPDATE *
my loadStoryToRead() looks like this:
async function loadStoryToRead () {
const client = await mongodb.MongoClient.connect(
'mongodb+srv://...', {
useNewUrlParser: true
})
return client.db('read').collection('selectedStory')
}
I will try to reformulate my question.
I want to ensure that the data is retrieved only by adding the 'story_name' parameter in the URL and not by adding any other parameter within the file.
The reading that I have done suggested to add the parameter to the get request, but when I do it, it doesn't matter what parameter I enter, I can still retrieve the data.
The delete request, however, is very specific. If I use router.delete('/:id'... the only way to delete the file is by using the ID parameter.
I would like to obtain the same with a get request and not using the 'id' but by using 'story_name'.
you can use regular expression capabilities for pattern matching strings in queries.
Syntax is:
db.<collection>.find({<fieldName>: /<string>/})
for example, you can use
var re = new RegExp(req.params.story_name,"g");
db.users.find({$or:[{"name": re}, {"_id": re}, {..other fields}]});
You can use $and and $or according to your requirement.
To read more follow the link
https://docs.mongodb.com/manual/reference/operator/query/regex/

Using a variable as a Map key MongoDB Groovy Query

I've a mongo db query to fetch some data as shown below
collection.find(lastUpdated: ['$gte': startDate.toLong(), '$lt': endDate.toLong()], resource: ['$gt': limit]).findAll().toList()
resource is a dynamic field which could have values like:
parameter1, parameter2 ... and so on.
I'm passing resource as method parameter in this query.
Can somebody suggest some way through which I can dynamically use resource without hard coding its value as (paramter1 or parameter2 or parameter3 ...) in my query
yeah you are almost there you just need to pass it as a variable and use () syntax on the Map to indicate that you are using a variable as a key instead of a literal value.
change this:
resource: ['$gt': limit]
to this:
(resource): ['$gt': limit]
Full query:
def resource = "parameter1"; //or parameter2 ...
collection.find(lastUpdated: ['$gte': startDate.toLong(), '$lt': endDate.toLong()], (resource): ['$gt': limit]).findAll().toList()

Passing an array of object having name and value attribute as query param

Here is scenario.
I have below pojo
Property {
String name;
String value;
}
I want to pass an array of above pojo as query param. How do i do that.
Something like
http://myservice.com?property:name=n1&property:value=v1&property:name=n2&property:value=v2
And I want to figure out at service end that v1 is the value for n1.
Is there a way to achieve this.
One way that you can pass this into a URL as the following -
sample.php?arr_args[key1]=value1&arr_args[key2]=value2

Conversion of a object into a particular datetime format

I want to convert a query string object into a datetime in this format:- "YYYY-mm-dd HH:mm:ss.xxx" in C#.net. But when i am using Convert.ToDateTime(object) for getting the datetime value then an exception is being fired.
Could anyone can provide me the iFormatProvider for the same.?
Thanks
Varun Sareen
Have a look at DateTime.TryParseExact Method
I think your problem is trying to convert the QueryString object, instead of getting a value from the query string and then converting the value.
A QueryString object is a keyed collection of the values specified in a URL from an HTTP request. So if you have a URL like: http://example.com?a=1&b=2&c=3, the request QueryString object will contain three values: 1, 2, and 3. To access the values you would use the keys:
var aValue = Request.QueryString["a"];
And the variable aValue would then contain the string value "1" (without the quotes).
After getting the value from the query string, you can use the TryParseExact method suggested by #astander.