Skip to content

Commit 8e13852

Browse files
update_schema instead of merge_schema
1 parent 2b23a85 commit 8e13852

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

src/template.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl<'a> Template<'a> {
7474
}
7575
};
7676

77-
merge_schema(&mut default_schema, &schema);
77+
update_schema(&mut default_schema, &schema);
7878
let shared = Shared::new(default_schema.clone());
7979

8080
Ok(Template {
@@ -144,7 +144,7 @@ impl<'a> Template<'a> {
144144
return Err("Is not a valid JSON file".to_string());
145145
}
146146
};
147-
merge_schema(&mut self.schema, &schema_value);
147+
update_schema(&mut self.schema, &schema_value);
148148

149149
Ok(())
150150
}
@@ -166,7 +166,7 @@ impl<'a> Template<'a> {
166166
return Err("Is not a valid JSON string".to_string());
167167
}
168168
};
169-
merge_schema(&mut self.schema, &schema_value);
169+
update_schema(&mut self.schema, &schema_value);
170170

171171
Ok(())
172172
}
@@ -177,7 +177,7 @@ impl<'a> Template<'a> {
177177
///
178178
/// * `schema` - The JSON Value to be merged with the current schema.
179179
pub fn merge_schema_value(&mut self, schema: Value) {
180-
merge_schema(&mut self.schema, &schema);
180+
update_schema(&mut self.schema, &schema);
181181
}
182182

183183
/// Renders the template content.

src/utils.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,41 @@ pub fn merge_schema(a: &mut Value, b: &Value) {
5050
}
5151
}
5252

53+
/// Merge schema and update some keys
54+
///
55+
/// This is a thin wrapper around `merge_schema` that additionally:
56+
/// 1. Copies the value of the header key `requested-with-ajax` (all lower-case) into the
57+
/// variants `Requested-With-Ajax` (Pascal-Case) and `REQUESTED-WITH-AJAX` (upper-case),
58+
/// or vice-versa, depending on which variant is present in the incoming schema.
59+
/// 2. Overwrites the top-level `version` field with the compile-time constant `VERSION`.
60+
///
61+
/// The three header variants are created so that downstream code can read the header
62+
/// regardless of the casing rules enforced by the environment (HTTP servers, proxies, etc.).
63+
///
64+
/// # Arguments
65+
/// * `a` – the target `Value` (must be an `Object`) that will receive the merge result.
66+
/// * `b` – the source `Value` (must be an `Object`) whose contents are merged into `a`.
67+
///
68+
pub fn update_schema(a: &mut Value, b: &Value) {
69+
merge_schema(a, b);
70+
71+
// Different environments may ignore or add capitalization in headers
72+
let headers = &b["data"]["CONTEXT"]["HEADERS"];
73+
if headers.get("requested-with-ajax").is_some() {
74+
a["data"]["CONTEXT"]["HEADERS"]["Requested-With-Ajax"] = b["data"]["CONTEXT"]["HEADERS"]["requested-with-ajax"].clone();
75+
a["data"]["CONTEXT"]["HEADERS"]["REQUESTED-WITH-AJAX"] = b["data"]["CONTEXT"]["HEADERS"]["requested-with-ajax"].clone();
76+
} else if headers.get("Requested-With-Ajax").is_some() {
77+
a["data"]["CONTEXT"]["HEADERS"]["requested-with-ajax"] = b["data"]["CONTEXT"]["HEADERS"]["Requested-With-Ajax"].clone();
78+
a["data"]["CONTEXT"]["HEADERS"]["REQUESTED-WITH-AJAX"] = b["data"]["CONTEXT"]["HEADERS"]["Requested-With-Ajax"].clone();
79+
} else if headers.get("REQUESTED-WITH-AJAX").is_some() {
80+
a["data"]["CONTEXT"]["HEADERS"]["requested-with-ajax"] = b["data"]["CONTEXT"]["HEADERS"]["REQUESTED-WITH-AJAX"].clone();
81+
a["data"]["CONTEXT"]["HEADERS"]["Requested-With-Ajax"] = b["data"]["CONTEXT"]["HEADERS"]["REQUESTED-WITH-AJAX"].clone();
82+
}
83+
84+
// Update version
85+
a["version"] = VERSION.to_string().to_string().into();
86+
}
87+
5388
/// Extract same level blocks positions.
5489
///
5590
/// ```text

0 commit comments

Comments
 (0)