Skip to content

Commit

Permalink
fix empty enum variants
Browse files Browse the repository at this point in the history
  • Loading branch information
oscartbeaumont committed May 9, 2024
1 parent d32f718 commit 9fa9333
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
12 changes: 8 additions & 4 deletions src/lang/ts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,15 +661,19 @@ fn enum_datatype(ctx: ExportContext, e: &EnumType, type_map: &TypeMap) -> Output
format!("{{ {tag}: {sanitised_name} }}")
}
(EnumRepr::Adjacent { tag, content }, _) => {
let ts_values = enum_variant_datatype(
let ts_value = enum_variant_datatype(
ctx.with(PathItem::Variant(variant_name.clone())),
type_map,
variant_name.clone(),
variant,
)?
.expect("Invalid Serde type");
)?;

format!("{{ {tag}: {sanitised_name}; {content}: {ts_values} }}")
let mut result = format!("{{ {tag}: {sanitised_name}");
if let Some(ts_value) = ts_value {
result.push_str(&format!("; {content}: {ts_value}"));
}
result.push_str(" }");
result
}
},
true,
Expand Down
36 changes: 24 additions & 12 deletions tests/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,23 @@ fn typescript_types() {
assert_ts!(ExtraBracketsInTupleVariant, "{ A: string }");
assert_ts!(ExtraBracketsInUnnamedStruct, "string");

// https://github.com/oscartbeaumont/specta/issues/156
assert_ts!(Vec<MyEnum>, r#"({ A: string } | { B: number })[]"#);

assert_ts!(InlineTuple, r#"{ demo: [string, boolean] }"#);
assert_ts!(
InlineTuple2,
r#"{ demo: [{ demo: [string, boolean] }, boolean] }"#
);

// https://github.com/oscartbeaumont/specta/issues/220
assert_ts!(Box<str>, r#"string"#);

assert_ts!(
SkippedFieldWithinVariant,
r#"{ type: "A" } | { type: "B"; data: string }"#
);

// https://github.com/oscartbeaumont/specta/issues/90
assert_ts!(RenameWithWeirdCharsField, r#"{ "@odata.context": string }"#);
assert_ts!(
Expand Down Expand Up @@ -291,18 +308,6 @@ fn typescript_types() {
r#"@odata.context"#.to_string()
)
);

// https://github.com/oscartbeaumont/specta/issues/156
assert_ts!(Vec<MyEnum>, r#"({ A: string } | { B: number })[]"#);

assert_ts!(InlineTuple, r#"{ demo: [string, boolean] }"#);
assert_ts!(
InlineTuple2,
r#"{ demo: [{ demo: [string, boolean] }, boolean] }"#
);

// https://github.com/oscartbeaumont/specta/issues/220
assert_ts!(Box<str>, r#"string"#);
}

#[derive(Type)]
Expand Down Expand Up @@ -644,3 +649,10 @@ pub struct InlineTuple2 {
#[specta(inline)]
demo: (InlineTuple, bool),
}

#[derive(Type)]
#[serde(tag = "type", content = "data")]
pub enum SkippedFieldWithinVariant {
A(#[serde(skip)] String),
B(String),
}

0 comments on commit 9fa9333

Please sign in to comment.