mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-08-28 14:17:25 +02:00
Auto-fix JSON comments and trailing commas for HTTP request bodies
Add a per-request "Automatically Fix JSON" toggle (default: on) that strips comments and trailing commas before sending HTTP requests. When disabled, comments are sent as-is and the editor linter flags them as errors. - New JsonBodyEditor component with settings dropdown and dismissible banner - Configurable JSON linter (allowComments/allowTrailingCommas) via lintExtension prop - strip_trailing_commas pass in Rust strip_json_comments pipeline - Fix JSON formatter pulling standalone comments onto previous comma line - Add get_bool_map helper, maybe_strip_json_comments, and related tests Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
ed6f9f8bd0
commit
c340a7d5ae
@@ -9,9 +9,9 @@ use std::collections::BTreeMap;
|
||||
use std::pin::Pin;
|
||||
use std::time::Duration;
|
||||
use tokio::io::AsyncRead;
|
||||
use yaak_common::serde::{get_bool, get_str, get_str_map};
|
||||
use yaak_common::serde::{get_bool, get_bool_map, get_str, get_str_map};
|
||||
use yaak_models::models::HttpRequest;
|
||||
use yaak_templates::strip_json_comments::strip_json_comments;
|
||||
use yaak_templates::strip_json_comments::{maybe_strip_json_comments, strip_json_comments};
|
||||
|
||||
pub(crate) const MULTIPART_BOUNDARY: &str = "------YaakFormBoundary";
|
||||
|
||||
@@ -195,7 +195,7 @@ async fn build_body(
|
||||
(build_form_body(&body), Some("application/x-www-form-urlencoded".to_string()))
|
||||
}
|
||||
"multipart/form-data" => build_multipart_body(&body, &headers).await?,
|
||||
_ if body.contains_key("text") => (build_text_body(&body), None),
|
||||
_ if body.contains_key("text") => (build_text_body(&body, body_type), None),
|
||||
t => {
|
||||
warn!("Unsupported body type: {}", t);
|
||||
(None, None)
|
||||
@@ -270,13 +270,20 @@ async fn build_binary_body(
|
||||
}))
|
||||
}
|
||||
|
||||
fn build_text_body(body: &BTreeMap<String, serde_json::Value>) -> Option<SendableBodyWithMeta> {
|
||||
fn build_text_body(body: &BTreeMap<String, serde_json::Value>, body_type: &str) -> Option<SendableBodyWithMeta> {
|
||||
let text = get_str_map(body, "text");
|
||||
if text.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(SendableBodyWithMeta::Bytes(Bytes::from(text.to_string())))
|
||||
return None;
|
||||
}
|
||||
|
||||
let send_comments = get_bool_map(body, "sendJsonComments", false);
|
||||
let text = if !send_comments && body_type == "application/json" {
|
||||
maybe_strip_json_comments(text)
|
||||
} else {
|
||||
text.to_string()
|
||||
};
|
||||
|
||||
Some(SendableBodyWithMeta::Bytes(Bytes::from(text)))
|
||||
}
|
||||
|
||||
fn build_graphql_body(
|
||||
@@ -702,7 +709,7 @@ mod tests {
|
||||
let mut body = BTreeMap::new();
|
||||
body.insert("text".to_string(), json!("Hello, World!"));
|
||||
|
||||
let result = build_text_body(&body);
|
||||
let result = build_text_body(&body, "application/json");
|
||||
match result {
|
||||
Some(SendableBodyWithMeta::Bytes(bytes)) => {
|
||||
assert_eq!(bytes, Bytes::from("Hello, World!"))
|
||||
@@ -716,7 +723,7 @@ mod tests {
|
||||
let mut body = BTreeMap::new();
|
||||
body.insert("text".to_string(), json!(""));
|
||||
|
||||
let result = build_text_body(&body);
|
||||
let result = build_text_body(&body, "application/json");
|
||||
assert!(result.is_none());
|
||||
}
|
||||
|
||||
@@ -724,10 +731,57 @@ mod tests {
|
||||
async fn test_text_body_missing() {
|
||||
let body = BTreeMap::new();
|
||||
|
||||
let result = build_text_body(&body);
|
||||
let result = build_text_body(&body, "application/json");
|
||||
assert!(result.is_none());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_text_body_strips_json_comments_by_default() {
|
||||
let mut body = BTreeMap::new();
|
||||
body.insert("text".to_string(), json!("{\n // comment\n \"foo\": \"bar\"\n}"));
|
||||
|
||||
let result = build_text_body(&body, "application/json");
|
||||
match result {
|
||||
Some(SendableBodyWithMeta::Bytes(bytes)) => {
|
||||
let text = String::from_utf8_lossy(&bytes);
|
||||
assert!(!text.contains("// comment"));
|
||||
assert!(text.contains("\"foo\": \"bar\""));
|
||||
}
|
||||
_ => panic!("Expected Some(SendableBody::Bytes)"),
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_text_body_send_json_comments_when_opted_in() {
|
||||
let mut body = BTreeMap::new();
|
||||
body.insert("text".to_string(), json!("{\n // comment\n \"foo\": \"bar\"\n}"));
|
||||
body.insert("sendJsonComments".to_string(), json!(true));
|
||||
|
||||
let result = build_text_body(&body, "application/json");
|
||||
match result {
|
||||
Some(SendableBodyWithMeta::Bytes(bytes)) => {
|
||||
let text = String::from_utf8_lossy(&bytes);
|
||||
assert!(text.contains("// comment"));
|
||||
}
|
||||
_ => panic!("Expected Some(SendableBody::Bytes)"),
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_text_body_no_strip_for_non_json() {
|
||||
let mut body = BTreeMap::new();
|
||||
body.insert("text".to_string(), json!("// not json\nsome text"));
|
||||
|
||||
let result = build_text_body(&body, "text/plain");
|
||||
match result {
|
||||
Some(SendableBodyWithMeta::Bytes(bytes)) => {
|
||||
let text = String::from_utf8_lossy(&bytes);
|
||||
assert!(text.contains("// not json"));
|
||||
}
|
||||
_ => panic!("Expected Some(SendableBody::Bytes)"),
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_form_urlencoded_body() -> Result<()> {
|
||||
let mut body = BTreeMap::new();
|
||||
|
||||
Reference in New Issue
Block a user