Skip to content
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

[Question]: Deserialize optional vector #510

Closed
sandersantema opened this issue Nov 15, 2022 · 4 comments · Fixed by #548
Closed

[Question]: Deserialize optional vector #510

sandersantema opened this issue Nov 15, 2022 · 4 comments · Fixed by #548
Labels
arrays Issues related to mapping XML content onto arrays using serde bug serde Issues related to mapping from Rust types to XML

Comments

@sandersantema
Copy link

sandersantema commented Nov 15, 2022

Afaik the following line in Cargo.toml should mean that I'm using the latest git version of quick-xml:

quick-xml = { git = "https://github.com/tafia/quick-xml", features = ['serialize']}

Although I've put question in the title this issue might be a bug, but I can't tell whether I'm doing something wrong or if it is indeed a bug.

I've got the following xml:

<ENTRY>
  <CUE_V2 NAME="foo"></CUE_V2>
  <CUE_V2 NAME="bar"></CUE_V2>
</ENTRY>

And I've got the following struct to deserialize:

use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename = "ENTRY")]
struct Entry {
    #[serde(rename = "CUE_V2")]
    cues: Vec<Cue>,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde_with::serde_as]
struct Cue {
    #[serde(rename = "@NAME")]
    name: String,
}

This works but is incorrect since the cue vector should be optional like so:

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename = "ENTRY")]
struct Entry {
    #[serde(rename = "CUE_V2")]
    cues: Option<Vec<Cue>>,
}

But this gives the error:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: UnexpectedEnd([69, 78, 84, 82, 89])', src/main.rs:330:56

There might be a trivial solution, but after two days of using rust in total I can't seem to find it :)

If I may take the opportunity to ask another unrelated question, how do I build the documentation for the git version of the package locally? I've been looking through #490 but I think having the actual documentation would be a bit better :)

@Mingun
Copy link
Collaborator

Mingun commented Nov 15, 2022

But this gives the error:

Yes, this looks like a bug, I faced with it but did not dig yet. Probably related to #497. Perhaps in your case it would more useful to define cues as #[serde(default)] to get an empty vector if no elements are present (that works now and that case is included to our tests):

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename = "ENTRY")]
struct Entry {
    #[serde(rename = "CUE_V2", default)]
    cues: Vec<Cue>,
}

To build the doc use the following command (powershell):

$env:RUSTDOCFLAGS="--cfg docs_rs"; cargo +nightly doc --all-features

(

$env:RUSTDOCFLAGS="--cfg docs_rs"; cargo +nightly doc --all-features --document-private-items

if you wish to learn internals and contribute to the project)

@Mingun Mingun added bug serde Issues related to mapping from Rust types to XML arrays Issues related to mapping XML content onto arrays using serde labels Nov 15, 2022
@sandersantema
Copy link
Author

Thanks so much for the very quick answer! This workaround serves me very well. Compiling the documentation using your command works well, although the online documentation still says 0.26.0 but I think that's wrong. I can't find much about using quick-xml together with structs however, is that missing or is something going wrong on my side?

@Mingun
Copy link
Collaborator

Mingun commented Nov 16, 2022

Compiling the documentation using your command works well, although the online documentation still says 0.26.0

Yes, because we update version number when we make a release, until that the reported version is the last released version.

I can't find much about using quick-xml together with structs however, is that missing or is something going wrong on my side?

The documentation for serde mapping is coming. Draft in #369 (but it is outdated in some places)

@sandersantema
Copy link
Author

sandersantema commented Nov 16, 2022

Thanks again for being so helpful, with #369 I think I should be able to figure out how it works. Looking forward to whenever 0.27.0 is released :)

Would you like to keep this issue open or can it be closed as a duplicate of #497? If you want to keep it as a separate bug I'll edit the issue accordingly.

Mingun added a commit to Mingun/quick-xml that referenced this issue Jan 28, 2023
failures (1):
    issue510
dralley pushed a commit that referenced this issue Feb 6, 2023
failures (1):
    issue510
crapStone added a commit to Calciumdibromid/CaBr2 that referenced this issue Mar 16, 2023
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [quick-xml](https://github.com/tafia/quick-xml) | dependencies | minor | `0.27.1` -> `0.28.0` |

---

### Release Notes

<details>
<summary>tafia/quick-xml</summary>

### [`v0.28.0`](https://github.com/tafia/quick-xml/blob/HEAD/Changelog.md#&#8203;0280----2023-03-13)

[Compare Source](tafia/quick-xml@v0.27.1...v0.28.0)

##### New Features

-   [#&#8203;541]: (De)serialize specially named `$text` enum variant in [externally tagged]
    enums to / from textual content
-   [#&#8203;556]: `to_writer` and `to_string` now accept `?Sized` types
-   [#&#8203;556]: Add new `to_writer_with_root` and `to_string_with_root` helper functions
-   [#&#8203;520]: Add methods `BytesText::inplace_trim_start` and `BytesText::inplace_trim_end`
    to trim leading and trailing spaces from text events
-   [#&#8203;565]: Allow deserialize special field names `$value` and `$text` into borrowed
    fields when use serde deserializer
-   [#&#8203;568]: Rename `Writter::inner` into `Writter::get_mut`
-   [#&#8203;568]: Add method `Writter::get_ref`
-   [#&#8203;569]: Rewrite the `Reader::read_event_into_async` as an async fn, making the future `Send` if possible.
-   [#&#8203;571]: Borrow element names (`<element>`) when deserialize with serde.
    This change allow to deserialize into `HashMap<&str, T>`, for example
-   [#&#8203;573]: Add basic support for async byte writers via tokio's `AsyncWrite`.

##### Bug Fixes

-   [#&#8203;537]: Restore ability to deserialize attributes that represents XML namespace
    mappings (`xmlns:xxx`) that was broken since [#&#8203;490]
-   [#&#8203;510]: Fix an error of deserialization of `Option<T>` fields where `T` is some
    sequence type (for example, `Vec` or tuple)
-   [#&#8203;540]: Fix a compilation error (probably a rustc bug) in some circumstances.
    `Serializer::new` and `Serializer::with_root` now accepts only references to `Write`r.
-   [#&#8203;520]: Merge consequent (delimited only by comments and processing instructions)
    texts and CDATA when deserialize using serde deserializer. `DeEvent::Text` and
    `DeEvent::CData` events was replaced by `DeEvent::Text` with merged content.
    The same behavior for the `Reader` does not implemented (yet?) and should be
    implemented manually
-   [#&#8203;562]: Correctly set minimum required version of memchr dependency to 2.1
-   [#&#8203;565]: Correctly set minimum required version of tokio dependency to 1.10
-   [#&#8203;565]: Fix compilation error when build with serde <1.0.139

[externally tagged]: https://serde.rs/enum-representations.html#externally-tagged

[#&#8203;490]: tafia/quick-xml#490

[#&#8203;510]: tafia/quick-xml#510

[#&#8203;520]: tafia/quick-xml#520

[#&#8203;537]: tafia/quick-xml#537

[#&#8203;540]: tafia/quick-xml#540

[#&#8203;541]: tafia/quick-xml#541

[#&#8203;556]: tafia/quick-xml#556

[#&#8203;562]: tafia/quick-xml#562

[#&#8203;565]: tafia/quick-xml#565

[#&#8203;568]: tafia/quick-xml#568

[#&#8203;569]: tafia/quick-xml#569

[#&#8203;571]: tafia/quick-xml#571

[#&#8203;573]: tafia/quick-xml#573

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNS42LjAiLCJ1cGRhdGVkSW5WZXIiOiIzNS42LjAifQ==-->

Co-authored-by: cabr2-bot <cabr2.help@gmail.com>
Co-authored-by: crapStone <crapstone01@gmail.com>
Reviewed-on: https://codeberg.org/Calciumdibromid/CaBr2/pulls/1818
Reviewed-by: crapStone <crapstone@noreply.codeberg.org>
Co-authored-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
Co-committed-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
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