Skip to content

#[serde(flatten)] causes struct to serialize into Map instead of Object #69

@Enduriel

Description

@Enduriel

Given the following rust code:

	#[derive(serde::Serialize, serde::Deserialize, tsify::Tsify)]
	#[serde(rename_all = "camelCase")]
	#[tsify(into_wasm_abi, from_wasm_abi)]
	pub struct Bar {
		content: String,
	}

	#[derive(serde::Serialize, serde::Deserialize, tsify::Tsify)]
	#[serde(rename_all = "camelCase")]
	#[tsify(into_wasm_abi, from_wasm_abi)]
	pub struct Foo {
		#[serde(flatten)]
		bar: Bar,
		more: String,
	}

	#[wasm_bindgen]
	pub fn foobar() -> Foo {
		Foo {
			bar: Bar {
				content: "hello".to_string(),
			},
			more: "world".to_string(),
		}
	}

Calling foobar TS side will give me a map, not a JS Object. So doing

console.log(foobar())

will print

Map(2) { 'content' => 'hello', 'more' => 'world' }

this is rather problematic as it means the value cannot be accessed via the normal object property access syntax.

I am not sure if this is relevant (have not tested without), but I am using serde-wasm-bindgen via the js feature.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions