-
Notifications
You must be signed in to change notification settings - Fork 13
Fix nested field access in message template formatting #175
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
Merged
+106
−25
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1d984ee
Fix nested field access in message template formatting
Ayaanplayz19 d05322a
Use Object.hasOwn for template field lookups and note bracket behavior
ayaangazali 4b82908
Clarify bracket-syntax wording in the changeset
ayaangazali 4ba94d4
Add paired regression tests for bracket-syntax behavior
ayaangazali File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'logfire': patch | ||
| --- | ||
|
|
||
| Fix nested field access in message templates. `{a.b}` previously resolved every path segment against the top-level attribute record, so it either fell back to the raw template or silently rendered an unrelated top-level attribute that shared the trailing segment name. Nested paths now walk into the attribute value, matching Python Logfire, and literal dotted attribute keys like `http.method` keep their existing precedence. Field lookups now use `Object.hasOwn`, so prototype members like `{user.toString}` no longer resolve. Bracket syntax such as `{a[0]}` was never supported; it previously rendered the string `undefined` and now warns and falls back to the raw template. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import { afterEach, describe, expect, test, vi } from 'vite-plus/test' | ||
|
|
||
| import { NoopScrubber } from './AttributeScrubber' | ||
| import { logfireFormatWithExtras } from './formatter' | ||
|
|
||
| function format(template: string, record: Record<string, unknown>): string { | ||
| return logfireFormatWithExtras(template, record, NoopScrubber).formattedMessage | ||
| } | ||
|
|
||
| describe('message template nested field access', () => { | ||
| afterEach(() => { | ||
| vi.restoreAllMocks() | ||
| }) | ||
|
|
||
| test('resolves a nested field', () => { | ||
| expect(format('hello {user.name}', { user: { name: 'Alice' } })).toBe('hello Alice') | ||
| }) | ||
|
|
||
| test('resolves a deeply nested field', () => { | ||
| expect(format('request by {req.user.id}', { req: { user: { id: 1 } } })).toBe('request by 1') | ||
| }) | ||
|
|
||
| test('nested traversal ignores unrelated top-level keys with the same trailing name', () => { | ||
| expect(format('value is {a.b}', { a: { b: 'nested' }, b: 'top' })).toBe('value is nested') | ||
| }) | ||
|
|
||
| test('a literal dotted attribute key wins over nested traversal', () => { | ||
| expect(format('{http.method} request', { http: { method: 'nested' }, 'http.method': 'GET' })).toBe('GET request') | ||
| }) | ||
|
|
||
| test('supports the debug format with nested fields', () => { | ||
| expect(format('{user.name=}', { user: { name: 'Alice' } })).toBe('user.name=Alice') | ||
| }) | ||
|
|
||
| test('falls back to the raw template when the nested field is missing', () => { | ||
| const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined) | ||
| expect(format('hello {user.name}', { user: {} })).toBe('hello {user.name}') | ||
| expect(warn).toHaveBeenCalledWith('Formatting error: The field user.name is not defined.') | ||
| }) | ||
|
|
||
| test('falls back to the raw template when the root of a nested field is missing', () => { | ||
| const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined) | ||
| expect(format('hello {user.name}', { other: 1 })).toBe('hello {user.name}') | ||
| expect(warn).toHaveBeenCalledWith('Formatting error: The field user.name is not defined.') | ||
| }) | ||
|
|
||
| test('falls back to the raw template when an intermediate value is not an object', () => { | ||
| const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined) | ||
| expect(format('{a.b}', { a: 5 })).toBe('{a.b}') | ||
| expect(warn).toHaveBeenCalledWith('Formatting error: The field a.b is not defined.') | ||
| }) | ||
|
|
||
| test('plain top-level fields keep working', () => { | ||
| expect(format('hello {name}', { name: 'Bob' })).toBe('hello Bob') | ||
| }) | ||
|
|
||
| test('nested traversal does not resolve prototype members', () => { | ||
| const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined) | ||
| expect(format('{user.toString}', { user: {} })).toBe('{user.toString}') | ||
| expect(warn).toHaveBeenCalledWith('Formatting error: The field user.toString is not defined.') | ||
| }) | ||
|
|
||
| test('top-level lookup does not resolve prototype members', () => { | ||
| const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined) | ||
| expect(format('{toString}', { name: 'Bob' })).toBe('{toString}') | ||
| expect(warn).toHaveBeenCalledWith('Formatting error: The field toString is not defined.') | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.