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

GUID pattern restriction on xs:string results in string instead of Guid #402

Closed
bzwieratinnovadis opened this issue Jul 23, 2023 · 10 comments

Comments

@bzwieratinnovadis
Copy link

bzwieratinnovadis commented Jul 23, 2023

Hi,

We are currently in the process of migrating our code generation process from xsd.exe to this library. There is one issue that I can't seem to figure out with the CLI options and that is to generate Guid based properties when there is a regex pattern restriction (below).

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="http://schemas.microsoft.com/2003/10/Serialization/"
           version="1.0">
    <xs:simpleType name="guid">
        <xs:annotation>
            <xs:documentation xml:lang="en">
                The representation of a GUID, generally the id of an element.
            </xs:documentation>
        </xs:annotation>
        <xs:restriction base="xs:string">
            <xs:pattern value="[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>

Running the CLI with these options:

dotnet xscgen --nf .\namespaceMapping.txt --uc --cc- --csm=PublicWithoutConstructorInitialization --nc -i=i --fb Xsds\**\*.xsd

which results in:

/// <summary>
/// <para xml:lang="en">The representation of a GUID, generally the id of an element.</para>
/// <para xml:lang="en">Pattern: [a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}.</para>
/// </summary>
[System.ComponentModel.DataAnnotations.RegularExpressionAttribute("[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}")]
[System.Xml.Serialization.XmlElementAttribute("BatchReferentie", IsNullable=true)]
public string BatchReferentie { get; set; }

When using xsd.exe, it generates Guid instead of string. Is there some option or alternate way to do this with the CLI?

Thanks!

@mganss
Copy link
Owner

mganss commented Jul 24, 2023

I can't repro using xsd.exe. Can you provide a full example with the corresponding command line to invoke xsd.exe?

@bzwieratinnovadis
Copy link
Author

See this post

@mganss
Copy link
Owner

mganss commented Jul 24, 2023

OK now I can repro. But it doesn't work if the namespace of the guid type is http://schemas.microsoft.com/2003/10/Serialization/ as in your original example. Can you get xsd.exe to generate a Guid property with that namespace as well? For me, it seems to only work with http://microsoft.com/wsdl/types/.

OTOH, the actual schema definition of guid doesn't seem to matter. You can even leave out the schema (results in a warning but still generates a Guid) or define guid as an int.

@bzwieratinnovadis
Copy link
Author

@mganss Thanks for looking into it! You're right that it doesn't seem to matter which schema definition to use.

I also found this and I'm not sure if that is still a relevant solution. Also, it wouldn't be through the CLI.

@mganss mganss closed this as completed in 0bcf3c6 Jul 25, 2023
@mganss
Copy link
Owner

mganss commented Jul 25, 2023

2.0.863 contains support for this out of the box.

@bzwieratinnovadis
Copy link
Author

bzwieratinnovadis commented Jul 26, 2023

@mganss Wow, thanks for implementing the solution so fast! I really appreciate it 👍

I just generated the code with the new version (2.0.863) and found out it's not working, yet. The reason is that we use an intermediate simpleType instead of using the guid type directly. This is causing the resulting properties to still be of type string. The benefit of this approach is that I can specify the guid namespace in only one XSD, rather than in all of the XSD's where I want to use it.

UUID.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:mxs="http://microsoft.com/wsdl/types/"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           id="UUID"
           targetNamespace="http://tempuri.org/v1"
           elementFormDefault="qualified"
           version="1.0">

    <xs:import schemaLocation="Guid.xsd" namespace="http://microsoft.com/wsdl/types/"/>
    <xs:simpleType name="UUID">
        <xs:restriction base="mxs:guid"/>
    </xs:simpleType>
</xs:schema>

Header.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           id="BerichtHeaderSHV"
           targetNamespace="http://tempuri.org/v1"
           elementFormDefault="qualified"
           xmlns="http://tempuri.org/v1"
           version="1.0">

    <xs:include schemaLocation="../SimpleTypes/UUID.xsd"/>

    <xs:complexType name="Header">
        <xs:sequence>
            <xs:element name="Reference" type="UUID"/>
            <xs:element name="Receiver" type="UUID" minOccurs="0" nillable="true"/>
            <xs:element name="CrossReference" type="UUID" minOccurs="0" nillable="true"/>
            <xs:element name="ConversationReference" type="UUID" minOccurs="0" nillable="true"/>
            <xs:element name="Version" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>

    <xs:element name="Header" type="Header"/>
</xs:schema>

which results in:

[System.CodeDom.Compiler.GeneratedCodeAttribute("XmlSchemaClassGenerator", "2.0.863.0")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute("Header", Namespace="http://tempuri.org/v1")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlRootAttribute("Header", Namespace="http://tempuri.org/v1")]
public partial class Header
{
    
    [System.ComponentModel.DataAnnotations.RequiredAttribute()]
    [System.Xml.Serialization.XmlElementAttribute("Reference")]
    public string Reference { get; set; }
    
    [System.Xml.Serialization.XmlElementAttribute("Receiver", IsNullable=true)]
    public string Receiver { get; set; }
    
    [System.Xml.Serialization.XmlElementAttribute("CrossReference", IsNullable=true)]
    public string CrossReference { get; set; }
    
    [System.Xml.Serialization.XmlElementAttribute("ConversationReference", IsNullable=true)]
    public string ConversationReference { get; set; }
    
    [System.ComponentModel.DataAnnotations.RequiredAttribute()]
    [System.Xml.Serialization.XmlElementAttribute("Version")]
    public string Version { get; set; }
}

Would it be possible to fix this as well? I'd really appreciate that!

@mganss
Copy link
Owner

mganss commented Jul 26, 2023

It should work in 2.0.864.

@bzwieratinnovadis
Copy link
Author

Thank you so much! It's all working now 👍

@bzwieratinnovadis
Copy link
Author

bzwieratinnovadis commented Jul 28, 2023

@mganss

I found out why we need the http://schemas.microsoft.com/2003/10/Serialization/ namespace instead. We use CoreWCF and when the generated code is serialized for the WSDL it expects the guid type to be present in that namespace.

I created a PR already: #403

@mganss
Copy link
Owner

mganss commented Jul 28, 2023

@bzwieratinnovadis Did you ever get xsd.exe to generate Guid for that namespace?

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

No branches or pull requests

2 participants