Fixed `OneofObject` restriction on inner types being unique. #923

This commit is contained in:
sunli829 2022-05-14 16:43:28 +08:00
parent da725575bd
commit 965ac6ae90
5 changed files with 38 additions and 15 deletions

View File

@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Bump [`uuid`](https://crates.io/crates/uuid) to `1.0.0`. [#907](https://github.com/async-graphql/async-graphql/pull/907/files)
- Add some options for exporting SDL. [#877](https://github.com/async-graphql/async-graphql/issues/877)
- Cache parsed `ExecuteDocument` in APQ. [#919](https://github.com/async-graphql/async-graphql/issues/919)
- Fixed `OneofObject` restriction on inner types being unique. [#923](https://github.com/async-graphql/async-graphql/issues/923)
# [3.0.38] 2022-4-8

View File

@ -1,5 +1,3 @@
use std::collections::HashSet;
use darling::ast::{Data, Style};
use proc_macro::TokenStream;
use quote::quote;
@ -31,7 +29,6 @@ pub fn generate(object_args: &args::OneofObject) -> GeneratorResult<TokenStream>
}
};
let mut enum_items = HashSet::new();
let mut enum_names = Vec::new();
let mut schema_fields = Vec::new();
let mut parse_item = Vec::new();
@ -70,14 +67,7 @@ pub fn generate(object_args: &args::OneofObject) -> GeneratorResult<TokenStream>
}
};
if let Type::Path(p) = ty {
// This validates that the field type wasn't already used
if !enum_items.insert(p) {
return Err(
Error::new_spanned(ty, "This type already used in another variant").into(),
);
}
if let Type::Path(_) = ty {
enum_names.push(enum_name);
let secret = variant.secret;

View File

@ -70,9 +70,11 @@ pub fn generate(union_args: &args::Union) -> GeneratorResult<TokenStream> {
if matches!(ty, Type::Path(_) | Type::Macro(_)) {
// This validates that the field type wasn't already used
if !enum_items.insert(ty) {
return Err(
Error::new_spanned(&ty, "This type already used in another variant").into(),
);
return Err(Error::new_spanned(
&ty,
"This type is already used in another variant",
)
.into());
}
enum_names.push(enum_name);

@ -1 +1 @@
Subproject commit d359d0ea3116326daf753f08233219530b8ec10b
Subproject commit fa8fc5dde0e1f70dee67719eceecfc9ade250d1f

View File

@ -313,3 +313,33 @@ async fn test_oneof_object_vec() {
})
);
}
#[tokio::test]
async fn test_issue_923() {
#[derive(OneofObject)]
enum Filter {
Any(Vec<String>),
All(Vec<String>),
}
pub struct Query;
#[Object]
impl Query {
async fn query(&self, filter: Filter) -> bool {
match filter {
Filter::Any(values) => assert_eq!(values, vec!["a".to_string(), "b".to_string()]),
Filter::All(values) => assert_eq!(values, vec!["c".to_string(), "d".to_string()]),
}
true
}
}
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
let query = r#"{ query(filter: {any: ["a", "b"]}) }"#;
schema.execute(query).await.into_result().unwrap();
let query = r#"{ query(filter: {all: ["c", "d"]}) }"#;
schema.execute(query).await.into_result().unwrap();
}