简体   繁体   中英

Allow null values in JSON response from python-flask swagger app?

I am using swagger/openAPI 3.0.0 to generate a python3-flask API. This API gives JSON responses. If the value for a property of my JSON response is None I need it to come out as null in the response. Instead, the property is getting completely dropped from the response. I understand that this is a dumb requirement, but I do not have a choice in the matter.

In my swagger definition I have:

my_property:
  type: number
  nullable: true 

I have also tried

my_property:
  type: number
  nullable: true 
  default: null

but the results were the same.

How do I make it so that my_property is not dropped from the response when the value is None ?

Figured it out. The beginning of the generated __main__.py looks like this:

from swagger_server import encoder


def main():
    app = connexion.App(__name__, specification_dir='./swagger/')
    app.app.json_encoder = encoder.JSONEncoder

I needed to add

app.app.json_encoder.include_nulls = True

Afterwards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM