Skip to content

schema conversion method(fixed) - #323

Closed
swabyra wants to merge 0 commit into
cedar-policy:mainfrom
swabyra:SchemaCoversionV3
Closed

schema conversion method(fixed)#323
swabyra wants to merge 0 commit into
cedar-policy:mainfrom
swabyra:SchemaCoversionV3

Conversation

@swabyra

@swabyra swabyra commented Jul 15, 2025

Copy link
Copy Markdown
Contributor

Issue #, if available:
Schema conversion methods for
Description of changes:
Wrote a new conversion method for Cedar -> JSON and JSON -> Cedar.
Along with the corresponding test cases for each method.

@swabyra
swabyra force-pushed the SchemaCoversionV3 branch from 2227727 to 1c377f3 Compare July 16, 2025 17:39
}
public String toCedarFormat() throws InternalException {
if (type != JsonOrCedar.Json || schemaJson.isEmpty()) {
throw new InternalException("Schema is not in JSON format");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@mark-creamer-amazon do we want it to throw an Exception in this case or just return the Schema? For example, if the schema is already in Cedar format and the user calls this method, we can return the schema which is already in the Cedar format.

@mark-creamer-amazon mark-creamer-amazon Jul 16, 2025

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I was imagining that we would want to return the schema content we have as a String instead of returning an exception!

You could imagine perhaps that if you had a list of Schema objects of mixed JSON or Cedar types, and you enumerated with toCedarFormat across all, you'd want the Cedar format string regardless of the type

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I tried implementing/writing to return the Schema as a string.

if (type != JsonOrCedar.Cedar || schemaText.isEmpty()) {
throw new InternalException("Schema is not in cedar format");
}
return cedarToJsonJni(schemaText.get());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I believe we should return a JsonNode instead of String here because that's how we represent JSON schema in Schema.

Can do something like:

return OBJECT_MAPPER.readTree(cedarToJsonJni(schemaText.get()));

and let it throw JSONProcessingException (which shouldn't occur ideally because we get this JSON converted string from Cedar).

Schema schema = Schema.parse(JsonOrCedar.Json, schemaJson);
String cedarSchema = schema.toCedarFormat();
assertNotNull(schema);
assertTrue(cedarSchema.contains("entity User"), "Expected Cedar to contain 'entity User'");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's assert the full Cedar schema value here instead of just "entity User"

Schema schema = Schema.parse(JsonOrCedar.Cedar, cedarSchema);
String jsonSchema = schema.toJsonFormat();
assertNotNull(schema);
assertTrue(jsonSchema.contains("User"), "Expected Json to contain 'User'");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Add assertion on the entire jsonSchema value

Comment on lines +163 to +191
public void toCedarFormatThrowsIfNotJson() {
String cedarSchema = """
entity Foo;
""";

assertThrows(InternalException.class, () -> {
Schema schema = Schema.parse(JsonOrCedar.Cedar, cedarSchema);
schema.toCedarFormat(); // should throw
});
}

@Test
public void toJsonFormatThrowsIfNotCedar() {
String jsonSchema = """
{
"schema": {
"entityTypes": {
"User": {}
}
}
}
""";

assertThrows(InternalException.class, () -> {
Schema schema = Schema.parse(JsonOrCedar.Json, jsonSchema);
schema.toJsonFormat(); // should throw
});
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We can modify these tests based on what we decide in this comment:

@mark-creamer-amazon do we want it to throw an Exception in this case or just return the Schema? For example, if the schema is already in Cedar format and the user calls this method, we can return the schema which is already in the Cedar format.

Comment thread CedarJavaFFI/src/interface.rs Outdated
pub fn cedarToJsonJni<'a>(mut env: JNIEnv<'a>, _: JClass, cedar_schema: JString<'a>) -> jvalue {
match get_json_schema_internal(&mut env, cedar_schema) {
Ok(text) => match env.new_string(&text) {
Ok(jstr) => JValueGen::Object(JObject::from(jstr)).as_jni(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same as the comment above

Comment thread CedarJavaFFI/src/interface.rs Outdated
Comment on lines +957 to +976
{
"schema": {
"entityTypes": {
"User": {
"memberOfTypes": ["Group"]
},
"Group": {},
"File": {}
},
"actions": {
"read": {
"appliesTo": {
"principalTypes": ["User"],
"resourceTypes": ["File"]
}
}
}
}
}
"#;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: Indentation

Comment thread CedarJavaFFI/src/interface.rs Outdated
Comment on lines +982 to +985
let cedar = result.unwrap();
assert!(
cedar.contains("entity User"),
"Expected output to contain 'entity User'"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This will have to be updated once the previous feedback is implemented (will have to convert JValue into string for assertion).

Also, assert on the full value instead of just entity User

Comment thread CedarJavaFFI/src/interface.rs Outdated

let json = result.unwrap();
assert!(
json.contains("\"entityTypes\""),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Assert on full value

});
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Add tests for Invalid values for both

@swabyra swabyra closed this Jul 17, 2025
@swabyra
swabyra force-pushed the SchemaCoversionV3 branch from 1c377f3 to 38bea1f Compare July 17, 2025 01:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants