diff --git a/engine/baml-lib/jsonish/src/tests/test_unions.rs b/engine/baml-lib/jsonish/src/tests/test_unions.rs index 5b93ca6b4..fc6d9dc79 100644 --- a/engine/baml-lib/jsonish/src/tests/test_unions.rs +++ b/engine/baml-lib/jsonish/src/tests/test_unions.rs @@ -70,3 +70,176 @@ test_deserializer!( "data": null } ); + +const MAP_VS_CLASS: &str = r###" +class Page { + object string @description("Always page") + // children (Bookmark | Breadcrumb | BulletedListItem | Callout | Code | ColumnList | Divider | Embed | Equation | File | Heading1 | Heading2 | Heading3 | ImageFile | NumberedListItem | Paragraph | PDF | Quote | Table | TableOfContents | ToDo | Toggle | Video)[] @description("Some blocks of the page") + children (Breadcrumb | ColumnList)[] @description("Some blocks of the page") +} + +// ------------ Breadcrumb ---------------- +class Breadcrumb { + type string @description("Always breadcrumb") + breadcrumb map @description("Always empty map") +} + +/// ------------ Column List ---------------- +class ColumnList { + type string @description("Always column_list") + column_list ColumnListBody @description("Column list block with columns") +} + +class ColumnListBody { + children Column[] @description("The columns in the column list. Max length is 5") +} + +class Column { + type string @description("Always column") + column ColumnBody @description("Always empty map for columns") +} + +class ColumnBody { + // children (Bookmark | Breadcrumb | BulletedListItem | Callout | Code | Divider | Embed | Equation | File | Heading1 | Heading2 | Heading3 | ImageFile | NumberedListItem | Paragraph | PDF | Quote | Table | TableOfContents | ToDo | Toggle | Video)[] @description("Content of the column. Can contain any block type. Min length is 1") + children (Breadcrumb | Paragraph)[] +} + +/// ------------ Other blocks ---------------- +/// +class Paragraph { + type string @description("Always paragraph") + paragraph string +} +"###; + +test_deserializer!( + prefer_class_to_map, + MAP_VS_CLASS, + r#"```json + { + "object": "page", + "children": [ + { + "type": "column_list", + "column_list": { + "children": [ + { + "type": "column", + "column": { + "children": [ + { + "untyped": "lorem ipsum", + }, + { + "untyped": "lorem ipsum", + }, + { + "untyped": "lorem ipsum", + }, + { + "type": "paragraph", + "paragraph": "J.R.R. Tolkien", + }, + { + "type": "to_do", + "to_do": { + "untyped": "dolor sit amet", + "rich_text": [ + { + "type": "text", + "text": { + "content": "Read again" + } + } + ], + "checked": false + } + } + ] + } + } + ] + } + } + ] + } + ```"#, + FieldType::class("Page"), + { + "children": [ + { + "type": "column_list", + "column_list": { + "children": [ + { + "type": "column", + "column": { + "children": [ + { + "type": "paragraph", + "paragraph": "J.R.R. Tolkien", + }, + ] + } + } + ] + } + } + ] + } +); + +test_deserializer!( + array_of_union_should_drop_unknown, + MAP_VS_CLASS, + r#"```json + { + "children": [ + { + "untyped": "lorem ipsum", + }, + { + "untyped": "lorem ipsum", + }, + { + "untyped": "lorem ipsum", + }, + { + "untyped": "lorem ipsum", + }, + { + "type": "paragraph", + "paragraph": "J.R.R. Tolkien", + }, + { + "type": "to_do", + "to_do": { + "untyped": "dolor sit amet", + "rich_text": [ + { + "type": "text", + "text": { + "content": "Read again" + } + } + ], + "checked": false + } + } + ] + } + ```"#, + FieldType::class("ColumnBody"), + { + "children": [ + { + "type": "paragraph", + "paragraph": "J.R.R. Tolkien", + }, + { + "type": "to_do", + "breadcrumb": {}, + } + ] + } +);