flask restx enum model

3k views Asked by At

How to create an Enum api model in flask restx with one string field without other properties so that the following description is generated in swagger.yml?

definitions:
    Colors:
      type: string
      enum: [black, white, red, green, blue]

enter image description here Maybe some hacks will help? Because now it seems like you can create an api model with properties only

2

There are 2 answers

0
m-ghonim On

For new comers, flask restx now supports an enum property for String type

        theme_color = api.model( "theme_color", {
            "appTheme": fields.String(description="UI primary colour",enum=["theme-red", "theme-pink", "theme-orange", "theme-yellow"])
        })

Example documentation here

0
Nicolay On

Oh, so I find solution for my question)

Self-answered, lol

You can define api model via json schema description: https://flask-restx.readthedocs.io/en/latest/marshalling.html#define-model-using-json-schema

colors_api_model = api.schema_model('Colors', {
    'enum':
        ['black', 'white', 'red', 'green', 'blue'],
    'type': 'string'
})