Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ameade opened this issue Jan 30, 2016 · 7 comments
Closed

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

ameade opened this issue Jan 30, 2016 · 7 comments

Comments

@ameade
Copy link

ameade commented Jan 30, 2016

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

@mission-liao
Copy link
Member

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
Copy link

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
Copy link
Member

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
Copy link

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
Copy link
Member

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

@LouisZou
Copy link

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

@mission-liao
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants