diff --git a/cedar-policy/CHANGELOG.md b/cedar-policy/CHANGELOG.md index 04fb9d175..0bb484f1c 100644 --- a/cedar-policy/CHANGELOG.md +++ b/cedar-policy/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- Marked the `Template::from_json` and `Template::to_json` apis as public - New APIs to `Entities` to make it easy to add a collection of entities to an existing `Entities` structure. (#276) - Export the `cedar_policy_core::evaluator::{EvaluationError, EvaluationErrorKind}` and diff --git a/cedar-policy/src/api.rs b/cedar-policy/src/api.rs index 20a00a340..2437af0a4 100644 --- a/cedar-policy/src/api.rs +++ b/cedar-policy/src/api.rs @@ -2298,8 +2298,7 @@ impl Template { /// If `id` is Some, the policy will be given that Policy Id. /// If `id` is None, then "JSON policy" will be used. /// The behavior around None may change in the future. - #[allow(dead_code)] // planned to be a public method in the future - fn from_json( + pub fn from_json( id: Option, json: serde_json::Value, ) -> Result { @@ -2312,8 +2311,7 @@ impl Template { } /// Get the JSON representation of this `Template`. - #[allow(dead_code)] // planned to be a public method in the future - fn to_json(&self) -> Result { + pub fn to_json(&self) -> Result { let est = self.lossless.est()?; let json = serde_json::to_value(est)?; Ok::<_, PolicyToJsonError>(json) diff --git a/cedar-policy/src/tests.rs b/cedar-policy/src/tests.rs index 808cd6bf9..a65ce0453 100644 --- a/cedar-policy/src/tests.rs +++ b/cedar-policy/src/tests.rs @@ -2726,3 +2726,28 @@ mod schema_based_parsing_tests { ); } } + +mod template_tests { + use crate::Template; + + #[test] + fn test_policy_template_to_json() { + let template = Template::parse( + None, + "permit(principal == ?principal, action, resource in ?resource);", + ); + assert_eq!( + template.unwrap().to_json().unwrap().to_string(), + r#"{"effect":"permit","principal":{"op":"==","slot":"?principal"},"action":{"op":"All"},"resource":{"op":"in","slot":"?resource"},"conditions":[]}"# + ); + } + + #[test] + fn test_policy_template_from_json() { + let template = Template::from_json(None, serde_json::from_str(r#"{"effect":"permit","principal":{"op":"==","slot":"?principal"},"action":{"op":"All"},"resource":{"op":"in","slot":"?resource"},"conditions":[]}"#).unwrap()); + assert_eq!( + template.unwrap().to_string(), + "permit(principal == ?principal, action, resource in ?resource);".to_string() + ); + } +}