How to map object within object in Swagger - swagger

Recently a fellow has recommended Swagger to me to write my API with...
Now after searching for a while, I haven't found the way to map my JSONs quite easily.
This is how my response looks like:
{ 1 : {
name: "foo", age: 22
},
2 : {
name: "bar", age:14
},
3 : {
name: "boo", age: 26
},
4 : {
name: "far", age: 19
}
}
So basically I have an object, where the key is an ID, and the value is another object, which has normal key/value pairs.
Now I'm sure someone before me has needed this, but I couldn't find the way to write this.
How would I write this in Swagger?
Thank you for any help / example / referance to another question!

Related

Is setting a multiple mat-select dropdown value to objects possible?

Using mat-select in multiple mode, is it possible to pre-set the selected values if the values that are part of the select options are objects?
For example if the options in the select are as follows
toppingList: any[] = [
{ id: 1, description: 'Extra cheese' },
{ id: 2, description: 'Mushroom' },
{ id: 3, description: 'Onion' },
{ id: 4, description: 'Pepperoni' },
{ id: 5, description: 'Sausage' },
{ id: 6, description: 'Tomato' }
];
Then can you pre-set the selected values for that mat-select as such?
this.toppings.setValue([{ id: 1, description: 'Extra Cheese' }]);
Taking this stackblitz as an example you see that the the form values are getting set as desired, but the select buttons in the drop down itself are not showing as checked.
Is this even possible, or am I doing something wrong?
as i know for equality of two object, for example in java, you need write equal / hashcode for detect what object equal to another one.
you bind select value to object and typescript could not understand what compare object together. so i change your sample and bind value of options to id and it works!
see this stackblits

How to split an array of objects into subarrays depending on the field value in Rails and Mongodb

I want to get several arrays of objects aggregated by months (and years) in a their property value.
I have class Request like this:
class Request
include Mongoid::Document
include MongoidDocument::Updated
field :name, type: String
field :start_date, type: DateTime
#...
end
And I want the resulting array of multiple hashes with
{month: m_value, year: y_value, request: requests_with_m_value_as_month_and_y_value_as_year_in_start_date_field}
as element of array
Can someone help me with this?
You can use Aggregation Pipeline to get the data in the right shape back from MongoDB:
db.requests.aggregate([
{
$group: {
_id: {
year: {
$year: "$start_date"
},
month: {
$month: "$start_date"
}
},
requests: {
$push: "$$ROOT"
}
}
},
{
$project: {
_id: 0,
year: "$_id.year",
month: "$_id.month",
requests: "$requests"
}
}
])
Obviously, this is using just the REPL and you will have to translate it to the DSL provided by Mongoid. Based on what I could find it should be possible to just get the underlying collection and call aggregate on it:
Request.collection.aggregate([...])
Now you just need to take the query and convert it into something that Mongoid will accept. I think you just need to add a bunch of quotes around the object keys but I don't the environment set up to try that myself.

How to structure falcor router to get all available IDs?

I'm experimenting with using Falcor to front the Guild Wars 2 API and want to use it to show game item details. I'm especially interested in building a router that can use multiple datasources to combine the results of different APIs.
The catch is, Item IDs in Guild Wars 2 aren't contiguous. Here's an example:
[
1,
2,
6,
11,
24,
56,
...
]
So I can't just write paths on the client like items[100..120].name because there's almost certainly going to be a bunch of holes in that list.
I've tried adding a route to my router so I can just request items, but that sends it into an infinite loop on the client. You can see that attempt on GitHub.
Any pointers on the correct way to structure this? As I think about it more maybe I want item.id instead?
You shouldn't find your self asking for ids from a Falcor JSON Graph object.
It seems like you want to build an array of game ids:
{
games: [
{ $type: "ref", value: ["gamesById", 352] },
{ $type: "ref", value: ["gamesById", 428] }
// ...
],
gamesById: {
352: {
gameProp1: ...,
},
428: {
gameProp2: ...
}
}
}
[games, {from: 5, to: 17 }, "gameProp1"]
Does that work?
You can use 'get' API of Falcor, It retrieves multiple values.. You can pass any number of required properties as shown below
var model=new falcor.Model({
cache:{
genereList:[
{name:"Recently Watched",
titles:[
{id:123,
name: "Ignatius",
rating: 4}
]
},
{name:"New Release",
titles:[
{id:124,
name: "Jessy",
rating: 3}
]
}
]
}
});
Getting single value
model.getValue('genereList[0].titles[0].name').
then(function(value){
console.log(value);
});
Getting multiple values
model.get('genereList[0..1].titles[0].name', 'genereList[0..1].titles[0].rating').
then(function(json){
console.log(JSON.stringify(json, null, 4));
})

Submitted form text fields not grouping correctly

In my GSP I have a form with a lot textfields populated by a map that came from the controller, let me put this into an example, because my actual form is a lot more complicated than this:
for example, if I use users to populate a bunch of textfields where I can enter each person's age, I grouped them up into a map called data, and I want to process and save all that information after submitting:
<g:form useToken="true" name='example_form' action='submit'>
<g:each in='${users}' var='user' status='i'>
<g:textField name="data.${user.id}.name" value="${i.name}">
<g:field name="data.${user.id}.age" value="">
</g:each>
<button>Submit</button>
</g:form>
But when I am printing out the params.data in my submit controller, I noticed that not only I am getting the data map that I've created, I am also getting a bunch of garbage within it:
for(i in params.data){
println "key: ${i.key} value: ${i.value}"
}
output:
key: 0.name value: john
key: 0 value: [age: 35, name: john]
key: 1.name value: liz
key: 1 value: [age: 24, name: liz]
key: 2.name value: robert
key: 3.name value: david
key: 0.age value: 35
key: 1.age value: 24
key: 2 value: [age: 44, name: robert]
key: 3 value: [age: 23, name: david]
key: 3.age value: 23
key: 2.age value: 44
Am I doing something wrong?
expected output:
key: 0 value: [age: 35, name: john]
key: 1 value: [age: 24, name: liz]
key: 2 value: [age: 44, name: robert]
key: 3 value: [age: 23, name: david]
It should work exactly this way. When you're submitting data from your form, the body of your POST request looks this way:
data.0.name=john&data.0.age=35&data.1.name=liz&data.1.age=24&data.2.name=robert&data.2.age=44&data.3.name=david&data.3.age=23
So, it's just a plain string, representing a plain key-value map and Grails could parse is just like that:
['data.0.name': 'john', 'data.0.age': '35', 'data.1.name': 'liz', 'data.1.age': '24', 'data.2.name': 'robert', 'data.2.age': '44', 'data.3.name': 'david', 'data.3.age': '23']
But Grails developers wanted to simplify programmers' life, and they decided that if the key contains a dot, the request may represent some kind of structured data. So they decided to put it to the map, in addition to the raw request data. Thus, the dot can be interpreted in two ways - as a plain symbol, or as a separator between map name and map key. And it's up to developer which way the dot should be interpreted.
If you prefer to have clearer params over the easy use like def name = params.data.0.name, then you can use "_" insted of ".". In the controller you can use split("_") in a loop.
In a previous post #Alexander Tokarev explained what happened. The solution is an if statement as shown below:
for(i in params.data){
if( i.key.isNumber() ) {
println "key: ${i.key} value: ${i.value}"
}
}

MongoDB - Mongoid map reduce basic operation

I have just started with MongoDB and mongoid.
The biggest problem I'm having is understanding the map/reduce functionality to be able to do some very basic grouping and such.
Lets say I have model like this:
class Person
include Mongoid::Document
field :age, type: Integer
field :name
field :sdate
end
That model would produce objects like these:
#<Person _id: 9xzy0, age: 22, name: "Lucas", sdate: "2013-10-07">
#<Person _id: 9xzy2, age: 32, name: "Paul", sdate: "2013-10-07">
#<Person _id: 9xzy3, age: 23, name: "Tom", sdate: "2013-10-08">
#<Person _id: 9xzy4, age: 11, name: "Joe", sdate: "2013-10-08">
Could someone show how to use mongoid map reduce to get a collection of those objects grouped by the sdate field? And to get the sum of ages of those that share the same sdate field?
I'm aware of this: http://mongoid.org/en/mongoid/docs/querying.html#map_reduce
But somehow it would help to see that applied to a real example. Where does that code go, in the model I guess, is a scope needed, etc.
I can make a simple search with mongoid, get the array and manually construct anything I need but I guess map reduce is the way here. And I imagine these js functions mentioned on the mongoid page are feeded to the DB that makes those operations internally. Coming from active record these new concepts are a bit strange.
I'm on Rails 4.0, Ruby 1.9.3, Mongoid 4.0.0, MongoDB 2.4.6 on Heroku (mongolab) though I have locally 2.0 that I should update.
Thanks.
Taking the examples from http://mongoid.org/en/mongoid/docs/querying.html#map_reduce and adapting them to your situation and adding comments to explain.
map = %Q{
function() {
emit(this.sdate, { age: this.age, name : this. name });
// here "this" is the record that map
// is going to be executed on
}
}
reduce = %Q{
function(key, values) {
// this will be executed for every group that
// has the same sdate value
var result = { avg_of_ages: 0 };
var sum = 0; // sum of all ages
var totalnum = 0 // total number of people
values.forEach(function(value) {
sum += value.age;
});
result.avg_of_ages = sum/total // finding the average
return result;
}
}
results = Person.map_reduce(map, reduce) //You can access this as an array of maps
first_average = results[0].avg_of_ages
results.each do |result|
// do whatever you want with result
end
Though i would suggest you use Aggregation and not map reduce for such a simple operation. The way to do this is as follows :
results = Person.collection.aggregate([{"$group" => { "_id" => {"sdate" => "$sdate"},
"avg_of_ages"=> {"$avg" : "$age"}}}])
and the result will be almost identical with map reduced and you would have written a lot less code.

Resources