Copilot commented on code in PR #697:
URL: https://github.com/apache/shenyu-dashboard/pull/697#discussion_r4229404097


##########
src/routes/Plugin/McpServer/ToolsModal.js:
##########
@@ -551,7 +713,67 @@ class AddModal extends Component {
         requestConfig: parsedJson.requestConfig || "{}",
       };
 
-      // Convert flattened data back to original format and add required 
rule-level fields
+      const currentFormValues =
+        form && form.getFieldsValue ? form.getFieldsValue() : {};
+
+      const rawSort =
+        parsedJson.sort !== undefined
+          ? parsedJson.sort
+          : currentFormValues.sort !== undefined
+            ? currentFormValues.sort
+            : formRuleValues.sort !== undefined
+              ? formRuleValues.sort
+              : propsSort;
+      const parsedSort = parseInt(rawSort, 10);
+      const finalSort = !isNaN(parsedSort) ? parsedSort : 1;
+
+      const finalLoged =
+        parsedJson.loged !== undefined
+          ? parsedJson.loged
+          : currentFormValues.loged !== undefined
+            ? currentFormValues.loged
+            : formRuleValues.loged !== undefined
+              ? formRuleValues.loged
+              : propsLoged;
+
+      const rawMatchMode =
+        parsedJson.matchMode !== undefined
+          ? parsedJson.matchMode
+          : currentFormValues.matchMode !== undefined
+            ? currentFormValues.matchMode
+            : formRuleValues.matchMode !== undefined
+              ? formRuleValues.matchMode
+              : propsMatchMode;
+      const finalMatchMode =
+        rawMatchMode !== undefined ? String(rawMatchMode) : "0";
+
+      const finalMatchRestful =
+        parsedJson.matchRestful !== undefined
+          ? parsedJson.matchRestful
+          : currentFormValues.matchRestful !== undefined
+            ? currentFormValues.matchRestful
+            : formRuleValues.matchRestful !== undefined
+              ? formRuleValues.matchRestful
+              : propsMatchRestful;

Review Comment:
   Rule-field resolution logic (source precedence + parsing/coercion) is now 
spread across multiple methods (`syncFormToJson`, `syncJsonToForm`, 
`syncJsonToFormRealtime`, `handleJsonSubmit`) with very similar patterns. This 
increases the chance of future inconsistencies (e.g., one path stringifies 
`matchMode` while another forgets, or sort parsing behavior diverges). Consider 
extracting a small helper (e.g., `resolveRuleFields({ parsedJson, formValues, 
stateRuleValues, propsDefaults })`) that returns `{ sort, loged, matchMode, 
matchRestful, ruleConditions }` and reuse it across these methods to keep 
precedence and coercion consistent in one place.`



##########
src/routes/Plugin/McpServer/ToolsModal.js:
##########
@@ -551,7 +713,67 @@ class AddModal extends Component {
         requestConfig: parsedJson.requestConfig || "{}",
       };
 
-      // Convert flattened data back to original format and add required 
rule-level fields
+      const currentFormValues =
+        form && form.getFieldsValue ? form.getFieldsValue() : {};
+
+      const rawSort =
+        parsedJson.sort !== undefined
+          ? parsedJson.sort
+          : currentFormValues.sort !== undefined
+            ? currentFormValues.sort
+            : formRuleValues.sort !== undefined
+              ? formRuleValues.sort
+              : propsSort;
+      const parsedSort = parseInt(rawSort, 10);
+      const finalSort = !isNaN(parsedSort) ? parsedSort : 1;
+
+      const finalLoged =
+        parsedJson.loged !== undefined
+          ? parsedJson.loged
+          : currentFormValues.loged !== undefined
+            ? currentFormValues.loged
+            : formRuleValues.loged !== undefined
+              ? formRuleValues.loged
+              : propsLoged;
+
+      const rawMatchMode =
+        parsedJson.matchMode !== undefined
+          ? parsedJson.matchMode
+          : currentFormValues.matchMode !== undefined
+            ? currentFormValues.matchMode
+            : formRuleValues.matchMode !== undefined
+              ? formRuleValues.matchMode
+              : propsMatchMode;
+      const finalMatchMode =
+        rawMatchMode !== undefined ? String(rawMatchMode) : "0";
+
+      const finalMatchRestful =
+        parsedJson.matchRestful !== undefined
+          ? parsedJson.matchRestful
+          : currentFormValues.matchRestful !== undefined
+            ? currentFormValues.matchRestful
+            : formRuleValues.matchRestful !== undefined
+              ? formRuleValues.matchRestful
+              : propsMatchRestful;
+
+      const finalRuleConditions =
+        parsedJson.ruleConditions && parsedJson.ruleConditions.length > 0
+          ? parsedJson.ruleConditions
+          : formRuleValues.ruleConditions &&
+              formRuleValues.ruleConditions.length > 0
+            ? formRuleValues.ruleConditions
+            : propsRuleConditions && propsRuleConditions.length > 0
+              ? propsRuleConditions
+              : [
+                  {
+                    paramType: "uri",
+                    operator: "pathPattern",
+                    paramName: "/",
+                    paramValue: "/**",
+                  },
+                ];

Review Comment:
   ruleConditions` cannot be intentionally cleared via JSON editor: if a user 
sets `"ruleConditions": []`, this logic treats it as “missing” and falls back 
to previous/default conditions. If an empty array is a valid/meaningful value, 
switch the checks to distinguish between `undefined` (missing) vs. an 
explicitly provided array (including empty). For example, prefer checking 
`parsedJson.ruleConditions !== undefined` rather than `length > 0` when 
deciding whether to use the JSON-provided value.



-- 
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]

Reply via email to