connexion
connexion copied to clipboard
nullable elements handled incorrectly in string arrays
Description
If nullable is used to specifiy that a single string element can be zero, it works. However, if the type is 'array' and nullable is used in the items, None/null is implicitly converted to the string 'None'
. With numeric values, everything works fine
Expected behaviour
If single string elements can be nullable, so should be array elements
Actual behaviour
They're not
Steps to reproduce
Using this api.yaml:
openapi: 3.0.0
info:
description: debug
version: '1.0'
title: Debug
servers:
- url: 'http://127.0.0.1:9191/v1'
paths:
/predict:
post:
operationId: debug.loaded_predict
responses:
'200':
description: OK
requestBody:
content:
application/json:
schema:
x-body-name: some_element
$ref: '#/components/schemas/some_element'
components:
schemas:
some_element:
type: object
properties:
some_property:
type: array
items:
type: string
nullable: true
Now, when
import requests
r = requests.post("http://127.0.0.1:9191/v1/predict", json={'some_property': ['some string', None]})
is used, the value of some_element will be {'some_property': ['some string', 'None']}
, which is wrong.
Additional info:
Has also been tested with curl to make sure this isn't a requests
problem
Output of the commands:
-
python --version
: Python 3.6.7 :: Anaconda custom (64-bit) -
pip show connexion | grep "^Version\:"
: Version 2.2.0
I continued with a workaround but found another, probably correlated, issue: If you have an array of type integer
, it seems to be impossible to make them nullable altogether. In the above example, just change the type
to integer
and try finding any request with nulled elements that goes trough. If numpy.nan
is passed as an element in the array (which will be converted to NaN
in the request json), the response will be 400 - nan is not of type 'integer'
. If None
is passed via requests.post (which will be converted to null
), the response will be 500 - The server encountered an internal error
with the console showing TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
. Do you want me to open another issue for this or do you think that the two problems have the same underlying cause?
Same problem here. Have you found a workaround for this?
For future reference, You can define an array of (nullable) integers like this:
value:
type: array
items:
oneOf:
- type: string
nullable: true
- type: number
Its not 100% perfect, but the api won't block if it encounters a None in the array.
This is fixed in version 3.