How to create a Json object from the Schema definitions of swagger - json

I am failing to to remove the outer array from the json request body that I want swagger to generate from the schema definitions.
I want to generate this object:
{
"name":
{
"value": "test04"
},
"mail": {
"value": "test04#gmail.com"
}
}
But swagger is giving me this:
[
{
"name": {
"value": "string"
},
"mail": {
"value": "string"
}
}
]
This is my definition section:
definitions:
user:
type: "object"
properties:
name:
type: object
properties:
value:
type: string
mail:
type: object
properties:
value:
type: string
May you kindly assist. I am new to swagger. I am using version 2.0

Your User schema is correct, so this means there's an extra type: array somewhere in the parameter, request body, or response where this schema is used.

Related

Is it possible to put multiple unnamed nested objects inside of an array in an openAPI YAML file?

I am writing a openapi specification in YAML and I want to have this JSON code as a YAML file for a openAPI specification:
{
"example": [
{
"example1":
{
"type": "string"
} ,
"example2":
{
"type":"string"
}
},
{
"example1":
{
"type": "string"
},
"example2":
{
"type": "string"
},
}
]
}
I tried:
schema:
type: object
properties:
example:
type: array
items:
type: object
properties:
example1:
type: string
example2:
type: string
type: object
properties:
example1:
type: string
example2:
type: string
But you cannot have properties again because every map key has to be unique.

Rswag schema does not fail for invalid properties of object in array

I have the following rswag test:
response '200', 'response with array of objects' do
schema type: :array,
items: {
type: :object,
properties: {
expected_id: { type: "integer", format: "int32" },
}
}
run_test!
end
My API responds with JSON:
[
"invalid_key": 1,
"invalid_key": 2
]
In other words, an invalid response according to the schema above.
However, the test does not fail. This is unexpected.
On the other hand, if my API responds with an array of null values: "[null]" I get the response I expect:
Failure/Error: test.run
Rswag::Specs::UnexpectedResponse:
Expected response body to match schema: The property '#/0' of type null did not match the following type: object in schema 19e6c168-1da4-58a6-93fc-a33d9d91233a
So my test is checking that there is an array of objects, but not checking that the properties in the nested objects are what is expected. How do I make it check the properties as well?
I was missing a required clause in my schema:
schema type: :array,
items: {
type: :object,
properties: {
expected_id: { type: "integer", format: "int32" },
}
required: ['expected_id'] # <- this was missing
}
This raises a test error if the response my endpoint generates does not include the expected_id key.

How can I describe this complex json model in swagger

I'm trying to use swagger to describe a rest api I added to some code.
This is one of my simple returns.
Saw a couple good examples but didn't have any luck trying to make sense how to apply it to my problem.
The different API calls will contain different content.
If I know what this one should look like I aught to be able to figure the others out.
Can anyone tell me what this description should look like?
{
"result" : {
"content" : {
"UNTAINTED_HOST" : "www.google.com",
"DATA" : [
"216.58.217.36"
],
"IP_or_NAME" : "NAME",
"RC" : "true",
"FAULT_MSG" : "No Faults"
},
"detail" : "Sucessfully terminated your request",
"short" : "Done" }
}
Or this even simpler one:
{
"result" : {
"short" : "Done",
"detail" : "Sucessfully terminated your request",
"content" : "Running"
}
}
Think it would look like -
{
"definitions": {
"result": {
"type": "object",
"required": [
"short", "detail", "content"
],
"properties": {
"type": "array",
"items": {
"type": "object",
"properties": {
"content": {
"type": "string"
},
"detail": {
"type": "string"
},
"short": {
"type": "string"
},
}
}
}
}
}
}
Tried this in the swagger editor... seems I missed something.
definitions:
results:
type: object
required: [ result ]
properties:
result:
type: array
items:
$ref: #/definitions/result
result:
type: object
properties:
short:
type: string
detail:
type: string
content:
type: string
This seemed to work:
results:
type: object
required: [ result ]
properties:
result:
type: array
items:
type: object
required: [ short, detail, content ]
properties:
short:
type: string
detail:
type: string
content:
type: string
Thanks
This worked for me:
definitions:
result:
type: object
properties:
short:
type: string
detail:
type: string
content:
type: string
results:
type: object
required: [result]
properties:
result:
type: array
items:
$ref: '#/definitions/result'
Notice the single quotes around the value of $ref. I didn't see those in your original post. However, you didn't mention the error you are seeing. If this answer doesn't help, please update your question with more details.

Kotlin Jackson generation objects from JSON

Please help! I'm trying to generate object from JSON with jackson kotlin module. Here is json source:
{
"name": "row",
"type": "layout",
"subviews": [{
"type": "horizontal",
"subviews": [{
"type": "image",
"icon": "ic_no_photo",
"styles": {
"view": {
"gravity": "center"
}
}
}, {
"type": "vertical",
"subviews": [{
"type": "text",
"fields": {
"text": "Some text 1"
}
}, {
"type": "text",
"fields": {
"text": "Some text 2"
}
}]
}, {
"type": "vertical",
"subviews": [{
"type": "text",
"fields": {
"text": "Some text 3"
}
}, {
"type": "text",
"fields": {
"text": "Some text 4"
}
}]
}, {
"type": "vertical",
"subviews": [{
"type": "image",
"icon": "ic_no_photo"
}, {
"type": "text",
"fields": {
"text": "Some text 5"
}
}]
}]
}]
}
I'm trying to generate instance of Skeleton class.
data class Skeleton (val type : String,
val name: String,
val icon: String,
val fields: List<Field>,
val styles: Map<String, Map<String, Any>>,
val subviews : List<Skeleton>)
data class Field (val type: String, val value: Any)
As you can see, Skeleton object can have other Skeleton objects inside (and these objects can have other Skeleton objects inside too), also Skeleton can have List of Field objects
val mapper = jacksonObjectMapper()
val skeleton: Skeleton = mapper.readValue(File(file))
This code ends with exception:
com.fasterxml.jackson.databind.JsonMappingException: Instantiation of [simple type, class com.uibuilder.controllers.parser.Skeleton] value failed (java.lang.IllegalArgumentException): Parameter specified as non-null is null: method com.uibuilder.controllers.parser.Skeleton.<init>, parameter name
at [Source: docs\layout.txt; line: 14, column: 3] (through reference chain: com.uibuilder.controllers.parser.Skeleton["subviews"]->java.util.ArrayList[0]->com.uibuilder.controllers.parser.Skeleton["subviews"]->java.util.ArrayList[0])
There are several issues I found about your mapping that prevent Jackson from reading the value from JSON:
Skeleton class has not-null constructor parameters (e.g. val type: String, not String?), and Jackson passes null to them if the value for those parameters is missing in JSON. This is what causes the exception you mentioned:
Parameter specified as non-null is null: method com.uibuilder.controllers.parser.Skeleton.<init>, parameter name
To avoid it, you should mark the parameters that might might have values missing as nullable (all of the parameters in your case):
data class Skeleton(val type: String?,
val name: String?,
val icon: String?,
val fields: List<Field>?,
val styles: Map<String, Map<String, Any>>?,
val subviews : List<Skeleton>?)
fields in Skeleton has type List<Field>, but in JSON it's represented by a single object, not by an array. The fix would be to change the fields parameter type to Field?:
data class Skeleton(...
val fields: Field?,
...)
Also, Field class in your code doesn't match the objects in JSON:
"fields": {
"text": "Some text 1"
}
You should change Field class as well, so that it has text property:
data class Field(val text: String)
After I made the changes I listed, Jackson could successfully read the JSON in question.
See also: "Null Safety" in Kotlin reference.

Kendo DataSource and nested Json

I'm having trouble finding any documentation that accurately describes what to do in a situation like this.
I have the following schema for my DataSource:
schema: {
model: {
id: "Id",
fields: {
BuyerProfile: [
{
Id: { type: "number" },
Name: { type: "string" },
City: { type: "string" },
State: { type: "string" },
Description: { type: "string" },
BuyerType: [
{
Id: { type: "number" },
Name: { type: "string" }
}
]
}
]
}
}
}
My JSON is formatted fine and I don't get any errors. But when I try to print any of the items out in my kendo template I just get undefined printed.
For example:
<script type="text/x-kendo-tmpl" id="profileTemplate">
<p> #:BuyerProfile.Name# </p>
</script>
The above example is literally just printing out undefined inside of the p tags. No JavaScript errors or anything though.
I might be wrong but I thought I read somewhere that this is how you go about using nested JSON objects with Kendo. However, it's clearly not since it's not working or I'm missing something.
Answer you are looking is probably here or here. Basically you either need to parse your schema or just make you schema flat without any nested properties.