Does anyone know how to define possible enum values in an OpenAPI 2.0 definition so that they will be displayed in the Model tab of Swagger UI? Example here has an enum option for the status property. How to do define such an enum in OpenAPI 2.0?
"enum" works like this in OpenAPI 2.0:
{
"in": "query",
"name": "sample",
"description": "a sample parameter with an enum value",
"type": "string",
"enum": [ "1", "2"],
"required": true
}
and in OpenAPI 3.0:
{
"in": "query",
"name": "sample",
"description": "a sample parameter with an enum value",
"schema": {
"type": "string",
"enum": [ "1", "2"]
},
"required": true
}
As you can see, there's a query parameter called sample of type string, and has an enum stating two possible values. In this case, the sample states the parameter is required, so the UI will not show an empty value as an option.
For a minimal working sample, try this:
{
"swagger": "2.0",
"info": {
"title": "title",
"description": "descriptor",
"version": "0.1"
},
"paths": {
"/sample": {
"post": {
"description": "sample",
"parameters": [
{
"in": "query",
"name": "sample",
"description": "a sample parameter with an enum value",
"type": "string",
"enum": [
"1",
"2"
],
"required": true
}
],
"responses": {
"200": {
"description": "Successful request."
}
}
}
}
}
}
To test it locally, you can declare a variable (for example spec) in your javascript, and pass it into the SwaggerUi object.
var spec = { ... };
window.swaggerUi = new SwaggerUi({
url: url,
spec: spec,
dom_id: "swagger-ui-container",
supportedSubmitMethods: ['get', 'post', 'put', 'delete'],
onComplete: function(swaggerApi, swaggerUi){
...
The url parameter will be ignored in this case.
Eventually, the output looks like this:
Updating this with YAML syntax.
OpenAPI 2.0:
parameters:
- in: query
name: sample
description: a sample parameter with an enum value
type: string
enum:
- 1
- 2
required: true
OpenAPI 3.0:
parameters:
- in: query
name: sample
description: a sample parameter with an enum value
schema:
type: string
enum:
- 1
- 2
required: true
This should work:
{
"name": "bookingType",
"in": "path",
"type": "array",
"items": {
"enum": [
"packages", "accommodations"
]
},
"description": "lorem ipsum"
}
Reference https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#itemsObject
This isn't the exact answer, but it might work for you until they add this functionality.
Simply declare the property like so
"MyObject":{
"properties":{
"MyEnum":{
"type":"Value1 or Value2 or Value3"
}
}
}
Your ModelSchema will show {}, but The Model will show MyEnum(Value1 or Value2 or Value3, optional)
Related
I'm not very strong in swagger 2.0, could you please help me? I'm trying to describe body parameters, but got an error. Here is my swagger.json file:
{
"swagger": "2.0",
"info": {
"title": "Simple API overview",
"version": "v2"
},
"host": "localhost:4000",
"basePath": "/",
"paths": {
"/user/register": {
"post": {
"operationId": "register",
"summary": "User registration",
"parameters": [{
"in": "body",
"name": "role",
"required": true,
"schema": {
"type": "integer",
"example": 1
}
}]
}
}
}
}
When I try to run it, I got an error:
Error: Expected `string` for value, got `1`
If I remove example field, I got this in Example Value section:
{}
Looks like the type definition is incorrect, but I couldn't figure out what's the difference between my code and examples from swagger docs.
Any help is appreciated.
Thanks.
I found the reason: there should be consumes field defined as text/plain. By default it is application/json.
The full code:
{
"swagger": "2.0",
"info": {
"title": "Simple API overview",
"version": "v2"
},
"host": "localhost:4000",
"basePath": "/",
"paths": {
"/user/register": {
"post": {
"operationId": "register",
"summary": "User registration",
"consumes": ["text/plain"],
"parameters": [{
"in": "body",
"name": "role",
"required": true,
"schema": {
"type": "integer",
"example": 1
}
}]
}
}
}
}
One way to get around this error is to change type from integer to string.
"parameters": [{
"in": "body",
"name": "role",
"required": true,
"schema": {
"type": "string",
"example": "1"
}
}]
I am new in swagger and want to deploy an API which is having query string. This is the API I am getting encoded URL after passing the parameter in the GET method.
API URL with Parameter should be:-
baseurl/v1/auth/getOTP?email=somename#email.com
but I am getting something like:-
baseurl/v1/auth/getOTP?email=somename%40email.com
what I have tried is:-
"/auth/getOTP": {
"get": {
"tags": [
"pet"
],
"summary": "",
"description": "",
"operationId": "findPetsByStatus",
"produces": [
"application/json",
"application/xml"
],
"parameters": [
{
"name": "email",
"in": "path",
"description": "",
"required": true,
"type": "string",
}
],
"responses": {
"200": {
"description": "successful operation",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/Pet"
}
}
},
"400": {
"description": "Invalid value"
}
},
"security": [
{
"petstore_auth": [
"write:pets",
"read:pets"
]
}
]
}
},
Swagger OpenAPI has Specified: in this GitHub Issue-1840, It is specifically disallowed on in: path parameters for the same reason they don't allow optional path parameters, e.g. {/foo} By having variable path segments it become difficult to resolve a URL to an operation.
If you want to use some kind of hack then follow this GitHub Issue-230.
If you really needed then Its support in in: query parameters, as below,
The allowReserved keyword specifies whether the reserved characters :/?#[]#!$&'()*+,;= in parameter values are allowed to be sent as they are, or should be percent-encoded. By default, allowReserved is false, and reserved characters are percent-encoded.
Here you need to set it to true,
"parameters": [
{
"name": "email",
"in": "query",
"description": "",
"required": true,
"type": "string",
"allowReserved": true
}
],
Look at the Example Swagger Describing Parameters
and For more details follow Swagger Serialization.
Is it possible (and how) to specify additional parameters that depend on the value of another given parameter?
Example
I have a call PUT /accounts/<account_id>/payment_method which takes some parameters besides the path parameter.
One is payment_method_type which defines the payment method to be set.
Now: if payment_method_type is DD for direct debit, there are some more parameters allowed (and needed) like account_holder and iban.
If it is something else, e. g. PP, other parameters are needed.
Excerpt from the json
"parameters": {
"payment_method_type": {
"name": "type",
"description": "Payment method type.",
"in": "query",
"required": true,
"type": "string",
"enum": [
"DD", "IV", "PP"
]
},
"payment_method_data_dd_account_holder": {
"name": "account_holder",
"description": "Name of account holder",
"in": "query",
"required": false, # but true if payment_method_type == DD
"type": "string"
},
"payment_method_data_dd_iban": {
"name": "iban",
"description": "IBAN",
"in": "query",
"required": false, # but true if payment_method_type == DD
"type": "string"
},
"payment_method_data_pp_some_info": {
"name": "some_info",
"description": "Some info needed for PP",
"in": "query",
"required": false, # but true if payment_method_type == PP
"type": "string"
},
}
"paths": {
"/accounts/{account_id}/payment_method": {
"put": {
"summary": "Update Payment Method",
"description": "...",
"parameters": {
{
"$ref": "#/parameters/path_psp_account_id"
},
{
"$ref": "#/parameters/payment_method_type"
},
{
"$ref": "#/parameters/payment_method_data_dd_account_holder"
},
{
"$ref": "#/parameters/payment_method_data_dd_iban"
},
{
"$ref": "#/parameters/payment_method_data_pp_some_info"
},
}
}
}
}
I'd like to get rid of the long parameter list (since there are more parameters left out here) but group them as described above and document that some parameters are required (and allowed) only for special types.
Is there a way to describe this?
Is there a way to define sets of parameters like all the direct debit parameters in one definition and reference it? Remark: those parameters are given next to the payment_method_type parameter and not inside a sub-object.
There is no way to have conditional parameters in the current swagger specification.
Yes, the parameters array allows a $ref pointer.
This is the description of a certain parameter I have:
{
"name": "myParam",
"description": "My param description",
"required": true,
"paramType": "query",
"type": "string",
"defaultValue":"myValue"
}
The defaultValue is the only value the parameter can have, so is there a way to declare this? Seen in the context of the swagger-ui, I need the parameter's textbox to be read-only. I'm using swagger 1.2.
Thanks
The proper way to declare this would be:
{
"name": "myParam",
"description": "My param description",
"required": true,
"paramType": "query",
"type": "string",
"enum": [ "myValue" ]
}
The "enum" property sets the possible values. Once you set a single value to it, that's the only one that could be used and that would be available in the UI for the user to choose from.
I'm a bit late to the game but with OpenAPI 3.1 you can do the following:
defaultValue:
type: string
const:
- myValue
You can also use 'pattern' of the string to avoid the enum - this could look better in Swagger:
{
"openapi": "3.0.0",
"components": {
"schemas": {
"object": {
"type": "object",
"required": [
"myParam"
],
"properties": {
"myParam": {
"type": "string",
"pattern": "onlyAllowedValue$"
}
}
}
}
}
}
Inside the Swagger API Documentation there is inside the json beside the apis array a model object entry but no documentation about it. How can I use this "models" part?
{
apiVersion: "0.2",
swaggerVersion: "1.1",
basePath: "http://petstore.swagger.wordnik.com/api",
resourcePath: "/pet.{format}"
...
apis: [...]
models: {...}
}
Models are nothing but like your POJO classes in java which have variables and properties. In models section you can define your own custom class and you can refer it as data type.
If you see below
{
"path": "/pet.{format}",
"description": "Operations about pets",
"operations": [
{
"httpMethod": "POST",
"summary": "Add a new pet to the store",
"responseClass": "void",
"nickname": "addPet",
"parameters": [
{
"description": "Pet object that needs to be added to the store",
"paramType": "body",
"required": true,
"allowMultiple": false,
"dataType": "Pet"
}
],
"errorResponses": [
{
"code": 405,
"reason": "Invalid input"
}
]
}
Here in parameter section it have one parameter who's dataType is Pet and pet is defined in models as below
{
"models": {
"Pet": {
"id": "Pet",
"properties": {
"id": {
"type": "long"
},
"status": {
"allowableValues": {
"valueType": "LIST",
"values": [
"available",
"pending",
"sold"
]
},
"description": "pet status in the store",
"type": "string"
},
"name": {
"type": "string"
},
"photoUrls": {
"items": {
"type": "string"
},
"type": "Array"
}
}
}
}}
You can have nested models , for more information see Swagger PetStore example
So models are nothing but like classes.