0

I'm looking for a way to make Swagger documentation field offer current date to the user each time it gets loaded.

Supplying value doesn't work as it stays static, obviously. Giving it a function also doesn't work since the view is rendered and kept static also.

For an api model:

request_model = api.model("v1/item", {
   "ItemId": fields.List(fields.String, description = 'ItemIds', required = True),
   "Date": fields.String(description = 'Date', default = (datetime.now().strftime('%Y-%m-%d')))
})

second field (Date), needs to hold current date as a default value.

Instead of showing current time each time loaded, it shows basically view render (save) time. I was considering adding custom javascript code to create this value or call an endpoint to load default, but didn't find the way to add it to the swagger API page. What is the best way to do this? Thanks.

protagora
  • 31
  • 1
  • 7
  • Change your annotations so that they generate the field definition as `type: string` + `format: date`. Swagger UI automatically shows the current date as a sample value for `format: date` fields (as can be seen here: http://petstore.swagger.io/#/store/placeOrder). – Helen Jun 07 '19 at 18:19

1 Answers1

0

use the param example inside of the type field method like this

fields.String(description = 'Date', example = (datetime.now().strftime('%Y-%m-%d')))})
Axel Anaya
  • 122
  • 9