Skip to content

Commit eefce3a

Browse files
authored
update schemars to 0.9.0 (#88)
Signed-off-by: Teo Koon Peng <[email protected]>
1 parent 0fec55c commit eefce3a

File tree

7 files changed

+515
-586
lines changed

7 files changed

+515
-586
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ thiserror = "1.0"
4646
bevy_core = "0.12"
4747
bevy_time = "0.12"
4848

49-
schemars = { version = "0.8.21", optional = true }
49+
schemars = { version = "0.9.0", optional = true }
5050
serde = { version = "1.0.210", features = ["derive", "rc"], optional = true }
5151
serde_json = { version = "1.0.128", optional = true }
5252
cel-interpreter = { version = "0.9.0", features = ["json"], optional = true }

diagram.schema.json

Lines changed: 488 additions & 465 deletions
Large diffs are not rendered by default.

examples/zenoh-examples/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ bevy_ecs = "0.12"
1818
bevy_time = "0.12"
1919
bevy_impulse = { version = "0.0.2", path = "../..", features = ["diagram"] }
2020
futures = "0.3"
21-
schemars = { version = "0.8.21" }
21+
schemars = { version = "0.9.0" }
2222
serde = { version = "1.0.210", features = ["derive", "rc"] }
2323
serde_json = "1.0.128"
2424
prost = "0.13.5"

src/diagram.rs

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,7 @@ use crate::{
6464
SpawnWorkflowExt, SplitConnectionError, StreamPack,
6565
};
6666

67-
use schemars::{
68-
r#gen::SchemaGenerator,
69-
schema::{InstanceType, Metadata, ObjectValidation, Schema, SchemaObject, SingleOrVec},
70-
JsonSchema,
71-
};
67+
use schemars::{json_schema, JsonSchema, Schema, SchemaGenerator};
7268

7369
use serde::{
7470
de::{Error, Visitor},
@@ -191,29 +187,21 @@ impl<'de> Deserialize<'de> for NamespacedOperation {
191187
}
192188

193189
impl JsonSchema for NamespacedOperation {
194-
fn json_schema(generator: &mut SchemaGenerator) -> Schema {
195-
let mut schema = SchemaObject::default();
196-
schema.instance_type = Some(SingleOrVec::Single(Box::new(InstanceType::Object)));
197-
schema.object = Some(Box::new(ObjectValidation {
198-
max_properties: Some(1),
199-
min_properties: Some(1),
200-
required: Default::default(),
201-
properties: Default::default(),
202-
pattern_properties: Default::default(),
203-
property_names: Default::default(),
204-
additional_properties: Some(Box::new(generator.subschema_for::<String>())),
205-
}));
206-
schema.metadata = Some(Box::new(Metadata {
207-
title: Some("NamespacedOperation".to_string()),
208-
description: Some("Refer to an operation inside of a namespace, e.g. { \"<namespace>\": \"<operation>\"".to_string()),
209-
..Default::default()
210-
}));
211-
212-
Schema::Object(schema)
190+
fn json_schema(_generator: &mut SchemaGenerator) -> Schema {
191+
json_schema!({
192+
"title": "NamespacedOperation",
193+
"description": "Refer to an operation inside of a namespace, e.g. { \"<namespace>\": \"<operation>\"",
194+
"type": "object",
195+
"maxProperties": 1,
196+
"minProperties": 1,
197+
"additionalProperties": {
198+
"type": "string"
199+
}
200+
})
213201
}
214202

215-
fn schema_name() -> String {
216-
"NamespacedOperation".to_string()
203+
fn schema_name() -> Cow<'static, str> {
204+
"NamespacedOperation".into()
217205
}
218206
}
219207

@@ -984,7 +972,7 @@ impl BuildDiagramOperation for DiagramOperation {
984972
}
985973

986974
/// Returns the schema for [`String`]
987-
fn schema_with_string(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
975+
fn schema_with_string(gen: &mut SchemaGenerator) -> Schema {
988976
gen.subschema_for::<String>()
989977
}
990978

src/diagram/registration.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,7 @@ use crate::{
3434
StreamPack,
3535
};
3636

37-
use schemars::{
38-
gen::{SchemaGenerator, SchemaSettings},
39-
schema::Schema,
40-
JsonSchema,
41-
};
37+
use schemars::{generate::SchemaSettings, JsonSchema, Schema, SchemaGenerator};
4238
use serde::{
4339
de::DeserializeOwned,
4440
ser::{SerializeMap, SerializeStruct},
@@ -732,7 +728,7 @@ impl Serialize for MessageOperation {
732728

733729
pub struct MessageRegistration {
734730
pub(super) type_name: &'static str,
735-
pub(super) schema: Option<schemars::schema::Schema>,
731+
pub(super) schema: Option<Schema>,
736732
pub(super) operations: MessageOperation,
737733
}
738734

@@ -776,7 +772,7 @@ pub struct MessageRegistry {
776772
impl MessageRegistry {
777773
fn new() -> Self {
778774
let mut settings = SchemaSettings::default();
779-
settings.definitions_path = "#/schemas/".to_string();
775+
settings.definitions_path = "#/schemas/".into();
780776

781777
Self {
782778
schema_generator: SchemaGenerator::new(settings),

src/diagram/section_schema.rs

Lines changed: 1 addition & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
use std::{collections::HashMap, sync::Arc};
1919

20-
use schemars::{schema::Schema, JsonSchema};
20+
use schemars::JsonSchema;
2121
use serde::{Deserialize, Serialize};
2222

2323
use crate::{
@@ -39,89 +39,10 @@ pub enum SectionProvider {
3939
Template(OperationName),
4040
}
4141

42-
/// schemars generates schemas with `additionalProperties: false` for enums.
43-
/// When the enum is flatten, that `additionalProperties: false` is inherited by the parent
44-
/// struct, which leads to schemas that can never be valid.
45-
///
46-
/// Example:
47-
///
48-
/// ```json
49-
/// {
50-
/// "description": "Connect the request to a registered section.\n\n``` # bevy_impulse::Diagram::from_json_str(r#\" { \"version\": \"0.1.0\", \"start\": \"section_op\", \"ops\": { \"section_op\": { \"type\": \"section\", \"builder\": \"my_section_builder\", \"connect\": { \"my_section_output\": { \"builtin\": \"terminate\" } } } } } # \"#)?; # Ok::<_, serde_json::Error>(()) ```\n\nCustom sections can also be created via templates ``` # bevy_impulse::Diagram::from_json_str(r#\" { \"version\": \"0.1.0\", \"templates\": { \"my_template\": { \"inputs\": [\"section_input\"], \"outputs\": [\"section_output\"], \"buffers\": [], \"ops\": { \"section_input\": { \"type\": \"node\", \"builder\": \"my_node\", \"next\": \"section_output\" } } } }, \"start\": \"section_op\", \"ops\": { \"section_op\": { \"type\": \"section\", \"template\": \"my_template\", \"connect\": { \"section_output\": { \"builtin\": \"terminate\" } } } } } # \"#)?; # Ok::<_, serde_json::Error>(()) ```",
51-
/// "type": "object",
52-
/// "oneOf": [
53-
/// {
54-
/// "type": "object",
55-
/// "required": [
56-
/// "builder"
57-
/// ],
58-
/// "properties": {
59-
/// "builder": {
60-
/// "type": "string"
61-
/// }
62-
/// },
63-
/// "additionalProperties": false
64-
/// },
65-
/// {
66-
/// "type": "object",
67-
/// "required": [
68-
/// "template"
69-
/// ],
70-
/// "properties": {
71-
/// "template": {
72-
/// "type": "string"
73-
/// }
74-
/// },
75-
/// "additionalProperties": false
76-
/// }
77-
/// ],
78-
/// "required": [
79-
/// "type"
80-
/// ],
81-
/// "properties": {
82-
/// "config": {
83-
/// "default": null
84-
/// },
85-
/// "connect": {
86-
/// "default": {},
87-
/// "type": "object",
88-
/// "additionalProperties": {
89-
/// "$ref": "#/definitions/NextOperation"
90-
/// }
91-
/// },
92-
/// "type": {
93-
/// "type": "string",
94-
/// "enum": [
95-
/// "section"
96-
/// ]
97-
/// }
98-
/// }
99-
/// },
100-
/// ```
101-
///
102-
/// Here the section schema needs to have a `builder` or `template` with no additional properties.
103-
/// Which includes other properties like `type`, `config` etc, but `type` is also required which
104-
/// breaks the schema.
105-
fn fix_additional_properties(generator: &mut schemars::SchemaGenerator) -> Schema {
106-
let mut schema = generator.root_schema_for::<SectionProvider>().schema;
107-
schema.metadata.as_mut().unwrap().title = None;
108-
let one_ofs = schema.subschemas.as_mut().unwrap().one_of.as_mut().unwrap();
109-
for subschema in one_ofs {
110-
match subschema {
111-
Schema::Object(schema) => schema.object.as_mut().unwrap().additional_properties = None,
112-
_ => {
113-
panic!("expected object schema")
114-
}
115-
}
116-
}
117-
Schema::Object(schema)
118-
}
119-
12042
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
12143
#[serde(rename_all = "snake_case")]
12244
pub struct SectionSchema {
12345
#[serde(flatten)]
124-
#[schemars(schema_with = "fix_additional_properties")]
12546
pub(super) provider: SectionProvider,
12647
#[serde(default)]
12748
pub(super) config: serde_json::Value,

src/diagram/serialization.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
*/
1717

1818
use std::{
19+
borrow::Cow,
1920
collections::{hash_map::Entry, HashMap},
2021
sync::Arc,
2122
};
2223

23-
use schemars::{gen::SchemaGenerator, JsonSchema};
24+
use schemars::{JsonSchema, Schema, SchemaGenerator};
2425
use serde::{de::DeserializeOwned, Serialize};
2526

2627
use super::{
@@ -31,20 +32,20 @@ use crate::{Builder, JsonBuffer};
3132

3233
pub trait DynType {
3334
/// Returns the type name of the request. Note that the type name must be unique.
34-
fn type_name() -> String;
35+
fn type_name() -> Cow<'static, str>;
3536

36-
fn json_schema(gen: &mut SchemaGenerator) -> schemars::schema::Schema;
37+
fn json_schema(gen: &mut SchemaGenerator) -> Schema;
3738
}
3839

3940
impl<T> DynType for T
4041
where
4142
T: JsonSchema,
4243
{
43-
fn type_name() -> String {
44+
fn type_name() -> Cow<'static, str> {
4445
<T>::schema_name()
4546
}
4647

47-
fn json_schema(gen: &mut SchemaGenerator) -> schemars::schema::Schema {
48+
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
4849
gen.subschema_for::<T>()
4950
}
5051
}

0 commit comments

Comments
 (0)