Connexion: How to define nullable response attributes?

Created on 17 May 2016  路  12Comments  路  Source: zalando/connexion

If I have the properties of a response defined as:

name:
  type: string
count:
  type: integer
  format: int32
description:
  type: string

and I want description to be nullable, that is, the values are a string or the non-string value null, how can I define that in my swagger spec?

Most helpful comment

@cbornet found a way to do it, patching the definitions given to the ResponseValidator on instantiation.

https://stackoverflow.com/a/49539868/2481696

All 12 comments

Seems that swagger v2 does not allow nulls. :/

https://github.com/OAI/OpenAPI-Specification/issues/229

We could add x-nullable or x-isnullable support in connexion. Thoughts on this?

@dfeinzeig We actually already support that: http://connexion.readthedocs.io/en/latest/request.html#nullable-parameters

@rafaelcaricio Does that only apply to request parameters? What about response properties?

@dfeinzeig it does apply for request parameters. I haven't tried it with response properties. But I think it should work too.

@rafaelcaricio , doesn't work for response properties. I'll submit a PR for responses, modeling it after your PR (https://github.com/zalando/connexion/pull/197/files) for requests.

actually, this doesn't seem straight forward. ResponseBodyValidator calls the jsonschema validate method passing the definitions and response data. I was thinking of following the pattern used by ParameterValidator, which seems to iterate over each parameter and call validate_parameter. But it feels like that will get quite complex based upon the objects returned as responses. so i'm not sure of a good way to handle this. thoughts? I think for the moment in my app I'm going to return empty strings instead of nulls (definitely not ideal, but seems like a reasonable bandaid).

@dfeinzeig Seems like you can just do it http://stackoverflow.com/a/22565006 in your own schema definitions? I tried that and it worked fine.

Oh yeah, that totally does work. Thanks @rafaelcaricio ! 馃憤

Closing this issue then.

Using type: [number, null] works but it's not compliant with swagger spec and it doesn't work with swagger-codegen for instance.
So it would be better to support the x-nullable flag for model attributes.
ResponseBodyValidator could transform x-nullable into [xxx, null] before passing it to jsonschema.

@cbornet my thoughts exactly, I've been trying to figured about the best place to patch the definitions before given to connexion.

@cbornet found a way to do it, patching the definitions given to the ResponseValidator on instantiation.

https://stackoverflow.com/a/49539868/2481696

Was this page helpful?
0 / 5 - 0 ratings