Test for default name of struct with type params
This commit is contained in:
parent
54cfd2ba0e
commit
d14db450cf
4 changed files with 100 additions and 2 deletions
|
@ -306,6 +306,18 @@ impl<T: MakeSchema> MakeSchema for Option<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> MakeSchema for std::marker::PhantomData<T> {
|
||||||
|
no_ref_schema!();
|
||||||
|
|
||||||
|
fn schema_name() -> String {
|
||||||
|
<()>::schema_name()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn make_schema(gen: &mut SchemaGenerator) -> Result {
|
||||||
|
<()>::make_schema(gen)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
////////// DEREF //////////
|
////////// DEREF //////////
|
||||||
|
|
||||||
macro_rules! deref_impl {
|
macro_rules! deref_impl {
|
||||||
|
|
87
schemars/tests/naming.rs
Normal file
87
schemars/tests/naming.rs
Normal file
|
@ -0,0 +1,87 @@
|
||||||
|
use pretty_assertions::assert_eq;
|
||||||
|
use schemars::{schema_for, MakeSchema};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::error::Error;
|
||||||
|
|
||||||
|
const EXPECTED: &str = r#"
|
||||||
|
{
|
||||||
|
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||||
|
"title": "MyStruct_For_Integer_And_Null_And_Boolean_And_Array_Of_String",
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"float": {
|
||||||
|
"type": "number"
|
||||||
|
},
|
||||||
|
"t": {
|
||||||
|
"type": "integer"
|
||||||
|
},
|
||||||
|
"u": {
|
||||||
|
"type": "null"
|
||||||
|
},
|
||||||
|
"v": {
|
||||||
|
"type": "boolean"
|
||||||
|
},
|
||||||
|
"w": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"#;
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug, PartialEq, MakeSchema)]
|
||||||
|
struct MyStruct<T, U, V, W> {
|
||||||
|
t: T,
|
||||||
|
u: U,
|
||||||
|
float: f32,
|
||||||
|
v: V,
|
||||||
|
w: W,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug, PartialEq, MakeSchema)]
|
||||||
|
#[serde(rename = "MyStruct")]
|
||||||
|
struct MyRenamedStruct<T, U, V, W> {
|
||||||
|
t: T,
|
||||||
|
u: U,
|
||||||
|
float: f32,
|
||||||
|
v: V,
|
||||||
|
w: W,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug, PartialEq, MakeSchema)]
|
||||||
|
#[serde(remote = "MyStruct")]
|
||||||
|
struct MyRemoteStruct<T, U, V, W> {
|
||||||
|
t: T,
|
||||||
|
u: U,
|
||||||
|
float: f32,
|
||||||
|
v: V,
|
||||||
|
w: W,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn default_name_multiple_type_params() -> Result<(), Box<dyn Error>> {
|
||||||
|
let actual = schema_for!(MyStruct<i32, (), bool, Vec<String>>)?;
|
||||||
|
let expected = serde_json::from_str(EXPECTED)?;
|
||||||
|
assert_eq!(actual, expected);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[ignore] // not yet implemented
|
||||||
|
fn overriden_with_rename_name_multiple_type_params() -> Result<(), Box<dyn Error>> {
|
||||||
|
let actual = schema_for!(MyRenamedStruct<i32, (), bool, Vec<String>>)?;
|
||||||
|
let expected = serde_json::from_str(EXPECTED)?;
|
||||||
|
assert_eq!(actual, expected);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[ignore] // not yet implemented
|
||||||
|
fn overriden_with_remote_name_multiple_type_params() -> Result<(), Box<dyn Error>> {
|
||||||
|
let actual = schema_for!(MyRemoteStruct<i32, (), bool, Vec<String>>)?;
|
||||||
|
let expected = serde_json::from_str(EXPECTED)?;
|
||||||
|
assert_eq!(actual, expected);
|
||||||
|
Ok(())
|
||||||
|
}
|
|
@ -33,8 +33,7 @@ pub fn derive_make_schema(input: proc_macro::TokenStream) -> proc_macro::TokenSt
|
||||||
let type_params: Vec<_> = cont.generics.type_params().map(|ty| &ty.ident).collect();
|
let type_params: Vec<_> = cont.generics.type_params().map(|ty| &ty.ident).collect();
|
||||||
let type_param_fmt = match type_params.len() {
|
let type_param_fmt = match type_params.len() {
|
||||||
0 => "{}".to_owned(),
|
0 => "{}".to_owned(),
|
||||||
1 => "{}_For_{}".to_owned(),
|
n => format!("{{}}_For_{{}}{}", "_And_{}".repeat(n - 1)),
|
||||||
n => format!("{{}}_For_{{}}_And{}", "_{}".repeat(n - 1)),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let (impl_generics, ty_generics, where_clause) = cont.generics.split_for_impl();
|
let (impl_generics, ty_generics, where_clause) = cont.generics.split_for_impl();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue