Skip to content

Commit

Permalink
take first object if no root
Browse files Browse the repository at this point in the history
  • Loading branch information
JR-1991 committed Jun 7, 2024
1 parent ccebaa3 commit eb94e32
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 15 deletions.
8 changes: 3 additions & 5 deletions src/bin/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,7 @@ fn convert(args: ConvertArgs) -> Result<(), Box<dyn Error>> {

// Render the template.
let rendered = match args.template {
Templates::JsonSchema => model.json_schema(args.root.expect(
"Root object name is required. Please add --root <object_name> or -r <object_name>",
)),
Templates::JsonSchema => model.json_schema(args.root),
_ => render_jinja_template(&args.template, &mut model)?,
};

Expand Down Expand Up @@ -304,7 +302,7 @@ mod tests {
}

#[test]
fn test_invalid_conversion() {
fn test_json_schema_no_root() {
let mut cmd = Command::cargo_bin("md-models").unwrap();
let assert = cmd
.arg("convert")
Expand All @@ -313,7 +311,7 @@ mod tests {
.arg("-t")
.arg("json-schema")
.assert();
assert.failure();
assert.success();
}

#[test]
Expand Down
14 changes: 9 additions & 5 deletions src/datamodel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,20 @@ impl DataModel {
// # Returns
//
// A JSON schema string
pub fn json_schema(&self, obj_name: String) -> String {
pub fn json_schema(&self, obj_name: Option<String>) -> String {
if self.objects.is_empty() {
panic!("No objects found in the markdown file");
}

if self.objects.iter().all(|o| o.name != obj_name) {
panic!("Object not found in the markdown file");
match obj_name {
Some(name) => {
if self.objects.iter().all(|o| o.name != name) {
panic!("Object not found in the markdown file");
}
schema::to_json_schema(&name, self)
}
None => schema::to_json_schema(&self.objects[0].name, self),
}

schema::to_json_schema(&obj_name, self)
}

// Get the JSON schema for all objects in the markdown file
Expand Down
29 changes: 24 additions & 5 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,32 @@ mod tests {
}

#[test]
fn test_json_schema() {
fn test_json_schema_known_obj() {
// Arrange
let path = Path::new("tests/data/model.md");
let model = DataModel::from_markdown(path).expect("Could not parse markdown");

// Act
let schema = model.json_schema("Test".to_string());
let schema = model.json_schema(Some("Test".to_string()));
let schema: serde_json::Value = serde_json::from_str(&schema).unwrap();

// Assert
let expected_schema =
std::fs::read_to_string("tests/data/expected_json_schema.json").unwrap();
// Parse with serde_json
let expected_schema: serde_json::Value = serde_json::from_str(&expected_schema).unwrap();

assert_eq!(schema, expected_schema);
}

#[test]
fn test_json_schema_unknown_obj() {
// Arrange
let path = Path::new("tests/data/model.md");
let model = DataModel::from_markdown(path).expect("Could not parse markdown");

// Act
let schema = model.json_schema(None);
let schema: serde_json::Value = serde_json::from_str(&schema).unwrap();

// Assert
Expand All @@ -134,7 +153,7 @@ mod tests {
let model = mdmodels::datamodel::DataModel::new(None, None);

// Act
model.json_schema("Test".to_string());
model.json_schema(Some("Test".to_string()));
}

#[test]
Expand All @@ -145,7 +164,7 @@ mod tests {
let model = DataModel::from_markdown(path).expect("Could not parse markdown");

// Act
model.json_schema("Test3".to_string());
model.json_schema(Some("Test3".to_string()));
}

#[test]
Expand Down Expand Up @@ -191,7 +210,7 @@ mod tests {
let obj_name = filename.replace(".json", "");
let expected_schema =
std::fs::read_to_string(format!("tests/intermediates/{}", filename)).unwrap();
let schema = model.json_schema(obj_name);
let schema = model.json_schema(Some(obj_name));

assert_eq!(
serde_json::from_str::<serde_json::Value>(schema.as_str())
Expand Down

0 comments on commit eb94e32

Please sign in to comment.