-
Notifications
You must be signed in to change notification settings - Fork 44
[SDK-433] JSON parse error when config contains unexpected type #858
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
lposen
wants to merge
1
commit into
emb-ootb/master
Choose a base branch
from
loren/embedded/SDK-433-json-parser-crashes-with-incorrect-configs
base: emb-ootb/master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+189
−2
Open
Changes from all commits
Commits
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
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,106 @@ | ||
| import { normalizeEmbeddedViewConfig } from './normalizeEmbeddedViewConfig'; | ||
|
|
||
| describe('normalizeEmbeddedViewConfig', () => { | ||
| let warnSpy: jest.SpyInstance; | ||
|
|
||
| beforeEach(() => { | ||
| warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| warnSpy.mockRestore(); | ||
| }); | ||
|
|
||
| it('returns null or undefined unchanged', () => { | ||
| expect(normalizeEmbeddedViewConfig(null)).toBeNull(); | ||
| expect(normalizeEmbeddedViewConfig(undefined)).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('parses numeric strings for borderWidth and borderCornerRadius', () => { | ||
| const input = { | ||
| borderWidth: '45', | ||
| borderCornerRadius: '12.5', | ||
| backgroundColor: '#fff', | ||
| }; | ||
|
|
||
| // Runtime JSON / native payloads may use strings for numeric fields. | ||
| const result = normalizeEmbeddedViewConfig(input as never); | ||
|
|
||
| expect(result).toEqual({ | ||
| borderWidth: 45, | ||
| borderCornerRadius: 12.5, | ||
| backgroundColor: '#fff', | ||
| }); | ||
| expect(warnSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('trims whitespace before parsing numeric strings', () => { | ||
| const result = normalizeEmbeddedViewConfig({ | ||
| borderWidth: ' 8 ', | ||
| } as never); | ||
|
|
||
| expect(result?.borderWidth).toBe(8); | ||
| expect(warnSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('leaves valid numbers unchanged', () => { | ||
| const result = normalizeEmbeddedViewConfig({ | ||
| borderWidth: 3, | ||
| borderCornerRadius: 0, | ||
| }); | ||
|
|
||
| expect(result?.borderWidth).toBe(3); | ||
| expect(result?.borderCornerRadius).toBe(0); | ||
| expect(warnSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('drops non-parsable strings and warns', () => { | ||
| const result = normalizeEmbeddedViewConfig({ | ||
| borderWidth: 'nope', | ||
| borderCornerRadius: '10', | ||
| } as never); | ||
|
|
||
| expect(result?.borderWidth).toBeUndefined(); | ||
| expect(result?.borderCornerRadius).toBe(10); | ||
| expect(warnSpy).toHaveBeenCalledTimes(1); | ||
| expect(warnSpy.mock.calls[0][0]).toContain('borderWidth'); | ||
| }); | ||
|
|
||
| it('drops empty strings and warns', () => { | ||
| const result = normalizeEmbeddedViewConfig({ | ||
| borderWidth: ' ', | ||
| } as never); | ||
|
|
||
| expect(result?.borderWidth).toBeUndefined(); | ||
| expect(warnSpy).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('drops NaN and Infinity numbers and warns', () => { | ||
| const result = normalizeEmbeddedViewConfig({ | ||
| borderWidth: Number.NaN, | ||
| borderCornerRadius: Number.POSITIVE_INFINITY, | ||
| } as never); | ||
|
|
||
| expect(result?.borderWidth).toBeUndefined(); | ||
| expect(result?.borderCornerRadius).toBeUndefined(); | ||
| expect(warnSpy).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it('drops invalid types and warns', () => { | ||
| const result = normalizeEmbeddedViewConfig({ | ||
| borderWidth: true, | ||
| } as never); | ||
|
|
||
| expect(result?.borderWidth).toBeUndefined(); | ||
| expect(warnSpy).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('does not mutate the original config object', () => { | ||
| const original = { borderWidth: '7' as const }; | ||
| const snapshot = { ...original }; | ||
|
|
||
| normalizeEmbeddedViewConfig(original as never); | ||
|
|
||
| expect(original).toEqual(snapshot); | ||
| }); | ||
| }); |
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,75 @@ | ||
| import type { IterableEmbeddedViewConfig } from '../types/IterableEmbeddedViewConfig'; | ||
|
|
||
| const NUMERIC_KEYS: (keyof Pick< | ||
| IterableEmbeddedViewConfig, | ||
| 'borderWidth' | 'borderCornerRadius' | ||
| >)[] = ['borderWidth', 'borderCornerRadius']; | ||
|
|
||
| function coerceNumericField( | ||
| key: 'borderWidth' | 'borderCornerRadius', | ||
| value: unknown | ||
| ): number | undefined { | ||
| if (value === undefined || value === null) { | ||
| return undefined; | ||
| } | ||
| if (typeof value === 'number') { | ||
| if (Number.isFinite(value)) { | ||
| return value; | ||
| } | ||
| console.warn( | ||
| `[IterableEmbeddedView] Ignoring ${String(key)}: expected a finite number, got ${String(value)}` | ||
| ); | ||
| return undefined; | ||
| } | ||
| if (typeof value === 'string') { | ||
| const trimmed = value.trim(); | ||
| if (trimmed === '') { | ||
| console.warn( | ||
| `[IterableEmbeddedView] Ignoring ${String(key)}: empty string is not a valid number` | ||
| ); | ||
| return undefined; | ||
| } | ||
| const n = parseFloat(trimmed); | ||
| if (Number.isFinite(n)) { | ||
| return n; | ||
| } | ||
| console.warn( | ||
| `[IterableEmbeddedView] Ignoring ${String(key)}: could not parse string as a number: ${JSON.stringify(value)}` | ||
| ); | ||
| return undefined; | ||
| } | ||
| console.warn( | ||
| `[IterableEmbeddedView] Ignoring ${String(key)}: expected number or numeric string, got ${typeof value}` | ||
| ); | ||
| return undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Returns a shallow copy of config with numeric fields coerced from strings when possible. | ||
| * Values that cannot be coerced are omitted so style resolution can fall back to defaults. | ||
| */ | ||
| export function normalizeEmbeddedViewConfig( | ||
| config: IterableEmbeddedViewConfig | null | undefined | ||
| ): IterableEmbeddedViewConfig | null | undefined { | ||
| if (config == null) { | ||
| return config; | ||
| } | ||
| const next: IterableEmbeddedViewConfig = { ...config }; | ||
| const loose = config as Record<string, unknown>; | ||
| for (const key of NUMERIC_KEYS) { | ||
| const raw = loose[key as string]; | ||
| if (raw === undefined) { | ||
| continue; | ||
| } | ||
| if (typeof raw === 'number' && Number.isFinite(raw)) { | ||
| continue; | ||
| } | ||
| const coerced = coerceNumericField(key, raw); | ||
| if (coerced === undefined) { | ||
| delete next[key]; | ||
| } else { | ||
| next[key] = coerced; | ||
| } | ||
| } | ||
| return next; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found 2 issues:
1. Function with many returns (count = 7): coerceNumericField [qlty:return-statements]
2. Function with high complexity (count = 10): coerceNumericField [qlty:function-complexity]