From 5d3f1ada227b8585a25f2b9e372e5e599692bf11 Mon Sep 17 00:00:00 2001 From: laststylebender <43403528+laststylebender14@users.noreply.github.com> Date: Mon, 12 Aug 2024 16:41:33 +0530 Subject: [PATCH] fix: add deny_unknown_fields to nested structs (#2666) --- src/cli/generator/config.rs | 87 ++++++++++++++++++++++++++++++++++--- 1 file changed, 80 insertions(+), 7 deletions(-) diff --git a/src/cli/generator/config.rs b/src/cli/generator/config.rs index d339a9feac..8ae02e5609 100644 --- a/src/cli/generator/config.rs +++ b/src/cli/generator/config.rs @@ -28,6 +28,7 @@ pub struct Config { #[derive(Clone, Deserialize, Serialize, Debug, Default)] #[serde(rename_all = "camelCase")] +#[serde(deny_unknown_fields)] pub struct PresetConfig { pub merge_type: Option, #[serde(rename = "consolidateURL")] @@ -60,6 +61,7 @@ pub struct Input { #[derive(Deserialize, Serialize, Debug)] #[serde(rename_all = "camelCase")] +#[serde(deny_unknown_fields)] pub enum Source { #[serde(rename_all = "camelCase")] Curl { @@ -77,6 +79,7 @@ pub enum Source { #[derive(Deserialize, Serialize, Debug, Default)] #[serde(rename_all = "camelCase")] +#[serde(deny_unknown_fields)] pub struct Output { #[serde(skip_serializing_if = "Location::is_empty")] pub path: Location, @@ -92,6 +95,7 @@ pub enum Resolved {} pub struct UnResolved {} #[derive(Deserialize, Serialize, Debug, Default)] +#[serde(deny_unknown_fields)] pub struct Schema { #[serde(skip_serializing_if = "Option::is_none")] pub query: Option, @@ -388,15 +392,84 @@ mod tests { assert!(!location_non_empty.is_empty()); } - #[test] - fn test_raise_error_when_unknown_fields_present() { - let config: Result, serde_json::Error> = - serde_json::from_str(r#"{"input": "value"}"#); - + fn assert_deserialization_error(json: &str, expected_error: &str) { + let config: Result, serde_json::Error> = serde_json::from_str(json); let actual = config.err().unwrap().to_string(); - let expected = + assert_eq!(actual, expected_error); + } + + #[test] + fn test_raise_error_unknown_field_at_root_level() { + let json = r#"{"input": "value"}"#; + let expected_error = "unknown field `input`, expected one of `inputs`, `output`, `preset`, `schema` at line 1 column 8"; + assert_deserialization_error(json, expected_error); + } + + #[test] + fn test_raise_error_unknown_field_in_inputs() { + let json = r#" + {"inputs": [{ + "curl": { + "src": "https://tailcall.run/graphql", + "headerss": { + "content-type": "application/json" + } + } + }]} + "#; + let expected_error = + "unknown field `headerss`, expected one of `src`, `headers`, `fieldName` at line 9 column 13"; + assert_deserialization_error(json, expected_error); + + let json = r#" + {"inputs": [{ + "curls": { + "src": "https://tailcall.run/graphql", + "headerss": { + "content-type": "application/json" + } + } + }]} + "#; + let expected_error = + "no variant of enum Source found in flattened data at line 9 column 13"; + assert_deserialization_error(json, expected_error); + } - assert_eq!(actual, expected); + #[test] + fn test_raise_error_unknown_field_in_preset() { + let json = r#" + {"preset": { + "mergeTypes": 1.0, + "consolidateURL": 0.5 + }} + "#; + let expected_error = + "unknown field `mergeTypes`, expected one of `mergeType`, `consolidateURL`, `inferTypeNames`, `treeShake`, `unwrapSingleFieldTypes` at line 3 column 28"; + assert_deserialization_error(json, expected_error); + } + + #[test] + fn test_raise_error_unknown_field_in_output() { + let json = r#" + {"output": { + "paths": "./output.graphql", + }} + "#; + let expected_error = + "unknown field `paths`, expected `path` or `format` at line 3 column 21"; + assert_deserialization_error(json, expected_error); + } + + #[test] + fn test_raise_error_unknown_field_in_schema() { + let json = r#" + {"schema": { + "querys": "Query", + }} + "#; + let expected_error = "unknown field `querys`, expected `query` at line 3 column 22"; + assert_deserialization_error(json, expected_error); } }