Repository navigation
Fix for invalid XML characters causing etree errors - #86
Merged
Merged
Conversation
Contributor
|
It could be good to have an example of a currently failing XML string that we could test against. |
Contributor
Author
See now-linked issue for an example DOI and a request that currently fails. Added tests for this as well. |
Decoding every record as UTF-8 before parsing broke valid ISO-8859-1 and UTF-16 metadata that lxml decodes from the XML declaration. Parse the original bytes first and only fall back to removing characters that XML 1.0 disallows when strict parsing fails on a UTF-8 document, meaning one that declares UTF-8 (or no encoding) and decodes as UTF-8. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 of 3 tasks
jrhoads
approved these changes
Nov 24, 2026
wendelfabianchinsamy
approved these changes
Nov 24, 2026
Contributor
Author
|
As far as I can tell, lupo doesn't serve ISO-8859-1 and UTF-16 records. The commit is still effective for the linked problem, though—tested locally. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Purpose
closes: datacite/datacite#2570
Some DOIs store DataCite XML that contains characters XML 1.0 doesn't allow, such as a literal U+0002 in
10.7805/hsmc-h261. lxml refuses to parse that XML. GetRecord and ListRecords then return HTTP 500 for thedataciteandoai_datacitemetadata prefixes.oai_dcwas already unaffected, because it strips these characters field by field.Approach
parse_datacite_xmlhelper inviringo/metadata.py, used by bothdatacite_writerandoai_datacite_writer.XMLSyntaxErroris raised. Records in other encodings are never re-decoded or altered.oai_dc_writer.xmlas bytes, matching what the DataCite API service returns after base64 decoding.Open Questions and Pre-Merge TODOs
masterserves fine, into 500s. It's fixed by parsing strictly first.Learning
#x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF](spec).lxml.etree.fromstringlets lxml detect the encoding from the BOM or the XML declaration. Decoding to text first loses that information.Types of changes
Bug fix (non-breaking change which fixes an issue)
New feature (non-breaking change which adds functionality)
Breaking change (fix or feature that would cause existing functionality to change)
Tests
dataciteandoai_dataciteuse a record that contains U+0002. The fixtures areoai_getrecord_datacite_invalid.xmlandoai_getrecord_oaidatacite_invalid.xml.tests/unit/test_metadata.pycovers:10.7805/hsmc-h261, GetRecord and ListRecords both return 200 locally in both formats.Reviewer, please remember our guidelines: