Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add deny_unknown_fields to nested structs #2666

Merged
merged 6 commits into from
Aug 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 80 additions & 7 deletions src/cli/generator/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub struct Config<Status = UnResolved> {

#[derive(Clone, Deserialize, Serialize, Debug, Default)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub struct PresetConfig {
pub merge_type: Option<f32>,
#[serde(rename = "consolidateURL")]
Expand Down Expand Up @@ -60,6 +61,7 @@ pub struct Input<Status = UnResolved> {

#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub enum Source<Status = UnResolved> {
#[serde(rename_all = "camelCase")]
Curl {
Expand All @@ -77,6 +79,7 @@ pub enum Source<Status = UnResolved> {

#[derive(Deserialize, Serialize, Debug, Default)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub struct Output<Status = UnResolved> {
#[serde(skip_serializing_if = "Location::is_empty")]
pub path: Location<Status>,
Expand All @@ -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<String>,
Expand Down Expand Up @@ -388,15 +392,84 @@ mod tests {
assert!(!location_non_empty.is_empty());
}

#[test]
fn test_raise_error_when_unknown_fields_present() {
let config: Result<Config<UnResolved>, serde_json::Error> =
serde_json::from_str(r#"{"input": "value"}"#);

fn assert_deserialization_error(json: &str, expected_error: &str) {
let config: Result<Config<UnResolved>, 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);
}
}
Loading