Mike Furlender
Mike Furlender

Reputation: 4019

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?

Upvotes: 3

Views: 973

Answers (1)

Mike Furlender
Mike Furlender

Reputation: 4019

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.

Upvotes: 5

Related Questions