Skip to content

Commit

Permalink
Add test for aliases on enum variant values
Browse files Browse the repository at this point in the history
  • Loading branch information
juhaku committed Oct 11, 2024
1 parent 9d076c4 commit 9b19cd9
Showing 1 changed file with 86 additions and 1 deletion.
87 changes: 86 additions & 1 deletion utoipa-config/config-test-crate/tests/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::borrow::Cow;

use utoipa::ToSchema;
use utoipa::{OpenApi, ToSchema};
use utoipa_config::Config;

#[test]
Expand Down Expand Up @@ -84,3 +84,88 @@ fn test_to_schema_with_aliases() {
println!("{actual}");
assert_eq!(expected.trim(), actual.trim())
}

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

#[derive(OpenApi)]
#[openapi(components(schemas(Transactions)))]
struct ApiDoc;

#[derive(ToSchema)]
pub enum Transactions {
Transaction(EntryAlias),
TransactionEntryString(EntryString),
}

pub type EntryAlias = Entry<i32>;
pub type EntryString = Entry<String>;

#[derive(ToSchema)]
pub struct Entry<I> {
pub entry_id: I,
}

let api = ApiDoc::openapi();
let value = serde_json::to_value(api).expect("OpenApi must be JSON serializable");
let schemas = value
.pointer("/components/schemas")
.expect("Must have schemas");

let expected = r###"{
"Entry_String": {
"properties": {
"entry_id": {
"type": "string"
}
},
"required": [
"entry_id"
],
"type": "object"
},
"Entry_i32": {
"properties": {
"entry_id": {
"format": "int32",
"type": "integer"
}
},
"required": [
"entry_id"
],
"type": "object"
},
"Transactions": {
"oneOf": [
{
"properties": {
"Transaction": {
"$ref": "#/components/schemas/Entry_i32"
}
},
"required": [
"Transaction"
],
"type": "object"
},
{
"properties": {
"TransactionEntryString": {
"$ref": "#/components/schemas/Entry_String"
}
},
"required": [
"TransactionEntryString"
],
"type": "object"
}
]
}
}"###;
assert_eq!(
serde_json::to_string_pretty(schemas).unwrap().trim(),
expected
);
}

0 comments on commit 9b19cd9

Please sign in to comment.