Skip to content
Open
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
28 changes: 26 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ categories = ["command-line-utilities", "parser-implementations"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
default = []
preserve_order = ["serde_json/preserve_order"]

[dependencies]
clap = { version = "4.5.4", features = ["derive"] }
indexmap = "2.10.0"
mimalloc = "0.1.41"
rayon = "1.10.0"
regex = "1.10.4"
Expand Down
14 changes: 7 additions & 7 deletions src/strategy/object.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::collections::HashMap;
use std::collections::hash_set::HashSet;
use indexmap::IndexMap;
use regex::Regex;

use serde_json::{Value, json, Map};
Expand All @@ -13,8 +13,8 @@ use crate::strategy::base::SchemaStrategy;
pub struct ObjectStrategy {
// TODO: this is redeclared everywhere, how to avoid this?
extra_keywords: Value,
properties: HashMap<String, SchemaNode>,
pattern_properties: HashMap<String, SchemaNode>,
properties: IndexMap<String, SchemaNode>,
pattern_properties: IndexMap<String, SchemaNode>,
required_properties: Option<HashSet<String>>,
include_empty_required: bool,
}
Expand All @@ -23,8 +23,8 @@ impl ObjectStrategy {
pub fn new() -> Self {
ObjectStrategy {
extra_keywords: json!({}),
properties: HashMap::new(),
pattern_properties: HashMap::new(),
properties: IndexMap::new(),
pattern_properties: IndexMap::new(),
required_properties: None,
include_empty_required: false,
}
Expand Down Expand Up @@ -89,7 +89,7 @@ impl SchemaStrategy for ObjectStrategy {
// properties updater updates the internal properties and pattern_properties with the schema_object,
// creating schema node as needed for each property
let properties_updater =
|properties: &mut HashMap<String, SchemaNode>, schema_object: &Map<String, Value>, prop_key: &str| {
|properties: &mut IndexMap<String, SchemaNode>, schema_object: &Map<String, Value>, prop_key: &str| {
if let Some(schema_properties) = schema_object[prop_key].as_object() {
schema_properties.iter().for_each(|(prop, sub_schema)| {
let sub_node = properties.entry(prop.to_string())
Expand Down Expand Up @@ -160,7 +160,7 @@ impl SchemaStrategy for ObjectStrategy {
}

impl ObjectStrategy {
fn properties_to_schema(&self, properties: &HashMap<String, SchemaNode>) -> Value {
fn properties_to_schema(&self, properties: &IndexMap<String, SchemaNode>) -> Value {
let mut schema_properties = json!({});
properties.iter().for_each(|(prop, node)| {
schema_properties[prop] = node.to_schema();
Expand Down
108 changes: 107 additions & 1 deletion tests/test_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,4 +392,110 @@ fn test_json_schema_should_not_contain_outer_array_when_ignore_outer_array_confi
]
});
assert_eq!(schema, expected_schema);
}
}

#[cfg(feature = "preserve_order")]
#[test]
fn test_properties_preserve_insertion_order() {
let config = BuildConfig {
delimiter: None,
ignore_outer_array: false,
};
let mut builder = get_builder(None);

// Create JSON with properties in a specific order: field_C, field_A, field_B
let mut test_object = json!(
[
{"field_C": "third", "field_A": "first", "field_B": "second"},
{"field_B": "different_order", "field_A": "still_first", "field_C": "still_third"}
]
).to_string().into_bytes();

let schema = build_json_schema(&mut builder, &mut test_object, &config);

// With preserve_order feature enabled, properties should maintain insertion order
let properties = schema["items"]["properties"].as_object().unwrap();
let property_names: Vec<&String> = properties.keys().collect();

// Properties should appear in the order they first appeared: field_C, field_A, field_B
let expected_order = vec!["field_C", "field_A", "field_B"];
assert_eq!(property_names, expected_order);

// Verify all properties are present and correctly typed
let expected_schema = json!({
"type": "array",
"items": {
"type": "object",
"properties": {
"field_C": {
"type": "string"
},
"field_A": {
"type": "string"
},
"field_B": {
"type": "string"
}
},
"required": [
"field_A",
"field_B",
"field_C"
]
}
});
assert_eq!(schema, expected_schema);
}

#[cfg(not(feature = "preserve_order"))]
#[test]
fn test_properties_alphabetical_order() {
let config = BuildConfig {
delimiter: None,
ignore_outer_array: false,
};
let mut builder = get_builder(None);

// Create JSON with properties in a specific order: field_C, field_A, field_B
let mut test_object = json!(
[
{"field_C": "third", "field_A": "first", "field_B": "second"},
{"field_B": "different_order", "field_A": "still_first", "field_C": "still_third"}
]
).to_string().into_bytes();

let schema = build_json_schema(&mut builder, &mut test_object, &config);

// Without preserve_order feature, properties are in alphabetical order
let properties = schema["items"]["properties"].as_object().unwrap();
let property_names: Vec<&String> = properties.keys().collect();

// Properties will be in alphabetical order due to serde_json::Value's BTreeMap
let expected_order = vec!["field_A", "field_B", "field_C"];
assert_eq!(property_names, expected_order);

// Verify all properties are present and correctly typed
let expected_schema = json!({
"type": "array",
"items": {
"type": "object",
"properties": {
"field_A": {
"type": "string"
},
"field_B": {
"type": "string"
},
"field_C": {
"type": "string"
}
},
"required": [
"field_A",
"field_B",
"field_C"
]
}
});
assert_eq!(schema, expected_schema);
}