Skip to content

Commit

Permalink
Refactor flatten and move it to _private, remove `TempFixupForTes…
Browse files Browse the repository at this point in the history
…ts`, regenerate test schemas
  • Loading branch information
GREsau committed May 13, 2024
1 parent 18300c6 commit c4d42ec
Show file tree
Hide file tree
Showing 45 changed files with 276 additions and 326 deletions.
60 changes: 60 additions & 0 deletions schemars/src/_private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::JsonSchema;
use crate::Schema;
use serde::Serialize;
use serde_json::json;
use serde_json::map::Entry;
use serde_json::Map;
use serde_json::Value;

Expand Down Expand Up @@ -174,3 +175,62 @@ pub fn apply_inner_validation(schema: &mut Schema, f: fn(&mut Schema) -> ()) {
f(inner_schema);
}
}

pub fn flatten(schema: &mut Schema, other: Schema) {
if let Value::Object(obj2) = other.to_value() {
let obj1 = schema.ensure_object();

for (key, value2) in obj2 {
match obj1.entry(key) {
Entry::Vacant(vacant) => {
vacant.insert(value2);
}
Entry::Occupied(mut occupied) => {
match occupied.key().as_str() {
// This special "type" handling can probably be removed once the enum variant `with`/`schema_with` behaviour is fixed
"type" => match (occupied.get_mut(), value2) {
(Value::Array(a1), Value::Array(mut a2)) => {
a2.retain(|v2| !a1.contains(v2));
a1.extend(a2);
}
(v1, Value::Array(mut a2)) => {
if !a2.contains(v1) {
a2.push(std::mem::take(v1));
*occupied.get_mut() = Value::Array(a2);
}
}
(Value::Array(a1), v2) => {
if !a1.contains(&v2) {
a1.push(v2);
}
}
(v1, v2) => {
if v1 != &v2 {
*occupied.get_mut() =
Value::Array(vec![std::mem::take(v1), v2]);
}
}
},
"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);
}
}
}
_ => {
// leave the original value as it is (don't modify `schema`)
}
};
}
}
}
}
}
71 changes: 0 additions & 71 deletions schemars/src/flatten.rs

This file was deleted.

1 change: 0 additions & 1 deletion schemars/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![deny(unsafe_code)]
#![doc = include_str!("../README.md")]

mod flatten;
mod json_schema_impls;
mod schema;
mod ser;
Expand Down
4 changes: 2 additions & 2 deletions schemars/tests/expected/bytes.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
"items": {
"type": "integer",
"format": "uint8",
"minimum": 0.0
"minimum": 0
}
},
{
"type": "array",
"items": {
"type": "integer",
"format": "uint8",
"minimum": 0.0
"minimum": 0
}
}
],
Expand Down
4 changes: 2 additions & 2 deletions schemars/tests/expected/chrono-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
}
},
"required": [
"weekday",
"date_time",
"naive_date",
"naive_date_time",
"naive_time",
"weekday"
"naive_time"
]
}
4 changes: 2 additions & 2 deletions schemars/tests/expected/crate_alias.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
}
},
"required": [
"bar",
"foo"
"foo",
"bar"
]
}
4 changes: 2 additions & 2 deletions schemars/tests/expected/deprecated-enum.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
}
},
"required": [
"deprecated_field",
"foo"
"foo",
"deprecated_field"
]
}
},
Expand Down
4 changes: 2 additions & 2 deletions schemars/tests/expected/deprecated-struct.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"deprecated": true,
"required": [
"deprecated_field",
"foo"
"foo",
"deprecated_field"
]
}
16 changes: 8 additions & 8 deletions schemars/tests/expected/duration_and_systemtime.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@
"nanos": {
"type": "integer",
"format": "uint32",
"minimum": 0.0
"minimum": 0
},
"secs": {
"type": "integer",
"format": "uint64",
"minimum": 0.0
"minimum": 0
}
},
"required": [
"nanos",
"secs"
"secs",
"nanos"
]
},
"SystemTime": {
Expand All @@ -40,17 +40,17 @@
"nanos_since_epoch": {
"type": "integer",
"format": "uint32",
"minimum": 0.0
"minimum": 0
},
"secs_since_epoch": {
"type": "integer",
"format": "uint64",
"minimum": 0.0
"minimum": 0
}
},
"required": [
"nanos_since_epoch",
"secs_since_epoch"
"secs_since_epoch",
"nanos_since_epoch"
]
}
}
Expand Down
32 changes: 16 additions & 16 deletions schemars/tests/expected/enum-adjacent-tagged-duf.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
},
"additionalProperties": false,
"required": [
"c",
"t"
"t",
"c"
]
},
{
Expand All @@ -54,8 +54,8 @@
},
"additionalProperties": false,
"required": [
"c",
"t"
"t",
"c"
]
},
{
Expand All @@ -73,8 +73,8 @@
},
"additionalProperties": false,
"required": [
"c",
"t"
"t",
"c"
]
},
{
Expand All @@ -93,8 +93,8 @@
},
"additionalProperties": false,
"required": [
"bar",
"foo"
"foo",
"bar"
]
},
"t": {
Expand All @@ -106,8 +106,8 @@
},
"additionalProperties": false,
"required": [
"c",
"t"
"t",
"c"
]
},
{
Expand Down Expand Up @@ -136,8 +136,8 @@
},
"additionalProperties": false,
"required": [
"c",
"t"
"t",
"c"
]
},
{
Expand Down Expand Up @@ -171,8 +171,8 @@
},
"additionalProperties": false,
"required": [
"c",
"t"
"t",
"c"
]
}
],
Expand All @@ -189,8 +189,8 @@
}
},
"required": [
"bar",
"foo"
"foo",
"bar"
]
},
"UnitStruct": {
Expand Down
Loading

0 comments on commit c4d42ec

Please sign in to comment.