Add support for multiple flatten enums (#320)
Co-authored-by: Graham Esau <gesau@hotmail.co.uk>
This commit is contained in:
parent
30a9a384e2
commit
5d5837741c
5 changed files with 279 additions and 42 deletions
|
@ -180,17 +180,7 @@ pub fn apply_inner_validation(schema: &mut Schema, f: fn(&mut Schema) -> ()) {
|
|||
}
|
||||
|
||||
pub fn flatten(schema: &mut Schema, other: Schema) {
|
||||
match other.try_to_object() {
|
||||
Err(false) => {}
|
||||
Err(true) => {
|
||||
schema
|
||||
.ensure_object()
|
||||
.insert("additionalProperties".to_owned(), true.into());
|
||||
}
|
||||
Ok(obj2) => {
|
||||
let obj1 = schema.ensure_object();
|
||||
|
||||
for (key, value2) in obj2 {
|
||||
fn flatten_property(obj1: &mut Map<String, Value>, key: String, value2: Value) {
|
||||
match obj1.entry(key) {
|
||||
Entry::Vacant(vacant) => match vacant.key().as_str() {
|
||||
"additionalProperties" | "unevaluatedProperties" => {
|
||||
|
@ -204,7 +194,7 @@ pub fn flatten(schema: &mut Schema, other: Schema) {
|
|||
},
|
||||
Entry::Occupied(occupied) => {
|
||||
match occupied.key().as_str() {
|
||||
"required" => {
|
||||
"required" | "allOf" => {
|
||||
if let Value::Array(a1) = occupied.into_mut() {
|
||||
if let Value::Array(a2) = value2 {
|
||||
a1.extend(a2);
|
||||
|
@ -225,6 +215,19 @@ pub fn flatten(schema: &mut Schema, other: Schema) {
|
|||
*occupied.into_mut() = value2;
|
||||
}
|
||||
}
|
||||
"oneOf" | "anyOf" => {
|
||||
// `OccupiedEntry` currently has no `.remove_entry()` method :(
|
||||
let key = occupied.key().clone();
|
||||
let current = occupied.remove();
|
||||
flatten_property(
|
||||
obj1,
|
||||
"allOf".to_owned(),
|
||||
json!([
|
||||
{ &key: current },
|
||||
{ key: value2 }
|
||||
]),
|
||||
);
|
||||
}
|
||||
_ => {
|
||||
// leave the original value as it is (don't modify `schema`)
|
||||
}
|
||||
|
@ -232,6 +235,20 @@ pub fn flatten(schema: &mut Schema, other: Schema) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
match other.try_to_object() {
|
||||
Err(false) => {}
|
||||
Err(true) => {
|
||||
schema
|
||||
.ensure_object()
|
||||
.insert("additionalProperties".to_owned(), true.into());
|
||||
}
|
||||
Ok(obj2) => {
|
||||
let obj1 = schema.ensure_object();
|
||||
|
||||
for (key, value2) in obj2 {
|
||||
flatten_property(obj1, key, value2);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use crate::gen::SchemaGenerator;
|
||||
use crate::{json_schema, JsonSchema, Schema};
|
||||
use chrono04::prelude::*;
|
||||
use alloc::borrow::Cow;
|
||||
use chrono04::prelude::*;
|
||||
|
||||
impl JsonSchema for Weekday {
|
||||
always_inline!();
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use crate::gen::SchemaGenerator;
|
||||
use crate::{json_schema, JsonSchema, Schema};
|
||||
use semver1::Version;
|
||||
use alloc::borrow::Cow;
|
||||
use semver1::Version;
|
||||
|
||||
impl JsonSchema for Version {
|
||||
always_inline!();
|
||||
|
|
60
schemars/tests/enum_flatten.rs
Normal file
60
schemars/tests/enum_flatten.rs
Normal file
|
@ -0,0 +1,60 @@
|
|||
mod util;
|
||||
use schemars::JsonSchema;
|
||||
use util::*;
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(JsonSchema)]
|
||||
#[schemars(rename = "Flat")]
|
||||
struct Flat {
|
||||
f: f32,
|
||||
#[schemars(flatten)]
|
||||
e1: Enum1,
|
||||
#[schemars(flatten)]
|
||||
e2: Enum2,
|
||||
#[schemars(flatten)]
|
||||
e3: Enum3,
|
||||
#[schemars(flatten)]
|
||||
e4: Enum4,
|
||||
#[schemars(flatten)]
|
||||
e5: Enum5,
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(JsonSchema)]
|
||||
enum Enum1 {
|
||||
B(bool),
|
||||
S(String),
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(JsonSchema)]
|
||||
enum Enum2 {
|
||||
U(u32),
|
||||
F(f64),
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(JsonSchema)]
|
||||
enum Enum3 {
|
||||
B2(bool),
|
||||
S2(String),
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(JsonSchema)]
|
||||
enum Enum4 {
|
||||
U2(u32),
|
||||
F2(f64),
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(JsonSchema)]
|
||||
enum Enum5 {
|
||||
B3(bool),
|
||||
S3(String),
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_flat_schema() -> TestResult {
|
||||
test_default_generated_schema::<Flat>("enum_flatten")
|
||||
}
|
160
schemars/tests/expected/enum_flatten.json
Normal file
160
schemars/tests/expected/enum_flatten.json
Normal file
|
@ -0,0 +1,160 @@
|
|||
{
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"title": "Flat",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"f": {
|
||||
"type": "number",
|
||||
"format": "float"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"f"
|
||||
],
|
||||
"allOf": [
|
||||
{
|
||||
"oneOf": [
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"B": {
|
||||
"type": "boolean"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"B"
|
||||
],
|
||||
"additionalProperties": false
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"S": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"S"
|
||||
],
|
||||
"additionalProperties": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"oneOf": [
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"U": {
|
||||
"type": "integer",
|
||||
"format": "uint32",
|
||||
"minimum": 0
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"U"
|
||||
],
|
||||
"additionalProperties": false
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"F": {
|
||||
"type": "number",
|
||||
"format": "double"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"F"
|
||||
],
|
||||
"additionalProperties": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"oneOf": [
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"B2": {
|
||||
"type": "boolean"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"B2"
|
||||
],
|
||||
"additionalProperties": false
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"S2": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"S2"
|
||||
],
|
||||
"additionalProperties": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"oneOf": [
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"U2": {
|
||||
"type": "integer",
|
||||
"format": "uint32",
|
||||
"minimum": 0
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"U2"
|
||||
],
|
||||
"additionalProperties": false
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"F2": {
|
||||
"type": "number",
|
||||
"format": "double"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"F2"
|
||||
],
|
||||
"additionalProperties": false
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"oneOf": [
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"B3": {
|
||||
"type": "boolean"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"B3"
|
||||
],
|
||||
"additionalProperties": false
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"S3": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"S3"
|
||||
],
|
||||
"additionalProperties": false
|
||||
}
|
||||
]
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue