FSharp.Data icon indicating copy to clipboard operation
FSharp.Data copied to clipboard

XmlProvider does not parse DateTimeOffset values in format "YYYY-MM-DDThh:mm:ss"

Open kstastny opened this issue 5 years ago • 5 comments

This problem is related to FSharp.Data version 3.3.3.

I am trying to create XmlProvider from XSD but it fails on parsing xs:dateTime values. When trying to access the property, I am getting an error System.Exception: Expecting DateTimeOffset in Value, got 2020-01-20T12:15:00

The same problem happens when accessing XML Values and when accessing XML Attributes.

See the minimal example below.

Is there some workaround or setting that I am missing or is this a bug?

thank you.

open FSharp.Data

[<Literal>]
let xsd =
    """
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
        <xs:element name="start" type="xs:dateTime" /> 
    </xs:schema>
"""

type Person = XmlProvider<Schema = """
  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="person">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="surname" type="xs:string"/>
          <xs:element name="birthDate" type="xs:dateTime"/>
        </xs:sequence>
      </xs:complexType>
    </xs:element>
  </xs:schema>""">



[<EntryPoint>]
let main argv =
    let p =
        Person.Parse(
          """
          <person>
            <surname>Stastny</surname>
            <birthDate>2020-01-20T12:15:00</birthDate>
          </person>
          """)
    printfn "%A" p.BirthDate
    0

kstastny avatar Jan 20 '20 12:01 kstastny

I'm afraid it's a bug because the XML is valid. An ugly workaround is to avoid accessing the BirthDate property and use the untyped XElement property instead to read the element value

giacomociti avatar Jan 21 '20 23:01 giacomociti

@giacomociti thank you for your answer, I was afraid of that. I am using exactly the same workaround as you have described, so it at least does not block. I will keep it in the code until this issue gets resolved.

kstastny avatar Jan 22 '20 06:01 kstastny

this commit may fix the issue, but I haven't submitted a PR yet because it is a breaking change for the other type providers (notice I had to change a test of CSV provider).

The problem is that the parsing functions are shared by all type providers.

giacomociti avatar Jan 24 '20 21:01 giacomociti

Thank you for a quick fix! I am on different project at the moment, I will try the fix next week.

kstastny avatar Jan 27 '20 08:01 kstastny

@giacomociti that does it for me, thank you

kstastny avatar Feb 03 '20 09:02 kstastny