This is an automated email from the ASF dual-hosted git repository.

lizhimins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-dashboard.git


The following commit(s) were added to refs/heads/master by this push:
     new a3763bcbf fix(web): preserve original numeric tokens in message body 
formatting (#7301)
a3763bcbf is described below

commit a3763bcbff7c2fca37bb52f0507a55d77dfde5b6
Author: bob <[email protected]>
AuthorDate: Fri Nov 27 11:59:46 2026 +0800

    fix(web): preserve original numeric tokens in message body formatting 
(#7301)
---
 .../__tests__/FormatBodyNumericTokens.test.ts      | 52 ++++++++++++++++++++++
 web/src/pages/instance/message.tsx                 | 20 ++++++++-
 2 files changed, 70 insertions(+), 2 deletions(-)

diff --git a/web/src/pages/instance/__tests__/FormatBodyNumericTokens.test.ts 
b/web/src/pages/instance/__tests__/FormatBodyNumericTokens.test.ts
new file mode 100644
index 000000000..e39b991bf
--- /dev/null
+++ b/web/src/pages/instance/__tests__/FormatBodyNumericTokens.test.ts
@@ -0,0 +1,52 @@
+/*
+ * 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 { formatBody } from '../message';
+
+describe('formatBody numeric token preservation', () => {
+  it('keeps int64 values beyond Number.MAX_SAFE_INTEGER verbatim', () => {
+    const body = '{"orderId":9007199254740993}';
+    expect(formatBody(body)).toBe(body);
+  });
+
+  it('keeps Int64 maximum verbatim', () => {
+    const body = '{"max":9223372036854775807}';
+    expect(formatBody(body)).toBe(body);
+  });
+
+  it('keeps high-precision decimals verbatim', () => {
+    const body = '{"pi":0.1234567890123456789012345}';
+    expect(formatBody(body)).toBe(body);
+  });
+
+  it('still pretty-prints bodies whose numbers round-trip unchanged', () => {
+    expect(formatBody('{"a":42,"b":[1.5,null,true]}')).toBe(
+      '{\n  "a": 42,\n  "b": [\n    1.5,\n    null,\n    true\n  ]\n}'
+    );
+  });
+
+  it('pretty-prints digits inside strings without touching them', () => {
+    const formatted = formatBody('{"note":"id 9007199254740993 here","n":7}');
+    expect(formatted).toContain('"note": "id 9007199254740993 here"');
+    expect(formatted).toContain('\n');
+  });
+
+  it('returns non-JSON bodies unchanged', () => {
+    expect(formatBody('plain text body')).toBe('plain text body');
+  });
+});
diff --git a/web/src/pages/instance/message.tsx 
b/web/src/pages/instance/message.tsx
index bd8957f36..b5d1d8c8e 100644
--- a/web/src/pages/instance/message.tsx
+++ b/web/src/pages/instance/message.tsx
@@ -128,9 +128,25 @@ const getDefaultRange = (): [Dayjs, Dayjs] => 
[dayjs().subtract(2, 'day').startO
 
 /* ─── Helpers ─── */
 
-const formatBody = (body: string): string => {
+const NUMBER_TOKEN_RE = /-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?/g;
+
+const numberTokens = (text: string): string[] => text.match(NUMBER_TOKEN_RE) 
?? [];
+
+// eslint-disable-next-line react-refresh/only-export-components
+export const formatBody = (body: string): string => {
   try {
-    return JSON.stringify(JSON.parse(body), null, 2);
+    const formatted = JSON.stringify(JSON.parse(body), null, 2);
+    // JSON.parse/stringify round-trips every number through an IEEE-754 
double,
+    // which silently rewrites tokens the payload never contained (int64 values
+    // beyond Number.MAX_SAFE_INTEGER, high-precision decimals, exponents).
+    // Keep the pretty-printed form only when every numeric token survives the
+    // round trip byte-for-byte; otherwise show the body verbatim.
+    const before = numberTokens(body);
+    const after = numberTokens(formatted);
+    if (before.length !== after.length || !before.every((token, i) => token 
=== after[i])) {
+      return body;
+    }
+    return formatted;
   } catch {
     return body;
   }

Reply via email to