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

Make WebSocketUpgraded public #415

Closed
ikrivosheev opened this issue Oct 24, 2022 · 8 comments
Closed

Make WebSocketUpgraded public #415

ikrivosheev opened this issue Oct 24, 2022 · 8 comments
Labels
question Further information is requested

Comments

@ikrivosheev
Copy link

Hello! I try do something like this:

#[OpenApi(prefix_path = "/example")]
impl ApiV1 {

    #[oai(path = "/", method = "get")]
    pub async fn web_socket(self, ws: WebSocket) -> impl IntoResponse { ... }
}

But I get error: impl Trait only allowed in function and inherent method return types, not in path. Can you make struct WebSocketUpgraded public?

@ikrivosheev ikrivosheev added the question Further information is requested label Oct 24, 2022
@sunli829
Copy link
Collaborator

because here:

pub async fn web_socket(&self, ws: WebSocket) -> impl IntoResponse { ... }
                        ^^^^^
pub async fn web_socket(self, ws: WebSocket) -> impl IntoResponse { ... }

@ikrivosheev
Copy link
Author

because here:

pub async fn web_socket(&self, ws: WebSocket) -> impl IntoResponse { ... }
                        ^^^^^
pub async fn web_socket(self, ws: WebSocket) -> impl IntoResponse { ... }

I get same error: impl Trait only allowed in function and inherent method return types, not in path

@sunli829
Copy link
Collaborator

I can't reproduce it, can you give me a repo?

@ikrivosheev
Copy link
Author

I can't reproduce it, can you give me a repo?

Simple code to reproduce:

main.rs:

use poem::{
    listener::TcpListener,
    web::websocket::WebSocket,
    IntoEndpoint, IntoResponse, Route, Server,
};
use poem_openapi::{OpenApi, OpenApiService};

pub struct ApiV1;

#[OpenApi(prefix_path = "/example")]
impl ApiV1 {
    #[oai(path = "/ws", method = "post")]
    pub async fn web_socket(&self, ws: WebSocket) -> impl IntoResponse {
        todo!();
    }
}

#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
    let api_service = OpenApiService::new(ApiV1, env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"))
        .description(env!("CARGO_PKG_DESCRIPTION"))
        .server("http://localhost:9000/api/v1");

    let swagger = api_service.swagger_ui();
    let json = api_service.spec_endpoint();
    let yaml = api_service.spec_endpoint_yaml();

    let app = Route::new()
        .nest("/api/v1", api_service.into_endpoint())
        .nest("/docs/swagger", swagger)
        .nest("/docs/openapi.json", json)
        .nest("/docs/openapi.yml", yaml);

    Server::new(TcpListener::bind("localhost:9000"))
        .run(app)
        .await
}

Cargo.toml

[package]
name = "ws_example"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
poem = { version = "1.3", features = ["websocket"] }
poem-openapi = { version = "2.0", features = ["swagger-ui"] }
tokio = { version = "1.0", features = ["full"] }

@sunli829
Copy link
Collaborator

I'm stupid, the error is obvious. I am working on it. 😂

@ikrivosheev
Copy link
Author

ikrivosheev commented Oct 24, 2022

I'm stupid, the error is obvious. I am working on it. joy

Don't worry) Thank you so mach!

sunli829 added a commit that referenced this issue Oct 24, 2022
Implement `ApiResponse` for `WebSocketUpgraded<T>` #415
@sunli829
Copy link
Collaborator

Fixed in master

#[OpenApi(prefix_path = "/example")]
impl ApiV1 {
    #[oai(path = "/ws", method = "post")]
    pub async fn web_socket(&self, ws: WebSocket) -> BoxWebSocketUpgraded {
       ws.on_upgrade(|stream| async move {
            // ....
       }).boxed()
    }
}
poem = { git="...", features = ["websocket"] }
poem-openapi = { git="...", features = [
    "swagger-ui",
    "websocket", # don't forget add this feature
] }

@ikrivosheev
Copy link
Author

@sunli829 thank you so mach!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants