Skip to content

Commit

Permalink
refactor: DataModelExtension JsonSchema impl to fix dep issues
Browse files Browse the repository at this point in the history
  • Loading branch information
raimundo-henriques committed Oct 8, 2024
1 parent 73ba24f commit a6ad666
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 83 deletions.
58 changes: 29 additions & 29 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"

[dependencies]
chrono = { version = "0.4", features = ["serde"] }
schemars = { version = "0.8", features = ["chrono"] }
schemars = { version = "0.8.21", features = ["chrono"] }
rust_decimal = "1.35.0"
uuid = { version = "1.8", features = ["v4", "serde"] }
serde = { version = "1.0.203", features = ["derive"] }
Expand Down
8 changes: 2 additions & 6 deletions schema/data-model-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -522,17 +522,13 @@
],
"properties": {
"data": {
"type": "object",
"additionalProperties": true
"type": "object"
},
"dataSchema": {
"type": "string"
},
"documentation": {
"type": [
"string",
"null"
]
"type": "string"
},
"specVersion": {
"$ref": "#/definitions/VersionString"
Expand Down
86 changes: 39 additions & 47 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@
//! emission data at product level.
//!
//! See https://wbcsd.github.io/data-exchange-protocol/v2 for further details.
use std::collections::{BTreeMap, BTreeSet};

use chrono::{DateTime, Utc};
use rust_decimal::Decimal;
use schemars::schema::{
ArrayValidation, InstanceType, NumberValidation, ObjectValidation, Schema,
SchemaObject, SingleOrVec, StringValidation,
ArrayValidation, InstanceType, NumberValidation, Schema, SchemaObject, StringValidation,
};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -453,7 +450,6 @@ pub struct DataModelExtension<T: JsonSchema> {
pub data_schema: String, // Replace String with URL
#[serde(skip_serializing_if = "Option::is_none")]
pub documentation: Option<String>, // Replace String with URL

pub data: T,
}

Expand Down Expand Up @@ -892,60 +888,56 @@ impl JsonSchema for PfId {

impl<T: JsonSchema> JsonSchema for DataModelExtension<T> {
fn schema_name() -> String {
"DataModelExtension".into()
"DataModelExtension".to_string()
}

fn json_schema(_gen: &mut schemars::gen::SchemaGenerator) -> Schema {
let mut properties = BTreeMap::new();
properties.insert(
"data".to_string(),
Schema::Object(SchemaObject {
fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> Schema {
let mut schema_object = SchemaObject {
instance_type: Some(InstanceType::Object.into()),
..Default::default()
};

schema_object.object().required.insert("data".to_owned());
schema_object
.object()
.required
.insert("dataSchema".to_owned());
schema_object
.object()
.required
.insert("specVersion".to_owned());

schema_object.object().properties.insert(
"data".to_owned(),
SchemaObject {
instance_type: Some(InstanceType::Object.into()),
object: Some(Box::new(ObjectValidation {
additional_properties: Some(Box::new(Schema::Bool(true))),
..Default::default()
})),
..Default::default()
}),
}
.into(),
);

properties.insert(
"dataSchema".to_string(),
Schema::Object(SchemaObject {
schema_object.object().properties.insert(
"dataSchema".to_owned(),
SchemaObject {
instance_type: Some(InstanceType::String.into()),
..Default::default()
}),
}
.into(),
);
properties.insert(
"documentation".to_string(),
Schema::Object(SchemaObject {
instance_type: Some(SingleOrVec::Vec(vec![
InstanceType::String,
InstanceType::Null,
])),

schema_object.object().properties.insert(
"documentation".to_owned(),
SchemaObject {
instance_type: Some(InstanceType::String.into()),
..Default::default()
}),
}
.into(),
);
properties.insert(
"specVersion".to_string(),
Schema::Object(SchemaObject {
reference: Some("#/definitions/VersionString".to_string()),
..Default::default()
}),

schema_object.object().properties.insert(
"specVersion".to_owned(),
gen.subschema_for::<SpecVersionString>(),
);
let schema_object = SchemaObject {
instance_type: Some(InstanceType::Object.into()),
object: Some(Box::new(ObjectValidation {
properties,
required: BTreeSet::from_iter(vec![
"data".to_string(),
"dataSchema".to_string(),
"specVersion".to_string(),
]),
..Default::default()
})),
..Default::default()
};

Schema::Object(schema_object)
}
Expand Down

0 comments on commit a6ad666

Please sign in to comment.