Skip to content

non JSON response example? #1476

@SamuelMarks

Description

@SamuelMarks
Image
use actix_web::{get, HttpResponse, Responder};

pub const SECRET_TEXT: &'static str = "secret endpoint";

#[derive(utoipa::ToSchema, utoipa::ToResponse, serde::Serialize, serde::Deserialize)]
#[response(
    description = "Override description for response",
    content_type = "application/text"
)]
#[response(
    example = json!(SECRET_TEXT)
)]
struct SecretText(&'static str);

impl Default for SecretText {
    fn default() -> Self {
        Self { 0: SECRET_TEXT }
    }
}

impl SecretText {
    const fn const_default() -> Self {
        Self { 0: SECRET_TEXT }
    }
}

const DEFAULT_SECRET_TEXT: SecretText = SecretText::const_default();



/// Shows secret to authenticated user (uses provided Bearer token from Header)
#[utoipa::path(
    responses(
        (status = 200, description = "secret endpoint", body=SecretText, example=SecretText::const_default)
    ),
    security(("password"=[]))
)]
#[get("/secret")]
pub async fn secret() -> impl Responder {
    HttpResponse::Ok().body(SECRET_TEXT)
}

…and yes I know example = json! means it'll be JSON but it gave an error when I didn't use json!.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions