Skip to content

Conversation

@synweap15
Copy link

Adds except_fields and only_fields params that take in field names as they appear in task and tasks endpoints.

My case was that I wanted to exclude huge children key list.

@auvipy auvipy requested review from auvipy and Copilot August 12, 2025 16:47
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds field filtering functionality to task and tasks API endpoints through new except_fields and only_fields query parameters. This allows API consumers to selectively include or exclude specific fields from the response, which is useful for reducing payload size when certain fields (like large children lists) are not needed.

  • Implements filter_dict utility function to handle field filtering logic
  • Updates task and tasks endpoints to accept and process field filtering parameters
  • Adds comprehensive test coverage for the new filtering functionality

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
flower/utils/tasks.py Adds filter_dict function and updates as_dict to support field filtering
flower/api/tasks.py Updates task and tasks endpoints to accept filtering parameters and apply them
tests/unit/api/test_tasks.py Adds test coverage for field filtering functionality and updates existing tests

Comment on lines +642 to 645
response = tasks.as_dict(task, only_fields=only_fields, except_fields=except_fields)
if task.worker is not None and 'worker' in response:
response['worker'] = task.worker.hostname

Copy link

Copilot AI Aug 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition checks if 'worker' exists in the filtered response, but the worker hostname assignment happens after filtering. If 'worker' is excluded by filtering, this condition will prevent the assignment, but if 'worker' is included, the assignment will overwrite the filtered value. Consider applying the worker hostname transformation before filtering.

Suggested change
response = tasks.as_dict(task, only_fields=only_fields, except_fields=except_fields)
if task.worker is not None and 'worker' in response:
response['worker'] = task.worker.hostname
# Ensure the worker field is set to the hostname before filtering
if task.worker is not None:
task.worker = task.worker.hostname
response = tasks.as_dict(task, only_fields=only_fields, except_fields=except_fields)

Copilot uses AI. Check for mistakes.
Comment on lines +529 to 532
task = tasks.as_dict(task, only_fields=only_fields, except_fields=except_fields)
worker = task.pop('worker', None)
if worker is not None:
task['worker'] = worker.hostname
Copy link

Copilot AI Aug 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The worker hostname transformation on lines 530-532 happens after filtering, which means if 'worker' is included in only_fields or excluded from except_fields, the transformation may not work as expected. The worker field should be transformed before applying field filtering.

Suggested change
task = tasks.as_dict(task, only_fields=only_fields, except_fields=except_fields)
worker = task.pop('worker', None)
if worker is not None:
task['worker'] = worker.hostname
if hasattr(task, 'worker') and task.worker is not None:
task.worker = task.worker.hostname
task = tasks.as_dict(task, only_fields=only_fields, except_fields=except_fields)

Copilot uses AI. Check for mistakes.
Comment on lines +79 to +80
dict: The filtered dictionary
"""
Copy link

Copilot AI Aug 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function doesn't validate mutual exclusivity of only_fields and except_fields parameters. If both are provided, only_fields takes precedence silently, which could lead to unexpected behavior. Consider adding validation to reject requests with both parameters or documenting this precedence clearly.

Suggested change
dict: The filtered dictionary
"""
dict: The filtered dictionary
Raises:
ValueError: If both only_fields and except_fields are provided.
"""
if only_fields and except_fields:
raise ValueError("Cannot specify both only_fields and except_fields. Please provide only one.")

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant