I have an API which has some mutually exclusive arguments for json payloads. I would like to display this in multiple examples but the schema in the yaml file seems to only be able to generate one example.
If my inputs could be:
{
"text": "some text"
}
OR
{
"list": ["some text", "some more"]
}
BUT NOT
{
"text": "some text",
"list": ["some text", "some more"]
}
how can this be done in swagger 2.0?
A schema definition like the following is misleading
definitions:
MutexSchema:
type: object
properties:
list:
type: array
items:
type: string
example: ["some text", "some more"]
text:
type: string
example: "Some text"
And it seems you cannot specify multiple body options. What would be a good way to show the mutually exclusive payloads with their corresponding responses?
OpenAPI 2.0 does not support mutually exclusive properties, but you can emulate this by adding minProperties: 1 and maxProperties: 1 to your schema. This essentially means that only text or only list can be passed, but not both.
definitions:
MutexSchema:
type: object
properties:
list:
type: array
items:
type: string
example: ["some text", "some more"]
text:
type: string
example: "Some text"
minProperties: 1 # <--------
maxProperties: 1
What would be a good way to show the mutually exclusive payloads with their corresponding responses?
Migrate to OpenAPI 3 which supports oneOf and multiple examples for requests and responses. Note that there's no way to correlate request and response examples, but you can provide additional information in the description fields.
paths:
/something:
post:
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/MutexSchema'
# Request body examples
examples:
text example:
summary: Example with text
value:
text: Some text
list example:
summary: Example with list
value:
list: [some text, some more]
responses:
'200':
description: OK
content:
application/json:
schema:
...
# Response examples
examples:
ex1:
summary: ...
value:
...
ex2:
summary: ...
value:
...
components:
schemas:
MutexSchema:
oneOf:
- $ref: '#/components/schemas/Text'
- $ref: '#/components/schemas/List'
Text:
type: object
required:
- text # <--- Property must be marked as required for oneOf to work
properties:
text:
type: string
example: Some text
additionalProperties: false
List:
type: object
required:
- list # <--- Property must be marked as required for oneOf to work
properties:
list:
type: array
items:
type: string
example: [some text, some more]
additionalProperties: false
Related
I'm using discriminator: resouce_type in my YAML for some of the definitions. They are not showing on UI. Can someone please help, is it supported in swagger UI or Am I doing something wrong..
Swagger/OpenAPI definition:
definitions:
myModel:
allOf:
- $ref: '#/definitions/ResponseWrapper'
- type: object
type: object
properties:
field1:
type: number
format: string
description: "My field 1"
field2:
type: string
description: "My field 2"
ResponseWrapper:
allOf:
- type: object
discriminator: resouce_type
Swagger-UI configuration options:
SwaggerUI({
"library": "spring-mvc",
"dateLibrary": "legacy",
"hideGenerationTimestamp": true,
"modelPackage": "com.example",
"apiPackage": "com.example",
"useTags": true,
"importMappings": {
"ResponseWrapper": "com.example.ResponseWrapper"
}
}
)
Can swagger read those classes from classpath and resolve and show in UI.?
I have a data model definition in OpenAPI 3.0, using SwaggerHub to display the UI. I want one of the properties of a model to be related, which is an array of properties of the same model.
Foo:
properties:
title:
type: string
related:
type: array
items:
$ref: '#/components/schemas/Foo'
The parser doesn't seem to like this - the UI shows the related property as an empty array. Is this kind of self-reference possible in OpenAPI 3.0?
Your definition is correct, it's just Swagger UI currently does not render circular-referenced definitions properly. See issue #3325 for details.
What you can do is add a model example, and Swagger UI will display this example instead of trying to generate an example from the definition.
Foo:
type: object
properties:
title:
type: string
related:
type: array
items:
$ref: '#/components/schemas/Foo'
example: # <-------
title: foo
related:
- title: bar
- title: baz
related:
- title: qux
Alternatively, you can add an example just for the related array:
Foo:
type: object
properties:
title:
type: string
related:
type: array
items:
$ref: '#/components/schemas/Foo'
example: # <--- Make sure "example" is on the same level as "type: array"
- title: bar
- title: baz
related:
- title: qux
I got tired of this pesky situation, so I went with no example at all and chose to get rid of the items property, add a description element and use an empty array instead:
Foo:
type: object
properties:
title:
type: string
related:
type: array
description: Array of Foo elements
example: []
You can achieve that by a proxy model:
...
_MessageProxy:
description: Message
type: object
required:
- id
- user
- body
- publishedAt
properties:
id:
title: Message id
type: string
readOnly: true
example: '595f4acf828b0b766ad11290'
user:
$ref: '#/components/schemas/User'
Message:
allOf:
- $ref: '#/components/schemas/_MessageProxy'
- type: object
properties:
parent:
title: Parent
readOnly: true
allOf:
- $ref: '#/components/schemas/_MessageProxy'
...
Using a dummy model and cross reference:
Foo:
properties:
title:
type: string
related:
type: array
items:
$ref: '#/components/schemas/_Foo'
_Foo:
properties:
title:
type: string
related:
type: array
items:
$ref: '#/components/schemas/Foo'
I am defining an API specification in SwaggerHub using OpenAPI 2.0. The /contacts request returns an array of contacts. The definition is below:
/contacts:
get:
tags:
- contacts
summary: Get all the contacts
description: This displays all the contacts present for the user.
operationId: getContact
produces:
- application/json
- application/xml
responses:
200:
description: successful operation
schema:
$ref: '#/definitions/AllContacts'
400:
description: Invalid id supplied
404:
description: Contact not found
500:
description: Server error
definitions:
AllContacts:
type: array
items:
- $ref: '#/definitions/ContactModel1'
- $ref: '#/definitions/ContactModel2'
ContactModel1:
type: object
properties:
id:
type: integer
example: 1
firstName:
type: string
example: 'someValue'
lastName:
type: string
example: 'someValue'
ContactModel2:
type: object
properties:
id:
type: integer
example: 2
firstName:
type: string
example: 'someValue1'
lastName:
type: string
example: 'someValue1'
For some reason, it only returns the second object not the whole array of objects.
I am using OpenAPI 2.0 and suspect that the arrays are not well supported in this version.
An array of objects is defined as follows. The value of items must be a single model that describes the array items.
definitions:
AllContacts:
type: array
items:
$ref: '#/definitions/ContactModel'
ContactModel:
type: object
properties:
id:
type: integer
example: 1
firstName:
type: string
example: Sherlock
lastName:
type: string
example: Holmes
By default, Swagger UI displays the array examples with just one item, like so:
[
{
"id": 1,
"firstName": "Sherlock",
"lastName": "Holmes"
}
]
If you want the array example to include multiple items, specify the multi-item example in the array model:
definitions:
AllContacts:
type: array
items:
$ref: '#/definitions/ContactModel1'
example:
- id: 1
firstName: Sherlock
lastName: Holmes
- id: 2
firstName: John
lastName: Watson
I realise this is a bit offtopic, but I landed here looking for an example for OpenApi 3.0. For others looking for the same thing, this is how to do it:
paths:
/product-category:
get:
summary: 'Returns all product categories'
operationId: readProductCategory
tags:
- productCategory
responses:
'200':
description: 'Details about all product categories'
content:
application/json:
schema:
type: array
items:
allOf:
- $ref: '#/components/schemas/Identifier'
- $ref: '#/components/schemas/ProductCategory'
I've been searching fruitlessly on how to swagger spec a POST body with a list inside of it. How do I do it? Here's what I have:
/groups:
post:
summary: Creates a group
parameters:
- name: body
in: body
schema:
properties:
name:
type: string
description:
type: string
groupType:
type: string
enum: [ "open", "closed", "secret" ]
users:
type: string list # <--------- a list of strings
responses:
201:
description: Group created
default:
description: Group creation failed
For property being an array of string, please refer to https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/test/resources/2_0/petstore.yaml#L660 as an example:
photoUrls:
type: array
items:
type: string
I'm trying to document a REST API using Swagger. A simplified JSON response from our API looks like:
{
"data": {
"type": "person"
"id": "1"
"attributes": {
"name": "Joe"
"age": 32
...
}
"links": {
...
}
}
}
or
{
"data": {
"type": "job"
"id": "22"
"attributes": {
"name": "Manager"
"location": "Somewhere"
...
}
"links": {
...
}
}
}
Their Swagger definitions for a successful GET might look like:
'200':
description: OK.
schema:
type: object
properties:
data:
type: object
properties:
id:
type: string
type:
type: string
attributes:
$ref: '#/definitions/person'
or
'200':
description: OK.
schema:
type: object
properties:
data:
type: object
properties:
id:
type: string
type:
type: string
attributes:
$ref: '#/definitions/job'
There's potentially a lot of repetition like this in our Swagger file. Is it possible to define these responses to share the common parts? i.e. I don't want to type out or copy/paste this part tens of times:
'200':
description: OK.
schema:
type: object
properties:
data:
type: object
properties:
id:
type: string
type:
type: string
I couldn't see how this would work using the discriminator field, or using $ref.
You can use allOf to do composition (in conjunction with discriminator you can do inheritance but it's not really functionnal)
allOf is used with an array of schema or reference, it will create a new definition containing all properties of all definitions in the array.
Given that you want some of your definitions to share id and type properties, it is done this way:
swagger: '2.0'
info:
version: 1.0.0
title: API
paths: {}
definitions:
SharedDefinition:
properties:
id:
type: string
type:
type: string
Person:
allOf:
- $ref: '#/definitions/SharedDefinition'
- properties:
firstname:
type: string
lastname:
type: string
JobSubDefinition:
properties:
name:
type: string
Job:
allOf:
- $ref: '#/definitions/SharedDefinition'
- $ref: '#/definitions/JobSubDefinition'
In this example:
Person = SharedDefinition + inline definition
Job = SharedDefinition + JobSubDefinition
More about this in
Writing OpenAPI (Swagger) Specification Tutorial – Part 4 – Advanced Data Modeling (disclosure: I wrote this tutorial)
OpenAPI Specification#models-with-composition