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

Could not resolve reference: JSON Pointer evaluation failed while evaluating token "schemas" against an ObjectElement #1284

Open
codewithmecoder opened this issue Jan 10, 2025 · 1 comment

Comments

@codewithmecoder
Copy link

codewithmecoder commented Jan 10, 2025

I am using otoipa with actix and facing some issues

image

#[derive(OpenApi)]
#[openapi(
    paths(authHandler::login),
    components(schemas(
        LoginUserDto,
        UserLoginResponseDto,
        Response
    )),
    tags(
        (name = "Rust Authentication Api", description = "Authentication in Rust API")
    ),
    modifiers(&SecurityAddon)
)]
struct ApiDoc;

struct SecurityAddon;

impl Modify for SecurityAddon {
    fn modify(&self, openapi: &mut utoipa::openapi::OpenApi) {
        openapi.components = Some(
            utoipa::openapi::ComponentsBuilder::new()
                .security_scheme(
                    "token",
                    SecurityScheme::Http(
                        HttpBuilder::new()
                            .scheme(HttpAuthScheme::Bearer)
                            .bearer_format("JWT")
                            .build(),
                    ),
                )
                .build(),
        )
    }
}

#[derive(Debug, Clone, Validate, Serialize, Deserialize, Default, ToSchema)]
pub struct LoginUserDto {
    #[validate(
        email(message = "Invalid email"),
        length(min = 1, message = "Email is required")
    )]
    pub email: String,

    #[validate(length(min = 1, message = "Password is required"))]
    pub password: String,
}

#[utoipa::path(
    post,
    path = "/api/auth/login",
    tag = "Login Endpoint",
    request_body(
        content = LoginUserDto, 
        description = "Credentials to log in to your account", 
        example = json!(
            {
                "email": "[email protected]",
                "password": "password123"
            })),
    responses(
        (
            status = 200, 
            description = "Login successfull", 
            body= UserLoginResponseDto 
        ),
        (
            status=400, 
            description= "Validation Errors", 
            body= Response
        ),
        (
            status=500, 
            description= "Internal Server Error", 
            body= Response
        ),
    )
)]
pub async fn login(
    app_state: web::Data<AppState>,
    body: web::Json<LoginUserDto>,
) -> Result<HttpResponse, HttpError> {
    ...
}
@turulix
Copy link

turulix commented Feb 5, 2025

Facing a simular issue where Parameters:

        "parameters": [
          {
            "name": "user_id",
            "in": "query",
            "required": true,
            "schema": {
              "$ref": "#/components/schemas/UserId"
            }
          },

Don't get added to the components:

"components": {
    "schemas": {
      "SignUpRequest": {
        "type": "object",
        "required": [
          "email",
          "password"
        ],
        "properties": {
          "email": {
            "type": "string"
          },
          "password": {
            "type": "string"
          }
        }
      }
    }
  }
#[derive(ToSchema, IntoParams, Deserialize, Debug)]
struct VerifyEmailQuery {
    user_id: UserId,
    token: Uuid,
}

#[utoipa::path(
    summary = "Verify Email",
    params(
        VerifyEmailQuery
    ),
    responses(
        (status = 200, description = "Successfully verified the email"),
    ),
    description = "Verify Email for an account",
    tags = ["auth"],
)]
#[get("/verify_email")]

where UserId implements PartialSchema and To Schema like this:

        #[cfg(feature = "utoipa")]
        impl utoipa::PartialSchema for $struct_name {
            fn schema() -> utoipa::openapi::RefOr<utoipa::openapi::Schema> {
                utoipa::openapi::Schema::Object(
                    utoipa::openapi::ObjectBuilder::new()
                        .schema_type(utoipa::openapi::Type::String)
                        .format(Some(utoipa::openapi::SchemaFormat::Custom(concat!(stringify!($prefix), ":uuid7").to_string())))
                        .examples(vec![concat!(stringify!($prefix), ":00000000000070000000000000000000").to_string()])
                        .build(),
                )
                .into()
            }
        }
        #[cfg(feature = "utoipa")]
        impl utoipa::ToSchema for UserId {
            fn name() -> std::borrow::Cow<'static, str> {
                std::borrow::Cow::Borrowed(stringify!($struct_name))
            }
        }

Any help on this would be amazing :D

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

No branches or pull requests

2 participants