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

type Integer does not work without 'format' specified #65

Closed ameade closed 8 years ago

ameade commented 8 years ago

The format field in a parameter object should be optional. Pyswagger fails if i define type: integer without a format specified.

mission-liao commented 8 years ago

Hi, the main reason is because Swagger (Open API) prefers explicit over implicit. There is no such primitive ('integer' without format) defined in spec.

You can still support this kind of definition by adding primitives to pyswagger, refer to here for more details.

Here is a pseudo code that might work for the primitive you described:

from pyswagger import SwaggerApp
from pyswagger.primitives import SwaggerPrimitive
from pyswagger.primitives._int import validate_int, create_int

# create a cusomized primitive factory
factory = SwaggerPrimitive()
# type == 'integer', format == ''
# type == 'integer', format == None
factory.register('integer', '', create_int, validate_int)
factory.register('integer', None, create_int, validate_int)

# init SwaggerApp with customized primitive factory
app = SwaggerApp.load(your_url, prim=factory)
LouisZou commented 8 years ago

hi mission-liao, I also met this issue.

I have a parameter whose type is integer in my definition file. I use the above code, I can load the definition file but when I try to get the op it failed like the following: self._op = self._app.op[gl._authPath.strip("/")] TypeError: 'NoneType' object has no attribute 'getitem'

Is there any else code needed to let I can use integer parameter in my request like the following? client.request(op(body={"count":1, "name"="Tom"}) Now it failed with: "Exception:Can't resolve type from:(integer, None)".

mission-liao commented 8 years ago

I tried and it works with this sample code, just add 'prepare' after load

from pyswagger import SwaggerApp                                                                              
from pyswagger.primitives import SwaggerPrimitive                              
from pyswagger.primitives._int import validate_int, create_int                 

# create a cusomized primitive factory                                                                        
factory = SwaggerPrimitive()                                                                                  
# type == 'integer', format == ''                                                                             
# type == 'integer', format == None                                                                           
factory.register('integer', '', create_int, validate_int)                      
factory.register('integer', None, create_int, validate_int)                    

# init SwaggerApp with customized primitive factory                            
app = SwaggerApp.load('./swagger.json', prim=factory)

# --- below are added code  ---
app.prepare()                                                                                                 
op = app.s('/t').get                                                                                          
req, resp = op(p1=1)

Sample swagger.json

{
   "swagger":"2.0",
   "host":"http://test.com",
   "basePath":"/v1",
   "paths":{
      "/t":{
         "get":{
             "parameters":[
             {
                 "name":"p1",
                 "in":"query",
                 "type":"integer"
             }
             ],
             "responses":{
                 "default":{
                     "description":"void"
                 }
             }
         }
      }
   }
}

If it doesn't help, could you provide more reproducible step/info?

LouisZou commented 8 years ago

Thanks mission, now it work done after I added this "app.prepare()". BTW, how to make pyswagger also support boolean or other native type except of string.

mission-liao commented 8 years ago

Hi, pyswagger should already support boolean, and also other primitives listed in here, did you try any primitive that doesn't work?

LouisZou commented 8 years ago

Thanks mission. I will check my test data and my definition files.

mission-liao commented 8 years ago

No more update for a while, reopen it when things still goes wrong.