Skip to content
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
33 changes: 27 additions & 6 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 policies/rust/now-policy-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ policy-compat = ["dep:now-policy"]
chrono = { version = "0.4", features = ["serde"] }
derive_more = { version = "2", features = ["as_ref", "deref", "display", "from"] }
now-policy = { version = "0.2", path = "../now-policy", optional = true }
schemars = { version = "0.8", features = ["chrono"] }
schemars = { version = "0.9", features = ["chrono04"] }
semver = "1"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
Expand Down
18 changes: 6 additions & 12 deletions policies/rust/now-policy-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,14 @@ macro_rules! fixed_string_marker {
}

impl JsonSchema for $name {
fn schema_name() -> String {
stringify!($name).to_owned()
fn schema_name() -> std::borrow::Cow<'static, str> {
stringify!($name).into()
}

fn json_schema(_gen: &mut schemars::r#gen::SchemaGenerator) -> schemars::schema::Schema {
schemars::schema::Schema::Object(schemars::schema::SchemaObject {
instance_type: Some(schemars::schema::SingleOrVec::Single(Box::new(
schemars::schema::InstanceType::String,
))),
string: Some(Box::new(schemars::schema::StringValidation {
pattern: Some(format!("^{}$", $value)),
..Default::default()
})),
..Default::default()
fn json_schema(_gen: &mut schemars::SchemaGenerator) -> schemars::Schema {
schemars::json_schema!({
"type": "string",
"pattern": format!("^{}$", $value),
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions policies/rust/now-policy-server-template/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ default = []
policy-compat = ["dep:now-policy", "now-policy-api/policy-compat"]

[dependencies]
aide = { version = "0.14", features = ["axum", "axum-json"] }
aide = { version = "0.15", features = ["axum", "axum-json"] }
async-trait = "0.1"
axum = { version = "0.8", default-features = false, features = ["json"] }
now-policy-api = { version = "0.3", path = "../now-policy-api" }
now-policy = { version = "0.2", path = "../now-policy", optional = true }
schemars = "0.8"
schemars = "0.9"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
serde_yaml = "0.9"
Expand Down
21 changes: 7 additions & 14 deletions policies/rust/now-policy-server-template/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub fn openapi() -> OpenApi {
}

fn openapi_schema_generator() -> SchemaGenerator {
use schemars::r#gen::SchemaSettings;
use schemars::generate::SchemaSettings;

SchemaSettings::openapi3().into()
}
Expand All @@ -108,24 +108,17 @@ fn openapi_schema_generator() -> SchemaGenerator {
fn register_policy_schema(api: &mut OpenApi) {
use aide::openapi::{Components, SchemaObject};
use now_policy::PolicyDocument;
use schemars::schema::Schema;
use schemars::Schema;

let root = openapi_schema_generator().into_root_schema_for::<PolicyDocument>();
let mut generator = openapi_schema_generator();
let _ = generator.subschema_for::<PolicyDocument>();
let definitions = generator.take_definitions(true);

let components = api.components.get_or_insert_with(Components::default);

components
.schemas
.entry("PolicyDocument".to_owned())
.or_insert_with(|| SchemaObject {
json_schema: Schema::Object(root.schema),
external_docs: None,
example: None,
});

for (name, schema) in root.definitions {
for (name, schema) in definitions {
components.schemas.entry(name).or_insert_with(|| SchemaObject {
json_schema: schema,
json_schema: Schema::try_from(schema).expect("schemars generated an invalid schema"),
external_docs: None,
example: None,
});
Expand Down
2 changes: 1 addition & 1 deletion policies/rust/now-policy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ workspace = true

[dependencies]
chrono = { version = "0.4", features = ["serde"] }
schemars = { version = "0.8", features = ["chrono"] }
schemars = { version = "0.9", features = ["chrono04"] }
semver = "1"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
Expand Down
18 changes: 7 additions & 11 deletions policies/rust/now-policy/src/markers.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//! Marker types -- zero-size structs that serialize to a fixed string constant.

use schemars::JsonSchema;
use schemars::r#gen::SchemaGenerator;
use schemars::schema::{InstanceType, Schema, SchemaObject, SingleOrVec};
use schemars::{JsonSchema, Schema, SchemaGenerator, json_schema};
use serde::{Deserialize, Serialize};

macro_rules! fixed_string_marker {
Expand Down Expand Up @@ -35,17 +33,15 @@ macro_rules! fixed_string_marker {
}

impl JsonSchema for $name {
fn schema_name() -> String {
stringify!($name).to_owned()
fn schema_name() -> std::borrow::Cow<'static, str> {
stringify!($name).into()
}

fn json_schema(_gen: &mut SchemaGenerator) -> Schema {
SchemaObject {
instance_type: Some(SingleOrVec::Single(Box::new(InstanceType::String))),
enum_values: Some(vec![serde_json::Value::String($value.to_owned())]),
..Default::default()
}
.into()
json_schema!({
"type": "string",
"enum": [$value],
})
}
}
};
Expand Down
25 changes: 8 additions & 17 deletions policies/rust/now-policy/src/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
use std::collections::BTreeSet;

use chrono::{DateTime, Utc};
use schemars::JsonSchema;
use schemars::r#gen::SchemaGenerator;
use schemars::schema::{ObjectValidation, Schema, SchemaObject, SubschemaValidation};
use schemars::{JsonSchema, Schema, SchemaGenerator, json_schema};
use serde::{Deserialize, Serialize};

use crate::{
Expand Down Expand Up @@ -154,25 +152,18 @@ fn deserialize_non_empty_match<'de, D: serde::Deserializer<'de>>(deserializer: D
struct NonEmptyPolicyMatchSchema;

impl JsonSchema for NonEmptyPolicyMatchSchema {
fn is_referenceable() -> bool {
false
fn inline_schema() -> bool {
true
}

fn schema_name() -> String {
"NonEmptyPolicyMatch".to_owned()
fn schema_name() -> std::borrow::Cow<'static, str> {
"NonEmptyPolicyMatch".into()
}

fn json_schema(generator: &mut SchemaGenerator) -> Schema {
Schema::Object(SchemaObject {
object: Some(Box::new(ObjectValidation {
min_properties: Some(1),
..Default::default()
})),
subschemas: Some(Box::new(SubschemaValidation {
all_of: Some(vec![generator.subschema_for::<PolicyMatch>()]),
..Default::default()
})),
..Default::default()
json_schema!({
"minProperties": 1,
"allOf": [generator.subschema_for::<PolicyMatch>()],
})
}
}
Expand Down
9 changes: 6 additions & 3 deletions policies/rust/now-policy/tests/policy_samples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,12 @@ fn policy_schema_generates_valid_json() {
#[test]
fn policy_match_schema_requires_at_least_one_property() {
let schema = now_policy::schema::policy_schema_json();
let min_properties = schema
.pointer("/definitions/PolicyRule/properties/Match/minProperties")
.and_then(serde_json::Value::as_u64);
let min_properties = [
"/definitions/PolicyRule/properties/Match/minProperties",
"/$defs/PolicyRule/properties/Match/minProperties",
]
.into_iter()
.find_map(|path| schema.pointer(path).and_then(serde_json::Value::as_u64));

assert_eq!(min_properties, Some(1));
}