From ab26234c205998e2a5809b3165088afb0be362d3 Mon Sep 17 00:00:00 2001 From: justinbaer Date: Wed, 21 May 2025 14:42:39 +0200 Subject: [PATCH] patchnotes_controller.py get patchnotes bug fixes --- .../controllers/patchnotes_controller.py | 55 +++++++++---------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/gen/openapi_server/controllers/patchnotes_controller.py b/gen/openapi_server/controllers/patchnotes_controller.py index a821803..12d0d85 100644 --- a/gen/openapi_server/controllers/patchnotes_controller.py +++ b/gen/openapi_server/controllers/patchnotes_controller.py @@ -8,7 +8,7 @@ from openapi_server.models.patch_notes_input import PatchNotesInput # noqa: E50 from openapi_server import util import logging import uuid -from datetime import datetime +from datetime import datetime, date from bson import json_util from connexion import ProblemException @@ -108,38 +108,35 @@ def get_patchnote(patch_id): # noqa: E501 status=200, ) -def list_patchnotes(since=None, limit=None, offset=None): # noqa: E501 - """Returns list of patch notes newer than the caller’s last login - # noqa: E501 +def list_patchnotes(since: str | None = None): + # 1) validate query param only if present + if since: + try: + datetime.strptime(since, "%Y-%m-%d") + except ValueError: + raise ProblemException( + title="Bad Request", + detail="The date is invalid. Needs to be in this format YYYY-MM-DD", + status=400, + ) + query = {"patch_date": {"$gte": since}} + else: + query = {} # no filter -> all notes - :param since: Only patchnotes newer than this date (YYYY-MM-DD) - :type since: str - :param limit: Maximum number of items to return - :type limit: int - :param offset: Pagination offset - :type offset: int + # 2) fetch from Mongo + docs = list(collection.find(query).sort("patch_date", 1)) - :rtype: Union[List[PatchNotes], Tuple[List[PatchNotes], int], Tuple[List[PatchNotes], int, Dict[str, str]] - """ - try: - datetime.strptime(since, '%Y-%m-%d') # 2025-01-0 - except ValueError as e: - raise ProblemException( - title="Bad Request", - detail="The date is invalid. Needs to be in this Format YYYY-MM-DD", - status=400, - ) - query = { - 'patch_date': {'$gte': since} - } - results = [] - for entry in collection.find(query): - patch_note = PatchNotes.from_dict(entry) - results.append(patch_note.to_dict()) + # 3) ensure every patch_date is *string* before serialising + for d in docs: + if isinstance(d.get("patch_date"), (datetime, date)): + d["patch_date"] = d["patch_date"].strftime("%Y-%m-%d") + d.pop("_id", None) # optional: hide Mongo’s internal id + + # 4) serialise – default=str handles any remaining non-JSON types return Response( - json_util.dumps(results), - mimetype='application/json', + json_util.dumps(docs, default=str), + mimetype="application/json", status=200, )