diff --git a/api/controllers/service_api_with_auth/user/profile.py b/api/controllers/service_api_with_auth/user/profile.py index 9b8757356e..16d162177c 100644 --- a/api/controllers/service_api_with_auth/user/profile.py +++ b/api/controllers/service_api_with_auth/user/profile.py @@ -105,8 +105,12 @@ class UserProfile(Resource): # Validate major if provided if 'major' in data: major = data['major'] - if len(major) > 20: + + if isinstance(major, str) and len(major) > 20: return {"success": False, "message": "Major exceeds maximum length"}, 400 + elif major is not None: # Handle non-string or None values appropriately + return {"success": False, "message": "Major must be a string value"}, 400 + validated_data['major'] = major # Use the service to update user profile diff --git a/api/services/end_user_service.py b/api/services/end_user_service.py index 5c2e1f0664..ddf18be6b2 100644 --- a/api/services/end_user_service.py +++ b/api/services/end_user_service.py @@ -70,12 +70,17 @@ class EndUserService: if 'major' in profile_data: major = profile_data['major'] - # Initialize extra_profile if it doesn't exist - if not end_user.extra_profile: + # Create a new dictionary if extra_profile is None + if end_user.extra_profile is None: end_user.extra_profile = {} - # Update major in extra_profile - end_user.extra_profile['major'] = major + # Make a copy of the existing dictionary to ensure changes are detected + extra_profile = dict(end_user.extra_profile) + extra_profile['major'] = major + end_user.extra_profile = extra_profile + + # Force the change to be detected + db.session.add(end_user) # Save changes to database db.session.commit()