Skip to content

Commit 9fa9333

Browse files
fix empty enum variants
1 parent d32f718 commit 9fa9333

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

src/lang/ts/mod.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -661,15 +661,19 @@ fn enum_datatype(ctx: ExportContext, e: &EnumType, type_map: &TypeMap) -> Output
661661
format!("{{ {tag}: {sanitised_name} }}")
662662
}
663663
(EnumRepr::Adjacent { tag, content }, _) => {
664-
let ts_values = enum_variant_datatype(
664+
let ts_value = enum_variant_datatype(
665665
ctx.with(PathItem::Variant(variant_name.clone())),
666666
type_map,
667667
variant_name.clone(),
668668
variant,
669-
)?
670-
.expect("Invalid Serde type");
669+
)?;
671670

672-
format!("{{ {tag}: {sanitised_name}; {content}: {ts_values} }}")
671+
let mut result = format!("{{ {tag}: {sanitised_name}");
672+
if let Some(ts_value) = ts_value {
673+
result.push_str(&format!("; {content}: {ts_value}"));
674+
}
675+
result.push_str(" }");
676+
result
673677
}
674678
},
675679
true,

tests/ts.rs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,23 @@ fn typescript_types() {
261261
assert_ts!(ExtraBracketsInTupleVariant, "{ A: string }");
262262
assert_ts!(ExtraBracketsInUnnamedStruct, "string");
263263

264+
// https://github.com/oscartbeaumont/specta/issues/156
265+
assert_ts!(Vec<MyEnum>, r#"({ A: string } | { B: number })[]"#);
266+
267+
assert_ts!(InlineTuple, r#"{ demo: [string, boolean] }"#);
268+
assert_ts!(
269+
InlineTuple2,
270+
r#"{ demo: [{ demo: [string, boolean] }, boolean] }"#
271+
);
272+
273+
// https://github.com/oscartbeaumont/specta/issues/220
274+
assert_ts!(Box<str>, r#"string"#);
275+
276+
assert_ts!(
277+
SkippedFieldWithinVariant,
278+
r#"{ type: "A" } | { type: "B"; data: string }"#
279+
);
280+
264281
// https://github.com/oscartbeaumont/specta/issues/90
265282
assert_ts!(RenameWithWeirdCharsField, r#"{ "@odata.context": string }"#);
266283
assert_ts!(
@@ -291,18 +308,6 @@ fn typescript_types() {
291308
r#"@odata.context"#.to_string()
292309
)
293310
);
294-
295-
// https://github.com/oscartbeaumont/specta/issues/156
296-
assert_ts!(Vec<MyEnum>, r#"({ A: string } | { B: number })[]"#);
297-
298-
assert_ts!(InlineTuple, r#"{ demo: [string, boolean] }"#);
299-
assert_ts!(
300-
InlineTuple2,
301-
r#"{ demo: [{ demo: [string, boolean] }, boolean] }"#
302-
);
303-
304-
// https://github.com/oscartbeaumont/specta/issues/220
305-
assert_ts!(Box<str>, r#"string"#);
306311
}
307312

308313
#[derive(Type)]
@@ -644,3 +649,10 @@ pub struct InlineTuple2 {
644649
#[specta(inline)]
645650
demo: (InlineTuple, bool),
646651
}
652+
653+
#[derive(Type)]
654+
#[serde(tag = "type", content = "data")]
655+
pub enum SkippedFieldWithinVariant {
656+
A(#[serde(skip)] String),
657+
B(String),
658+
}

0 commit comments

Comments
 (0)