Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor flat_schema function to handle AllOf schemas #1

Merged
merged 3 commits into from
Dec 29, 2023
Merged
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

### TODO

- [ ] Resolve `oneOf`, `anyOf`
- [ ] Resolve schema type
- [ ] anyOf
- [ ] not
- [ ] Resolve external reference

## How to use
Expand Down
61 changes: 58 additions & 3 deletions src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fs::File;
use std::io::Read;
use std::{env::current_dir, path::PathBuf};

use anyhow::{anyhow, Context, Result};
use anyhow::{anyhow, Context, Ok, Result};
use indexmap::IndexMap;
use inquire::Select;
use openapiv3::{
Expand Down Expand Up @@ -222,7 +222,25 @@ pub fn flat_schema(

Ok((select, is_required))
}
openapiv3::SchemaKind::AllOf { .. } => todo!("AllOf"),
openapiv3::SchemaKind::AllOf { all_of } => {
let all_of = items(all_of, api)
.map(|x| {
let x = x?;
let (x, _) = flat_schema(x, api, is_required)?;
Ok(x)
})
.collect::<Result<Vec<_>>>()?;
let mut obj = IndexMap::new();
for x in all_of {
if let SchemaType::Object(x) = x {
for (k, v) in x {
obj.insert(k.to_owned(), v);
}
}
}

Ok((SchemaType::Object(obj), is_required))
}
openapiv3::SchemaKind::AnyOf { .. } => todo!("AnyOf"),
openapiv3::SchemaKind::Not { .. } => todo!("Not"),
openapiv3::SchemaKind::Any(_) => todo!("Any"),
Expand All @@ -231,8 +249,10 @@ pub fn flat_schema(

#[cfg(test)]
mod tests {
use crate::schema::SchemaType;

use super::ReadSchema;
use openapiv3::{OpenAPI, PathItem, Type};
use openapiv3::{MediaType, OpenAPI, PathItem, Type};
use std::path::PathBuf;

#[test]
Expand Down Expand Up @@ -260,4 +280,39 @@ mod tests {

assert!(schema.schema.get.is_some());
}

#[test]
fn test_flatten_all_of() {
let schema = r#"
schema:
allOf:
- type: object
required:
- created_at
properties:
created_at:
type: string
format: date-time
updated_at:
type: string
format: date-time
deleted_at:
type: string
format: date-time
- type: object
properties:
test:
type: string
"#;
let schema = serde_yaml::from_str::<MediaType>(schema).unwrap();
let schema = schema.schema.unwrap();
let schema = schema.as_item().unwrap();
let (schema, _) = super::flat_schema(schema, &OpenAPI::default(), true).unwrap();

if let SchemaType::Object(obj) = schema {
assert_eq!(obj.len(), 4);
} else {
unreachable!()
}
}
}
Loading
Loading