Skip to content

Fixed rendered template values not displaying with proper code format… #53657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

dheerajturaga
Copy link
Contributor

@dheerajturaga dheerajturaga commented Jul 23, 2025

This PR attempts to fix #50324 , #49064

The issue was caused by double JSON serialization in the frontend:

  1. Backend already serializes template field values to strings via serialize_template_field()
  2. Frontend was calling JSON.stringify() again on these already-stringified values
  3. This resulted in escaped quotes and poor formatting
image

Core Fixes

  • Fixed double serialization: Replaced JSON.stringify(value) with proper type checking in RenderedTemplates.tsx:97
    • Use string values directly if already strings
    • Only JSON stringify non-string values (objects/arrays)
  • Added whitespace preservation: Replaced <Code> component with <SyntaxHighlighter> to preserve newlines and indentation

Syntax Highlighting Enhancement

  • Integrated react-syntax-highlighter with automatic language detection:
    • JSON: Detects objects/arrays with valid JSON structure
    • YAML: Detects key-value pairs with colons and lists
    • SQL: Detects keywords like SELECT, INSERT, CREATE, etc.
    • Bash: Detects commands, shebangs, control structures, and operators
    • Text: Falls back to plain text for unrecognized content
  • Theme support: Respects dark/light mode themes consistent with DAG code viewer
  • Improved styling: Added proper spacing, border radius, and responsive alignment

…ting and syntax highlighting in the Task Instance UI
@boring-cyborg boring-cyborg bot added the area:UI Related to UI/UX. For Frontend Developers. label Jul 23, 2025
@dheerajturaga
Copy link
Contributor Author

Added line number support
image

@dheerajturaga
Copy link
Contributor Author

@pierrejeambrun, @jscheffl thoughts on this one?

Copy link
Contributor

@jscheffl jscheffl left a comment

Choose a reason for hiding this comment

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

From reading code I really like the lean approach. Some matching nit from my side to make it a bit more safe.

Would like to have an UI expert second opinion though.

SyntaxHighlighter.registerLanguage("sql", sql);
SyntaxHighlighter.registerLanguage("bash", bash);

const detectLanguage = (value: string): string => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you separate this function out into a separate TS module file?

}

// Try to detect YAML (basic heuristics)
if (trimmed.includes(":") && (trimmed.includes("\n") || trimmed.includes("- "))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Detection safety:

  • If a colon is matched it should be followed by a newline (":\n")
  • Should we not rather use a lib like https://www.npmjs.com/package/yaml to test parsing after some basic heuristics before making a decision (similar to JSON)?

// Try to detect SQL (basic heuristics)
const sqlKeywords = /\b(?:select|insert|update|delete|create|alter|drop|from|where|join)\b/iu;

if (sqlKeywords.test(trimmed)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it might be better to match more than a single keyword, as SELECT will in almost all cases will be followed by a FROM and otherwise DROP will also usually be followed by a TABLE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:UI Related to UI/UX. For Frontend Developers.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rendered Template field is hard to read in Airflow 3
2 participants