Add test for schema generation.This test will frequently fail as new features/fixes are implemented - but at least it gives an easy way of visualising changes!
This commit is contained in:
parent
076cf423c3
commit
fc346da692
7 changed files with 295 additions and 6 deletions
1
schemars/.gitignore
vendored
1
schemars/.gitignore
vendored
|
@ -1,3 +1,4 @@
|
||||||
/target
|
/target
|
||||||
**/*.rs.bk
|
**/*.rs.bk
|
||||||
Cargo.lock
|
Cargo.lock
|
||||||
|
/tests/*.actual.*
|
|
@ -2,7 +2,6 @@ pub mod gen;
|
||||||
pub mod make_schema;
|
pub mod make_schema;
|
||||||
pub mod schema;
|
pub mod schema;
|
||||||
|
|
||||||
pub use schema::{Schema, SchemaObject, SchemaRef};
|
|
||||||
pub use make_schema::MakeSchema;
|
pub use make_schema::MakeSchema;
|
||||||
|
|
||||||
pub use schemars_derive::*;
|
pub use schemars_derive::*;
|
||||||
|
|
|
@ -3,7 +3,7 @@ use serde_json::Result;
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let gen = gen::SchemaGenerator::new();
|
let gen = gen::SchemaGenerator::new();
|
||||||
let schema = gen.into_root_schema_for::<Schema>();
|
let schema = gen.into_root_schema_for::<schema::Schema>();
|
||||||
let json = serde_json::to_string_pretty(&schema)?;
|
let json = serde_json::to_string_pretty(&schema)?;
|
||||||
println!("{}", json);
|
println!("{}", json);
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use crate as schemars;
|
use crate as schemars;
|
||||||
use schemars::MakeSchema;
|
use crate::MakeSchema;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use std::collections::BTreeMap as Map;
|
use std::collections::BTreeMap as Map;
|
||||||
|
@ -39,7 +39,7 @@ pub struct SchemaRef {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq, MakeSchema)]
|
#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq, MakeSchema)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase", default)]
|
||||||
pub struct SchemaObject {
|
pub struct SchemaObject {
|
||||||
#[serde(rename = "$schema", skip_serializing_if = "Option::is_none")]
|
#[serde(rename = "$schema", skip_serializing_if = "Option::is_none")]
|
||||||
pub schema: Option<String>,
|
pub schema: Option<String>,
|
||||||
|
|
267
schemars/tests/schema.json
Normal file
267
schemars/tests/schema.json
Normal file
|
@ -0,0 +1,267 @@
|
||||||
|
{
|
||||||
|
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||||
|
"title": "schemars__schema__Schema",
|
||||||
|
"anyOf": [
|
||||||
|
{
|
||||||
|
"properties": {
|
||||||
|
"Bool": {
|
||||||
|
"type": "boolean"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"properties": {
|
||||||
|
"Ref": {
|
||||||
|
"$ref": "#/definitions/schemars__schema__SchemaRef"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"properties": {
|
||||||
|
"Object": {
|
||||||
|
"$ref": "#/definitions/schemars__schema__SchemaObject"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"definitions": {
|
||||||
|
"schemars__schema__InstanceType": {
|
||||||
|
"enum": [
|
||||||
|
"null",
|
||||||
|
"boolean",
|
||||||
|
"object",
|
||||||
|
"array",
|
||||||
|
"number",
|
||||||
|
"string",
|
||||||
|
"integer"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"schemars__schema__Schema": {
|
||||||
|
"anyOf": [
|
||||||
|
{
|
||||||
|
"properties": {
|
||||||
|
"Bool": {
|
||||||
|
"type": "boolean"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"properties": {
|
||||||
|
"Ref": {
|
||||||
|
"$ref": "#/definitions/schemars__schema__SchemaRef"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"properties": {
|
||||||
|
"Object": {
|
||||||
|
"$ref": "#/definitions/schemars__schema__SchemaObject"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"schemars__schema__SchemaObject": {
|
||||||
|
"properties": {
|
||||||
|
"$id": {
|
||||||
|
"anyOf": [
|
||||||
|
{
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "null"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"$schema": {
|
||||||
|
"anyOf": [
|
||||||
|
{
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "null"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"allOf": {
|
||||||
|
"anyOf": [
|
||||||
|
{
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"$ref": "#/definitions/schemars__schema__Schema"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "null"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"anyOf": {
|
||||||
|
"anyOf": [
|
||||||
|
{
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"$ref": "#/definitions/schemars__schema__Schema"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "null"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"definitions": {
|
||||||
|
"type": "object",
|
||||||
|
"additionalProperties": {
|
||||||
|
"$ref": "#/definitions/schemars__schema__Schema"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"description": {
|
||||||
|
"anyOf": [
|
||||||
|
{
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "null"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"enum": {
|
||||||
|
"anyOf": [
|
||||||
|
{
|
||||||
|
"type": "array",
|
||||||
|
"items": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "null"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"extensions": {
|
||||||
|
"type": "object",
|
||||||
|
"additionalProperties": true
|
||||||
|
},
|
||||||
|
"items": {
|
||||||
|
"anyOf": [
|
||||||
|
{
|
||||||
|
"$ref": "#/definitions/schemars__schema__SingleOrVec_schemars__schema__Schema_"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "null"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"not": {
|
||||||
|
"anyOf": [
|
||||||
|
{
|
||||||
|
"$ref": "#/definitions/schemars__schema__Schema"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "null"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"oneOf": {
|
||||||
|
"anyOf": [
|
||||||
|
{
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"$ref": "#/definitions/schemars__schema__Schema"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "null"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"properties": {
|
||||||
|
"type": "object",
|
||||||
|
"additionalProperties": {
|
||||||
|
"$ref": "#/definitions/schemars__schema__Schema"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": {
|
||||||
|
"anyOf": [
|
||||||
|
{
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "null"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"title": {
|
||||||
|
"anyOf": [
|
||||||
|
{
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "null"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"type": {
|
||||||
|
"anyOf": [
|
||||||
|
{
|
||||||
|
"$ref": "#/definitions/schemars__schema__SingleOrVec_schemars__schema__InstanceType_"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "null"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"schemars__schema__SchemaRef": {
|
||||||
|
"properties": {
|
||||||
|
"$ref": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"schemars__schema__SingleOrVec_schemars__schema__InstanceType_": {
|
||||||
|
"anyOf": [
|
||||||
|
{
|
||||||
|
"properties": {
|
||||||
|
"Single": {
|
||||||
|
"$ref": "#/definitions/schemars__schema__InstanceType"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"properties": {
|
||||||
|
"Vec": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"$ref": "#/definitions/schemars__schema__InstanceType"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"schemars__schema__SingleOrVec_schemars__schema__Schema_": {
|
||||||
|
"anyOf": [
|
||||||
|
{
|
||||||
|
"properties": {
|
||||||
|
"Single": {
|
||||||
|
"$ref": "#/definitions/schemars__schema__Schema"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"properties": {
|
||||||
|
"Vec": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"$ref": "#/definitions/schemars__schema__Schema"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
22
schemars/tests/test.rs
Normal file
22
schemars/tests/test.rs
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
use schemars::gen::SchemaGenerator;
|
||||||
|
use schemars::schema::*;
|
||||||
|
use serde_json::{from_str, to_string_pretty};
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn schema_matches() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
let expected_json = fs::read_to_string("tests/schema.json")?;
|
||||||
|
let expected: Schema = from_str(&expected_json)?;
|
||||||
|
|
||||||
|
let gen = SchemaGenerator::new();
|
||||||
|
let actual = gen.into_root_schema_for::<Schema>();
|
||||||
|
fs::write("tests/schema.actual.json", to_string_pretty(&actual)?)?;
|
||||||
|
|
||||||
|
assert_eq!(actual, expected, "\n\nGenerated schema did not match saved schema - generated schema has been written to \"tests/schema.actual.json\".");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
|
@ -34,7 +34,7 @@ pub fn derive_make_schema(input: proc_macro::TokenStream) -> proc_macro::TokenSt
|
||||||
let impl_block = quote! {
|
let impl_block = quote! {
|
||||||
#[automatically_derived]
|
#[automatically_derived]
|
||||||
impl #impl_generics schemars::MakeSchema for #name #ty_generics #where_clause {
|
impl #impl_generics schemars::MakeSchema for #name #ty_generics #where_clause {
|
||||||
fn make_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::Schema {
|
fn make_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
|
||||||
#schema
|
#schema
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -52,7 +52,7 @@ fn add_trait_bounds(generics: &mut Generics) {
|
||||||
|
|
||||||
fn wrap_schema_fields(schema_contents: TokenStream) -> TokenStream {
|
fn wrap_schema_fields(schema_contents: TokenStream) -> TokenStream {
|
||||||
quote! {
|
quote! {
|
||||||
schemars::SchemaObject {
|
schemars::schema::SchemaObject {
|
||||||
#schema_contents
|
#schema_contents
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue