feat: add typed conditional API responses

This commit is contained in:
2026-09-23 19:32:05 +09:00
parent 97822cf691
commit 237ab0e592
25 changed files with 1871 additions and 276 deletions
@@ -0,0 +1,14 @@
use api_macros::api;
pub struct Body;
#[api]
pub trait DuplicateResponseHeaderApi {
#[get(
"/duplicate",
responses = [(status = 200, body = Body, headers = [("etag", String), ("ETag", String)])]
)]
async fn duplicate(&self) -> duplicate_response_header_api_responses::Duplicate;
}
fn main() {}
@@ -0,0 +1,5 @@
error: duplicate response header name within one response
--> tests/ui/duplicate_response_header.rs:9:80
|
9 | responses = [(status = 200, body = Body, headers = [("etag", String), ("ETag", String)])]
| ^^^^^^
@@ -0,0 +1,14 @@
use api_macros::api;
pub struct Body;
#[api]
pub trait InvalidResponseHeaderApi {
#[get(
"/invalid",
responses = [(status = 200, body = Body, headers = [("bad header", String)])]
)]
async fn invalid(&self) -> invalid_response_header_api_responses::Invalid;
}
fn main() {}
@@ -0,0 +1,5 @@
error: response header name must be a valid HTTP field name
--> tests/ui/invalid_response_header.rs:9:62
|
9 | responses = [(status = 200, body = Body, headers = [("bad header", String)])]
| ^^^^^^^^^^^^
@@ -0,0 +1,14 @@
use api_macros::api;
pub struct Body;
#[api]
pub trait NotModifiedBodyApi {
#[get(
"/not-modified",
responses = [(status = 304, body = Body)]
)]
async fn not_modified(&self) -> not_modified_body_api_responses::NotModified;
}
fn main() {}
@@ -0,0 +1,5 @@
error: status 304 cannot have a response body
--> tests/ui/not_modified_body.rs:9:32
|
9 | responses = [(status = 304, body = Body)]
| ^^^
@@ -0,0 +1,18 @@
use api_macros::api;
pub struct Body;
#[api]
pub trait UndeclaredStatusApi {
#[get(
"/status",
responses = [(status = 200, body = Body)]
)]
async fn status(&self) -> undeclared_status_api_responses::Status;
}
fn invalid() -> undeclared_status_api_responses::Status {
undeclared_status_api_responses::Status::Status201 { body: Body }
}
fn main() {}
@@ -0,0 +1,14 @@
error[E0599]: no variant named `Status201` found for enum `undeclared_status_api_responses::Status`
--> tests/ui/undeclared_response_status.rs:15:46
|
5 | #[api]
| ------ variant `Status201` not found here
...
15 | undeclared_status_api_responses::Status::Status201 { body: Body }
| ^^^^^^^^^
|
help: there is a variant with a similar name
|
15 - undeclared_status_api_responses::Status::Status201 { body: Body }
15 + undeclared_status_api_responses::Status::Status200 { body: Body }
|