Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multiple flatten enums #320

Merged
merged 4 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 57 additions & 40 deletions schemars/src/_private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,62 @@ pub fn apply_inner_validation(schema: &mut Schema, f: fn(&mut Schema) -> ()) {
}

pub fn flatten(schema: &mut Schema, other: Schema) {
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" => {
if value2 != Value::Bool(false) {
vacant.insert(value2);
}
}
_ => {
vacant.insert(value2);
}
},
Entry::Occupied(occupied) => {
match occupied.key().as_str() {
"required" | "allOf" => {
if let Value::Array(a1) = occupied.into_mut() {
if let Value::Array(a2) = value2 {
a1.extend(a2);
}
}
}
"properties" | "patternProperties" => {
if let Value::Object(o1) = occupied.into_mut() {
if let Value::Object(o2) = value2 {
o1.extend(o2);
}
}
}
"additionalProperties" | "unevaluatedProperties" => {
// Even if an outer type has `deny_unknown_fields`, unknown fields
// may be accepted by the flattened type
if occupied.get() == &Value::Bool(false) {
*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`)
}
};
}
}
}

match other.try_to_object() {
Err(false) => {}
Err(true) => {
Expand All @@ -191,46 +247,7 @@ pub fn flatten(schema: &mut Schema, other: Schema) {
let obj1 = schema.ensure_object();

for (key, value2) in obj2 {
match obj1.entry(key) {
Entry::Vacant(vacant) => match vacant.key().as_str() {
"additionalProperties" | "unevaluatedProperties" => {
if value2 != Value::Bool(false) {
vacant.insert(value2);
}
}
_ => {
vacant.insert(value2);
}
},
Entry::Occupied(occupied) => {
match occupied.key().as_str() {
"required" => {
if let Value::Array(a1) = occupied.into_mut() {
if let Value::Array(a2) = value2 {
a1.extend(a2);
}
}
}
"properties" | "patternProperties" => {
if let Value::Object(o1) = occupied.into_mut() {
if let Value::Object(o2) = value2 {
o1.extend(o2);
}
}
}
"additionalProperties" | "unevaluatedProperties" => {
// Even if an outer type has `deny_unknown_fields`, unknown fields
// may be accepted by the flattened type
if occupied.get() == &Value::Bool(false) {
*occupied.into_mut() = value2;
}
}
_ => {
// leave the original value as it is (don't modify `schema`)
}
};
}
}
flatten_property(obj1, key, value2);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion schemars/src/json_schema_impls/chrono04.rs
Original file line number Diff line number Diff line change
@@ -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!();
Expand Down
2 changes: 1 addition & 1 deletion schemars/src/json_schema_impls/semver1.rs
Original file line number Diff line number Diff line change
@@ -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!();
Expand Down
60 changes: 60 additions & 0 deletions schemars/tests/enum_flatten.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 160 additions & 0 deletions schemars/tests/expected/enum_flatten.json
Original file line number Diff line number Diff line change
@@ -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