Process #[schemars] attributes

This commit is contained in:
Graham Esau 2019-08-08 08:24:30 +01:00
parent 1d0fd18c9e
commit 6b64cedb91
9 changed files with 113 additions and 26 deletions

View file

@ -0,0 +1,16 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "MyStruct",
"type": "object",
"properties": {
"camelCase": {
"type": "integer"
},
"new_name_1": {
"type": "integer"
},
"new_name_2": {
"type": "integer"
}
}
}

View file

@ -12,7 +12,7 @@ struct Flat {
}
#[derive(Debug, MakeSchema)]
#[serde(rename = "Flat")]
#[schemars(rename = "Flat")]
struct Deep1 {
foo: f32,
#[serde(flatten)]

View file

@ -0,0 +1,19 @@
mod util;
use schemars::MakeSchema;
use util::*;
#[derive(Debug, MakeSchema)]
#[serde(rename_all = "camelCase")]
struct MyStruct {
camel_case: i32,
#[serde(rename = "new_name_1")]
old_name_1: i32,
#[serde(rename = "ignored")]
#[schemars(rename = "new_name_2")]
old_name_2: i32,
}
#[test]
fn set_struct_property_names() -> TestResult {
test_default_generated_schema::<MyStruct>("property-name-struct")
}

View file

@ -4,11 +4,11 @@ use util::*;
#[derive(Debug, MakeSchema)]
struct MyStruct<T, U, V, W> {
t: T,
u: U,
v: V,
w: W,
inner: MySimpleStruct,
t: T,
u: U,
v: V,
w: W,
inner: MySimpleStruct,
}
#[derive(Debug, MakeSchema)]
@ -16,24 +16,25 @@ struct MySimpleStruct {}
#[test]
fn default_name_multiple_type_params() -> TestResult {
test_default_generated_schema::<MyStruct<i32, (), bool, Vec<String>>>("naming-default")
test_default_generated_schema::<MyStruct<i32, (), bool, Vec<String>>>("schema-name-default")
}
#[derive(Debug, MakeSchema)]
#[serde(rename = "a-new-name-{W}-{T}-{T}")]
struct MyRenamedStruct<T, U, V, W> {
t: T,
u: U,
v: V,
w: W,
inner: MySimpleRenamedStruct,
t: T,
u: U,
v: V,
w: W,
inner: MySimpleRenamedStruct,
}
#[derive(Debug, MakeSchema)]
#[serde(rename = "another-new-name")]
#[serde(rename = "this-attribute-is-ignored")]
#[schemars(rename = "another-new-name")]
struct MySimpleRenamedStruct {}
#[test]
fn overriden_with_rename_multiple_type_params() -> TestResult {
test_default_generated_schema::<MyRenamedStruct<i32, (), bool, Vec<String>>>("naming-custom")
test_default_generated_schema::<MyRenamedStruct<i32, (), bool, Vec<String>>>("schema-name-custom")
}