Skip to content
Merged
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
6 changes: 6 additions & 0 deletions reflectapi-demo/src/tests/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,15 @@ macro_rules! assert_builder_snapshot {
.unwrap();
reflectapi::codegen::strip_boilerplate(&files["generated.ts"])
};
let python = reflectapi::codegen::python::generate(
schema.clone(),
&reflectapi::codegen::python::Config::default(),
)
.unwrap();
insta::assert_json_snapshot!(schema);
insta::assert_snapshot!(typescript);
insta::assert_snapshot!(rust);
insta::assert_json_snapshot!(reflectapi::codegen::openapi::Spec::from(&schema));
insta::assert_snapshot!(python);
}};
}
61 changes: 61 additions & 0 deletions reflectapi-demo/src/tests/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,67 @@ fn test_internally_tagged_enum_with_tuple_variants() {
assert_snapshot!(E);
}

#[test]
fn test_internally_tagged_enum_with_optional_fields() {
// Repro for https://github.com/thepartly/reflectapi/issues/167:
// a nullable `Option<T>` field on a struct used as an internally-tagged
// enum variant must be generated as an optional key (serde omits the key
// when the value is `None`), matching the standalone-struct behavior.
#[derive(reflectapi::Input, reflectapi::Output, serde::Deserialize, serde::Serialize)]
pub struct VariantBody {
#[serde(skip_serializing_if = "Option::is_none")]
pub amount: Option<String>,
pub always_null_on_wire: Option<String>,
}

#[derive(reflectapi::Input, reflectapi::Output, serde::Deserialize, serde::Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum Thing {
Variant(VariantBody),
Named {
#[serde(skip_serializing_if = "Option::is_none")]
amount: Option<String>,
},
}

assert_snapshot!(Thing);
}

#[test]
fn test_struct_with_flattened_internally_tagged_enum_with_optional_fields() {
// Repro for https://github.com/thepartly/reflectapi/issues/167:
// same as test_internally_tagged_enum_with_optional_fields, but with the
// enum flattened into a parent struct, which is rendered by a separate
// code path generating one model per variant.
#[derive(reflectapi::Input, reflectapi::Output, serde::Deserialize, serde::Serialize)]
pub struct VariantBody {
#[serde(skip_serializing_if = "Option::is_none")]
pub amount: Option<String>,
pub always_null_on_wire: Option<String>,
}

#[derive(reflectapi::Input, reflectapi::Output, serde::Deserialize, serde::Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum Thing {
Variant(VariantBody),
Named {
#[serde(skip_serializing_if = "Option::is_none")]
amount: Option<String>,
},
}

#[derive(reflectapi::Input, reflectapi::Output, serde::Deserialize, serde::Serialize)]
pub struct Offer {
pub id: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub note: Option<String>,
#[serde(flatten)]
pub details: Thing,
}

assert_snapshot!(Offer);
}

#[test]
fn test_internally_tagged_enum_with_unit_variants() {
#[derive(reflectapi::Input, reflectapi::Output, serde::Deserialize, serde::Serialize)]
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Comment thread
hardbyte marked this conversation as resolved.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading