Skip to content

Commit 7f6c93b

Browse files
author
Mike Storey
committed
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.
1 parent 135e84a commit 7f6c93b

File tree

2 files changed

+2
-14
lines changed

2 files changed

+2
-14
lines changed

configurator/routes/test_data_routes.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,7 @@
88
import os
99
logger = logging.getLogger(__name__)
1010

11-
class TestDataJSONEncoder(json.JSONEncoder):
12-
"""Custom JSON encoder that preserves MongoDB extended JSON format for test data"""
13-
def default(self, obj):
14-
# For test data, we want to preserve the original format
15-
# This encoder will be used specifically for test data responses
16-
return super().default(obj)
17-
18-
def encode(self, obj):
19-
# Override encode to preserve MongoDB extended JSON format
20-
if isinstance(obj, dict):
21-
# Recursively process dictionaries to preserve $oid and $date
22-
return super().encode(obj)
23-
return super().encode(obj)
11+
# Removed TestDataJSONEncoder as it's not being used and the global MongoJSONEncoder handles MongoDB objects
2412

2513
# Define the Blueprint for test_data routes
2614
def create_test_data_routes():

configurator/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def handle_exit(signum, frame):
3131
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
3232
docs_path = os.path.join(project_root, 'docs')
3333
app = Flask(__name__, static_folder=docs_path, static_url_path='/docs')
34-
# app.json = MongoJSONEncoder(app) # Commented out to test if this causes MongoDB object conversion
34+
app.json = MongoJSONEncoder(app) # Enable MongoDB object conversion
3535

3636
# Auto-processing logic - runs when module is imported (including by Gunicorn)
3737
if config.AUTO_PROCESS:

0 commit comments

Comments
 (0)