Skip to content

Commit

Permalink
Fix allow response content_type without schema (#1073)
Browse files Browse the repository at this point in the history
Fix allow response with `content_type` without schema.

```rust
 #[utoipa::path(post, path = "/api/secret", responses(
     (status = OK, content_type = "application/octet-stream")
 ))]
 async fn post_secret() {}
```

Fixes #965
  • Loading branch information
juhaku authored Oct 2, 2024
1 parent 8a5bb72 commit 4147119
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
3 changes: 2 additions & 1 deletion utoipa-gen/src/path/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,8 @@ impl ToTokensDiagnostics for ResponseTuple<'_> {
});

for media_type in value.content.iter().filter(|media_type| {
!matches!(media_type.schema, Schema::Default(DefaultSchema::None))
!(matches!(media_type.schema, Schema::Default(DefaultSchema::None))
&& media_type.content_type.is_none())
}) {
let default_content_type = media_type.schema.get_default_content_type()?;

Expand Down
30 changes: 30 additions & 0 deletions utoipa-gen/tests/path_response_derive_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,3 +685,33 @@ fn path_response_default_no_value_nor_ref() {
})
)
}

#[test]
fn path_response_with_no_schema() {
#![allow(unused)]

/// Post some secret inner handler
#[utoipa::path(post, path = "/api/inner/secret", responses(
(status = OK, content_type = "application/octet-stream")
))]
pub async fn post_secret() {}

let operation = __path_post_secret::operation();
let value = serde_json::to_value(operation).expect("operation is JSON serializable");

assert_json_eq!(
value,
json!({
"operationId": "post_secret",
"responses": {
"200": {
"content": {
"application/octet-stream": {}
},
"description": ""
}
},
"summary": "Post some secret inner handler"
})
)
}

0 comments on commit 4147119

Please sign in to comment.