Skip to content

[SDK-433] JSON parse error when config contains unexpected type#858

Open
lposen wants to merge 2 commits intoemb-ootb/masterfrom
loren/embedded/SDK-433-json-parser-crashes-with-incorrect-configs
Open

[SDK-433] JSON parse error when config contains unexpected type#858
lposen wants to merge 2 commits intoemb-ootb/masterfrom
loren/embedded/SDK-433-json-parser-crashes-with-incorrect-configs

Conversation

@lposen
Copy link
Copy Markdown
Contributor

@lposen lposen commented Apr 22, 2026

🔹 JIRA Ticket(s) if any

✏️ Description

Fix error when config contains unexpected type

Testing

  • Run app with embedded
  • Go to embedded tab
  • Click "Get Messages"
  • Click "Set View Config"
  • Add JSON with incorrect type: EG: {"borderWidth":"45"}
  • The app should not crash

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 22, 2026

Lines Statements Branches Functions
Coverage: 68%
68.83% (519/754) 56.11% (202/360) 64.82% (164/253)

@qltysh
Copy link
Copy Markdown

qltysh Bot commented Apr 22, 2026

Qlty


Coverage Impact

⬆️ Merging this pull request will increase total coverage on emb-ootb/master by 1.36%.

Modified Files with Diff Coverage (2)

RatingFile% DiffUncovered Line #s
Coverage rating: A Coverage rating: A
src/embedded/hooks/useEmbeddedView/useEmbeddedView.ts100.0%
New Coverage rating: A
src/embedded/utils/normalizeEmbeddedViewConfig.ts94.3%13, 17
Total94.7%
🤖 Increase coverage with AI coding...
In the `loren/embedded/SDK-433-json-parser-crashes-with-incorrect-configs` branch, add test coverage for this new code:

- `src/embedded/utils/normalizeEmbeddedViewConfig.ts` -- Lines 13 and 17

🚦 See full report on Qlty Cloud »

🛟 Help
  • Diff Coverage: Coverage for added or modified lines of code (excludes deleted files). Learn more.

  • Total Coverage: Coverage for the whole repository, calculated as the sum of all File Coverage. Learn more.

  • File Coverage: Covered Lines divided by Covered Lines plus Missed Lines. (Excludes non-executable lines including blank lines and comments.)

    • Indirect Changes: Changes to File Coverage for files that were not modified in this PR. Learn more.

@qltysh
Copy link
Copy Markdown

qltysh Bot commented Apr 22, 2026

3 new issues

Tool Category Rule Count
qlty Structure Function with high complexity (count = 10): coerceNumericField 2
qlty Structure Function with many returns (count = 7): coerceNumericField 1

console.warn(
`[IterableEmbeddedView] Ignoring ${String(key)}: expected number or numeric string, got ${typeof value}`
);
return undefined;
Copy link
Copy Markdown

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]

next[key] = coerced;
}
}
return next;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Function with high complexity (count = 12): normalizeEmbeddedViewConfig [qlty:function-complexity]

@lposen lposen requested a review from Copilot April 24, 2026 19:31
Copy link
Copy Markdown
Contributor

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

Fixes crashes/JS errors when embedded view config contains unexpected types by normalizing numeric style fields before style resolution.

Changes:

  • Added normalizeEmbeddedViewConfig utility to coerce numeric config fields (e.g., borderWidth, borderCornerRadius) from strings and drop invalid values with warnings.
  • Added Jest coverage for normalization behavior (coercion, warnings, non-mutation).
  • Updated useEmbeddedView to normalize config before calling getStyles.

Reviewed changes

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

File Description
src/embedded/utils/normalizeEmbeddedViewConfig.ts Introduces config normalization/coercion for numeric style fields to prevent invalid types from reaching style resolution.
src/embedded/utils/normalizeEmbeddedViewConfig.test.ts Adds unit tests validating coercion, dropping invalid values, warning behavior, and non-mutation.
src/embedded/hooks/useEmbeddedView/useEmbeddedView.ts Wires normalization into the embedded view hook so getStyles receives sanitized config.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +26 to +41
// 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);

Copy link

Copilot AI Apr 24, 2026

Choose a reason for hiding this comment

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

The tests repeatedly cast inputs to never to bypass type checking. That makes the intent harder to follow and can hide mistakes in the test setup; prefer a more explicit cast like unknown as IterableEmbeddedViewConfig (or a small helper type) for invalid-shape inputs.

Copilot uses AI. Check for mistakes.
Comment on lines +32 to +35
const n = parseFloat(trimmed);
if (Number.isFinite(n)) {
return n;
}
Copy link

Copilot AI Apr 24, 2026

Choose a reason for hiding this comment

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

parseFloat() is permissive (e.g., it will accept values like "12px" or "10abc" and return a finite number), which means malformed numeric strings won’t trigger the warning and will be applied as styles. Consider using a stricter conversion (e.g., Number(trimmed) with a full-string numeric check) so only truly numeric strings are coerced and everything else falls back to defaults.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I thought the same as the warning above so that parseFloat("3abc") === 3 but I guess we can live with assuming people will always type 3px instead.

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.

3 participants