pythonflaskswaggerschemaflask-restx

How to specify schema for nested json in flask_restx?


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

Update

As per r-m-n answer i have tried to apply the same. But here the problem is there is a key i.e in Line No.2 of the above defined scheme -> fields.String: { . I think this is causing the issue. I have tried to use it as follows:

dr_status_fields = app_api.model('Backup and restore related fields', {
            "backup_status": fields.String,
            "backup_folder": fields.String,
})

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

dr_timestamp_field = app_api.model('Timestamp', {
    fields.String: fields.Nested(dr_db_fields)
})

Here the dr_timestamp_field has resulted in the same issue.


Solution

  • use fields.Nested:

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