Skip to content

Commit

Permalink
utoipa-axum: Add basic path params test (#1268)
Browse files Browse the repository at this point in the history
This ensures that `/pet/{petId}` is correctly turned a path that `axum` understands.
  • Loading branch information
Turbo87 authored Jan 2, 2025
1 parent d1f706e commit 682d5bf
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
4 changes: 4 additions & 0 deletions utoipa-axum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ paste = "1.0"
[dev-dependencies]
utoipa = { path = "../utoipa", features = ["debug"] }
axum = { version = "0.7", default-features = false, features = ["json"] }
http = "1.2"
insta = { version = "1.41", features = ["json"] }
serde = "1"
tokio = { version = "1.42", features = ["macros", "rt"]}
tower = "0.5"

[package.metadata.docs.rs]
features = []
Expand Down
44 changes: 42 additions & 2 deletions utoipa-axum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,11 @@ mod tests {
use std::collections::BTreeMap;

use super::*;
use axum::extract::State;
use axum::extract::{Path, State};
use insta::assert_json_snapshot;
use router::*;
use utoipa::openapi::{Content, Ref, ResponseBuilder};
use tower::util::ServiceExt;
use utoipa::openapi::{Content, OpenApi, Ref, ResponseBuilder};
use utoipa::PartialSchema;

#[utoipa::path(get, path = "/")]
Expand Down Expand Up @@ -423,4 +425,42 @@ mod tests {
);
assert_eq!(expected_paths.build(), paths);
}

#[tokio::test]
async fn test_axum_router() {
#[utoipa::path(
get,
path = "/pet/{pet_id}",
params(("pet_id" = u32, Path, description = "ID of pet to return")),
)]
pub async fn get_pet_by_id(Path(pet_id): Path<u32>) -> String {
pet_id.to_string()
}

let (router, openapi) = OpenApiRouter::with_openapi(OpenApi::default())
.routes(routes!(get_pet_by_id))
.split_for_parts();

assert_json_snapshot!(openapi);

let request = http::Request::builder()
.uri("/pet/1")
.body("".to_string())
.unwrap();

let response = router.clone().oneshot(request).await.unwrap();
assert_eq!(response.status(), http::StatusCode::OK);

let body = response.into_body();
let body = axum::body::to_bytes(body, usize::MAX).await.unwrap();
assert_eq!(body, "1");

let request = http::Request::builder()
.uri("/pet/foo")
.body("".to_string())
.unwrap();

let response = router.oneshot(request).await.unwrap();
assert_eq!(response.status(), http::StatusCode::BAD_REQUEST);
}
}
34 changes: 34 additions & 0 deletions utoipa-axum/src/snapshots/utoipa_axum__tests__axum_router.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
source: utoipa-axum/src/lib.rs
expression: openapi
snapshot_kind: text
---
{
"openapi": "3.1.0",
"info": {
"title": "",
"version": ""
},
"paths": {
"/pet/{pet_id}": {
"get": {
"operationId": "get_pet_by_id",
"parameters": [
{
"name": "pet_id",
"in": "path",
"description": "ID of pet to return",
"required": true,
"schema": {
"type": "integer",
"format": "int32",
"minimum": 0
}
}
],
"responses": {}
}
}
},
"components": {}
}

0 comments on commit 682d5bf

Please sign in to comment.