Skip to content
This repository has been archived by the owner on Apr 29, 2023. It is now read-only.

Marshalling of restricted dateTime ignores restriction pattern #119

Open
teake opened this issue Nov 18, 2019 · 3 comments
Open

Marshalling of restricted dateTime ignores restriction pattern #119

teake opened this issue Nov 18, 2019 · 3 comments
Milestone

Comments

@teake
Copy link

teake commented Nov 18, 2019

When an xs:dateTime is restricted to a subset of the allowed lexical space with fixed number of fractional seconds digits, marshalling Python instances to XML ignores this restriction.

For instance, with the following schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="datetimetest" 
  xmlns="datetimetest" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="testelement" type="datetimerestriction"/>
    <xs:simpleType name="datetimerestriction">
        <xs:restriction base="xs:dateTime">
            <xs:pattern value="\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d\.\d\d\dZ"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>

And the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<testelement xmlns="datetimetest" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="datetimetest file:<path to above xsd>"
>2019-11-29T15:30:16.100Z</testelement>

Converting from and to XML then gives 2019-11-29T15:30:16.1Z. This is in the canonical lexical space of the unrestricted dateTime type, but does not correspond to the above schema.

Similarly, when the original value is more precise than what the XSD restriction pattern indicates, e.g. 2019-11-29T15:30:16.100131Z, the generated XML will always be as precise as the original value (well, up to a maximum of six digits, as Python's datetime.datetime goes to microsecond accuracy).

Is there some way to work around this, or should I just tell our XSD modellers to stop restricting dateTimes in such a way?

Btw, I'm on pyxb 1.2.6, Python 3.6.7 and a Mac.

@pabigot
Copy link
Owner

pabigot commented Nov 19, 2019

There's no simple way to work around this. The explanation can be found at:

@note: In PyXB, pattern constraints are ignored for any type with

Basically the value has already been converted to a Python dateTime instance at the point where the pattern constraint is checked. That means the original lexical representation has been discarded. So PyXB doesn't validate the document when it's parsed.

PyXB also doesn't attempt to generate lexical representations that are consistent with restrictions, since that's outside the scope of validation (and would be really really hard to do). So the generated representation isn't going to satisfy the restriction either.

It's possible there'd be a way to work around this with a customized binding class but it'd probably be painful.

@teake
Copy link
Author

teake commented Nov 19, 2019

Ok, thanks for the explanation, that's pretty clear.

There's an @todo just below the comment you linked to:

pyxb/pyxb/binding/facets.py

Lines 410 to 412 in 14737c2

@todo: On creating new instances of non-string simple types from
string representations, we could apply pattern constraints. That
would mean checking them prior to invoking the Factory method.

I'm not sure how that squares with you what said above, but if implemented it would make life a bit easier in these situations. But if this @todo is no longer on the roadmap, I think this issue can be closed.

@pabigot pabigot added this to the When Pigs Fly milestone Nov 19, 2019
@pabigot
Copy link
Owner

pabigot commented Nov 19, 2019

Given #100 and the fact the @todo was introduced on 2009-09-06 I think we can say it's not on the roadmap. Thanks for using PyXB and for your report. I'll leave it open since it's legitimate and unresolved, but have marked it as unlikely to be worked.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants