codeant-ai-for-open-source[bot] commented on code in PR #24444: URL: https://github.com/apache/superset/pull/24444#discussion_r3616138056
########## superset-frontend/packages/superset-ui-core/src/number-format/factories/createLengthFormatter.ts: ########## @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import NumberFormatter from '../NumberFormatter'; + +export type LengthConvertType = 'm => km' | 'cm => km' | 'cm => m'; + +export default function createLengthFormatter( + config: { + description?: string; + id?: string; + label?: string; + convertType?: LengthConvertType; + } = {}, +) { + const { description, id, label, convertType } = config; + + return new NumberFormatter({ + description, + formatFunc: (value: number) => { + if (convertType === 'm => km') { + return `${(value / 1000).toFixed(2)}km`; + } + if (convertType === 'cm => km') { + return `${(value / 100_000).toFixed(2)}km`; + } + if (convertType === 'cm => m') { + return `${(value / 100).toFixed(2)}m`; + } + return value.toString(); Review Comment: **Suggestion:** The fallback branch calls `.toString()` directly, which will throw at runtime when chart/table values are nullish (a common case in Superset metric results). Add a null/undefined-safe conversion path before stringifying so missing values do not crash formatting. [null pointer] <details> <summary><b>Severity Level:</b> Major ⚠️</summary> ```mdx - ❌ Length formatter crashes when value is null/undefined. - ⚠️ Charts/tables with missing lengths fail to render. ``` </details> <details> <summary><b>Steps of Reproduction ✅ </b></summary> ```mdx 1. Use the new length formatter by importing `createLengthFormatter` from `superset-frontend/packages/superset-ui-core/src/number-format/factories/createLengthFormatter.ts:24` and calling it with `convertType` omitted or an unrecognized value, so the fallback branch is used. 2. In Superset's number-format pipeline, pass a metric value that is `null` or `undefined` into the resulting `NumberFormatter` instance, which invokes the `formatFunc` defined at `createLengthFormatter.ts:36`. 3. Because `convertType` does not match any of the three explicit branches (`'m => km'`, `'cm => km'`, `'cm => m'` at lines 37–44), execution falls through to the fallback `return value.toString();` at `createLengthFormatter.ts:46`. 4. At runtime, JavaScript evaluates `null.toString()` / `undefined.toString()`, which throws a `TypeError` and causes the formatting operation (and thus the chart/table rendering using this formatter) to fail. ``` </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=9049a41340784f50b8474019d7fecd89&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=9049a41340784f50b8474019d7fecd89&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <details> <summary><b>Prompt for AI Agent 🤖 </b></summary> ```mdx This is a comment left during a code review. **Path:** superset-frontend/packages/superset-ui-core/src/number-format/factories/createLengthFormatter.ts **Line:** 46:46 **Comment:** *Null Pointer: The fallback branch calls `.toString()` directly, which will throw at runtime when chart/table values are nullish (a common case in Superset metric results). Add a null/undefined-safe conversion path before stringifying so missing values do not crash formatting. Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F24444&comment_hash=4c16486796f91955ad1cefdc64d4f55702a908b3629b11e81509e3f2136f08a4&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F24444&comment_hash=4c16486796f91955ad1cefdc64d4f55702a908b3629b11e81509e3f2136f08a4&reaction=dislike'>👎</a> -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
