pyopenapi / pyswagger

An OpenAPI (fka Swagger) client & converter in python, which is type-safe, dynamic, spec-compliant.
MIT License
385 stars 89 forks source link

pyswagger can handle the properties of object type is empty or not ? #73

Closed WillaLuo closed 8 years ago

WillaLuo commented 8 years ago

Hi mission, I wonder whether pyswagger can handle this situation or not?

"plan":{
   "type":"object",
   "additionalProperties":true,
   "properties":{

   }
},
"oplans":{
   "items":{
      "$ref":"#/definitions/plan"
   },
   "type":"array",
   "description":"List of oplans"
},

When I run client, the parameter of plan can't be validate successfully. Thanks, Shuang

mission-liao commented 8 years ago

Hi, I try this schema

{
   "swagger":"2.0",
   "host":"http://test.com",
   "basePath":"/v1",
   "paths":{
      "/t":{
         "get":{
            "operationId":"kk",
            "parameters":[
               {
                  "in":"body",
                  "name":"body",
                  "schema":{
                     "$ref":"#/definitions/oplans"
                  }
               }
            ],
            "responses":{
               "default":{
                  "description":"void"
               }
            }
         }
      }
   },
   "definitions":{
      "plan":{
         "type":"object",
         "additionalProperties":true,
         "properties":{

         }
      },
      "oplans":{
         "items":{
            "$ref":"#/definitions/plan"
         },
         "type":"array",
         "description":"List of oplans"
      }
   }
}

with this code snippet:

from pyswagger import SwaggerApp

app = SwaggerApp.create('swagger.json')

op = app.op["kk"]
req_and_resp = op(body=[
    dict(a=1, b=2, c=3),
    dict(name='john', email='xx@gmail.com'),
])

and it didn't raise any exception. If it's not what you expect, could you provide your code snippet / schema for me to investigate?

WillaLuo commented 8 years ago

Hi, mission, I try this schema , I modify the code snippet: from pyswagger import SwaggerApp

app = SwaggerApp.create('swagger.json')

op = app.op["kk"] req, resp = op(body=[ dict(a=1, b=2, c=3), dict(name='john', email='xx@gmail.com'), ])

print req._p["body"]["body"]

please see the body value of req, the data (a=1, b=2, c=3) and (name='john', email='xx@gmail.com') is not validated and pass to body of req. (virtualenv)-bash-4.1$ python test.py {},{}

mission-liao commented 8 years ago

ya, u r right, that seems a bug.

mission-liao commented 8 years ago

I think I found the bug, for some reason, the cleanup of Model primitive is not called. You could patch pyswagger.primitive.init.py (L224) from

if ret and cleanup and hasattr(ret, 'cleanup'):

to

if ret != None and hasattr(ret, 'cleanup'):

before I provide an official fix.

WillaLuo commented 8 years ago

Ok, That's great, Thanks! Shuang

2016-03-16 23:35 GMT+08:00 mission.liao notifications@github.com:

I think I found the bug, for some reason, the cleanup of Model primitive is not called. You could patch pyswagger.primitive.init.py (L224) from

if ret and cleanup and hasattr(ret, 'cleanup'):

to

if ret != None and hasattr(ret, 'cleanup'):

before I provide an official fix.

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub https://github.com/mission-liao/pyswagger/issues/73#issuecomment-197386469

WillaLuo commented 8 years ago

Hi mission, I want to confirm with you about this: After you fix this bug and the default additionproperties value, whether this schema object can be validate successfully? "oplans": { "items": { "type":"object", "additionalproperties": true }, "type": "array", "description": "List of oplans" },

The "oplans" is an array of object, and the content of object can be anything I want. Thanks. Shuang

2016-03-16 23:55 GMT+08:00 Willa luo willa.l.luo@gmail.com:

Ok, That's great, Thanks! Shuang

2016-03-16 23:35 GMT+08:00 mission.liao notifications@github.com:

I think I found the bug, for some reason, the cleanup of Model primitive is not called. You could patch pyswagger.primitive.init.py (L224) from

if ret and cleanup and hasattr(ret, 'cleanup'):

to

if ret != None and hasattr(ret, 'cleanup'):

before I provide an official fix.

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub https://github.com/mission-liao/pyswagger/issues/73#issuecomment-197386469

mission-liao commented 8 years ago

I think your schema object should be ok, I'll include it as a testing case to make sure it works in your workaround. But remember that, "additionalProperties: true" is not spec-compliant and might be fixed in the future.