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

Fix primary key always being marked as nullable in schema #1249

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ninja/orm/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def get_schema_field(
internal_type = field.get_internal_type()
python_type = TYPES[internal_type]

if field.primary_key or blank or null or optional:
if blank or null or optional:
default = None
nullable = True

Expand Down
12 changes: 6 additions & 6 deletions tests/test_orm_metaclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ class Meta:
"title": "SampleSchema2",
"type": "object",
"properties": {
"id": {"anyOf": [{"type": "integer"}, {"type": "null"}], "title": "ID"},
"id": {"title": "ID", "type": "integer"},
"firstname": {"title": "Firstname", "type": "string"},
},
"required": ["firstname"],
"required": ["id", "firstname"],
}


Expand Down Expand Up @@ -119,9 +119,9 @@ class Meta:
fields = "__all__"
fields_optional = "__all__"

assert OptSchema.json_schema().get("required") == ["extra"]
assert OptSchema.json_schema().get("required") == ["id", "extra"]
assert OptSchema.json_schema()["properties"] == {
"id": {"anyOf": [{"type": "integer"}, {"type": "null"}], "title": "ID"},
"id": {"title": "ID", "type": "integer"},
"title": {"anyOf": [{"type": "string"}, {"type": "null"}], "title": "Title"},
"other": {"anyOf": [{"type": "string"}, {"type": "null"}], "title": "Other"},
"extra": {"title": "Extra", "type": "integer"},
Expand Down Expand Up @@ -164,14 +164,14 @@ class Meta:
"title": "SomeSchema",
"type": "object",
"properties": {
"id": {"anyOf": [{"type": "integer"}, {"type": "null"}], "title": "ID"},
"id": {"title": "ID", "type": "integer"},
"field1": {"title": "Field1", "type": "string"},
"field2": {
"anyOf": [{"type": "string"}, {"type": "null"}],
"title": "Field2",
},
},
"required": ["field1"],
"required": ["id", "field1"],
}


Expand Down
76 changes: 46 additions & 30 deletions tests/test_orm_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ class Meta:
"title": "ChildModel",
"type": "object",
"properties": {
"id": {"anyOf": [{"type": "integer"}, {"type": "null"}], "title": "ID"},
"id": {"title": "ID", "type": "integer"},
"parent_field": {"type": "string", "title": "Parent Field"},
"parentmodel_ptr_id": {"type": "integer", "title": "Parentmodel Ptr"},
"child_field": {"type": "string", "title": "Child Field"},
},
"required": ["parent_field", "parentmodel_ptr_id", "child_field"],
"required": ["id", "parent_field", "parentmodel_ptr_id", "child_field"],
}


Expand Down Expand Up @@ -86,7 +86,7 @@ class Meta:
"title": "AllFields",
"type": "object",
"properties": {
"id": {"anyOf": [{"type": "integer"}, {"type": "null"}], "title": "ID"},
"id": {"title": "ID", "type": "integer"},
"bigintegerfield": {"title": "Bigintegerfield", "type": "integer"},
"binaryfield": {
"title": "Binaryfield",
Expand Down Expand Up @@ -156,6 +156,7 @@ class Meta:
"hstorefield": {"type": "object", "title": "Hstorefield"},
},
"required": [
"id",
"bigintegerfield",
"binaryfield",
"booleanfield",
Expand Down Expand Up @@ -208,10 +209,7 @@ class Meta:
SchemaCls = create_schema(ModelAltAuto)
# print(SchemaCls.json_schema())
assert SchemaCls.json_schema()["properties"] == {
"altautofield": {
"anyOf": [{"type": "integer"}, {"type": "null"}],
"title": "Altautofield",
}
"altautofield": {"title": "Altautofield", "type": "integer"}
}


Expand All @@ -229,14 +227,14 @@ class Meta:
"title": "ModelNewFields",
"type": "object",
"properties": {
"id": {"title": "ID", "anyOf": [{"type": "integer"}, {"type": "null"}]},
"id": {"title": "ID", "type": "integer"},
"jsonfield": {"title": "Jsonfield", "type": "object"},
"positivebigintegerfield": {
"title": "Positivebigintegerfield",
"type": "integer",
},
},
"required": ["jsonfield", "positivebigintegerfield"],
"required": ["id", "jsonfield", "positivebigintegerfield"],
}

obj = Schema(id=1, jsonfield={"any": "data"}, positivebigintegerfield=1)
Expand Down Expand Up @@ -268,7 +266,7 @@ class Meta:
"title": "TestSchema",
"type": "object",
"properties": {
"id": {"anyOf": [{"type": "integer"}, {"type": "null"}], "title": "ID"},
"id": {"title": "ID", "type": "integer"},
"onetoonefield_id": {"title": "Onetoonefield", "type": "integer"},
"foreignkey_id": {
"anyOf": [{"type": "integer"}, {"type": "null"}],
Expand All @@ -280,15 +278,15 @@ class Meta:
"items": {"type": "integer"},
},
},
"required": ["onetoonefield_id", "manytomanyfield"],
"required": ["id", "onetoonefield_id", "manytomanyfield"],
}

SchemaClsDeep = create_schema(TestModel, name="TestSchemaDeep", depth=1)
# print(SchemaClsDeep.json_schema())
assert SchemaClsDeep.json_schema() == {
"type": "object",
"properties": {
"id": {"anyOf": [{"type": "integer"}, {"type": "null"}], "title": "ID"},
"id": {"title": "ID", "type": "integer"},
"onetoonefield": {
"title": "Onetoonefield",
"description": "",
Expand All @@ -306,20 +304,17 @@ class Meta:
"description": "",
},
},
"required": ["onetoonefield", "manytomanyfield"],
"required": ["id", "onetoonefield", "manytomanyfield"],
"title": "TestSchemaDeep",
"$defs": {
"Related": {
"title": "Related",
"type": "object",
"properties": {
"id": {
"anyOf": [{"type": "integer"}, {"type": "null"}],
"title": "ID",
},
"id": {"title": "ID", "type": "integer"},
"charfield": {"type": "string", "title": "Charfield"},
},
"required": ["charfield"],
"required": ["id", "charfield"],
}
},
}
Expand All @@ -339,14 +334,15 @@ class Meta:
"title": "MyModel",
"type": "object",
"properties": {
"id": {"anyOf": [{"type": "integer"}, {"type": "null"}], "title": "ID"},
"id": {"title": "ID", "type": "integer"},
"default_static": {
"default": "hello",
"title": "Default Static",
"type": "string",
},
"default_dynamic": {"title": "Default Dynamic", "type": "string"},
},
"required": ["id"],
}


Expand Down Expand Up @@ -388,11 +384,11 @@ class Meta:
assert Schema3.json_schema() == {
"type": "object",
"properties": {
"id": {"anyOf": [{"type": "integer"}, {"type": "null"}], "title": "ID"},
"id": {"title": "ID", "type": "integer"},
"f1": {"type": "string", "title": "F1"},
"f2": {"type": "string", "title": "F2"},
},
"required": ["f1", "f2"],
"required": ["id", "f1", "f2"],
"title": "SampleModel3",
}

Expand Down Expand Up @@ -437,10 +433,10 @@ def test_with_relations():
"title": "Category",
"type": "object",
"properties": {
"id": {"anyOf": [{"type": "integer"}, {"type": "null"}], "title": "ID"},
"id": {"title": "ID", "type": "integer"},
"title": {"title": "Title", "maxLength": 100, "type": "string"},
},
"required": ["title"],
"required": ["id", "title"],
}


Expand Down Expand Up @@ -491,12 +487,12 @@ class Meta:
assert Schema1.json_schema() == {
"type": "object",
"properties": {
"id": {"anyOf": [{"type": "integer"}, {"type": "null"}], "title": "ID"},
"id": {"title": "ID", "type": "integer"},
"f1": {"type": "string", "title": "F1"},
"f2": {"type": "string", "title": "F2"},
"custom": {"type": "integer", "title": "Custom"},
},
"required": ["f1", "f2", "custom"],
"required": ["id", "f1", "f2", "custom"],
"title": "SmallModel",
}

Expand All @@ -506,11 +502,11 @@ class Meta:
assert Schema2.json_schema() == {
"type": "object",
"properties": {
"id": {"anyOf": [{"type": "integer"}, {"type": "null"}], "title": "ID"},
"id": {"title": "ID", "type": "integer"},
"f1": {"type": "integer", "title": "F1"},
"f2": {"type": "string", "title": "F2"},
},
"required": ["f1", "f2"],
"required": ["id", "f1", "f2"],
"title": "SmallModel2",
}

Expand Down Expand Up @@ -569,12 +565,32 @@ class Meta:
app_label = "tests"

Schema = create_schema(SomeReqFieldModel)
assert Schema.json_schema()["required"] == ["some_field", "other_field"]
assert Schema.json_schema()["required"] == ["id", "some_field", "other_field"]

Schema = create_schema(SomeReqFieldModel, optional_fields=["some_field"])
assert Schema.json_schema()["required"] == ["other_field"]
assert Schema.json_schema()["required"] == ["id", "other_field"]

Schema = create_schema(
SomeReqFieldModel, optional_fields=["some_field", "other_field", "optional"]
)
assert Schema.json_schema().get("required") is None
assert Schema.json_schema()["required"] == ["id"]


def test_optional_primary_key():
class ModelWithOptionalPK(models.Model):
id = models.AutoField(primary_key=True, null=True)
name = models.CharField()

class Meta:
app_label = "tests"

Schema = create_schema(ModelWithOptionalPK)
assert Schema.json_schema() == {
"properties": {
"id": {"anyOf": [{"type": "integer"}, {"type": "null"}], "title": "Id"},
"name": {"title": "Name", "type": "string"},
},
"required": ["name"],
"title": "ModelWithOptionalPK",
"type": "object",
}
Loading