Skip to content

Commit bcb8bd9

Browse files
Print tags when printing schemas in the Cedar format (#1304)
Signed-off-by: Shaobo He <shaobohe@amazon.com>
1 parent be447bd commit bcb8bd9

4 files changed

Lines changed: 49 additions & 7 deletions

File tree

‎Cargo.lock‎

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎cedar-policy-validator/src/cedar_schema/fmt.rs‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ impl<N: Display> Display for json_schema::EntityType<N> {
118118
write!(f, " = {ty}")?;
119119
}
120120

121+
if let Some(tags) = &self.tags {
122+
write!(f, " tags {tags}")?;
123+
}
124+
121125
Ok(())
122126
}
123127
}
@@ -232,3 +236,27 @@ pub fn json_schema_to_cedar_schema_str<N: Display>(
232236
}
233237
Ok(json_schema.to_string())
234238
}
239+
240+
#[cfg(test)]
241+
mod tests {
242+
use cedar_policy_core::extensions::Extensions;
243+
244+
use crate::cedar_schema::parser::parse_cedar_schema_fragment;
245+
246+
#[test]
247+
fn rfc_example() {
248+
let src = "entity User = {
249+
jobLevel: Long,
250+
} tags Set<String>;
251+
entity Document = {
252+
owner: User,
253+
} tags Set<String>;";
254+
let (cedar_schema, _) =
255+
parse_cedar_schema_fragment(src, Extensions::none()).expect("should parse");
256+
let printed_cedar_schema = cedar_schema.to_cedarschema().expect("should convert");
257+
let (parsed_cedar_schema, _) =
258+
parse_cedar_schema_fragment(&printed_cedar_schema, Extensions::none())
259+
.expect("should parse");
260+
assert_eq!(cedar_schema, parsed_cedar_schema);
261+
}
262+
}

‎cedar-policy-validator/src/json_schema.rs‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2511,9 +2511,9 @@ mod entity_tags {
25112511
use serde_json::json;
25122512

25132513
/// This schema taken directly from the RFC 82 text
2514-
#[test]
2515-
fn basic() {
2516-
let json = json!({"": {
2514+
#[track_caller]
2515+
fn example_json_schema() -> serde_json::Value {
2516+
json!({"": {
25172517
"entityTypes": {
25182518
"User" : {
25192519
"shape" : {
@@ -2546,7 +2546,20 @@ mod entity_tags {
25462546
}
25472547
},
25482548
"actions": {}
2549-
}});
2549+
}})
2550+
}
2551+
2552+
#[test]
2553+
fn roundtrip() {
2554+
let json = example_json_schema();
2555+
let json_schema = Fragment::from_json_value(json.clone()).expect("should be valid");
2556+
let serialized_json_schema = serde_json::to_value(json_schema).expect("should be valid");
2557+
assert_eq!(json, serialized_json_schema);
2558+
}
2559+
2560+
#[test]
2561+
fn basic() {
2562+
let json = example_json_schema();
25502563
assert_matches!(Fragment::from_json_value(json), Ok(frag) => {
25512564
let user = frag.0.get(&None).unwrap().entity_types.get(&"User".parse().unwrap()).unwrap();
25522565
assert_matches!(&user.tags, Some(Type::Type(TypeVariant::Set { element })) => {

‎cedar-policy/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Cedar Language Version: TBD
2828
### Fixed
2929

3030
- Some misleading parser errors for JSON schema with mistakes in nested attribute definitions (#1270, resolving #417)
31+
- Cedar schema printer now correctly prints entity tags (#1304)
3132

3233
## [4.2.1] - 2024-10-08
3334
Cedar Language version: 4.1

0 commit comments

Comments
 (0)