Fix panic on f32-64::INFINITE/f32-64::NEG_INFINITE/f32-64::NAN output. #735

This commit is contained in:
Sunli 2021-12-02 20:19:17 +08:00
parent 0bc0283012
commit 09be5e0021
3 changed files with 35 additions and 2 deletions

View File

@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## Unreleased
- Fix panic on f32-64::INFINITE/f32-64::NEG_INFINITE/f32-64::NAN output. [#735](https://github.com/async-graphql/async-graphql/issues/735)
## [3.0.10] 2021-11-30
- Fix the custom validator cannot work on `Option<Vec<T>>`.

View File

@ -18,7 +18,10 @@ impl ScalarType for f32 {
}
fn to_value(&self) -> Value {
Value::Number(Number::from_f64(*self as f64).unwrap())
match Number::from_f64(*self as f64) {
Some(n) => Value::Number(n),
None => Value::Null,
}
}
}
@ -40,6 +43,9 @@ impl ScalarType for f64 {
}
fn to_value(&self) -> Value {
Value::Number(Number::from_f64(*self as f64).unwrap())
match Number::from_f64(*self as f64) {
Some(n) => Value::Number(n),
None => Value::Null,
}
}
}

View File

@ -45,3 +45,26 @@ pub async fn test_scalar_macro() {
})
);
}
#[tokio::test]
pub async fn test_float_inf() {
struct Query;
#[Object]
impl Query {
async fn value(&self) -> f32 {
f32::INFINITY
}
}
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
assert_eq!(
schema
.execute("{ value }")
.await
.into_result()
.unwrap()
.data,
value!({ "value": null })
);
}