question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] Is it possible to generate openapi.json with "nullable" : "true" for non-required property?

See original GitHub issue

Description

Is it possible to generate openapi.json with "nullable" : "true" for non-required property?

Additional context

I’m using NSwag to generate C# code from FastAPI openapi.json. I need to set “nullable” as “true” to make it work correctly.

I have a FastAPI Model like this:

class Topic(BaseModel):
    first_id: str = None

I want to get a schema like this:

"Topic": {
  "title": "Topic",
  "type": "object",
    "properties": {
      "first_id": {
        "title": "First_Id",
        "type": "string",
        "nullable" : "true"
      }
  }
}

But I haven’t found a way to set the field “nullable”. And if it’s not provided, it is “false” by default.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
nkhitrovcommented, Jan 21, 2020

You can use Field to set nullable=True by default.

Example:


from fastapi import FastAPI, Depends, Query, HTTPException
from typing import Optional
from pydantic import BaseModel, Field

app = FastAPI()


class Model(BaseModel):
    a: Optional[int]
    b: Optional[int] = None
    c: Optional[int] = Field(None, nullable=True)


@app.get("/test", response_model=Model)
def foo(m: Model):
    return m

Component in /openapi.json

...
Model: {
    title: "Model",
    required: [
		"c"
	],
    type: "object",
    properties: {
    a: {
    	title: "A",
    	type: "integer"
	},
    b: {
    	title: "B",
    	type: "integer"
	},
    c: {
    	title: "C",
    	type: "integer",
    	nullable: true
	}
}
...
2reactions
dennismeisselcommented, Jan 23, 2020

@Slyfoxy Thanks for answer, it helped!

I found out, that Field is not working with FastAPI

But Schema did the job!

from pydantic import BaseModel, Schema

class Topic(BaseModel):
    first_id: str = Schema(None, nullable=True)

And /openapi.json

"Topic": {
    "title": "Topic",
    "type": "object",
    "properties": {
        "first_id": {
        "title": "First_Id",
        "type": "string",
        "nullable": true
        }
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Swashbuckle: Make non-nullable properties required
I found a solution for this: I was able to implement a Swashbuckle ISchemaFilter that does the trick. Implementation is:
Read more >
How to specify a property can be null or a reference
This is a real-world problem for my project: complex objects reference other objects, but in some cases, the referenced object is NULL, i.e, ......
Read more >
Model generation · GitBook - Goswagger.Io
Non-required or nullable property? Use-Case: when a definition has a property N, if N is a number and is not required, the corresponding...
Read more >
How to manage nullable properties - Jane - Read the Docs
You can use it as follows: type: string x-nullable: true. If you are using OpenAPI v2, consider migrating to OpenAPI v3 to get...
Read more >
Solving OpenAPI and JSON Schema Divergence - Medium
Creating json -schema-to-openapi was mostly just a case of flipping the tests ... in OpenAPI; Switches type: ['foo', 'null'] to type: foo and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found