I define a path that takes MyObject as a parameter. MyObject has properties for cats and dogs. These have default values. In swagger-editor, the example doesn't show the default values, but try-it-out does create a MyObject with correct defaults.
In swagger-ui, I can see the defaults under Models, but not in the API. Is there a way to set these defaults ? swagger: '2.0' info: title: pass object with default properties as a parameter description: etc version: "Draft 0.1.1" host: example.com basePath: / produces: - application/json
paths:
/myobject:
post:
summary: |
post an object.
parameters:
- name: myObject
in: body
required: true
schema:
type: array
items:
$ref: '#/definitions/MyObject'
responses:
200:
description: OK
definitions:
MyObject: # move to/models/model.yml
type: object
description: Contains default properties
required:
- cats
- dogs
properties:
cats:
type: number
default: 9
dogs:
type: string
default: "fido"
It seems like there are 2 kinds of defaults:
For a client-side default, we can define it by setting required=True and enum to the only allowed value. See this example below:
And you can see it work in the swagger-editor here: https://editor.swagger.io/The default parameter is a bit confusing because swagger 2.0 initially described the default parameter without specifying a server or client reference frame.
Swagger 2.0 spec Defines schema default as
OpenAPI v3.0 spec
Your usage of
default
is wrong. You probably wantexample
instead.default
is only used with optional fields and is handled on the server side. That is, if the client does not supply a value in the payload, the server will use thedefault
value.Consider this
User
model:The
role
property is optional and defaults touser
. So, if the client sends the payload withoutrole
:the server will assume
role
=user
.In your case, it looks like you want to provide example values for the fields. This is what the
example
keyword is for: