Skip to content

Commit

Permalink
fix post process not initiated wo prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
JR-1991 committed Dec 13, 2024
1 parent 99435ad commit 40eb27f
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 23 deletions.
26 changes: 13 additions & 13 deletions src/json/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const SCHEMA: &str = "https://json-schema.org/draft/2020-12/schema";
///
/// * `model` - A reference to the `DataModel` to be converted.
/// * `root` - The root object name in the model.
/// * `openai` - A boolean flag indicating whether to use the OpenAI schema.
///
/// # Returns
///
Expand Down Expand Up @@ -225,22 +226,21 @@ fn post_process_schema(
openai: bool,
) {
schema_object.id = Some(config.repo.clone());
if let Some(prefixes) = &config.prefixes {
resolve_prefixes(schema_object, prefixes);
post_process_object(schema_object, config, openai);

if openai {
remove_options(schema_object);
for (_, definition) in schema_object.definitions.iter_mut() {
if let schema::SchemaType::Object(definition) = definition {
post_process_object(definition, config, openai);
}
}
}

for (_, definition) in schema_object.definitions.iter_mut() {
if let schema::SchemaType::Object(schema_object) = definition {
resolve_prefixes(schema_object, prefixes);

if openai {
remove_options(schema_object);
}
}
}
fn post_process_object(object: &mut schema::SchemaObject, config: &FrontMatter, openai: bool) {
if let Some(prefixes) = &config.prefixes {
resolve_prefixes(object, prefixes);
}
if openai {
remove_options(object);
}
}

Expand Down
3 changes: 2 additions & 1 deletion tests/data/expected_json_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"number": {
"title": "number",
"type": "number",
"$term": "http://schema.org/one"
"$term": "http://schema.org/one",
"minimum": 0.0
},
"ontology": {
"title": "ontology",
Expand Down
11 changes: 3 additions & 8 deletions tests/data/expected_json_schema_openai.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"array_valued": {
"title": "array_valued",
"type": "array",
"$term": "http://schema.org/something",
"items": {
"$ref": "#/$defs/Test2"
}
Expand Down Expand Up @@ -40,13 +39,11 @@
"name": {
"title": "name",
"type": "string",
"description": "A test description",
"$term": "http://schema.org/hello"
"description": "A test description"
},
"number": {
"title": "number",
"type": "number",
"$term": "http://schema.org/one"
"type": "number"
},
"ontology": {
"title": "ontology",
Expand Down Expand Up @@ -75,15 +72,13 @@
"names": {
"title": "names",
"type": "array",
"$term": "http://schema.org/hello",
"items": {
"type": "string"
}
},
"number": {
"title": "number",
"type": "number",
"$term": "http://schema.org/one"
"type": "number"
}
},
"required": [],
Expand Down
1 change: 1 addition & 0 deletions tests/data/model_json_schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ nsmap:
- number
- Type: float
- Term: schema:one
- Minimum: 0
- array_valued
- Type: [Test2](#test2)[]
- Term: schema:something
Expand Down
46 changes: 46 additions & 0 deletions tests/data/model_json_schema_openai.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
repo: "https://www.github.com/my/repo/"
prefix: "tst"
---

### Test

- **name**
- Type: Identifier
- Description: A test description
- number
- Type: float
- Minimum: 0
- array_valued
- Type: [Test2](#test2)[]
- single_valued
- Type: [Test2](#test2)
- ontology
- Type: Ontology
- multiple_types
- Type: float, Test2
- multiple_types_array
- Type: float, Test2
- Multiple: true

### Test2

- names
- Type: string[]
- XML: name
- number
- Type: float
- XML: @number
- Minimum: 0

## Enumerations

### Ontology

Ontology endpoints for different types of sequences.

```
GO = "https://amigo.geneontology.org/amigo/term/"
SIO = "http://semanticscience.org/resource/"
ECO = "https://www.evidenceontology.org/term/"
```
2 changes: 1 addition & 1 deletion tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ mod tests {
#[test]
fn test_json_schema_openai() {
// Arrange
let path = Path::new("tests/data/model_json_schema.md");
let path = Path::new("tests/data/model_json_schema_openai.md");
let model = DataModel::from_markdown(path).expect("Could not parse markdown");

// Act
Expand Down

0 comments on commit 40eb27f

Please sign in to comment.