Ajay9704 opened a new pull request, #59999:
URL: https://github.com/apache/airflow/pull/59999
## Description
This PR fixes issue #59877 by addressing incorrect rendered template
truncation when `AIRFLOW__CORE__MAX_TEMPLATED_FIELD_LENGTH` is set to very
small values (like 1, 5, 10).
## Problem
When `max_templated_field_length` was set to very small values, the original
code used a fixed offset of 79 characters (`max_length - 79`), which resulted
in:
- Negative slicing when `max_length` was smaller than 79
- Empty or malformed output
- Inconsistent behavior across different field types
## Context and Previous Work
I've carefully reviewed the existing issue #59877 and the previous PR #59882
by @Jeevankumar-s. While their approach provided a good foundation, I
identified that there were additional edge cases with the `!r` formatting
(repr() adding quotes to strings) that needed proper handling to ensure the
output never exceeds the configured maximum length. The conversation in the
issue also highlighted the importance of prioritizing clear communication to
users over potentially confusing partial results.
## Solution
I've replaced the problematic logic with a comprehensive solution that:
1. Handles zero or negative max_length by returning an empty string
2. For very small max_length (≤ suffix length), returns truncated suffix
3. For small max_length (≤ prefix + suffix), returns truncated combination
of prefix and suffix
4. For adequate max_length, properly calculates available space accounting
for:
- The prefix: "Truncated. You can change this behaviour in
[core]max_templated_field_length. " (78 chars)
- The suffix: "..." (3 chars)
- The quotes added by `!r` formatting (repr() adds 2 quotes around
strings)
My approach specifically addresses the `!r` formatting issue that could
cause output to exceed the max_length when the repr() adds additional quote
characters around strings.
## Files Changed
- `task-sdk/src/airflow/sdk/execution_time/task_runner.py` - Runtime
truncation logic
- `airflow-core/src/airflow/serialization/helpers.py` - Serialization
truncation logic
Both paths now have consistent behavior.
## Testing
I've verified the fix with various test cases including the edge cases
mentioned in the issue, ensuring output length never exceeds the configured
maximum.
## Personal Note
As a new contributor to Apache Airflow, I've spent considerable time
understanding the issue, reviewing the existing PR, and carefully considering
the community's feedback. I aimed to create a solution that not only fixes the
technical issue but also follows the principle of prioritizing clear
communication to users, as discussed in the issue comments.
Closes #59877
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]