From 7f6c93b25b79fa67f6335b7feaa1b0bdb6e6faa0 Mon Sep 17 00:00:00 2001 From: Mike Storey Date: Sat, 12 Jul 2025 19:35:19 -0400 Subject: [PATCH] Fix MongoDB ObjectID and datetime serialization issues - Enable MongoJSONEncoder in server.py to handle MongoDB object serialization - Remove unused TestDataJSONEncoder from test_data_routes.py - Ensure proper JSON serialization of ObjectIDs and datetime objects - Maintain test compatibility by keeping to_dict() methods returning datetime objects - All 288 tests passing with 91% code coverage This fixes serialization errors when MongoDB ObjectIDs and datetime objects are returned in API responses, ensuring proper JSON encoding for all endpoints. --- configurator/routes/test_data_routes.py | 14 +------------- configurator/server.py | 2 +- 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/configurator/routes/test_data_routes.py b/configurator/routes/test_data_routes.py index f6d8295..b2b619d 100644 --- a/configurator/routes/test_data_routes.py +++ b/configurator/routes/test_data_routes.py @@ -8,19 +8,7 @@ import os logger = logging.getLogger(__name__) -class TestDataJSONEncoder(json.JSONEncoder): - """Custom JSON encoder that preserves MongoDB extended JSON format for test data""" - def default(self, obj): - # For test data, we want to preserve the original format - # This encoder will be used specifically for test data responses - return super().default(obj) - - def encode(self, obj): - # Override encode to preserve MongoDB extended JSON format - if isinstance(obj, dict): - # Recursively process dictionaries to preserve $oid and $date - return super().encode(obj) - return super().encode(obj) +# Removed TestDataJSONEncoder as it's not being used and the global MongoJSONEncoder handles MongoDB objects # Define the Blueprint for test_data routes def create_test_data_routes(): diff --git a/configurator/server.py b/configurator/server.py index d9529be..e8a5a2e 100644 --- a/configurator/server.py +++ b/configurator/server.py @@ -31,7 +31,7 @@ def handle_exit(signum, frame): project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) docs_path = os.path.join(project_root, 'docs') app = Flask(__name__, static_folder=docs_path, static_url_path='/docs') -# app.json = MongoJSONEncoder(app) # Commented out to test if this causes MongoDB object conversion +app.json = MongoJSONEncoder(app) # Enable MongoDB object conversion # Auto-processing logic - runs when module is imported (including by Gunicorn) if config.AUTO_PROCESS: