Skip to content

Commit

Permalink
Do not allow annotations on the empty namespace (#1386)
Browse files Browse the repository at this point in the history
Signed-off-by: Shaobo He <[email protected]>
  • Loading branch information
shaobo-he-aws authored Dec 20, 2024
1 parent 99bb87b commit 6f28be2
Showing 1 changed file with 38 additions and 15 deletions.
53 changes: 38 additions & 15 deletions cedar-policy-validator/src/json_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ where
raw.into_iter()
.map(|(key, value)| {
let key = if key.is_empty() {
if !value.annotations.is_empty() {
Err(serde::de::Error::custom(format!(
"annotations are not allowed on the empty namespace"
)))?
}
None
} else {
Some(Name::from_normalized_str(&key).map_err(|err| {
Expand Down Expand Up @@ -3054,7 +3059,7 @@ mod annotations {
use super::Fragment;

#[test]
fn basic() {
fn empty_namespace() {
let src = serde_json::json!(
{
"" : {
Expand All @@ -3066,11 +3071,29 @@ mod annotations {
}
});
let schema: Result<Fragment<RawName>, _> = serde_json::from_value(src);
assert_matches!(schema, Err(err) => {
assert_eq!(&err.to_string(), "annotations are not allowed on the empty namespace");
});
}

#[test]
fn basic() {
let src = serde_json::json!(
{
"N" : {
"entityTypes": {},
"actions": {},
"annotations": {
"doc": "this is a doc"
}
}
});
let schema: Result<Fragment<RawName>, _> = serde_json::from_value(src);
assert_matches!(schema, Ok(_));

let src = serde_json::json!(
{
"" : {
"N" : {
"entityTypes": {
"a": {
"annotations": {
Expand All @@ -3095,7 +3118,7 @@ mod annotations {

let src = serde_json::json!(
{
"" : {
"N" : {
"entityTypes": {
"a": {
"annotations": {
Expand Down Expand Up @@ -3127,7 +3150,7 @@ mod annotations {
assert_matches!(schema, Ok(_));

let src = serde_json::json!({
"": {
"N": {
"entityTypes": {},
"actions": {},
"commonTypes": {
Expand Down Expand Up @@ -3155,7 +3178,7 @@ mod annotations {
assert_matches!(schema, Ok(_));

let src = serde_json::json!({
"": {
"N": {
"entityTypes": {
"User" : {
"shape" : {
Expand All @@ -3182,7 +3205,7 @@ mod annotations {

// nested record
let src = serde_json::json!({
"": {
"N": {
"entityTypes": {
"User" : {
"shape" : {
Expand Down Expand Up @@ -3243,7 +3266,7 @@ mod annotations {
fn unknown_fields() {
let src = serde_json::json!(
{
"": {
"N": {
"entityTypes": {
"UserGroup": {
"shape44": {
Expand All @@ -3260,7 +3283,7 @@ mod annotations {

let src = serde_json::json!(
{
"": {
"N": {
"entityTypes": {},
"actions": {},
"commonTypes": {
Expand All @@ -3278,7 +3301,7 @@ mod annotations {

let src = serde_json::json!(
{
"": {
"N": {
"entityTypes": {},
"actions": {},
"commonTypes": {
Expand All @@ -3293,7 +3316,7 @@ mod annotations {

let src = serde_json::json!(
{
"": {
"N": {
"entityTypes": {},
"actions": {},
"commonTypes": {
Expand All @@ -3314,7 +3337,7 @@ mod annotations {

let src = serde_json::json!(
{
"": {
"N": {
"entityTypes": {},
"actions": {},
"commonTypes": {
Expand Down Expand Up @@ -3343,7 +3366,7 @@ mod annotations {

let src = serde_json::json!(
{
"": {
"N": {
"entityTypes": {
"UserGroup": {
"shape": {
Expand All @@ -3363,7 +3386,7 @@ mod annotations {

let src = serde_json::json!(
{
"": {
"N": {
"entityTypes": {},
"actions": {
"a": {
Expand All @@ -3381,7 +3404,7 @@ mod annotations {

let src = serde_json::json!(
{
"" : {
"N" : {
"entityTypes": {},
"actions": {},
"foo": "",
Expand Down Expand Up @@ -3412,7 +3435,7 @@ mod annotations {

let src = serde_json::json!(
{
"" : {
"N" : {
"entityTypes": {},
"actions": {},
"commonTypes": {
Expand Down

0 comments on commit 6f28be2

Please sign in to comment.