flask-restful-swagger icon indicating copy to clipboard operation
flask-restful-swagger copied to clipboard

Nested class not generated right

Open trondhindenes opened this issue 9 years ago • 1 comments

I'm trying to express the following:

{                                               
    "module":  "ping",                          
    "extra_args":  {                            
                       "var2":  "value2",       
                       "var":  "value1"         
                   }                            
}                                               

I've done this in python like so:

@swagger.model
class AnsibleExtraArgsModel:
    resource_fields = {
        'arg_name' : fields.String,
        'arg_value' : fields.String,
        }

@swagger.model
class AnsibleCommandModel:
    resource_fields = {
        'module': fields.String,
        'extra_args': fields.List(fields.Nested(AnsibleExtraArgsModel.resource_fields)),
    }

However, the resulting swagger spec doesn't include the nested class:

{
    "apiVersion": "0.1", 
    "apis": [
        {
            "description": null, 
            "notes": null, 
            "operations": [], 
            "path": "/api/starttask"
        }, 
        {
            "description": null, 
            "notes": null, 
            "operations": [
                {
                    "method": "post", 
                    "nickname": "nickname", 
                    "notes": "Run ad-hoc Ansible command", 
                    "parameters": [
                        {
                            "allowMultiple": false, 
                            "dataType": "AnsibleCommandModel", 
                            "description": "Inut object", 
                            "name": "body", 
                            "paramType": "body", 
                            "required": true
                        }
                    ], 
                    "responseClass": "AnsibleRequestResultModel", 
                    "responseMessages": [
                        {
                            "code": 200, 
                            "message": "Ansible command started"
                        }, 
                        {
                            "code": 400, 
                            "message": "Invalid input"
                        }
                    ], 
                    "summary": null
                }
            ], 
            "path": "/ansiblecommand"
        }, 
        {
            "description": null, 
            "notes": null, 
            "operations": [], 
            "path": "/ansibletaskstatus/{task_id}"
        }
    ], 
    "basePath": "http://localhost", 
    "description": "Auto generated API docs by flask-restful-swagger", 
    "models": {
        "AnsibleCommandModel": {
            "description": null, 
            "id": "AnsibleCommandModel", 
            "notes": null, 
            "properties": {
                "extra_args": {
                    "items": {
                        "$ref": null
                    }, 
                    "type": "array"
                }, 
                "module": {
                    "type": "string"
                }
            }
        }, 
        "AnsibleExtraArgsModel": {
            "description": null, 
            "id": "AnsibleExtraArgsModel", 
            "notes": null, 
            "properties": {
                "arg_name": {
                    "type": "string"
                }, 
                "arg_value": {
                    "type": "string"
                }
            }
        }, 
        "AnsibleRequestResultModel": {
            "description": null, 
            "id": "AnsibleRequestResultModel", 
            "notes": null, 
            "properties": {
                "task_id": {
                    "type": "string"
                }
            }, 
            "required": [
                "task_id"
            ]
        }
    }, 
    "produces": [
        "application/json"
    ], 
    "resourcePath": "/", 
    "spec_endpoint_path": "/api/spec", 
    "swaggerVersion": "1.2"
}

How can I troubleshoot this?

trondhindenes avatar Jun 08 '16 22:06 trondhindenes

I know this is an old issue, but it seems it still hasn't been resolved. The way I had it - partially - working was by adding @swagger.nested decorator, which in the above case would be:

@swagger.model
@swagger.nested(extra_args=AnsibleExtraArgsModel.__name__)
class AnsibleCommandModel:
    resource_fields = {
        'module': fields.String,
        'extra_args': fields.List(fields.Nested(AnsibleExtraArgsModel.resource_fields)),
    }

and the swagger doc was generated correctly. The problem I got - and that why I mentioned it worked partially - was that the nested decorator broke @classmethod decorator and SQLAlchemy select queries were not working.

robxrocks avatar May 26 '20 11:05 robxrocks