Copilot commented on code in PR #1527: URL: https://github.com/apache/dubbo-admin/pull/1527#discussion_r3817915590
########## ui-vue3/src/utils/JsonUtil.ts: ########## @@ -0,0 +1,24 @@ +/* + * 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 { isSafeNumber, parse } from 'lossless-json' + +export function parseJsonWithSafeNumbers<T>(text: string): T { + return parse(text, undefined, { + parseNumber: (value) => (isSafeNumber(value) ? parseFloat(value) : value) + }) as T +} Review Comment: `parseJsonWithSafeNumbers` is typed as returning generic `T`, but it can change the JSON value types (unsafe numbers become strings). This makes the function’s TypeScript signature unsound and can mislead callers into treating an unsafe integer as a `number`. Consider returning `unknown` (or a JSON-value union) and letting call sites narrow/cast explicitly. ########## ui-vue3/src/utils/JsonUtil.spec.ts: ########## @@ -0,0 +1,36 @@ +/* + * 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 { describe, expect, it } from 'vitest' +import { parseJsonWithSafeNumbers } from './JsonUtil' + +describe('parseJsonWithSafeNumbers', () => { + it('preserves integers outside the JavaScript safe range', () => { + const args = parseJsonWithSafeNumbers<unknown[]>('[1694620889412087810]') Review Comment: This test currently relies on the generic return type from `parseJsonWithSafeNumbers`. If the helper is changed to return `unknown` (so it can’t promise numeric fields stay numeric), cast locally in the test instead of encoding a potentially incorrect type in the helper’s signature. This issue also appears on line 30 of the same file. ########## ui-vue3/src/views/resources/services/tabs/debug.vue: ########## @@ -555,9 +556,11 @@ async function handleInvoke() { } let args: any[] try { - args = JSON.parse(requestValue.value) - if (!Array.isArray(args)) { - args = [args] + const parsedArgs = parseJsonWithSafeNumbers<unknown>(requestValue.value) + if (Array.isArray(parsedArgs)) { + args = parsedArgs + } else { + args = [parsedArgs] } Review Comment: If `parseJsonWithSafeNumbers` is updated to return `unknown` (so its signature matches runtime behavior), this call site should drop the generic type argument and rely on the runtime `Array.isArray` guard + the `any[]` assignment. -- 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]
