How to declare an object with un-named primitives in swagger? - swagger

I need to represent the following array in swagger-2.0, but am unsure how to do it, since I cannot figure out how to declare 'un-named' properties.
For example, here's what I need to define:
coords: [
[
37.782984,
-122.420973
],
[
37.772309,
-122.418555
],
...
]
How can I define these array-entries in swagger ?

If you model it as an array of arrays with two items, it should look like this:
parameters:
- name: coords
in: body
schema:
type: array
items:
type: array
items:
type: number
maxItems: 2
minItems: 2

Related

How to define in swagger spec either of two properties are not null and required

I have a requirement to have two properties in the payload say property1 and propert2. Both are array type. Condition is, that either both can have values, or only one of them will be non-null. But both cannot be null at the same time.
How can I define this requirement in swagger spec, so that I can enforce schema such that both should not be null but any one of them can be null.
Valid Examples:
Ex1--> {"Property1": ["value1"], "Property2": ["value2"]}
Ex2--> {"Property2": ["value2"]}
Ex3--> {"Property1": ["value1"]}
Invalid Example:
{"Property1": [], "Property2": []}
Assuming the object can only contain these two properties (Property1 and/or Property2) and no other properties, your use case can be defined using a combination of object-level minProperties and array-level minItems:
MyObject:
type: object
minProperties: 1 # <-- requires at least one of Property1 or Property2 to be present
properties:
Property1:
type: array
items:
type: string
minItems: 1 # <-----
Property2:
type: array
items:
type: string
minItems: 1 # <-----
If the payload can also contain other properties, the "either Property1 or Property2 must be present" condition can be defined using anyOf + required. This requires OpenAPI 3.x:
# openapi: 3.x.x
MyObject:
type: object
anyOf: # <----------------
- required: [Property1]
- required: [Property2]
properties:
Property1:
type: array
items:
type: string
minItems: 1
Property2:
type: array
items:
type: string
minItems: 1
SomeOtherProperty:
...

RoR - Find if part of a string matches an array of hashes

I know there are a lot of similar questions but I am struggling to find a specific answer.
i have an array of hashes with key of Symbol and a value of Price, I am looking to filter the array to only include the hashes that have a Symbol that ends in the letters ETL
Data looks like:
[ [
{
"symbol": "ABCDEF",
"price": "4"
},
{
"symbol": "GHIETL",
"price": "5"
}
]
You can use something like this:
array.select { |hash| hash[:symbol].end_with? "ETL" }
From the Ruby docs for select:
Returns an array containing all elements of enum for which the given block returns a true value.
You can also provide multiple suffixes to end_with? if you need to filter by multiple suffixes. For example:
array.select { |hash| hash[:symbol].end_with? "ETL", "DEF" }

Model composition in swagger spec without additional nesting layer

If I have the following example where Settings definition is embedded in Thing via composition:
definitions:
Settings:
properties:
foobar:
type: number
format: double
boofar:
type: string
Thing:
properties:
allOf:
$ref: '#/definitions/Settings'
name:
type: string
If I define a method to POST a Thing in editor.swagger.io, it ends up constructing JSON that looks like this:
{
settings: {
foobar: 1,
boofar: "text here"
},
name: "some name"
}
I want to embed a model definition with composition but without the additional nested property definition -- is this possible? This is the JSON structure I would like to have for Thing:
{
foobar: 1,
boofar: "text here",
name: "some name"
}
Is there a way to achieve this?
Your example do not really use composition as allOf is a property.
allOf is supposed to be on the root of the definition and it's an array of schema (reference or inline).
Here's the proper way to use allOf for your example:
swagger: '2.0'
info:
title: API
version: 1.0.0
paths:
/thing:
get:
responses:
200:
description: OK
schema:
$ref: '#/definitions/Thing'
definitions:
Settings:
properties:
foobar:
type: number
format: double
boofar:
type: string
Thing:
allOf:
- $ref: '#/definitions/Settings'
- properties:
name:
type: string
Resulting rendering in SwaggerUI:

Does Dust.js provide a way to reference an object key/value by keywords "key" and "value"?

I want to use Dust.js as a client template engine.
I have a data json like this:
var data = {
"Foo": [{
"somekey": "somevalue",
"otherkey": "othervalue"
}, {
"somekey": "somevalue",
"otherkey": "othervalue"
}],
"Bar": [{
"somekey": "somevalue",
"otherkey": "othervalue"
}, {
"somekey": "somevalue",
"otherkey": "othervalue"
}]
}
I do not know in advance what uppermost object keys will be - I do not know Foo and Bar keys, they can be any value.
So, I need to iterate through this json by keywords like key and value. Something like in this pseudo-code:
{% for(key, value) in data %}
{key}: {value}
{% /for %}
I know that Dust.js has {#section/} to loop through an object. But again, you have to provide a key name:
{#extraData}
{!
Inside this section, Dust looks for
values within the extraData object
!}
Inside the section, the value of name is: {name}{~n}
{/extraData}
And I do not know extraData name in advance.
So, does Dust.js provide a way to reference object keys/values by key and value keywords?
Dust does not provide built-in iteration over objects.
However, you can add the {#iterate} helper to do this type of iteration.
You can get it at https://www.npmjs.com/package/dustmotes-iterate
Example usage:
Data: { obj: {a:"A", b:"B", c:"C" } }
{#iterate key=obj}
{$key}:{$value} {$type}
{/iterate}
Output: a:A string b:B string c:C string

Use an array of objects for series data in Highcharts

I have an array of objects that I want to display in highcharts. Every object has a name and a value.
I have tried to get this to work by doing
var objects = objectArray[]; // objectArray being an array of the objects I want data on
var objectNames = nameArray[]; // This being an array of all the names of the objects
var objectValues = valueArray[]; // An array of all the values of the objects
series: [{
data: objects.value,
name: objects.name
}]
This blew up on me. So I tried building the series like this:
series: [{
data: objectValues,
name: objectNames
}]
This gave me data for the values, but the name was all of the names in the objectNames array... for every single piece of data. so I tried using
series: [{
data: objectValues
},
{
data: objectNames
}]
This resulted in seeing the chart for the objectValues, and in the legend, another option for the names - which is completely unacceptable because there's no point in having a series of labels, right?
So I decided I would programmatically build out a series, using a foreach loop and then pass that into the constructor. However, http://www.highcharts.com/docs/getting-started/how-to-set-options/ says this is "bad code".
What I'm wanting is to be able to pass an array of objects to highcharts, tell it that every piece of data's 'name' is going to be the name value on that particular object, and the data is going to be tied to that particular object's value field. Is there a way to do this? Or is the only option what highcharts considers 'bad'?
So I found a solution.
After getting the data, I did
$.each(item, function (index, value) {
objects.push([value.name, value.value]);
});
And then bound my series with
series: [{
data: objects,
name: 'Value Type Description'
}]
So I have "Value Type Description" in the legend, but when I hover over a specific point I have the name as the label, and valid data displaying in graph form.
I found at http://api.highcharts.com/highcharts#series that if you have an array of two dimensional arrays, you can just pass a string as the first parameter and it would parse it as the label for that point.
EDIT: Example per request.
So you have two pieces to the series field, data and name. Name does NOT apply to the data, that will be the name of the axis.
So data is an array of key/value pairs.
data: [
{[key1, value1]},
{[key2, value2]},
{[key3, value3]}
]
And name is what the "main" label will be - "My Data Stuff" for example.
Then, when you load the chart, in the legend it should say "My Data Stuff", but when you hover over a specific point, say the first one, it will display the Key1 Value1 information.
data: [{
name: 'Point 1',
y: 1,
test1:9,
test2:'ddaf'
}, { -------> right
name: 'Point 2',
y: 5,
test1:12,
test2:'ddddaf'
}]
data: [{
my_name: 'Point 1',
y: 1,
test1:9,
test2:'ddaf'
}, { -------> we change 'name' to 'my_name',
my_name: 'Point 2', then the name you want to show become
y: 5, 'Slice', instead of 'Point 1','Point 2'
test1:12,
test2:'ddddaf'
}]
data: [{
my_name: 'Point 1',
my_y: 1,
test1:9,
test2:'ddaf'
}, { -------> Now,we get nothing.
my_name: 'Point 2',
my_y: 5,
test1:12,
test2:'ddddaf'
}]
So,the 'name' and 'y' is the key.

Resources