pipermerriam/flex

Response status code validation doesn't work

Closed this issue · 9 comments

validate_api_call doesn't normalize the requests correctly - swagger responses objects keys are strings, not integers whereas flex expects them to be integers and thus isn't able to correctly validate.

I was able to get around this issue by setting response.status_code as a string value.

This should be pretty simple to fix by adding the appropriate type casting somewhere around here

If you'd like to take a stab at a pull request for this I'd be happy to review and get it merged.

Yep. Agreed. Just wanted to make sure it's a real bug before sending in a PR.

This should be available on pypi as 5.4.0

Thanks!

The yaml parsing should probably be adjusted too, since it seems idiomatic to use 200: and not "200":. Swagger-ui uses this for their example at http://editor.swagger.io/#.

But, after the change, specs like that will cause an error, eg:

Request status code was not found in the known response codes.  Got `200`: Expected one of: `[200]`

@simon-weber not sure what you mean by that - are you talking about flex.core.load for loading a YAML file? If so, that should load the dictionary with string (rather than int) as per YAML spec. And json.loads will fail to load an invalid JSON (such as {1: 2}).

that should load the dictionary with string (rather than int) as per YAML spec

Huh. pyyaml/flex seem to parse it as an int for me, eg:

# go to http://editor.swagger.io/#, hit "file > download yaml", then:
$ python -c 'import flex; s = flex.load("swagger.yaml"); code = s["paths"]["/products"]["get"]["responses"].keys()[0]; print code, type(code)'
200 <type 'int'>

However, http://yaml-online-parser.appspot.com/ will parse the same yaml to a string.

@simon-weber ah you're totally right. Odd that yaml parses that as int. In any case, this looks like a bug in swagger-editor since responses.keys are supposed to be strings as per Swagger v2.0 spec.

Sounds good; I'll raise it with them.