fix(charts): prevent TypeError crash when streaming nullish data#651
Open
serhiizghama wants to merge 2 commits into
Open
fix(charts): prevent TypeError crash when streaming nullish data#651serhiizghama wants to merge 2 commits into
serhiizghama wants to merge 2 commits into
Conversation
Add normalizeChartData() to coerce a chart's data prop to a stable array, and make getDataKeys() null-safe. During streaming, data can be null/undefined before the full payload arrives, which previously threw inside the chart internals.
Every public chart component read the data prop directly (getDataKeys, data.length, [...data]) assuming it was always an array. While generative UI streams, data can momentarily be null/undefined, crashing the chart with 'Cannot read properties of null'. Normalize data at each component entry so charts render an empty state instead of throwing.
Contributor
|
Thanks for the PR. We’re going to address this at the genui-lib streaming/parser layer rather than in each chart component. Please keep this PR open for now while we implement and verify that approach. Once the replacement is confirmed, we’ll close this PR as superseded. We’ll track the work in #355. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
Closes #355.
Every public chart component (
BarChart,BarChartCondensed,LineChart,AreaChart,PieChart,RadarChart,ScatterChart, …) reads itsdataprop directly —getDataKeys(data, …),data.length,[...data].sort()— assumingdatais always an array.While generative UI is streaming,
datacan momentarily benull/undefinedbefore the full payload has been parsed. Passing that into the chart internals throws:This crashes the whole render mid-stream.
Solution
Add a small shared helper
normalizeChartData()inCharts/utils/dataUtils.tsthat coercesdatato an array, and call it once at the top of each chart component so the rest of the component always works with a real array (an empty chart instead of a crash).Two details that make it safe:
datais nullish it falls back to a single sharedEMPTY_CHART_DATAconstant, so normalization never creates a new[]on every render (which would change identity and defeat the existinguseMemo/React.memooptimizations). Whendatais a valid array it is returned by reference, unchanged.getDataKeys()is also made null-safe (data?.[0]) as defense-in-depth, since it is the shared first access point.The
genui-libschema layer already guards its own path viahasAllProps/buildChartData; this PR hardens the public exported chart components themselves, which are part of the package surface and can be rendered directly by consumers (and during streaming).This matches the approach agreed on in the issue thread (
Array.isArray(data) ? data : [], applied consistently across all chart types).Testing
pnpm --filter @openuidev/react-ui typecheck— passespnpm --filter @openuidev/react-ui ci(lint:check+format:check) — passesnormalizeChartData(null)/(undefined)return the same stable empty array, valid arrays pass through by reference, andgetDataKeys(null)returns[]instead of throwing.