Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request/ Bug report: Fields are not marked as nullable in json schema #44

Open
martinostvik opened this issue Dec 1, 2024 · 0 comments

Comments

@martinostvik
Copy link

If you choose to set json_schema_extra={"nullable": False}) etc on the original model, the resulting model will not be marked as nullable in the json schema for the model.

Example:

from pydantic import BaseModel, Field
from pydantic_partial import create_partial_model

class A(BaseModel):
    name: str = Field(default="Bob", json_schema_extra={"required": True})

class B(BaseModel):
    name: str = Field(default="Bob", json_schema_extra={"nullable": False})

class C(BaseModel):
    name: str = Field(default="Bob")
    
PartialA = create_partial_model(A)
PartialB = create_partial_model(B)
PartialC = create_partial_model(C)

print(PartialA.model_json_schema())
print(PartialB.model_json_schema())
print(PartialC.model_json_schema())

Partial A:

{
   "properties":{
      "name":{
         "default":"Bob",
         "required":true,  <---- required and not nullable
         "title":"Name",
         "type":"string"
      }
   },
   "title":"A",
   "type":"object"
}

PartialB:

{
   "properties":{
      "name":{
         "default":"Bob",
         "nullable":false, <--- Not nullable
         "title":"Name",
         "type":"string"
      }
   },
   "title":"B",
   "type":"object"
}

PartialC: (Has a default value, and no "required" or "nullable" values. Means it is nullable)

{
   "properties":{
      "name":{
         "default":"Bob",
         "title":"Name",
         "type":"string"
      }
   },
   "title":"C",
   "type":"object"
}

I created a PR here to address this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant