Skip to content

fix: serialize page_content to resolve mockserver create page error#1780

Merged
chilingling merged 1 commit intoopentiny:developfrom
hexqi:fix/mockserver-create-page
Mar 6, 2026
Merged

fix: serialize page_content to resolve mockserver create page error#1780
chilingling merged 1 commit intoopentiny:developfrom
hexqi:fix/mockserver-create-page

Conversation

@hexqi
Copy link
Collaborator

@hexqi hexqi commented Mar 5, 2026

English | 简体中文

PR

修复 mockServer 创建页面时因字段包含点号(.)导致报错的问题

背景与问题: 在调用 mockServer 的 /app-center/api/pages/create 接口创建页面时,若传入的 page_content(例如 css 的 class 名)包含 .(点号),底层依赖的 @seald-io/nedb 会因为 键名规范抛出 "Field names cannot contain a ." 错误,从而导致创建失败。

修复方案: 对mockserver pages相关接口page_content进行序列化
Create/Update:将对象的 page_content 字段通过 JSON.stringify 序列化为字符串
List/Detail:将 page_content 字符串通过 JSON.parse 重新反序列化为对象

PR Checklist

Please check if your PR fulfills the following requirements:

  • The commit message follows our Commit Message Guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)
  • Built its own designer, fully self-validated

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

Background and solution

What is the current behavior?

Issue Number: N/A

What is the new behavior?

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

@github-actions github-actions bot added the bug Something isn't working label Mar 5, 2026
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 5, 2026

Walkthrough

The page service now includes a helper function to safely parse and normalize page_content across all CRUD operations. When creating or updating pages with object-type content, it stringifies before storage. When retrieving pages, it parses JSON strings back to objects, ensuring consistent data representation throughout the lifecycle.

Changes

Cohort / File(s) Summary
Page Content Serialization
mockServer/src/services/pages.js
Introduces parsePageContent helper function to normalize JSON serialization/deserialization. Applied to create, update, list, detail, and delete operations; all responses pass through this helper before returning to normalize page_content fields.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

A helper born to parse and mend, 🐰
Where JSON strings and objects blend,
Through create, update, list, and more,
Content flows consistent, pure, and sure.

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: automatically serializing page_content to JSON string to fix a mockserver error.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@hexqi hexqi changed the title fix: Automatically serialize page_content to JSON string to resolve… fix: serialize page_content to resolve mockserver create page error Mar 5, 2026
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (2)
mockServer/src/services/pages.js (2)

20-22: Don’t silently ignore JSON parse errors.

Line 20-Line 22 suppresses parse failures completely. Keeping fallback behavior is fine, but add at least a warning log (with page id/route when available) to preserve debuggability.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@mockServer/src/services/pages.js` around lines 20 - 22, The catch block in
mockServer/src/services/pages.js currently swallows JSON parse errors (the catch
around the JSON parsing logic) — update it to log a warning instead of ignoring:
in the catch for the JSON.parse/parse attempt inside the function handling page
data (refer to the try/catch surrounding the parse in pages.js), call the logger
(or console.warn if no logger exists) with a clear message that includes the
page id/route variables available in that scope and the caught error object,
then continue with the existing fallback behavior; ensure the log message
includes both context (page id/route) and the error to aid debugging.

83-85: Consider explicit error handling for JSON.stringify to improve observability.

Lines 84 and 97 serialize request data without explicit error handling. While a global error middleware catches exceptions, adding try-catch at the point of serialization would improve code clarity and enable better error context (e.g., distinguishing serialization failures from other errors). Non-serializable payloads like circular references will propagate to clients as generic errors without this clarity.

Suggested improvement
+const serializePageContent = (value) => {
+  if (!value || typeof value !== 'object') {
+    return value
+  }
+  try {
+    return JSON.stringify(value)
+  } catch (error) {
+    error.message = `Invalid page_content: ${error.message}`
+    throw error
+  }
+}
+
 async create(params) {
   const model = params.isPage ? this.pageModel : this.folderModel
   const pageData = { ...model, ...params }
-
-  if (pageData.page_content && typeof pageData.page_content === 'object') {
-    pageData.page_content = JSON.stringify(pageData.page_content)
-  }
+  pageData.page_content = serializePageContent(pageData.page_content)
   ...
 }
 
 async update(id, params) {
   const updateData = { ...params }
-  if (updateData.page_content && typeof updateData.page_content === 'object') {
-    updateData.page_content = JSON.stringify(updateData.page_content)
-  }
+  updateData.page_content = serializePageContent(updateData.page_content)
   ...
 }

Also applies to: 96-98

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@mockServer/src/services/pages.js` around lines 83 - 85, Wrap each
JSON.stringify call in pages.js (the one serializing pageData.page_content and
the other at lines around 96-98) in a try-catch: on failure catch the error, log
it with contextual info (e.g., include pageData id or request body and the fact
serialization failed), and then propagate a clear, specific error to upstream
middleware (e.g., call next with a new Error/TypeError that includes the
original error message or rethrow an error with that context) so serialization
failures are distinguishable from other errors.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@mockServer/src/services/pages.js`:
- Around line 20-22: The catch block in mockServer/src/services/pages.js
currently swallows JSON parse errors (the catch around the JSON parsing logic) —
update it to log a warning instead of ignoring: in the catch for the
JSON.parse/parse attempt inside the function handling page data (refer to the
try/catch surrounding the parse in pages.js), call the logger (or console.warn
if no logger exists) with a clear message that includes the page id/route
variables available in that scope and the caught error object, then continue
with the existing fallback behavior; ensure the log message includes both
context (page id/route) and the error to aid debugging.
- Around line 83-85: Wrap each JSON.stringify call in pages.js (the one
serializing pageData.page_content and the other at lines around 96-98) in a
try-catch: on failure catch the error, log it with contextual info (e.g.,
include pageData id or request body and the fact serialization failed), and then
propagate a clear, specific error to upstream middleware (e.g., call next with a
new Error/TypeError that includes the original error message or rethrow an error
with that context) so serialization failures are distinguishable from other
errors.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 198ec8c1-8a42-4ee9-921b-865ca7f9c5cf

📥 Commits

Reviewing files that changed from the base of the PR and between 8caa5aa and a9f6985.

📒 Files selected for processing (1)
  • mockServer/src/services/pages.js

@chilingling chilingling merged commit 8319cbf into opentiny:develop Mar 6, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants