Skip to content

text data in tag contents #868

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
eirnym opened this issue Jun 14, 2025 · 3 comments · May be fixed by #869
Open

text data in tag contents #868

eirnym opened this issue Jun 14, 2025 · 3 comments · May be fixed by #869
Labels
arrays Issues related to mapping XML content onto arrays using serde bug serde Issues related to mapping from Rust types to XML

Comments

@eirnym
Copy link

eirnym commented Jun 14, 2025

I have a data file which I cannot (shouldn't ask to) change:

<?xml version="1.0" encoding="utf-8"?>
<root>
	<key>
		<value id="1">text1</value>
		<value id="2">text2</value>
		<value id="3">text3</value>
		<value id="4">text4</value>d
		<value id="5">text5</value>
		<value id="6">text6</value>
	<key>
</root>

When I deserialize using quick-xml & serde, I have an error, with this d, which I'd love to ignore completely

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename = "root")]
pub struct Root {
  #[serde(rename = "key")]
  pub key: Key,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename = "key")]
pub struct Key {
  #[serde(rename = "value")]
  pub values: Option<Vec<Value>>
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename = "Value")]
pub struct Value {
  #[serde(rename = "@id")]
  pub id: String

  #[serde(rename = "$text")]
  pub text: Option<String>
}

Error I have and I'd like to ignore is Error: invalid type: string "d", expected struct Value

@Mingun Mingun added question serde Issues related to mapping from Rust types to XML arrays Issues related to mapping XML content onto arrays using serde labels Jun 14, 2025
@Mingun
Copy link
Collaborator

Mingun commented Jun 14, 2025

You could introduce intermediate type to accept text and ignore it. Something like

fn deserialize_values<'de, D>(deserializer: D) -> Result<Option<Vec<Value>>, D::Error>
where D: Deserializer<'de>,
{
  #[derive(Deserialize)]
  enum Workaround {
    #[serde(rename = "value")]
    Value(Value),
    #[serde(rename = "$text")]
    Text,
  }
  let data: Option<Vec<Workaround>> = Deserialize::deserialize(deserializer)?;
  // convert to Option<Vec<Value>> by skipping Workaround::Text elements
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename = "key")]
pub struct Key {
  #[serde(rename = "$value", deserialize_with = "deserialize_values")]
  pub values: Option<Vec<Value>>,
}

Note, that values should be renamed to $value so we can catch text data, and not only <value> tags

@eirnym
Copy link
Author

eirnym commented Jun 14, 2025

this is a possibility, thank you, I do this way in another problematic XML node, but there was a Choice enum from the beginning

I wonder why I have to specify this in quick-xml, while I can ignore it in serde_json. Maybe you can give some light on the difference.

@Mingun
Copy link
Collaborator

Mingun commented Jun 14, 2025

This is a bug. It should skip texts when deserialize sequences of elements with fixed name. The fix is coming

@Mingun Mingun added bug and removed question labels Jun 14, 2025
Mingun added a commit to Mingun/quick-xml that referenced this issue Jun 14, 2025
Mingun added a commit to Mingun/quick-xml that referenced this issue Jun 14, 2025
@Mingun Mingun linked a pull request Jun 14, 2025 that will close this issue
Mingun added a commit to Mingun/quick-xml that referenced this issue Jun 14, 2025
Mingun added a commit to Mingun/quick-xml that referenced this issue Jun 15, 2025
failures:
  serde-de-seq:
    fixed_name::variable_size::text_and_value
    variable_name::variable_size::text_and_value
  serde-issues:
    issue868
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrays Issues related to mapping XML content onto arrays using serde bug serde Issues related to mapping from Rust types to XML
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants