Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions utoipa-gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ utoipa = { path = "../utoipa", features = [
], default-features = false }
serde_json = "1"
serde = "1"
serde_repr = "0.1"
actix-web = { version = "4", features = ["macros"], default-features = false }
axum = { version = "0.8.4", default-features = false, features = [
"json",
Expand Down
36 changes: 33 additions & 3 deletions utoipa-gen/tests/schema_derive_test.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use std::{borrow::Cow, cell::RefCell, collections::HashMap, marker::PhantomData};

use insta::assert_json_snapshot;
use serde::Serialize;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use utoipa::openapi::{Object, ObjectBuilder};
use utoipa::{OpenApi, ToSchema};

#[cfg(feature = "repr")]
use serde_repr::{Serialize_repr, Deserialize_repr};

mod common;

macro_rules! api_doc {
Expand Down Expand Up @@ -2366,8 +2369,16 @@ fn derive_struct_with_no_additional_properties() {
#[test]
#[cfg(feature = "repr")]
fn derive_schema_for_repr_enum() {
#[derive(Deserialize_repr, Serialize_repr)]
#[repr(i32)]
enum ExitCode {
Error = -1,
Ok = 0,
Unknown = 1,
}

let value = api_doc! {
#[derive(serde::Deserialize)]
#[derive(Deserialize_repr, Serialize_repr)]
#[repr(i32)]
#[schema(example = 1, default = 0)]
enum ExitCode {
Expand All @@ -2377,14 +2388,26 @@ fn derive_schema_for_repr_enum() {
}
};

assert_eq!(serde_json::to_string(&ExitCode::Error).unwrap(), "-1");
assert!(matches!(serde_json::from_str("1").unwrap(), ExitCode::Unknown));

assert_json_snapshot!(value);
}

#[test]
#[cfg(feature = "repr")]
fn derive_schema_for_tagged_repr_enum() {
#[derive(Deserialize_repr, Serialize_repr)]
#[serde(tag = "tag")]
#[repr(u8)]
enum TaggedEnum {
One = 0,
Two,
Three,
}

let value: Value = api_doc! {
#[derive(serde::Deserialize, serde::Serialize)]
#[derive(Deserialize_repr, Serialize_repr)]
#[serde(tag = "tag")]
#[repr(u8)]
enum TaggedEnum {
Expand All @@ -2394,6 +2417,13 @@ fn derive_schema_for_tagged_repr_enum() {
}
};

assert_eq!(serde_json::to_string(&TaggedEnum::One).unwrap(), r#"{"tag": 0}"#);
assert_eq!(serde_json::to_string(&TaggedEnum::Two).unwrap(), r#"{"tag": 1}"#);
assert_eq!(serde_json::to_string(&TaggedEnum::Three).unwrap(), r#"{"tag": 2}"#);
assert!(matches!(serde_json::from_str(r#"{"tag": 0}"#).unwrap(), TaggedEnum::One));
assert!(matches!(serde_json::from_str(r#"{"tag": 1}"#).unwrap(), TaggedEnum::Two));
assert!(matches!(serde_json::from_str(r#"{"tag": 2}"#).unwrap(), TaggedEnum::Three));

assert_json_snapshot!(value);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: utoipa-gen/tests/schema_derive_test.rs
assertion_line: 2394
expression: value
---
{
"default": 0,
"enum": [
-1,
0,
1
],
"example": 1,
"type": "integer"
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please insta accept the snapshots and commit the result. .new is just the proto-snapshot and needs to be renamed first

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the one where the generated openapi doesn't match the serde_repr runtime behavior, so the snapshot output here is wrong (potentially). That's why I left it in this state.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CommanderStorm I wasn't sure what to do here in the case where I'm not sure whether an existing behavior is a bug or not, as I've never used repr enums but the output seems to not make sense. Should I open an issue?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the behaviour is wrong, that would be best.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot that I already did: #1584

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
source: utoipa-gen/tests/schema_derive_test.rs
assertion_line: 2397
expression: value
---
{
"oneOf": [
{
"properties": {
"tag": {
"enum": [
0
],
"type": "integer"
}
},
"required": [
"tag"
],
"type": "object"
},
{
"properties": {
"tag": {
"enum": [
1
],
"type": "integer"
}
},
"required": [
"tag"
],
"type": "object"
},
{
"properties": {
"tag": {
"enum": [
2
],
"type": "integer"
}
},
"required": [
"tag"
],
"type": "object"
}
]
}