Skip to content

Commit e86e7b7

Browse files
authored
Merge pull request #8 from pmdevita/bug/primary-key
Fix primary key always being marked as nullable in schema
2 parents 2685bbf + 9f0037f commit e86e7b7

File tree

5 files changed

+55
-41
lines changed

5 files changed

+55
-41
lines changed

ninja/orm/factory.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ def create_schema(
6262

6363
definitions = {}
6464
for fld in model_fields_list:
65-
# types: ignore
6665
field_name, python_type, field_info = get_schema_field(
6766
fld,
6867
depth=depth,

ninja/orm/fields.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@ def get_schema_field(
161161

162162
else:
163163
_f_name, _f_path, _f_pos, field_options = field.deconstruct()
164-
blank = field_options.get("blank", False)
165164
null = field_options.get("null", False)
166165
max_length = field_options.get("max_length")
167166

@@ -176,7 +175,7 @@ def get_schema_field(
176175
]
177176
raise ConfigError("\n".join(msg)) from e
178177

179-
if field.primary_key or blank or null or optional:
178+
if null or optional:
180179
default = None
181180
nullable = True
182181

tests/test_alias.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class Meta:
7070
assert BookSchema.json_schema() == {
7171
"properties": {
7272
"authorId": {"title": "Author", "type": "integer"},
73-
"id": {"anyOf": [{"type": "integer"}, {"type": "null"}], "title": "Id"},
73+
"id": {"title": "Id", "type": "integer"},
7474
"name": {"maxLength": 100, "title": "Name", "type": "string"},
7575
"publishedDate": {
7676
"default": "2024-01-01",
@@ -79,7 +79,7 @@ class Meta:
7979
"type": "string",
8080
},
8181
},
82-
"required": ["name", "authorId"],
82+
"required": ["id", "name", "authorId"],
8383
"title": "BookSchema",
8484
"type": "object",
8585
}

tests/test_orm_metaclass.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ class Meta:
4646
"title": "SampleSchema2",
4747
"type": "object",
4848
"properties": {
49-
"id": {"anyOf": [{"type": "integer"}, {"type": "null"}], "title": "ID"},
49+
"id": {"title": "ID", "type": "integer"},
5050
"firstname": {"title": "Firstname", "type": "string"},
5151
},
52-
"required": ["firstname"],
52+
"required": ["id", "firstname"],
5353
}
5454

5555

@@ -119,9 +119,9 @@ class Meta:
119119
fields = "__all__"
120120
fields_optional = "__all__"
121121

122-
assert OptSchema.json_schema().get("required") == ["extra"]
122+
assert OptSchema.json_schema().get("required") == ["id", "extra"]
123123
assert OptSchema.json_schema()["properties"] == {
124-
"id": {"anyOf": [{"type": "integer"}, {"type": "null"}], "title": "ID"},
124+
"id": {"title": "ID", "type": "integer"},
125125
"title": {"anyOf": [{"type": "string"}, {"type": "null"}], "title": "Title"},
126126
"other": {"anyOf": [{"type": "string"}, {"type": "null"}], "title": "Other"},
127127
"extra": {"title": "Extra", "type": "integer"},
@@ -164,14 +164,14 @@ class Meta:
164164
"title": "SomeSchema",
165165
"type": "object",
166166
"properties": {
167-
"id": {"anyOf": [{"type": "integer"}, {"type": "null"}], "title": "ID"},
167+
"id": {"title": "ID", "type": "integer"},
168168
"field1": {"title": "Field1", "type": "string"},
169169
"field2": {
170170
"anyOf": [{"type": "string"}, {"type": "null"}],
171171
"title": "Field2",
172172
},
173173
},
174-
"required": ["field1"],
174+
"required": ["id", "field1"],
175175
}
176176

177177

tests/test_orm_schemas.py

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ class Meta:
3333
"title": "ChildModel",
3434
"type": "object",
3535
"properties": {
36-
"id": {"anyOf": [{"type": "integer"}, {"type": "null"}], "title": "ID"},
36+
"id": {"title": "ID", "type": "integer"},
3737
"parent_field": {"type": "string", "title": "Parent Field"},
3838
"parentmodel_ptr_id": {"type": "integer", "title": "Parentmodel Ptr"},
3939
"child_field": {"type": "string", "title": "Child Field"},
4040
},
41-
"required": ["parent_field", "parentmodel_ptr_id", "child_field"],
41+
"required": ["id", "parent_field", "parentmodel_ptr_id", "child_field"],
4242
}
4343

4444

@@ -87,7 +87,7 @@ class Meta:
8787
"title": "AllFields",
8888
"type": "object",
8989
"properties": {
90-
"id": {"anyOf": [{"type": "integer"}, {"type": "null"}], "title": "ID"},
90+
"id": {"title": "ID", "type": "integer"},
9191
"bigintegerfield": {"title": "Bigintegerfield", "type": "integer"},
9292
"binaryfield": {
9393
"title": "Binaryfield",
@@ -160,6 +160,7 @@ class Meta:
160160
}),
161161
},
162162
"required": [
163+
"id",
163164
"bigintegerfield",
164165
"binaryfield",
165166
"booleanfield",
@@ -212,10 +213,7 @@ class Meta:
212213
SchemaCls = create_schema(ModelAltAuto)
213214
# print(SchemaCls.json_schema())
214215
assert SchemaCls.json_schema()["properties"] == {
215-
"altautofield": {
216-
"anyOf": [{"type": "integer"}, {"type": "null"}],
217-
"title": "Altautofield",
218-
}
216+
"altautofield": {"title": "Altautofield", "type": "integer"}
219217
}
220218

221219

@@ -233,14 +231,14 @@ class Meta:
233231
"title": "ModelNewFields",
234232
"type": "object",
235233
"properties": {
236-
"id": {"title": "ID", "anyOf": [{"type": "integer"}, {"type": "null"}]},
234+
"id": {"title": "ID", "type": "integer"},
237235
"jsonfield": {"title": "Jsonfield", "type": "object"},
238236
"positivebigintegerfield": {
239237
"title": "Positivebigintegerfield",
240238
"type": "integer",
241239
},
242240
},
243-
"required": ["jsonfield", "positivebigintegerfield"],
241+
"required": ["id", "jsonfield", "positivebigintegerfield"],
244242
}
245243

246244
obj = Schema(id=1, jsonfield={"any": "data"}, positivebigintegerfield=1)
@@ -272,7 +270,7 @@ class Meta:
272270
"title": "TestSchema",
273271
"type": "object",
274272
"properties": {
275-
"id": {"anyOf": [{"type": "integer"}, {"type": "null"}], "title": "ID"},
273+
"id": {"title": "ID", "type": "integer"},
276274
"onetoonefield_id": {"title": "Onetoonefield", "type": "integer"},
277275
"foreignkey_id": {
278276
"anyOf": [{"type": "integer"}, {"type": "null"}],
@@ -284,15 +282,15 @@ class Meta:
284282
"items": {"type": "integer"},
285283
},
286284
},
287-
"required": ["onetoonefield_id", "manytomanyfield"],
285+
"required": ["id", "onetoonefield_id", "manytomanyfield"],
288286
}
289287

290288
SchemaClsDeep = create_schema(TestModel, name="TestSchemaDeep", depth=1)
291289
print(SchemaClsDeep.json_schema())
292290
assert SchemaClsDeep.json_schema() == {
293291
"type": "object",
294292
"properties": {
295-
"id": {"anyOf": [{"type": "integer"}, {"type": "null"}], "title": "ID"},
293+
"id": {"title": "ID", "type": "integer"},
296294
"onetoonefield": pydantic_ref_fix({
297295
"title": "Onetoonefield",
298296
"description": "",
@@ -310,20 +308,17 @@ class Meta:
310308
"description": "",
311309
},
312310
},
313-
"required": ["onetoonefield", "manytomanyfield"],
311+
"required": ["id", "onetoonefield", "manytomanyfield"],
314312
"title": "TestSchemaDeep",
315313
"$defs": {
316314
"Related": {
317315
"title": "Related",
318316
"type": "object",
319317
"properties": {
320-
"id": {
321-
"anyOf": [{"type": "integer"}, {"type": "null"}],
322-
"title": "ID",
323-
},
318+
"id": {"title": "ID", "type": "integer"},
324319
"charfield": {"type": "string", "title": "Charfield"},
325320
},
326-
"required": ["charfield"],
321+
"required": ["id", "charfield"],
327322
}
328323
},
329324
}
@@ -343,14 +338,15 @@ class Meta:
343338
"title": "MyModel",
344339
"type": "object",
345340
"properties": {
346-
"id": {"anyOf": [{"type": "integer"}, {"type": "null"}], "title": "ID"},
341+
"id": {"title": "ID", "type": "integer"},
347342
"default_static": {
348343
"default": "hello",
349344
"title": "Default Static",
350345
"type": "string",
351346
},
352347
"default_dynamic": {"title": "Default Dynamic", "type": "string"},
353348
},
349+
"required": ["id"],
354350
}
355351

356352

@@ -392,11 +388,11 @@ class Meta:
392388
assert Schema3.json_schema() == {
393389
"type": "object",
394390
"properties": {
395-
"id": {"anyOf": [{"type": "integer"}, {"type": "null"}], "title": "ID"},
391+
"id": {"title": "ID", "type": "integer"},
396392
"f1": {"type": "string", "title": "F1"},
397393
"f2": {"type": "string", "title": "F2"},
398394
},
399-
"required": ["f1", "f2"],
395+
"required": ["id", "f1", "f2"],
400396
"title": "SampleModel3",
401397
}
402398

@@ -441,10 +437,10 @@ def test_with_relations():
441437
"title": "Category",
442438
"type": "object",
443439
"properties": {
444-
"id": {"anyOf": [{"type": "integer"}, {"type": "null"}], "title": "ID"},
440+
"id": {"title": "ID", "type": "integer"},
445441
"title": {"title": "Title", "maxLength": 100, "type": "string"},
446442
},
447-
"required": ["title"],
443+
"required": ["id", "title"],
448444
}
449445

450446

@@ -495,12 +491,12 @@ class Meta:
495491
assert Schema1.json_schema() == {
496492
"type": "object",
497493
"properties": {
498-
"id": {"anyOf": [{"type": "integer"}, {"type": "null"}], "title": "ID"},
494+
"id": {"title": "ID", "type": "integer"},
499495
"f1": {"type": "string", "title": "F1"},
500496
"f2": {"type": "string", "title": "F2"},
501497
"custom": {"type": "integer", "title": "Custom"},
502498
},
503-
"required": ["f1", "f2", "custom"],
499+
"required": ["id", "f1", "f2", "custom"],
504500
"title": "SmallModel",
505501
}
506502

@@ -510,11 +506,11 @@ class Meta:
510506
assert Schema2.json_schema() == {
511507
"type": "object",
512508
"properties": {
513-
"id": {"anyOf": [{"type": "integer"}, {"type": "null"}], "title": "ID"},
509+
"id": {"title": "ID", "type": "integer"},
514510
"f1": {"type": "integer", "title": "F1"},
515511
"f2": {"type": "string", "title": "F2"},
516512
},
517-
"required": ["f1", "f2"],
513+
"required": ["id", "f1", "f2"],
518514
"title": "SmallModel2",
519515
}
520516

@@ -573,15 +569,35 @@ class Meta:
573569
app_label = "tests"
574570

575571
Schema = create_schema(SomeReqFieldModel)
576-
assert Schema.json_schema()["required"] == ["some_field", "other_field"]
572+
assert Schema.json_schema()["required"] == ["id", "some_field", "other_field"]
577573

578574
Schema = create_schema(SomeReqFieldModel, optional_fields=["some_field"])
579-
assert Schema.json_schema()["required"] == ["other_field"]
575+
assert Schema.json_schema()["required"] == ["id", "other_field"]
580576

581577
Schema = create_schema(
582578
SomeReqFieldModel, optional_fields=["some_field", "other_field", "optional"]
583579
)
584-
assert Schema.json_schema().get("required") is None
580+
assert Schema.json_schema()["required"] == ["id"]
581+
582+
583+
def test_optional_primary_key():
584+
class ModelWithOptionalPK(models.Model):
585+
id = models.AutoField(primary_key=True, null=True)
586+
name = models.CharField()
587+
588+
class Meta:
589+
app_label = "tests"
590+
591+
Schema = create_schema(ModelWithOptionalPK)
592+
assert Schema.json_schema() == {
593+
"properties": {
594+
"id": {"anyOf": [{"type": "integer"}, {"type": "null"}], "title": "Id"},
595+
"name": {"title": "Name", "type": "string"},
596+
},
597+
"required": ["name"],
598+
"title": "ModelWithOptionalPK",
599+
"type": "object",
600+
}
585601

586602

587603
def test_register_custom_field():

0 commit comments

Comments
 (0)