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
20 changes: 10 additions & 10 deletions cedar-policy-validator/src/cedar_schema/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2337,7 +2337,7 @@ mod ea_maps {
#[test]
fn entity_attribute() {
let src = "entity E { tags: { ?: Set<String> } };";
assert_matches!(collect_warnings(json_schema::Fragment::from_cedarschema_str(src, &Extensions::all_available())), Ok((frag, warnings)) => {
assert_matches!(collect_warnings(json_schema::Fragment::from_cedarschema_str(src, Extensions::all_available())), Ok((frag, warnings)) => {
assert!(warnings.is_empty());
let entity_type = frag.0.get(&None).unwrap().entity_types.get(&"E".parse().unwrap()).unwrap();
assert_matches!(&entity_type.shape, json_schema::EntityAttributes::EntityAttributes(json_schema::EntityAttributesInternal { attrs, .. }) => {
Expand All @@ -2356,7 +2356,7 @@ mod ea_maps {
#[test]
fn record_attribute_inside_entity_attribute() {
let src = "entity E { tags: { foo: { ?: Set<String> } } };";
assert_matches!(collect_warnings(json_schema::Fragment::from_cedarschema_str(src, &Extensions::all_available())), Err(e) => {
assert_matches!(collect_warnings(json_schema::Fragment::from_cedarschema_str(src, Extensions::all_available())), Err(e) => {
expect_err(
src,
&miette::Report::new(e),
Expand All @@ -2370,7 +2370,7 @@ mod ea_maps {
#[test]
fn context_attribute() {
let src = "entity E; action read appliesTo { principal: [E], resource: [E], context: { operationDetails: { ?: String } } };";
assert_matches!(collect_warnings(json_schema::Fragment::from_cedarschema_str(src, &Extensions::all_available())), Err(e) => {
assert_matches!(collect_warnings(json_schema::Fragment::from_cedarschema_str(src, Extensions::all_available())), Err(e) => {
expect_err(
src,
&miette::Report::new(e),
Expand All @@ -2384,7 +2384,7 @@ mod ea_maps {
#[test]
fn toplevel_entity() {
let src = "entity E { ?: Set<String> };";
assert_matches!(collect_warnings(json_schema::Fragment::from_cedarschema_str(src, &Extensions::all_available())), Err(e) => {
assert_matches!(collect_warnings(json_schema::Fragment::from_cedarschema_str(src, Extensions::all_available())), Err(e) => {
expect_err(
src,
&miette::Report::new(e),
Expand All @@ -2399,7 +2399,7 @@ mod ea_maps {
#[test]
fn toplevel_context() {
let src = "entity E; action read appliesTo { principal: [E], resource: [E], context: { ?: String } };";
assert_matches!(collect_warnings(json_schema::Fragment::from_cedarschema_str(src, &Extensions::all_available())), Err(e) => {
assert_matches!(collect_warnings(json_schema::Fragment::from_cedarschema_str(src, Extensions::all_available())), Err(e) => {
expect_err(
src,
&miette::Report::new(e),
Expand All @@ -2413,7 +2413,7 @@ mod ea_maps {
#[test]
fn common_type() {
let src = "type blah = { ?: String }; entity User { blah: blah };";
assert_matches!(collect_warnings(json_schema::Fragment::from_cedarschema_str(src, &Extensions::all_available())), Err(e) => {
assert_matches!(collect_warnings(json_schema::Fragment::from_cedarschema_str(src, Extensions::all_available())), Err(e) => {
expect_err(
src,
&miette::Report::new(e),
Expand All @@ -2427,7 +2427,7 @@ mod ea_maps {
#[test]
fn value_type_is_common_type() {
let src = "type blah = { foo: String }; entity User { blah: { ?: blah } };";
assert_matches!(collect_warnings(json_schema::Fragment::from_cedarschema_str(src, &Extensions::all_available())), Ok((frag, warnings)) => {
assert_matches!(collect_warnings(json_schema::Fragment::from_cedarschema_str(src, Extensions::all_available())), Ok((frag, warnings)) => {
assert!(warnings.is_empty());
let user = frag.0.get(&None).unwrap().entity_types.get(&"User".parse().unwrap()).unwrap();
assert_matches!(&user.shape, json_schema::EntityAttributes::EntityAttributes(json_schema::EntityAttributesInternal { attrs, .. }) => {
Expand All @@ -2444,7 +2444,7 @@ mod ea_maps {
#[test]
fn nested_ea_map() {
let src = "entity E { tags: { ?: { ?: String } } };";
assert_matches!(collect_warnings(json_schema::Fragment::from_cedarschema_str(src, &Extensions::all_available())), Err(e) => {
assert_matches!(collect_warnings(json_schema::Fragment::from_cedarschema_str(src, Extensions::all_available())), Err(e) => {
expect_err(
src,
&miette::Report::new(e),
Expand All @@ -2458,7 +2458,7 @@ mod ea_maps {
#[test]
fn ea_map_and_attribute() {
let src = "entity E { tags: { foo: Long, ?: String } };";
assert_matches!(collect_warnings(json_schema::Fragment::from_cedarschema_str(src, &Extensions::all_available())), Err(e) => {
assert_matches!(collect_warnings(json_schema::Fragment::from_cedarschema_str(src, Extensions::all_available())), Err(e) => {
expect_err(
src,
&miette::Report::new(e),
Expand All @@ -2472,7 +2472,7 @@ mod ea_maps {
#[test]
fn two_ea_maps() {
let src = "entity E { tags: { ?: Long, ?: String } };";
assert_matches!(collect_warnings(json_schema::Fragment::from_cedarschema_str(src, &Extensions::all_available())), Err(e) => {
assert_matches!(collect_warnings(json_schema::Fragment::from_cedarschema_str(src, Extensions::all_available())), Err(e) => {
expect_err(
src,
&miette::Report::new(e),
Expand Down
2 changes: 1 addition & 1 deletion cedar-policy-validator/src/coreschema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ mod test {
}
}
}});
ValidatorSchema::from_json_value(src, &Extensions::all_available())
ValidatorSchema::from_json_value(src, Extensions::all_available())
.expect("failed to create ValidatorSchema")
}

Expand Down
4 changes: 2 additions & 2 deletions cedar-policy-validator/src/json_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,7 @@ impl ActionEntityUID<ConditionalName> {
}
}
}
Err(ActionNotDefinedError(nonempty!(self)).into())
Err(ActionNotDefinedError(nonempty!(self)))
}

/// Get the possible fully-qualified [`ActionEntityUID<InternalName>`]s
Expand Down Expand Up @@ -2564,7 +2564,7 @@ mod test {
"actions": {}
}
});
let schema = ValidatorSchema::from_json_value(src.clone(), &Extensions::all_available());
let schema = ValidatorSchema::from_json_value(src.clone(), Extensions::all_available());
assert_matches!(schema, Err(e) => {
expect_err(
&src,
Expand Down
2 changes: 1 addition & 1 deletion cedar-policy-validator/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ impl ValidatorSchema {
for parent in entity_type.parents.iter() {
entity_children
.entry(internal_name_to_entity_type(parent.clone())?)
.or_insert_with(HashSet::new)
.or_default()
Copy link
Contributor

@john-h-kastner-aws john-h-kastner-aws Sep 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you check if this is actually the same? I can imagine that or_default could reasonably not insert the default value

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.insert(name.clone());
}
}
Expand Down
4 changes: 1 addition & 3 deletions cedar-policy-validator/src/schema/namespace_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,9 +511,7 @@ impl EntityTypeFragment<ConditionalName> {
attributes,
parents,
}),
(Ok(_), Some(undeclared_parents)) => {
Err(TypeNotDefinedError(undeclared_parents).into())
}
(Ok(_), Some(undeclared_parents)) => Err(TypeNotDefinedError(undeclared_parents)),
(Err(e), None) => Err(e),
(Err(e), Some(mut undeclared)) => {
undeclared.extend(e.0);
Expand Down
14 changes: 6 additions & 8 deletions cedar-policy-validator/src/schema/raw_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,18 +264,16 @@ impl ConditionalName {
if matches!(
self.reference_type,
ReferenceType::Common | ReferenceType::CommonOrEntity
) {
if all_defs.is_defined_as_common(possibility) {
return Ok(possibility.clone());
}
) && all_defs.is_defined_as_common(possibility)
{
return Ok(possibility.clone());
}
if matches!(
self.reference_type,
ReferenceType::Entity | ReferenceType::CommonOrEntity
) {
if all_defs.is_defined_as_entity(possibility) {
return Ok(possibility.clone());
}
) && all_defs.is_defined_as_entity(possibility)
{
return Ok(possibility.clone());
}
}
Err(TypeNotDefinedError(nonempty![self]))
Expand Down
36 changes: 18 additions & 18 deletions cedar-policy-validator/src/schema/test_579.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fn assert_parses_successfully_cedar(s: &str) -> (ValidatorSchema, Vec<SchemaWarn
println!("{s}");
collect_warnings(ValidatorSchema::from_cedarschema_str(
s,
&Extensions::all_available(),
Extensions::all_available(),
))
.map_err(miette::Report::new)
.unwrap()
Expand All @@ -82,28 +82,28 @@ fn assert_parses_successfully_cedar(s: &str) -> (ValidatorSchema, Vec<SchemaWarn
#[track_caller]
fn assert_parses_successfully_json(v: serde_json::Value) -> ValidatorSchema {
println!("{}", serde_json::to_string_pretty(&v).unwrap());
ValidatorSchema::from_json_value(v, &Extensions::all_available())
ValidatorSchema::from_json_value(v, Extensions::all_available())
.map_err(miette::Report::new)
.unwrap()
}

#[track_caller]
fn assert_parse_error_cedar(s: &str, e: &ExpectedErrorMessage<'_>) {
println!("{s}");
assert_matches!(collect_warnings(ValidatorSchema::from_cedarschema_str(s, &Extensions::all_available())), Err(err) => {
assert_matches!(collect_warnings(ValidatorSchema::from_cedarschema_str(s, Extensions::all_available())), Err(err) => {
expect_err(s, &miette::Report::new(err), e);
});
}

#[track_caller]
fn assert_parse_error_json(v: serde_json::Value, e: &ExpectedErrorMessage<'_>) {
println!("{}", serde_json::to_string_pretty(&v).unwrap());
assert_matches!(ValidatorSchema::from_json_value(v.clone(), &Extensions::all_available()), Err(err) => {
assert_matches!(ValidatorSchema::from_json_value(v.clone(), Extensions::all_available()), Err(err) => {
expect_err(&v, &miette::Report::new(err), e);
});
}

/// Makes a schema for all the XXa1 test cases, where different XX plug in
/// Makes a schema for all the `XXa1` test cases, where different XX plug in
/// different `mytype_use` (schema constructs that use `MyType`).
///
/// In all of these cases, `MyType` is declared as an entity type in the
Expand All @@ -120,7 +120,7 @@ fn a1_cedar(mytype_use: &str) -> String {
)
}

/// Makes a schema for all the XXa1 test cases, where different XX plug in a
/// Makes a schema for all the `XXa1` test cases, where different XX plug in a
/// different schema construct that uses `MyType` (e.g., with a function
/// like `A1X1_json()`).
///
Expand All @@ -139,7 +139,7 @@ fn a1_json() -> serde_json::Value {
})
}

/// Makes a schema for all the XXa2 test cases, where different XX plug in
/// Makes a schema for all the `XXa2` test cases, where different XX plug in
/// different `mytype_use` (schema constructs that use `MyType`).
///
/// In all of these cases, `MyType` is declared as a common type in the
Expand All @@ -156,7 +156,7 @@ fn a2_cedar(mytype_use: &str) -> String {
)
}

/// Makes a schema for all the XXa2 test cases, where different XX plug in a
/// Makes a schema for all the `XXa2` test cases, where different XX plug in a
/// different schema construct that uses `MyType` (e.g., with a function
/// like `A1X1_json()`).
///
Expand All @@ -177,7 +177,7 @@ fn a2_json() -> serde_json::Value {
})
}

/// Makes a schema for all the XXb1 test cases, where different XX plug in
/// Makes a schema for all the `XXb1` test cases, where different XX plug in
/// different `mytype_use` (schema constructs that use `MyType`).
///
/// In all of these cases, `MyType` is declared as an entity type in the
Expand All @@ -194,7 +194,7 @@ fn b1_cedar(mytype_use: &str) -> String {
)
}

/// Makes a schema for all the XXb1 test cases, where different XX plug in a
/// Makes a schema for all the `XXb1` test cases, where different XX plug in a
/// different schema construct that uses `MyType` (e.g., with a function
/// like `A1X1_json()`).
///
Expand All @@ -218,7 +218,7 @@ fn b1_json() -> serde_json::Value {
})
}

/// Makes a schema for all the XXb2 test cases, where different XX plug in
/// Makes a schema for all the `XXb2` test cases, where different XX plug in
/// different `mytype_use` (schema constructs that use `MyType`).
///
/// In all of these cases, `MyType` is declared as a common type in the
Expand All @@ -235,7 +235,7 @@ fn b2_cedar(mytype_use: &str) -> String {
)
}

/// Makes a schema for all the XXb2 test cases, where different XX plug in a
/// Makes a schema for all the `XXb2` test cases, where different XX plug in a
/// different schema construct that uses `MyType` (e.g., with a function
/// like `A1X1_json()`).
///
Expand All @@ -260,7 +260,7 @@ fn b2_json() -> serde_json::Value {
})
}

/// Makes a schema for all the XXc test cases, where different XX plug in
/// Makes a schema for all the `XXc` test cases, where different XX plug in
/// different `mytype_use` (schema constructs that use `MyType`).
///
/// In all of these cases, `MyType` is not declared in any namespace.
Expand All @@ -275,7 +275,7 @@ fn c_cedar(mytype_use: &str) -> String {
)
}

/// Makes a schema for all the XXc test cases, where different XX plug in a
/// Makes a schema for all the `XXc` test cases, where different XX plug in a
/// different schema construct that uses `MyType` (e.g., with a function
/// like `A1X1_json()`).
///
Expand All @@ -292,7 +292,7 @@ fn c_json() -> serde_json::Value {
})
}

/// Makes a schema for all the XXd1 test cases, where different XX plug in
/// Makes a schema for all the `XXd1` test cases, where different XX plug in
/// different `mytype_use` (schema constructs that use `MyType`).
///
/// In all of these cases, `MyType` is declared as an entity type in an
Expand All @@ -311,7 +311,7 @@ fn d1_cedar(mytype_use: &str) -> String {
)
}

/// Makes a schema for all the XXd1 test cases, where different XX plug in a
/// Makes a schema for all the `XXd1` test cases, where different XX plug in a
/// different schema construct that uses `MyType` (e.g., with a function
/// like `A1X1_json()`).
///
Expand All @@ -335,7 +335,7 @@ fn d1_json() -> serde_json::Value {
})
}

/// Makes a schema for all the XXd2 test cases, where different XX plug in
/// Makes a schema for all the `XXd2` test cases, where different XX plug in
/// different `mytype_use` (schema constructs that use `MyType`).
///
/// In all of these cases, `MyType` is declared as a common type in an
Expand All @@ -354,7 +354,7 @@ fn d2_cedar(mytype_use: &str) -> String {
)
}

/// Makes a schema for all the XXd2 test cases, where different XX plug in a
/// Makes a schema for all the `XXd2` test cases, where different XX plug in a
/// different schema construct that uses `MyType` (e.g., with a function
/// like `A1X1_json()`).
///
Expand Down
2 changes: 1 addition & 1 deletion cedar-policy-validator/src/typecheck/test/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl SchemaProvider for NamespaceDefinitionWithActionAttributes<RawName> {

impl<'a> SchemaProvider for &'a str {
fn schema(self) -> ValidatorSchema {
ValidatorSchema::from_cedarschema_str(self, &Extensions::all_available())
ValidatorSchema::from_cedarschema_str(self, Extensions::all_available())
.unwrap_or_else(|e| panic!("failed to construct schema: {:?}", miette::Report::new(e)))
.0
}
Expand Down
13 changes: 6 additions & 7 deletions cedar-policy-validator/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1042,8 +1042,7 @@ impl Attributes {
) -> Attributes {
Attributes::with_attributes(
Self::attributes_lub_iter(schema, attrs0, attrs1, ValidationMode::Permissive)
.map(|r| r.map(|(k, v)| (k.clone(), v)))
.flatten(),
.flat_map(|r| r.map(|(k, v)| (k.clone(), v))),
)
}
}
Expand Down Expand Up @@ -1868,7 +1867,7 @@ mod test {
}}))
.expect("Expected valid schema"),
ActionBehavior::PermitAttributes,
&Extensions::all_available(),
Extensions::all_available(),
)
.expect("Expected valid schema")
}
Expand Down Expand Up @@ -1979,7 +1978,7 @@ mod test {
}}))
.expect("Expected valid schema"),
ActionBehavior::PermitAttributes,
&Extensions::all_available(),
Extensions::all_available(),
)
.expect("Expected valid schema")
}
Expand Down Expand Up @@ -2105,7 +2104,7 @@ mod test {
}}))
.expect("Expected valid schema"),
ActionBehavior::PermitAttributes,
&Extensions::all_available(),
Extensions::all_available(),
)
.expect("Expected valid schema");

Expand Down Expand Up @@ -2147,7 +2146,7 @@ mod test {
))
.expect("Expected valid schema"),
ActionBehavior::PermitAttributes,
&Extensions::all_available(),
Extensions::all_available(),
)
.expect("Expected valid schema")
}
Expand Down Expand Up @@ -2192,7 +2191,7 @@ mod test {
let type_str = format!("type T = {ty}; entity E {{ foo: T }};");
println!("{type_str}");
let (schema, _) =
ValidatorSchema::from_cedarschema_str(&type_str, &Extensions::all_available()).unwrap();
ValidatorSchema::from_cedarschema_str(&type_str, Extensions::all_available()).unwrap();
assert_eq!(
&schema
.get_entity_type(&EntityType::from_normalized_str("E").unwrap())
Expand Down
Loading