Skip to content

Commit

Permalink
update JSON schema (#257)
Browse files Browse the repository at this point in the history
  • Loading branch information
dantownsend authored Oct 19, 2023
1 parent ccbeabf commit dc61090
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 36 deletions.
2 changes: 1 addition & 1 deletion piccolo_api/crud/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def pydantic_model(self) -> t.Type[pydantic.BaseModel]:
self.table,
model_name=f"{self.table.__name__}In",
exclude_columns=(self.table._meta.primary_key,),
**self.schema_extra,
json_schema_extra={"extra": self.schema_extra},
)

def _pydantic_model_output(
Expand Down
50 changes: 31 additions & 19 deletions tests/crud/test_crud_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,14 +461,18 @@ def test_get_schema(self):
self.assertEqual(
response.json(),
{
"help_text": None,
"primary_key_name": "id",
"extra": {
"help_text": None,
"primary_key_name": "id",
"visible_fields_options": ["id", "name", "rating"],
},
"properties": {
"name": {
"extra": {
"choices": None,
"help_text": None,
"nullable": False,
"secret": False,
},
"maxLength": 100,
"title": "Name",
Expand All @@ -481,14 +485,14 @@ def test_get_schema(self):
"choices": None,
"help_text": None,
"nullable": False,
"secret": False,
},
"title": "Rating",
},
},
"required": ["name"],
"title": "MovieIn",
"type": "object",
"visible_fields_options": ["id", "name", "rating"],
},
)

Expand All @@ -515,8 +519,11 @@ class Rating(Enum):
self.assertEqual(
response.json(),
{
"help_text": None,
"primary_key_name": "id",
"extra": {
"help_text": None,
"primary_key_name": "id",
"visible_fields_options": ["id", "score"],
},
"properties": {
"score": {
"anyOf": [{"type": "integer"}, {"type": "null"}],
Expand All @@ -533,13 +540,13 @@ class Rating(Enum):
},
"help_text": None,
"nullable": False,
"secret": False,
},
"title": "Score",
}
},
"title": "ReviewIn",
"type": "object",
"visible_fields_options": ["id", "score"],
},
)

Expand All @@ -558,19 +565,31 @@ def test_get_schema_with_joins(self):
self.assertEqual(
response.json(),
{
"help_text": None,
"primary_key_name": "id",
"extra": {
"help_text": None,
"primary_key_name": "id",
"visible_fields_options": [
"id",
"movie",
"movie.id",
"movie.name",
"movie.rating",
"name",
],
},
"properties": {
"movie": {
"anyOf": [{"type": "integer"}, {"type": "null"}],
"default": None,
"extra": {
"choices": None,
"foreign_key": True,
"foreign_key": {
"target_column": "id",
"to": "movie",
},
"help_text": None,
"nullable": True,
"target_column": "id",
"to": "movie",
"secret": False,
},
"title": "Movie",
},
Expand All @@ -584,20 +603,13 @@ def test_get_schema_with_joins(self):
"choices": None,
"help_text": None,
"nullable": False,
"secret": False,
},
"title": "Name",
},
},
"title": "RoleIn",
"type": "object",
"visible_fields_options": [
"id",
"movie",
"movie.id",
"movie.name",
"movie.rating",
"name",
],
},
)

Expand Down
41 changes: 25 additions & 16 deletions tests/fastapi/test_fastapi_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,11 @@ def test_schema(self):
self.assertEqual(
response.json(),
{
"help_text": None,
"primary_key_name": "id",
"extra": {
"help_text": None,
"primary_key_name": "id",
"visible_fields_options": ["id", "name", "rating"],
},
"properties": {
"name": {
"anyOf": [
Expand All @@ -116,6 +119,7 @@ def test_schema(self):
"choices": None,
"help_text": None,
"nullable": False,
"secret": False,
},
"title": "Name",
},
Expand All @@ -126,13 +130,13 @@ def test_schema(self):
"choices": None,
"help_text": None,
"nullable": False,
"secret": False,
},
"title": "Rating",
},
},
"title": "MovieIn",
"type": "object",
"visible_fields_options": ["id", "name", "rating"],
},
)

Expand All @@ -143,19 +147,31 @@ def test_schema_joins(self):
self.assertEqual(
response.json(),
{
"help_text": None,
"primary_key_name": "id",
"extra": {
"help_text": None,
"primary_key_name": "id",
"visible_fields_options": [
"id",
"movie",
"movie.id",
"movie.name",
"movie.rating",
"name",
],
},
"properties": {
"movie": {
"anyOf": [{"type": "integer"}, {"type": "null"}],
"default": None,
"extra": {
"choices": None,
"foreign_key": True,
"foreign_key": {
"target_column": "id",
"to": "movie",
},
"help_text": None,
"nullable": True,
"target_column": "id",
"to": "movie",
"secret": False,
},
"title": "Movie",
},
Expand All @@ -169,20 +185,13 @@ def test_schema_joins(self):
"choices": None,
"help_text": None,
"nullable": False,
"secret": False,
},
"title": "Name",
},
},
"title": "RoleIn",
"type": "object",
"visible_fields_options": [
"id",
"movie",
"movie.id",
"movie.name",
"movie.rating",
"name",
],
},
)

Expand Down

0 comments on commit dc61090

Please sign in to comment.