MongoMapper and custom type properties - ruby-on-rails

I am trying to create a MongoMapper model which has an array of attributes. This array should contain all kind of information (Integers, Floats, URLs, Enums, Dates, Relations to other documents) depending on the user`s desire.
My problem is how implement the array of attributes, how to implement the custom data type of the attributes. I suppose that the document serialized should look similar to this:
{
...
attributes: {
age: {
name: "Age",
type: "Integer",
value: 12
},
height: {
name: "Height",
type: "Float",
value: 1.86
},
mother: {
name: "Mother",
type: "Relation\Person",
value: "\people\sara"
}
}
}
Any ideas how to approach this problem? My first tought was to create a base class like a factory which has to serialize and deserialize the attributes, but from what I have managed to code, nothing was working properly.
Thanks in advance!

You could make a custom type, but if you have an array, why not just use an embedded object?
See this:
http://speakerdeck.com/u/jnunemaker/p/mongomapper-mapping-ruby-to-and-from-mongo
Slides 47-49.

Related

OpenAPI 3 Dictionary Define Key & Value Types

I have read https://swagger.io/docs/specification/data-models/dictionaries/, but am still having trouble modeling something like:
{
"top": {
"elem0": {
...
},
"elem1": {
...
},
...
}
}
top is a dictionary, containing an arbitrary number of entries. The trouble is that I want to define a reusable schema for the key and value types.
E.g., instead of saying that the keys are of type string, I say they are of type #/components/schemas/KEY. And instead of saying that the values are of type object, I say they are of type #/components/schemas/VALUE.
Is this possible?

Specifying multiple types for additionalProperties through Swagger/OpenAPI

I am looking to represent the following JSON Object in OpenAPI:
{
"name": "Bob",
"age": 4,
...
}
The number of properties and the property names are not fully predetermined, so I look to use additionalProperties. However, I'm not too certain how it would be represented through OpenAPI/Swagger 2.0. I tried this:
Person:
type: object
additionalProperties:
type:
- int
- string
or the JSON equivalent:
{
"Person": {
"type": "object",
"additionalProperties": {
"type": ["int", "string"]
}
}
}
but that didn't quite work. Is there any way to keep the structure of the JSON Object I want to represent, for specifically strings and integers, and not arbitrary object types?
OpenAPI 3.1
In OpenAPI 3.1, the type keyword can take a list of types:
Person:
type: object
additionalProperties:
type: [string, integer]
OpenAPI 3.x
OpenAPI 3.0+ supports oneOf so you can use:
Person:
type: object
additionalProperties:
oneOf:
- type: string
- type: integer
OpenAPI 2.0
OpenAPI 2.0 does not support multi-type values. The most you can do is use the typeless schema, which means the additional properties can be anything - strings, numbers, booleans, and so on - but you can't specify the exact types.
Person:
type: object
additionalProperties: {}
This is equivalent to:
Person:
type: object

How to parse JSON data with the same key and different data?

I have a little problem with parse JSON data.
There have a Web APIļ¼Œbasic response data like this:
{
topic_name: "Kevin",
topic_type: 1,
extraData: {}
}
With the different topic_type value, the extraData maybe have different data structure, e.g the extraData object has different key-values.
In this case, how to create model classes and parse the JSON string to models?
Or does this API design reasonable? Is there a better API design to solve these cases?
update 1:
With the same topic_type, the extraData's structure is always same.
I have considered use subclasses, but it need a subclass for every topic_type.
update 2:
Here is some example of JSON data, different topic_type with different extraData.
when topic_type equal to 1,
{
topic_name: "Kevin",
topic_type: 1,
extraData: {
data_type1: value,
data_type2: value2
}
}
when topic_type equal to 2,
{
topic_name: "David",
topic_type: 2,
extraData: {
data_type3: value3
}
}
it not real data, I'm not deal with a 'topic' issue, just a example, the key is the extraData object has different type keys.
JSONModel might be exactly what you're looking for. Parses json and gives you models
try to define all types in a class:
{
topic_name: "Kevin",
topic_type: 1,
extraData: {
data_type1: value,
data_type2: value2,
data_type3: value3,
}
}
after Json parsing, take the data types u need based on topic type.
Ps. All data types need to be nullable

How to map ordered NSDictionary using JSONModel

I've got a JSON as below.
odds: {
0501: {
x: 2.75,
description: "a"
},
0502: {
x: 3.25,
description: "b"
},
0513: {
x: 3.5,
description: "c"
},
0503: {
x: 3.5,
description: "d"
},
0505: {
x: 7.5,
description: "e"
},
0504: {
x: 7.5,
description: "f"
},
0512: {
x: 10,
description: "g"
}
}
This hash comes from HTTP response as I want to show but the thing that I use JSONModel to map it and there is only way to map that NSDictionary. When map this JSON to NSDictionary (as you can guess) this an unordered and sequence of data comes up mixed.
So, how to map this JSON, without broke up its sequence using JSONModel and NSDictionary ?
NSDictionary is inherently unordered:
Are keys and values in an NSDictionary ordered?
If you want to preserve the order of key-value entries, you need to use a data structure other than NSDictionary. Any library that passes your data through an NSDictionary cannot preserve the order.
Something I've done in this situation is to sort the dictionary keys in a separate array, in addition to the dictionary. Use the ordered key array to determine how to display your dictionary values.
Dictionaries can not be sorted, but as your JSON seems to be an array of objects, iterate thru your resulting NSDictionary with for...in and add the elements to a mutable array.
Afterwards sort the resulting array using .sortInPlace by comparing the x-value.

How to represent the following response in a Swagger Doc definition

I have several levels of nested objects in my response and I cannot change these. How can I represent these in a Swagger doc definition?
{
name.response: {
name.result: {
name.row: [
{
name.first: "John",
name.last: "Doe",
}
]
}
}
}
Here is what I have right now:
person:
type: object
properties:
name.first:
type:string
name.last
type.string
name.result:
type: object
properties:
name.row:
type: array
items:
$ref: '#/definitions/person'
name.response:
type: object
properties:
name.result:
type: object
description: Result
I don't think this is right because the Swagger Editor is warning me that the name.result definition is not being used.
Nevermind!
I figured it out:
name.response:
type: object
properties:
name.result:
type: object
description: Result
$ref: '#/definitions/name.result'
I had tried this already, but the warning that name.result wasn't being used was still there...
But then I refreshed the Swagger Editor and the warning went away.

Resources