roshan
June 22, 2024, 4:05am
1
Is it possible to conditionally include paths, components using Utoipa. I want the openapi specification that is generated to be feature based.
Consider this example -
response::IntoResponse,
routing, Json, Router,
};
use hyper::{HeaderMap, StatusCode};
use serde::{Deserialize, Serialize};
use tokio::sync::Mutex;
use utoipa::{IntoParams, OpenApi, ToSchema};
#[derive(OpenApi)]
#[openapi(
paths(list_todos, search_todos, create_todo, mark_done, delete_todo,),
components(schemas(Todo, TodoError))
)]
pub(super) struct TodoApi;
/// In-memory todo store
type Store = Mutex<Vec<Todo>>;
/// Item to do.
#[derive(Serialize, Deserialize, ToSchema, Clone)]
struct Todo {
If feature = "read" only list_todos, search_todos should be generated.
If feature = "write" only create_todo, delete_todo, mark_done should be generated.
Does anyone know if that is possible with utoipa?
Thanks,
Roshan.
jofas
June 22, 2024, 4:58am
2
You can use cfg_attr
for this.
#[derive(OpenApi)]
#[openapi(components(schemas(Todo, TodoError))]
#[cfg_attr(feature = "write", openapi(paths(create_todo, mark_done, delete_todo)))]
#[cfg_attr(feature = "read", openapi(paths(list_todos, search_todos)))]
pub(super) struct TodoApi;
2 Likes
system
Closed
September 20, 2024, 4:58am
3
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.