Why are non-nullable Django fields marked as nullable in JSON schema?
Hello.
I have a question about how types are calculated in the json schema. Let me explain with an example:
Suppose I have a field id, automatically generated by Django. If I use something like this:
class Obj(models.Model):
...
class ObjSchema(ModelSchema):
class Meta:
model = Obj
fields = (
"id",
)The following metadata will be generated:
typing.Optional[int] annotation=NoneType required=False default=None title='ID'And meta data for the json schema:
'id': {'anyOf': [{'type': 'integer'}, {'type': 'null'}], 'title': 'ID'}The problem is that the primary key is not null by default. And in general, the logic is incorrect for text fields where blank=True. The documentation will also indicate that null is possible, even if we specify blank=True, null=False, default="".
Here is the line of code, where this should be corrected. The question is, what was the idea behind this, or did you just accidentally miss it?
p.s. This is a bit annoying, as it misleads readers of the documentation.
Source: vitalik/django-ninja