Skip to content

Commit

Permalink
Add support for more generic types
Browse files Browse the repository at this point in the history
Add support for generic types which are serializable via serde
* Add support for Box
* Add support for RefCell
  • Loading branch information
juhaku committed Jan 28, 2022
1 parent d5e8cc5 commit a711146
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
25 changes: 24 additions & 1 deletion tests/component_derive_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{borrow::Cow, collections::HashMap, vec};
use std::{borrow::Cow, cell::RefCell, collections::HashMap, vec};

use serde_json::Value;
use utoipa::{Component, OpenApi};
Expand Down Expand Up @@ -468,3 +468,26 @@ fn derive_struct_with_cow() {
"required.[0]" = r###""greeting""###, "Greeting required"
};
}

#[test]
fn derive_with_box_and_refcell() {
#[allow(unused)]
struct Foo {
name: &'static str,
}

let greeting = api_doc! {
struct Greeting {
foo: Box<Foo>,
ref_cell_foo: RefCell<Foo>
}
};

common::assert_json_array_len(greeting.get("required").unwrap(), 2);
assert_value! {greeting=>
"properties.foo.$ref" = r###""#/components/schemas/Foo""###, "Greeting foo field"
"properties.ref_cell_foo.$ref" = r###""#/components/schemas/Foo""###, "Greeting ref_cell_foo field"
"required.[0]" = r###""foo""###, "Greeting required 0"
"required.[1]" = r###""ref_cell_foo""###, "Greeting required 1"
};
}
9 changes: 8 additions & 1 deletion utoipa-gen/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,8 @@ impl<'a> ComponentPart<'a> {
"Vec" => Some(GenericType::Vec),
"Option" => Some(GenericType::Option),
"Cow" => Some(GenericType::Cow),
"Box" => Some(GenericType::Box),
"RefCell" => Some(GenericType::RefCell),
_ => None,
}
}
Expand All @@ -404,6 +406,8 @@ enum GenericType {
Map,
Option,
Cow,
Box,
RefCell,
}

#[cfg_attr(feature = "debug", derive(Debug))]
Expand Down Expand Up @@ -469,7 +473,10 @@ where
#component_property.to_array()
});
}
Some(GenericType::Option) | Some(GenericType::Cow) => {
Some(GenericType::Option)
| Some(GenericType::Cow)
| Some(GenericType::Box)
| Some(GenericType::RefCell) => {
let component_property = ComponentProperty::new(
self.component_part.child.as_ref().unwrap(),
self.comments,
Expand Down

0 comments on commit a711146

Please sign in to comment.