python-restx / flask-restx

Fork of Flask-RESTPlus: Fully featured framework for fast, easy and documented API development with Flask
https://flask-restx.readthedocs.io/en/latest/
Other
2.17k stars 336 forks source link

How to specify schema for nested json in flask_restx? #404

Open rakeshk121 opened 2 years ago

rakeshk121 commented 2 years ago

Im using flask_restx for swagger API's. The versions are as follows:

python - 3.8.0
flask  - 2.0.2
flask_restx - 0.5.1

The following is the nested json I need to specify the schema for:

dr_status_fields = app_api.model('DR Status',{
    fields.String: {
        "elasticsearch": {
            "backup_status": fields.String,
            "backup_folder": fields.String,
        },
        "mongodb": {
            "backup_status": fields.String,
            "backup_folder": fields.String,
        },
        "postgresdb": {
            "backup_status": fields.String,
            "backup_folder": fields.String,
        },
        "overall_backup_status": fields.String
    }
})

But with this when I load the swagger URL in browser , i get error in the command line:

Unable to render schema
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/flask_restx/api.py", line 571, in __schema__
    self._schema = Swagger(self).as_dict()
  File "/usr/local/lib/python3.8/site-packages/flask_restx/swagger.py", line 268, in as_dict
    "definitions": self.serialize_definitions() or None,
  File "/usr/local/lib/python3.8/site-packages/flask_restx/swagger.py", line 623, in serialize_definitions
    return dict(
  File "/usr/local/lib/python3.8/site-packages/flask_restx/swagger.py", line 624, in <genexpr>
    (name, model.__schema__)
  File "/usr/local/lib/python3.8/site-packages/flask_restx/model.py", line 76, in __schema__
    schema = self._schema
  File "/usr/local/lib/python3.8/site-packages/flask_restx/model.py", line 159, in _schema
    properties[name] = field.__schema__
AttributeError: 'dict' object has no attribute '__schema__'

I have tried checking the documentaion of flask_restx for any sample example usage. But could not find any. Please help regarding the same

Smixi commented 2 years ago

You need to use fields.Nested and create another api model, ref : https://flask-restx.readthedocs.io/en/latest/marshalling.html#nested-field