Enable renaming schemas using attribute on struct
This commit is contained in:
parent
67d44533d6
commit
1d0fd18c9e
6 changed files with 78 additions and 60 deletions
|
@ -4,7 +4,7 @@
|
|||
"type": "object",
|
||||
"properties": {
|
||||
"inner": {
|
||||
"$ref": "#/definitions/MySimpleStruct"
|
||||
"$ref": "#/definitions/another-new-name"
|
||||
},
|
||||
"t": {
|
||||
"type": "integer"
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
mod util;
|
||||
use pretty_assertions::assert_eq;
|
||||
use schemars::{schema::*, schema_for, MakeSchema};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::error::Error;
|
||||
use schemars::{schema_for, MakeSchema};
|
||||
use util::*;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, PartialEq, MakeSchema)]
|
||||
#[derive(Debug, MakeSchema)]
|
||||
struct Flat {
|
||||
foo: f32,
|
||||
bar: bool,
|
||||
|
@ -11,7 +11,8 @@ struct Flat {
|
|||
foobar: Vec<i32>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, PartialEq, MakeSchema)]
|
||||
#[derive(Debug, MakeSchema)]
|
||||
#[serde(rename = "Flat")]
|
||||
struct Deep1 {
|
||||
foo: f32,
|
||||
#[serde(flatten)]
|
||||
|
@ -19,26 +20,22 @@ struct Deep1 {
|
|||
foobar: Vec<i32>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, PartialEq, MakeSchema)]
|
||||
#[derive(Debug, MakeSchema)]
|
||||
struct Deep2 {
|
||||
bar: bool,
|
||||
#[serde(flatten)]
|
||||
deep3: Deep3,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, PartialEq, MakeSchema)]
|
||||
#[derive(Debug, MakeSchema)]
|
||||
struct Deep3 {
|
||||
baz: String,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn flatten_schema() -> Result<(), Box<dyn Error>> {
|
||||
fn flatten_schema() -> TestResult {
|
||||
let flat = schema_for!(Flat)?;
|
||||
let mut deep = schema_for!(Deep1)?;
|
||||
match deep {
|
||||
Schema::Object(ref mut o) => o.title = Some("Flat".to_owned()),
|
||||
_ => assert!(false, "Schema was not object: {:?}", deep),
|
||||
};
|
||||
let deep = schema_for!(Deep1)?;
|
||||
assert_eq!(flat, deep);
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -1,41 +1,39 @@
|
|||
use schemars::MakeSchema;
|
||||
|
||||
mod util;
|
||||
use schemars::MakeSchema;
|
||||
use util::*;
|
||||
|
||||
#[derive(MakeSchema)]
|
||||
pub struct MyStruct<T, U, V, W> {
|
||||
pub t: T,
|
||||
pub u: U,
|
||||
pub v: V,
|
||||
pub w: W,
|
||||
pub inner: MySimpleStruct,
|
||||
#[derive(Debug, MakeSchema)]
|
||||
struct MyStruct<T, U, V, W> {
|
||||
t: T,
|
||||
u: U,
|
||||
v: V,
|
||||
w: W,
|
||||
inner: MySimpleStruct,
|
||||
}
|
||||
|
||||
#[derive(MakeSchema)]
|
||||
pub struct MySimpleStruct {}
|
||||
#[derive(Debug, MakeSchema)]
|
||||
struct MySimpleStruct {}
|
||||
|
||||
#[test]
|
||||
fn default_name_multiple_type_params() -> TestResult {
|
||||
test_default_generated_schema::<MyStruct<i32, (), bool, Vec<String>>>("naming-default")
|
||||
}
|
||||
|
||||
#[derive(MakeSchema)]
|
||||
#[serde(rename = "a-new-name-<W>-<T>-<T>")]
|
||||
pub struct MyRenamedStruct<T, U, V, W> {
|
||||
pub t: T,
|
||||
pub u: U,
|
||||
pub v: V,
|
||||
pub w: W,
|
||||
pub inner: MySimpleRenamedStruct,
|
||||
#[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,
|
||||
}
|
||||
|
||||
#[derive(MakeSchema)]
|
||||
#[derive(Debug, MakeSchema)]
|
||||
#[serde(rename = "another-new-name")]
|
||||
pub struct MySimpleRenamedStruct {}
|
||||
struct MySimpleRenamedStruct {}
|
||||
|
||||
#[test]
|
||||
#[ignore] // not yet implemented
|
||||
fn overriden_with_rename_name_multiple_type_params() -> TestResult {
|
||||
fn overriden_with_rename_multiple_type_params() -> TestResult {
|
||||
test_default_generated_schema::<MyRenamedStruct<i32, (), bool, Vec<String>>>("naming-custom")
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
mod util;
|
||||
use schemars::gen::SchemaSettings;
|
||||
use schemars::schema::Schema;
|
||||
|
||||
mod util;
|
||||
use util::*;
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -6,21 +6,7 @@ use std::panic;
|
|||
|
||||
pub type TestResult = Result<(), Box<dyn Error>>;
|
||||
|
||||
pub fn test_default_generated_schema<T: MakeSchema>(file: &str) -> TestResult {
|
||||
let expected_json = fs::read_to_string(format!("tests/expected/{}.json", file))?;
|
||||
let expected = serde_json::from_str(&expected_json)?;
|
||||
|
||||
let actual = schema_for!(T)?;
|
||||
|
||||
if actual != expected {
|
||||
let actual_json = serde_json::to_string_pretty(&actual)?;
|
||||
fs::write(format!("tests/actual/{}.json", file), actual_json)?;
|
||||
}
|
||||
|
||||
assert_eq!(actual, expected);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[allow(dead_code)] // https://github.com/rust-lang/rust/issues/46379
|
||||
pub fn test_generated_schema<T: MakeSchema>(file: &str, settings: SchemaSettings) -> TestResult {
|
||||
let expected_json = fs::read_to_string(format!("tests/expected/{}.json", file))?;
|
||||
let expected = serde_json::from_str(&expected_json)?;
|
||||
|
@ -35,3 +21,19 @@ pub fn test_generated_schema<T: MakeSchema>(file: &str, settings: SchemaSettings
|
|||
assert_eq!(actual, expected);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[allow(dead_code)] // https://github.com/rust-lang/rust/issues/46379
|
||||
pub fn test_default_generated_schema<T: MakeSchema>(file: &str) -> TestResult {
|
||||
let expected_json = fs::read_to_string(format!("tests/expected/{}.json", file))?;
|
||||
let expected = serde_json::from_str(&expected_json)?;
|
||||
|
||||
let actual = schema_for!(T)?;
|
||||
|
||||
if actual != expected {
|
||||
let actual_json = serde_json::to_string_pretty(&actual)?;
|
||||
fs::write(format!("tests/actual/{}.json", file), actual_json)?;
|
||||
}
|
||||
|
||||
assert_eq!(actual, expected);
|
||||
Ok(())
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue