fix: URL encoding parameters not decompiled#2409
Merged
shaohuzhang1 merged 1 commit intomainfrom Feb 26, 2025
Merged
Conversation
shaohuzhang1
commented
Feb 26, 2025
| } | ||
| defineExpose({ checkInputParam }) | ||
| onMounted(() => { | ||
| handleInputFieldList() |
Contributor
Author
There was a problem hiding this comment.
The provided code has some minor issues that can be addressed:
Issues Identified
- Duplicate Code: The
decodeQueryfunction is declared twice, which should not duplicate functionality. - Comment Formatting: The comments need to be formatted consistently and properly.
- Variable Naming: There's a redundant
const _value = route.query[f.field];line that can be removed before the assignment.
Optimization Suggestions
-
Avoid Unnecessary Mapping: If you know the type of array elements (e.g., objects), avoid filtering out
nullvalues when mapping them back.if (_value instanceof Array) { _value = _value.map((item) => decodeURIComponent(item)); // Remove duplicates if they exist // _value = [...new Set(_value)]; } else { _value = decodeURIComponent(_value); }
-
Handle Edge Cases: Ensure that edge cases like empty strings or non-string query parameters are correctly handled during decoding.
Revised Code
Here's the revised version with these suggestions applied:
@@ -286,28 +286,45 @@ const checkInputParam = () => {
for (let f of apiInputFieldList.value) {
if (!api_form_data_context.value[f.field]) {
let _value = route.query[f.field];
if (_value != null) {
if (_value instanceof Array) {
_value = _value.map((item) => decodeURIComponent(item));
api_form_data_context.value[f.field] = [...uniqueValues];
} else {
try {
_value = decodeURIComponent(_value);
} catch (e) {
console.error('Error decoding query parameter:', e);
_value = null;
}
}
} else {
_value = route.query[f.field]
}
api_form_data_context.value[f.field] = _value;
}
if (f.required && !api_form_data_context.value[f.field]) {
msg.push(f.field)
}
}
if (msg.length > 0) {
- MsgWarning(`${t('chat.tip.inputParamMessage1')} ${msg.join('、')}${t('chat.tip.inputParamMessage2')}`)
+ MsgWarning(
+ `${t('chat.tip.inputParamMessage1')} ${msg.join('、')}${t('chat.tip.inputParamMessage2')}`
+ );
return false
}
return true;
}
const decodeQuery = (query: string) => {
let decodedValue = '';
try {
decodedValue = decodeURIComponent(query);
} catch (e) {
console.error('Error decoding URI component', e);
}
return decodedValue;
}
defineExpose({checkInputParam})
onMounted(() => {
handleInputFieldList();
});This revision ensures that the codeDecodeUriComponent() is only called once and also handles potential errors in URL decoding more gracefully. Additionally, it uses []([...array].concat(array)) to remove duplicates from an array.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix: URL encoding parameters not decompiled