From 5f443fe5326ca002671565b0c1a9f6dc0033a0b4 Mon Sep 17 00:00:00 2001 From: Edward Rudd Date: Mon, 4 Jul 2022 18:55:01 -0400 Subject: [PATCH] Fix serializing of JSON default values (#969) * Fix serializing of JSON default values * fix format of other code due to new nightly --- src/types/json.rs | 4 +- tests/introspection.rs | 133 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+), 2 deletions(-) diff --git a/src/types/json.rs b/src/types/json.rs index af110cfc..5f1873b1 100644 --- a/src/types/json.rs +++ b/src/types/json.rs @@ -63,7 +63,7 @@ impl InputType for Json { } fn to_value(&self) -> Value { - to_value(&self.0).unwrap_or_default() + Value::String(serde_json::to_string(&self.0).unwrap_or_default()) } fn as_raw_value(&self) -> Option<&Self::RawValueType> { @@ -120,7 +120,7 @@ impl InputType for serde_json::Value { } fn to_value(&self) -> Value { - to_value(&self).unwrap_or_default() + Value::String(self.to_string()) } fn as_raw_value(&self) -> Option<&Self::RawValueType> { diff --git a/tests/introspection.rs b/tests/introspection.rs index 6eb2d90f..f2891dc7 100644 --- a/tests/introspection.rs +++ b/tests/introspection.rs @@ -1,4 +1,5 @@ use async_graphql::*; +use chrono::{NaiveDate, NaiveDateTime}; use futures_util::stream::{self, Stream}; #[derive(Clone, Debug)] @@ -1378,3 +1379,135 @@ pub async fn test_introspection_only() { let res = schema.execute(query).await.into_result().unwrap().data; assert_eq!(res, res_json); } + +#[tokio::test] +pub async fn test_introspection_default() { + #[derive(serde::Serialize, serde::Deserialize, Default)] + pub struct MyStruct { + a: i32, + b: i32, + } + + #[derive(InputObject)] + pub struct DefaultInput { + #[graphql(default)] + pub str: String, + #[graphql(default_with = "NaiveDate::from_ymd(2016, 7, 8).and_hms(9, 10, 11)")] + pub date: NaiveDateTime, + // a required json with no default + pub json: serde_json::Value, + // basic default (JSON null) + #[graphql(default)] + pub json_def: serde_json::Value, + // complex default (JSON object) + #[graphql(default_with = "serde_json::Value::Object(Default::default())")] + pub json_def_obj: serde_json::Value, + #[graphql(default)] + pub json_def_struct: Json, + } + + struct LocalMutation; + + #[Object] + #[allow(unreachable_code)] + impl LocalMutation { + async fn simple_mutation(&self, _input: DefaultInput) -> SimpleObject { + unimplemented!() + } + } + + let schema = Schema::build(Query, LocalMutation, EmptySubscription) + .introspection_only() + .finish(); + + // Test whether introspection works. + let query = r#" + { + __type(name: "DefaultInput") { + name + kind + inputFields { + name + defaultValue + type { kind ofType { kind name } } + } + } + } + "#; + let res_json = value!({ + "__type": { + "name": "DefaultInput", + "kind": "INPUT_OBJECT", + "inputFields": [ + { + "name": "str", + "defaultValue": "\"\"", + "type": { + "kind": "NON_NULL", + "ofType": { + "kind": "SCALAR", + "name": "String" + }, + }, + }, + { + "name": "date", + "defaultValue": "\"2016-07-08T09:10:11\"", + "type": { + "kind": "NON_NULL", + "ofType": { + "kind": "SCALAR", + "name": "NaiveDateTime" + }, + }, + }, + { + "name": "json", + "defaultValue": null, + "type": { + "kind": "NON_NULL", + "ofType": { + "kind": "SCALAR", + "name": "JSON" + }, + }, + }, + { + "name": "jsonDef", + "defaultValue": "\"null\"", + "type": { + "kind": "NON_NULL", + "ofType": { + "kind": "SCALAR", + "name": "JSON" + }, + }, + }, + { + "name": "jsonDefObj", + "defaultValue": "\"{}\"", + "type": { + "kind": "NON_NULL", + "ofType": { + "kind": "SCALAR", + "name": "JSON" + }, + }, + }, + { + "name": "jsonDefStruct", + "defaultValue": "\"{\\\"a\\\":0,\\\"b\\\":0}\"", + "type": { + "kind": "NON_NULL", + "ofType": { + "kind": "SCALAR", + "name": "JSON" + }, + }, + }, + ] + } + }); + let res = schema.execute(query).await.into_result().unwrap().data; + assert_eq!(res, res_json); +}