You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to use okapi to generate openapi.yaml, but I'm having problems because the datatypes that I'm trying to use perhaps aren't supported by okapi, or perhaps that I don't understand the way they are supported. I have ended up implementing a lot of Traits and I'm just wondering if I haven't taken a wrong turn and made it harder than it needs to be.
I want to use an endpoint with a Form that contains a file. This is fairly easy in Rocket using the TempFile struct. When I try to mark the endpoint with openapi, I have errors that JsonSchema, Deserialize, Serialize and Default are not implemented for TempFile. In order to implement the Traits, I have to create a local struct for TempFile because you are not allowed to implement a trait you didn't define for a struct you didn't define. Then you need to write code incorporating the functionality from the original struct into your struct. Below are the stubs that I have implemented to satisfy the compiler for now. In the current state, it complains that OpenApiFromData is not implemented for rocket::form::Form<ExampleForm<'_>>. To solve this it looks like I might have to declare a local struct and add all of the traits and functions to it that I need.
Am I going about this the right way?
Here is my code:
#[macro_use]externcrate rocket;use rocket::serde::json::Json;use std::path::Path;use rocket::form::Form;use rocket_okapi::{ openapi, routes_with_openapi };use serde::{Deserialize,Serialize};use schemars::JsonSchema;use rocket::fs::TempFile;use schemars::gen::SchemaGenerator;///////////// This is the code I want to use //////////////////////////////////////////#[derive(Serialize,Deserialize,FromForm,JsonSchema)]pubstructExample{example_id:u64,}#[derive(FromForm,JsonSchema,Debug,Default,Serialize,Deserialize)]pubstructExampleForm<'f>{example_data:ExampleTempFile<'f>,}#[openapi]#[post("/example", format = "multipart/form-data", data = "<form>")]pubasyncfnpost_example(form:Form<ExampleForm<'_ >>) -> std::result::Result<Json<Example>,String>{let example = Example{example_id:1};Ok(Json(example))}#[rocket::main]#[doc = "The main entry point for Rocket"]asyncfnmain() -> Result<(), rocket::Error>{
rocket::build().mount("/",routes_with_openapi![post_example]).launch().await}///////////// This is the supporting code stubs I think I need to fill out //////////#[derive (Debug)]structExampleTempFile<'a>(&'a mutTempFile<'a>);implExampleTempFile<'_>{pubasyncfnpersist_to<P>(&mutself,path:P) -> std::io::Result<()>whereP:AsRef<Path>{self.0.persist_to(path).await}}impl<'de>Deserialize<'de>forExampleTempFile<'_>{fndeserialize<D>(_:D) -> Result<Self, <Das rocket::serde::Deserializer<'de>>::Error>whereD: rocket::serde::Deserializer<'de>{Ok(ExampleTempFile{0:&mut TempFile::Buffered{content:""}})}}implSerializeforExampleTempFile<'_>{fnserialize<S>(&self,serializer:S) -> Result<S::Ok,S::Error>whereS: rocket::serde::Serializer{
serializer.serialize_bool(true)}}implDefaultforExampleTempFile<'_>{fndefault() -> Self{ExampleTempFile{0:&mut TempFile::default().unwrap()}}}impl<'a> schemars::JsonSchemaforExampleTempFile<'a>{fnschema_name() -> String{"TempFile".to_owned()}fnjson_schema( _:&mutSchemaGenerator) -> schemars::schema::Schema{
schemars::schema::SchemaObject::default().into()}}use rocket::form::FromFormField;impl<'__f>FromFormField<'_>forExampleTempFile<'__f>{}//////////// This is the bit where it looks like I might have to do the same trick with the local struct again //////////////////use okapi::Map;use rocket_okapi::{ gen::OpenApiGenerator, request::OpenApiFromData,ResultasOpenApiResult};use okapi::openapi3::{RequestBody};impl<'r>OpenApiFromData<'r>forForm<ExampleForm<'r>>{fnrequest_body(gen:&mutOpenApiGenerator) -> OpenApiResult<RequestBody>{Ok(RequestBody{content:{Map::new()},required:true,
..Default::default()})}}
Here is my Cargo.toml:
[package]
name = "example"version = "0.1.0"edition = "2018"
[dependencies]
rocket = { version = "0.5.0-rc.1", features = [ "json" ] }
serde = { version = "1.0.64", features = ["derive"] }
serde_json = "1.0.64"log = "0.4.14"async-global-executor = "2.0.2"okapi = "0.6.0-alpha-1"rocket_okapi = "0.7.0-alpha-1"schemars = "^0.8"
The text was updated successfully, but these errors were encountered:
Yeah I'm currently updating the crate to add better support for Rocket 0.5.
I also noticed that TempFile was not implemented.
Because okapi depend on Schema for the JsonSchema implementation I created an issue there: GREsau/schemars#103
For now there is not really an easy way to do this. So your code might be the best you can do for now until it is fully implemented.
I implemented TempFile in all the places where allowed and I can get it to work.
But for the JsonSchema impl this need to be done in Schemars.
Once this issue is solved in Schemars it will automatically work in okapi (the next version, because of generics)
So I'll close this issue here as there is nothing more to do in this crate.
Hi,
I'm trying to use okapi to generate openapi.yaml, but I'm having problems because the datatypes that I'm trying to use perhaps aren't supported by okapi, or perhaps that I don't understand the way they are supported. I have ended up implementing a lot of Traits and I'm just wondering if I haven't taken a wrong turn and made it harder than it needs to be.
I want to use an endpoint with a Form that contains a file. This is fairly easy in Rocket using the TempFile struct. When I try to mark the endpoint with openapi, I have errors that JsonSchema, Deserialize, Serialize and Default are not implemented for TempFile. In order to implement the Traits, I have to create a local struct for TempFile because you are not allowed to implement a trait you didn't define for a struct you didn't define. Then you need to write code incorporating the functionality from the original struct into your struct. Below are the stubs that I have implemented to satisfy the compiler for now. In the current state, it complains that OpenApiFromData is not implemented for rocket::form::Form<ExampleForm<'_>>. To solve this it looks like I might have to declare a local struct and add all of the traits and functions to it that I need.
Am I going about this the right way?
Here is my code:
Here is my Cargo.toml:
The text was updated successfully, but these errors were encountered: