schemars
schemars copied to clipboard
Impl JsonSchema for TempFile/RawStr
I'm trying to implement OpenApiFromFormField for rocket::fs::TempFile.
But it looks like Schemars need to implement JsonSchema for TempFile first.
Then I can do let schema = gen.json_schema::<rocket::fs::TempFile>(); and use the value of it in okapi.
The same is true for rocket::http::RawStr
For now I'll use Vec::<u8> for both implementations, where possible. But some places have generics set up, so there it still depends on the JsonSchema trait.
My first thought is that it should be possible to implement OpenApiFromFormField for TempFile without it implementing JsonSchema, indeed this is already done in okapi.
But after looking into it a little, I can see value in having JsonSchema implemented on TempFile directly. Mainly because it would make it much easier to generate correct json schema/openapi documents involving user-defined structs that derive FromForm, which may contain TempFile fields.
e.g. this struct:
#[derive(FromForm, JsonSchema)]
struct MyStruct {
num: i32,
file: TempFile,
}
should produce this schema (following Swagger's guide to file uploads):
{
"type": "object",
"properties": {
"num": {
"type": "integer",
"format": "int32"
},
"file": {
"type": "string",
"format": "binary"
}
}
}
I assume RawStr would just have an equivalent json schema to str/String?
Rocket also allows renaming fields (and setting defaults) in FromForm derives - see docs. It would be nice if okapi/schemars also respected that, but that's a separate issue entirely.
But after looking into it a little, I can see value in having
JsonSchemaimplemented onTempFiledirectly.
This would also be very useful because I can now use generic traits so no manual implementation is needed for every type. This instead of this. Which was incomplete at this point in time, would have been 2 times bigger without using generics.
I assume
RawStrwould just have an equivalent json schema tostr/String?
Yes, RawStr is just an unsanitized version of str.
Best also add this under a Optional dependency, but you where most likely already do that.
Rocket also allows renaming fields (and setting defaults) in
FromFormderives - see docs. It would be nice if okapi/schemars also respected that, but that's a separate issue entirely.
Yes I know, this has come up in: https://github.com/GREsau/okapi/issues/43 I hope to add this at some point, but have to look into this more.
Is there any forward progress on this? I'm running into this issue and debating how to handle it.
HI, I just ran into this issue too. Does the PR need testing, can we help in any way?
Hey! When will the PR be merged?
Last work was two months ago in PR #187 This one will be closed based on what is written in 187. GitHub may not make that totally clear but thats what I will expect to happen when I read the entry just above your comment :)