-
Notifications
You must be signed in to change notification settings - Fork 163
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
Add support for unit type ()
#464
Conversation
…ple' has no elements and map to 'TypeTreeValue::UnitType' in this case; implement conversion from 'TypeTreeValue::UnitType' to 'TypeTree'
…for 'SchemaProperty<'_>' to create an OpenAPI object for the unit type; change conversion implementation from 'TypeTreeValue::UnitType' to 'TypeTree' to mark the unit type as 'ValueType::Tupple'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pretty good. Then there is 2 places where the ValueType::Tuple
is being used. You might want to update them as well.
- into_params.rs
- media_type.rs
Also add a test cases for these in utoipa-gen/test/schema_derive_test.rs and for IntoParams you can add test somewhere where IntoParams tests have been added before.
Run clippy and cargo fmt for the project as well. :)
Apply code suggestion. Co-authored-by: Juha Kukkonen <juha7kukkonen@gmail.com>
What exactly do you mean with |
For But for IntoParams is another struct what is used to parse path parameters for the operation. |
…nit type; implement tests for unit type support
Thank you for your help! I applied your code suggestion, updated the other two places where |
impl<'__s> ToSchema<'__s> for () { | ||
fn schema() -> (&'__s str, openapi::RefOr<openapi::schema::Schema>) { | ||
( | ||
"UnitType", | ||
openapi::schema::ObjectBuilder::new() | ||
.nullable(true) | ||
.default(Some(serde_json::Value::Null)) | ||
.into(), | ||
) | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I belive this is unnecessary since the implementation is handled in utoipa-gen where the tuple type with zero arguments is being tokenized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding this originates in me trying to use ()
in a generic like this:
type UT = ();
#[derive(ToSchema)]
#[aliases(StrData = Data<UT>)]
pub struct Data<T> {
#[allow(unused)]
value: T
}
#[utoipa::path(
get,
path = "/root",
responses (
( status = 200, description = "", body = StrData )
)
)]
pub fn root() -> () {}
It's not possible to use ()
in aliases like this #[aliases(StrData = Data<()>)]
. Therefore I created the type alias type UT = ();
and used it in #[aliases(...)]
. With this change the "value"
property is a reference with "$ref": "#/components/schemas/UT"
. And for this reference to resolve I need to add UT
to the schemas components(schemas(StrData, UT))
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay
#[utoipa::path( | ||
post, | ||
path = "/unit_type_test", | ||
request_body = UT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just wondering whether you are able now to directly use the request_body = ()
because the change you made to media_type_schema.rs
file for the tuple
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just checked and it's not possible. I get the following error message: "ValueType::Primitive must have path".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay. Hm.. then there is something else missing as well.. Though I can check this later as well. It should not be primitive type but ValueType::Tuple
Here is a example test for #[derive(IntoParams)]
struct Params {
value: (),
} utoipa/utoipa-gen/tests/path_derive.rs Lines 1186 to 1225 in 96acebf
|
Thank's again for your help! It's still hard for me to understand how everything works together in utoipa. As you suggested I created a test very similar to the one you mentioned and it passes as expected 👍 |
Sorry to keep you await, merged it finally |
No problem, thank you very much 👍 |
Fixes #448.