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'
Related
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"
There is some questions around this topic, but I dind't find the right way to do it.
What I want is to define all parameters in a single place and reuse it with no need to write it again. I already get that by using "allOf", but this limited the use of "additionalProperties".
My schema has this structure:
SchemaBase:
type: object
properties:
foo:
type: string
SchemaFull:
allOf:
- $ref: '#/components/schemas/SchemaBase'
- type: object
properties:
bar:
type: string
I tried using using definitions but it seems is not anymore in OpenApi version 3.
Here is a solution but its not what I'm looking for, becasue that is for properties, not the entire schema.
You need to use components like this:
openapi: 3.0.1
info:
title: OAS 3
version: 1.0.0
tags:
- name: example
paths:
/exe:
post:
requestBody:
content:
application/json:
schema:
additionalProperties: false
allOf:
- $ref: '#/components/schemas/SchemaBase'
- type: object
properties:
bar:
type: string
responses:
200:
description: Foo
content: {}
components:
schemas:
SchemaBase:
type: object
properties:
foo:
type: string
You may see and play with this here: https://editor.swagger.io/
JSON schema mapped:
{
"additionalProperties": false,
"allOf": [
{ "$ref": "#/definitions/SchemaBase" },
{
"type": "object",
"properties": {
"foo": {
"type": "string"
}
}
}
],
"definitions": {
"SchemaBase": {
"type": "object",
"properties": {
"foo": {
"type": "string"
}
}
}
}
}
Using this schema definition:
schemas:
AllContacts:
type: array
items:
$ref: '#/definitions/ContactModel1'
example:
- id: 1
firstName: Sherlock
lastName: Holmes
- id: 2
firstName: John
lastName: Watson
I get this expected result:
[
{
"id": 1,
"firstName": "Sherlock",
"lastName": "Holmes"
},
{
"id": 2,
"firstName": "John",
"lastName": "Watson"
}
]
Now I'd like to reuse the Holmes example for both the single user (ContactModel1) and as part of an array of users (AllContacts). But if I use the referenced examples:
schemas:
AllContacts:
type: array
items:
$ref: '#/definitions/ContactModel1'
example:
Homes:
$ref: '#/components/examples/Homes'
Watson:
$ref: '#/components/examples/Watson'
examples:
Holmes:
value:
id: 1
first_name: Sherlock
last_name: Holmes
Watson:
value:
id: 2
first_name: John
last_name: Watson
I get this unexpected result in Swagger UI:
[
{
"value": {
"id": 1,
"first_name": "Sherlock",
"last_name": "Holmes",
},
"$$ref": "#/components/examples/Holmes"
},
{
"value": {
"id": 2,
"first_name": "John",
"last_name": "Watson",
},
"$$ref": "#/components/examples/Watson"
}
]
and a similar unexpected example for GET /user/1:
[
{
"value": {
"id": 1,
"first_name": "Sherlock",
"last_name": "Holmes",
},
"$$ref": "#/components/examples/Holmes"
}
]
What am I doing wrong?
I am using this doc as reference:
https://swagger.io/docs/specification/adding-examples/#reuse
This is NOT a valid definition:
components:
schemas:
AllContacts:
type: array
items:
$ref: '#/definitions/ContactModel1'
example:
Homes:
$ref: '#/components/examples/Homes'
Watson:
$ref: '#/components/examples/Watson'
1) The example syntax is wrong. OpenAPI 3.0 has two keywords for examples - example (singular) and examples (plural). They work differently:
example requires an inline example and does not support $ref.
examples is a map (collection) of named examples. It supports $ref - but you can only $ref whole examples, not individual parts of an example. This also means it's not possible to build an example from multiple $refs. Note that not all elements support plural examples.
Note for Swagger UI users: Swagger UI currently supports example (singular) but not examples (plural). Support for examples is tracked in this issue.
2) The Schema Object only supports singular example but not plural examples. In other words, schemas support inline examples only.
3) In OpenAPI 3.0, schema references use the format #/components/schemas/..., not #/definitions/...
I'd like to use the same EXAMPLE definition for Holmes in both cases, the array of users and the single user.
There's no way to reuse a part of an example in this case. You'll have to repeat the example value in both schemas:
components:
schemas:
ContactModel1:
type: object
properties:
...
example:
id: 1
first_name: Sherlock
last_name: Holmes
AllContacts:
type: array
items:
$ref: '#/components/schemas/ContactModel1'
example:
- id: 1
first_name: Sherlock
last_name: Holmes
- id: 2
first_name: John
last_name: Watson
Use allOf for arrays
components:
schemas:
AllContacts:
type: array
items:
$ref: '#/definitions/ContactModel1'
example:
allOf:
- $ref: '#/components/examples/Homes'
- $ref: '#/components/examples/Watson'
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!
I want to model a response object containing an array of different types of objects in swagger, something like this:
{
"table": [
{
"user" : []
},
{
"customer": []
},
{
"employee": []
}
]
}
I have tried a solution below but it wraps all the properties in a single object { [ { "user": [], "customer": [] } ] }.
responses:
200:
schema:
type: array
items:
type: object
properties:
user:
type: array
items:
$ref: '#/definitions/User'
customer:
type: array
items:
$ref: '#/definitions/Customer'
employee:
type: array
items:
$ref: '#/definitions/Employee'
That will be supported in the next release of OpenAPI spec (3.0) and here is the related discussion about this feature:
https://github.com/OAI/OpenAPI-Specification/issues/57
Here is an example (provided in the URL above):
{
"oneOf": [
{ "$ref": "Cat" },
{ "$ref": "Dog" }
]
}