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

Impl JsonSchema for TempFile/RawStr #103

Open
ralpha opened this issue Sep 8, 2021 · 9 comments · May be fixed by #187
Open

Impl JsonSchema for TempFile/RawStr #103

ralpha opened this issue Sep 8, 2021 · 9 comments · May be fixed by #187

Comments

@ralpha
Copy link
Contributor

ralpha commented Sep 8, 2021

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
Copy link
Contributor Author

ralpha commented Sep 8, 2021

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

@ralpha ralpha changed the title Impl JsonSchema for TempFile Impl JsonSchema for TempFile/RawStr Sep 8, 2021
@ralpha
Copy link
Contributor Author

ralpha commented Sep 9, 2021

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.

@GREsau
Copy link
Owner

GREsau commented Sep 12, 2021

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
Copy link
Owner

GREsau commented Sep 12, 2021

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.

@ralpha
Copy link
Contributor Author

ralpha commented Sep 12, 2021

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: GREsau/okapi#43
I hope to add this at some point, but have to look into this more.

@jkellz-dev
Copy link

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

@Toasterson
Copy link

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

ralpha added a commit to ralpha/schemars that referenced this issue Nov 9, 2022
Implement  JsonSchema for:
- TempFile
- NamedFile
- RawStr
- RawStrBuf

Closes: GREsau#103
@ralpha ralpha linked a pull request Nov 9, 2022 that will close this issue
@barthofu
Copy link

Hey!
When will the PR be merged?

@Toasterson
Copy link

Toasterson commented Jan 19, 2023

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

ralpha added a commit to ralpha/schemars that referenced this issue Nov 8, 2024
Implement  JsonSchema for:
- TempFile
- NamedFile
- RawStr
- RawStrBuf

Closes: GREsau#103
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants