Feature based openapi ApiDocs in Utoipa?

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 -

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.

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