Allow making a schema to fail by returning an Err

This commit is contained in:
Graham Esau 2019-08-06 20:56:04 +01:00
parent 2aa1835240
commit 51ed13218c
8 changed files with 149 additions and 99 deletions

View file

@ -1,6 +1,7 @@
use pretty_assertions::assert_eq;
use schemars::{schema_for, MakeSchema};
use serde::{Deserialize, Serialize};
use std::error::Error;
#[derive(Serialize, Deserialize, Debug, PartialEq, MakeSchema)]
struct Flat {
@ -32,6 +33,7 @@ struct Deep3 {
#[test]
#[ignore = "flattening is not yet implemented"]
fn flatten_schema() {
assert_eq!(schema_for!(Flat), schema_for!(Deep1));
fn flatten_schema() -> Result<(), Box<dyn Error>> {
assert_eq!(schema_for!(Flat)?, schema_for!(Deep1)?);
Ok(())
}

View file

@ -3,13 +3,14 @@ use schemars::schema::*;
use schemars::{gen, schema_for};
use serde_json::{from_str, to_string_pretty};
use std::fs;
use std::error::Error;
#[test]
fn schema_matches_default_settings() -> Result<(), Box<dyn std::error::Error>> {
fn schema_matches_default_settings() -> Result<(), Box<dyn Error>> {
let expected_json = fs::read_to_string("tests/schema.json")?;
let expected: Schema = from_str(&expected_json)?;
let actual = schema_for!(Schema);
let actual = schema_for!(Schema)?;
fs::write("tests/schema.actual.json", to_string_pretty(&actual)?)?;
assert_eq!(actual, expected, "Generated schema did not match saved schema - generated schema has been written to \"tests/schema.actual.json\".");
@ -17,13 +18,13 @@ fn schema_matches_default_settings() -> Result<(), Box<dyn std::error::Error>> {
}
#[test]
fn schema_matches_openapi3() -> Result<(), Box<dyn std::error::Error>> {
fn schema_matches_openapi3() -> Result<(), Box<dyn Error>> {
let expected_json = fs::read_to_string("tests/schema-openapi3.json")?;
let expected: Schema = from_str(&expected_json)?;
let actual = gen::SchemaSettings::openapi3()
.into_generator()
.into_root_schema_for::<Schema>();
.into_root_schema_for::<Schema>()?;
fs::write(
"tests/schema-openapi3.actual.json",
to_string_pretty(&actual)?,