schemars icon indicating copy to clipboard operation
schemars copied to clipboard

Impl JsonSchema for TempFile/RawStr

Open ralpha opened this issue 4 years ago • 9 comments
trafficstars

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.

ralpha avatar Sep 08 '21 14:09 ralpha

The same is true for rocket::http::RawStr

ralpha avatar Sep 08 '21 22:09 ralpha

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.

ralpha avatar Sep 09 '21 14:09 ralpha

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?

GREsau avatar Sep 12 '21 20:09 GREsau

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.

GREsau avatar Sep 12 '21 20:09 GREsau

But after looking into it a little, I can see value in having JsonSchema implemented on TempFile directly.

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 RawStr would just have an equivalent json schema to str/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 FromForm derives - 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.

ralpha avatar Sep 12 '21 20:09 ralpha

Is there any forward progress on this? I'm running into this issue and debating how to handle it.

jkellz-dev avatar Dec 06 '21 23:12 jkellz-dev

HI, I just ran into this issue too. Does the PR need testing, can we help in any way?

Toasterson avatar Jul 05 '22 00:07 Toasterson

Hey! When will the PR be merged?

barthofu avatar Jan 19 '23 17:01 barthofu

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 :)

Toasterson avatar Jan 19 '23 17:01 Toasterson