how to place multiple items in example array using refs - Open API - swagger

I'm writing an API doc, and I have an endpoint returning multiple items of the same thing.
I would like to have more items in the response example, but coming from different refs
here is the endpoint response documentation:
responses:
'200':
description: json containing the updated notification
content:
application/json:
schema:
type: object
properties:
payload:
type: array
items:
$ref: "#/components/schemas/forecast_item"
here's the item schema:
forecast_item:
type: object
properties:
transmission_date:
type: string
timestamp:
type: number
temperature:
type: number
humidity:
type: number
rain:
type: number
icon:
type: string
example:
transmission_date: "2022-06-08 12:00:00"
timestamp: 1654689600
temperature: 28.28
humidity: 33
rain: 0
icon: 04d
the above produce the following example:
{
"payload": [
{
"transmission_date": "2022-06-08 12:00:00",
"timestamp": 1654689600,
"temperature": 28.28,
"humidity": 33,
"rain": 0,
"icon": "04d"
}
]
}
I tried the following
properties:
payload:
type: array
items:
$ref: "#/components/schemas/device"
example:
payload: [
$ref: "#/components/schemas/device",
$ref: "#/components/schemas/device",
$ref: "#/components/schemas/device"
]
hoping I would achieve:
{
"payload": [
{
"transmission_date": "2022-06-08 12:00:00",
"timestamp": 1654689600,
"temperature": 28.28,
"humidity": 33,
"rain": 0,
"icon": "04d"
},
{
"transmission_date": "2022-06-08 12:00:00",
"timestamp": 1654689600,
"temperature": 28.28,
"humidity": 33,
"rain": 0,
"icon": "04d"
},
{
"transmission_date": "2022-06-08 12:00:00",
"timestamp": 1654689600,
"temperature": 28.28,
"humidity": 33,
"rain": 0,
"icon": "04d"
}
]
}
but i did not.

To add a multi-item example for an array, place the example on the same level as type: array. The example value should be the full sample array, in the YAML or JSON format. $ref is not supported within the example value.
properties:
payload:
type: array
items:
$ref: "#/components/schemas/device"
example:
- transmission_date: '2022-06-08 12:00:00'
timestamp: 1654689600
temperature: 28.28
humidity: 33
rain: 0
icon: 04d
- transmission_date: '2022-06-08 12:00:00'
timestamp: 1654689600
temperature: 28.28
humidity: 33
rain: 0
icon: 04d
- transmission_date: '2022-06-08 12:00:00'
timestamp: 1654689600
temperature: 28.28
humidity: 33
rain: 0
icon: 04d

Related

How to define this array of objects in OpenAPI?

How to write an OpenAPI definition for the following JSON?
Bazically it is an array consisting of two objects with similar attributes but different fields.
[
{
"studentname": "somename",
"studentrollno": "somerollno",
"studentsubjects": [
{
"level": "third",
"physics": "xyz",
"maths": "somevalue"
},
{
"level": "second",
"physics": "abc",
"maths": "somevalue11"
}
],
"studentresult": "pass"
},
{
"studentname": "newname",
"studentrollno": "newrollno",
"studentsubjects": [
{
"level": "third",
"physics": "xyz",
"maths": "somevalue"
},
{
"level": "second",
"physics": "abc",
"maths": "somevalue11"
}
],
"studentresult": "fail"
}
]
type: array
items:
type: object
properties:
studentname:
type: string
example: somename
studentrollno:
type: string
example: somerollno
studentsubjects:
type: array
items:
type:object
properties:
level:
type: string
example: third
physics:
type: string
example: xyz
maths:
type: string
example: somevalue
type: object
This is how i have written , i have written it for the first object in studentsubjects array , but not able to code it for second object in the same array , when i am trying to do it , it is throwing me erro saying duplication of type at last line

Swagger OpenAPI array documentation in response

How a Json like this be documented inside response section in swagger?
{
"data": [{
"id": "",
"name": "James",
"chat": "1",
"messages": {
"current": null,
"count": null,
"length": null,
"rows": null,
"type": null
},
"cid": "204",
"cat": "messages",
"mark": false
}],
"total": 200,
"action": "success"
}
I tried
responses:
'200':
description: test data
content:
application/json:
schema:
example:
- id: null
name: James
chat: 1
messages:
current:
count:
length:
rows:
type:
cid: 204
cat: chat
mark: false
totalCount: 200
action: success
But show error bad indentation of a mapping entry
You have to define the expected object structure as a separate schema in the #/components/schemas section.
For example you name the resulting model ResponseObj, which has the properties data, total and action. The data property is another complex type with its own properties, so you also define a model for that type. This continues until you have defined all the models down to their simple property types.
Here is the definition for your example:
components:
schemas:
ResponseObj:
properties:
data:
type: array
items:
$ref: '#/components/schemas/DataObj'
total:
type: string
action:
type: string
DataObj:
properties:
id:
type: string
name:
type: string
chat:
type: string
messages:
type: array
items:
$ref: '#/components/schemas/MessageObj'
cid:
type: number
cat:
type: string
mark:
type: boolean
MessageObj:
properties:
current:
type: string
count:
type: string
length:
type: number
rows:
type: number
type:
type: string
Now you can reference the ResponseObj model as the expected result of your operation:
paths:
/example:
get:
responses:
"200":
content:
application/json:
schema:
$ref: '#/components/schemas/ResponseObj'

Associative array in Swagger (OpenApi 3.0.0)

I have an API that always answers with an associative array where there is only one entry with key "data" containing the final result. The result can be an object or array of objects. This is API output:
{
"data": {
"id": 1,
"name": "product1 name",
"type": "type a",
"created_at": "2011-09-28T13:20:15+02:00"
}
}
{
"data": [
{
"id": 1,
"name": "product1 name",
"type": "type a",
"created_at": "2010-09-28T13:20:15+02:00"
},
{
"id": 6,
"name": "product6 name",
"type": "type f",
"created_at": "2010-09-28T13:20:28+02:00"
},
{
"id": 17,
"name": "product17 name",
"type": "type Q",
"created_at": "2010-09-28T13:20:42+02:00"
}
]
}
How can I get swagger to show data which is the name of the key, in the docs?
As for now I only get nested array in swagger:
[
{
"id": 1,
"name": "product1 name",
"type": "type a",
"created_at": "2010-09-28T13:20:15+02:00"
}
]
[
[
{
"id": 1,
"name": "product1 name",
"type": "type_a",
"created_at": "2010-01-01T18:21:20+02:00"
}
]
]
I need the data key to be shown in swagger as it comes back from the API. Is it doable? I wasn't able to find any solution to this yet...:/
parts of my yaml file:
openapi: "3.0.0"
info:
version: 1.0.0
paths:
/products/:
get:
summary: List all available products
operationId: listProducts
tags:
- products
responses:
200:
description: Array of products
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/Products"
/products/{product_id}:
get:
summary: Get specific product
operationId: showProductById
tags:
- products
parameters:
- name: product_id
in: path
required: true
description: The id of the product to retrieve
schema:
type: integer
format: int32
example: 1
responses:
200:
description: Product object
content:
application/json:
schema:
type: array
properties:
datar: string
items:
$ref: "#/components/schemas/Product"
components:
schemas:
Product:
type: object
properties:
id:
type: integer
format: int32
example: 10
name:
type: string
example: "producta name"
type:
type: string
example: "type a"
created_at:
type: string
format: date
example: "2010-01-01T18:21:20+02:00"
Products:
type: array
items:
$ref: "#/components/schemas/Product"
Any help will be much appreciated:)
Cheers
try with a "additionalProperties" in a intermediate layer for a assoc-array, source: https://swagger.io/docs/specification/data-models/dictionaries/
components:
schemas:
Product:
type: object
properties:
id:
type: integer
format: int32
example: 10
name:
type: string
example: "producta name"
type:
type: string
example: "type a"
created_at:
type: string
format: date
example: "2010-01-01T18:21:20+02:00"
ArrayOfProduct:
type: object
additionalProperties:
$ref: "#/components/schemas/Product"
Products:
type: object
properties:
data:
type: object
$ref: "#/components/schemas/ArrayOfProduct"

Implement dynamic values

I am trying to implement this request:
curl -ik -X POST "https://<HOST_IP>/api/v1/roles/" -H "accept: application/json" -H "x-access-token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwidXNlcl9pZCI6IjVlMGM0Nzg0OGZiMDNhMzJmMTc0Nzc0NSIsInNvdXJjZSI6bnVsbCwicGVybWlzc2lvbnMiOnsiQUxMIjpbdHJ1ZV19LCJpYXQiOjE1Nzc4NzY0ODIsImV4cCI6MTU3NzkxOTY4Mn0.Z-WJ7ouQ5XdDqNvxMgTc-74qgW4sShtJrqTaEH-rykA" -H "Content-Type: application/json" -d "{\"users\":[{\"id\":\"5e0c47848fb03a32f1747745\",\"title\":\"admin\"}],\"groups\":null,\"permissions\":{\"ISSUES\":[\"All\"]},\"devicePermissions\":null,\"name\":\"role name\",\"description\":\"\"}"
I have this request body:
"users": [
{
"id": "5e0c47848fb03a32f1747745",
"title": "admin"
}
],
"groups": [
{
"id": "5e0c8f0d8a2c1676ed67fd2e",
"title": "asgsagas"
}
],
"permissions": {
"ISSUES": [
"All"
],
"RULES": [
"All"
]
},
"devicePermissions": null,
"name": "gasgag",
"description": ""
}
where under "permissions" object I can add (optional) additional string arrays , But I am struggling how to implement it, should I implement all options? how can I make it generic?
This is how I implemented it:
permissions:
type: object
properties:
ISSUES:
type: array
items:
type: string
example: "All"
KNOWLEDGE_EXPLORER:
type: array
items:
type: string
example: "Archive"
ANALYSIS_AND_REPORTS:
type: array
items:
type: string
example: "All"
DEVICES:
type: array
items:
type: string
example: "All"
CREDENTIALS_MANAGEMENT:
type: array
items:
type: string
example: "All"
BACKUPS:
type: array
items:
type: string
example: "All"
ABOUT:
type: array
items:
type: string
example: "All"
INTEGRATIONS:
type: array
items:
type: string
example: "All"
USERS:
type: array
items:
type: string
example: "All"
AUDIT:
type: array
items:
type: string
example: "All"
APPLICATION_SETTINGS:
type: array
items:
type: string
example: "All"
DEV_TOOLS:
type: array
items:
type: string
example: "All"
OTHER:
type: array
items:
type: string
example: "All"
I am using java in order to use this api request and I can see that maybe my implementation is not correct.
This is the expected response:
{
"ok": true,
"code": "",
"message": "Create was success",
"hasException": false,
"exception": null,
"errors": [],
"info": null,
"content": {
"devicePermissions": {
"showAll": true,
"labels": []
},
"_id": "5e0cb0ed8a2c1676ed67fd56",
"name": "devices_delete_labels_role_278932055218587",
"description": "New Role added to the system",
"permissions": {},
"updatedAt": "2020-01-01T14:47:09.792Z",
"createdAt": "2020-01-01T14:47:09.792Z",
"__v": 0
}
}

How to write the Array In Swagger?

API Documentation in Swagger
- name: orderNo
in: query
description: Number of the order
required: false
schema:
type: integer
format: int32
- name: orderName
in: query
description: Name of the order
required: false
schema:
type: string
JSON
"orderNo": 6362131469,
"orderName": "HuntPrivateOrder",
"lineItems": [
{
"name": "Hunt T-Shirt",
"id": 1,
"sku": 11456873613,
"quantity": 1
},
{
"name": "Hunt Tomato",
"id": 1,
"sku": 11456873677,
"quantity": 1
}
],
How do I convert my array in my JSON File to the proper Swagger Format? I only know how to convert the non-array but not the actual array. Thanks for the help!

Resources