Skip to content

Commit c8fcde5

Browse files
committed
fix: Solve bad string response
1 parent c0d94e9 commit c8fcde5

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

app/controllers/student_controller.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
delete_student
1212
)
1313

14-
from app.utils.json_encoder import JSONEncoder
14+
from app.utils.json_encoder import json_encoder
1515

1616

1717
router = APIRouter()
@@ -24,13 +24,12 @@ async def create_student_data(student: StudentModel = Body(...)) -> JSONResponse
2424
new_student = await create_student(student_json)
2525
return JSONResponse(
2626
status_code=status.HTTP_201_CREATED,
27-
content=JSONEncoder().encode(new_student)
27+
content=json_encoder(new_student)
2828
)
2929
except HTTPException as error:
3030
return JSONResponse(
3131
status_code=error.status_code,
32-
content=JSONEncoder().encode({'message': error.detail})
33-
)
32+
content=json_encoder({'message': error.detail}))
3433

3534

3635
@router.get(
@@ -43,12 +42,12 @@ async def read_student_data(student_id: str) -> JSONResponse:
4342
student = await read_student(student_id)
4443
return JSONResponse(
4544
status_code=status.HTTP_200_OK,
46-
content=JSONEncoder().encode(student)
45+
content=json_encoder(student)
4746
)
4847
except HTTPException as error:
4948
return JSONResponse(
5049
status_code=error.status_code,
51-
content=JSONEncoder().encode({'message': error.detail})
50+
content=json_encoder({'message': error.detail})
5251
)
5352

5453

@@ -63,14 +62,14 @@ async def update_student_data(student_id: str, student: StudentModel = Body(...)
6362
await update_student(student_id, student_json)
6463
return JSONResponse(
6564
status_code=status.HTTP_200_OK,
66-
content=JSONEncoder().encode(
65+
content=json_encoder(
6766
{'message': f'Student with id {student_id} updated successfully'}
6867
)
6968
)
7069
except HTTPException as error:
7170
return JSONResponse(
7271
status_code=error.status_code,
73-
content=JSONEncoder().encode({'message': error.detail})
72+
content=json_encoder({'message': error.detail})
7473
)
7574

7675

@@ -84,12 +83,12 @@ async def delete_student_data(student_id: str) -> JSONResponse:
8483
await delete_student(student_id)
8584
return JSONResponse(
8685
status_code=status.HTTP_200_OK,
87-
content=JSONEncoder().encode(
86+
content=json_encoder(
8887
{'message': f'Student with id {student_id} deleted successfully'}
8988
)
9089
)
9190
except HTTPException as error:
9291
return JSONResponse(
9392
status_code=error.status_code,
94-
content=JSONEncoder().encode({'message': error.detail})
93+
content=json_encoder({'message': error.detail})
9594
)

app/utils/json_encoder.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
import json
1+
from json import JSONEncoder, loads
22

33
from typing import Any
44

5+
from datetime import datetime
6+
57
from bson import ObjectId
68

79

8-
class JSONEncoder(json.JSONEncoder):
9-
def default(self, o: Any) -> json.JSONEncoder | str:
10+
class JSONencoder(JSONEncoder):
11+
def default(self, o: Any) -> Any:
1012
if isinstance(o, ObjectId):
1113
return str(o)
12-
return json.JSONEncoder.default(self, o)
14+
if isinstance(o, datetime):
15+
return str(o)
16+
return JSONEncoder.default(self, o)
17+
18+
def json_encoder(object: dict) -> Any:
19+
return loads(JSONencoder().encode(object))

0 commit comments

Comments
 (0)