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

SimpleType restrictions applied to their container collection #506

Closed
juhvo opened this issue Apr 25, 2024 · 5 comments
Closed

SimpleType restrictions applied to their container collection #506

juhvo opened this issue Apr 25, 2024 · 5 comments

Comments

@juhvo
Copy link

juhvo commented Apr 25, 2024

Hi,

So I have an xsd that contains following definitions. Problem is that whenever an element has maxOccurs > 1 and type refers to a simpleType, restrictions (at least minLength and maxLength) of the simpleType are added to generated collection itself.

Take a look at BuyerOrganisationDepartment for example; intention of the xsd is a collection of 0-2 strings each the length of 0-35 characters but DataAnnotations of generated code seem to be picked up from the simpleType resulting to a collection of 0-35 strings of any length.

Is there a workaround for this or do I have to fix the classes manually?

Code is generated with following command
xscgen --netCore --separateFiles --datetime-offset --output ./MyPath --namespace MyNamespace ./MyXsd.xsd

<xs:complexType name="BuyerPartyDetailsType">
  <xs:sequence>
    <xs:element name="BuyerPartyIdentifier" type="PartyLegalRegIdType" minOccurs="0"/>
    <xs:element name="BuyerOrganisationName" type="genericStringType2_70" maxOccurs="unbounded"/>
    <xs:element name="BuyerOrganisationTradingName" type="genericStringType2_70" minOccurs="0"/>
    <xs:element name="BuyerOrganisationDepartment" type="genericStringType0_35" minOccurs="0" maxOccurs="2"/>
    <xs:element name="BuyerOrganisationTaxCode" type="genericStringType0_35" minOccurs="0"/>
    <xs:element name="BuyerCode" type="PartyIdentifierType" minOccurs="0"/>
    <xs:element name="BuyerPostalAddressDetails" type="BuyerPostalAddressDetailsType" minOccurs="0"/>
  </xs:sequence>
</xs:complexType>
 <xs:simpleType name="genericStringType2_70">
  <xs:restriction base="xs:string">
    <xs:minLength value="2"/>
    <xs:maxLength value="70"/>
  </xs:restriction>
</xs:simpleType>
<xs:simpleType name="genericStringType0_35">
  <xs:restriction base="xs:string">
    <xs:minLength value="0"/>
    <xs:maxLength value="35"/>
  </xs:restriction>
</xs:simpleType>
       /// <summary>
       /// <para xml:lang="en">Minimum length: 2.</para>
       /// <para xml:lang="en">Maximum length: 70.</para>
       /// </summary>
       [System.ComponentModel.DataAnnotations.MinLengthAttribute(2)]
       [System.ComponentModel.DataAnnotations.MaxLengthAttribute(70)]
       [System.ComponentModel.DataAnnotations.RequiredAttribute()]
       [System.Xml.Serialization.XmlElementAttribute("BuyerOrganisationName")]
       public System.Collections.ObjectModel.Collection<string> BuyerOrganisationName
       {
           get
           {
               return _buyerOrganisationName;
           }
           private set
           {
               _buyerOrganisationName = value;
           }
       }
       
       /// <summary>
       /// <para xml:lang="en">Maximum length: 35.</para>
       /// </summary>
       [System.ComponentModel.DataAnnotations.MaxLengthAttribute(35)]
       [System.Xml.Serialization.XmlElementAttribute("BuyerOrganisationDepartment")]
       public System.Collections.ObjectModel.Collection<string> BuyerOrganisationDepartment
       {
           get
           {
               return _buyerOrganisationDepartment;
           }
           private set
           {
               _buyerOrganisationDepartment = value;
           }
       }
@mganss mganss closed this as completed in ac85827 May 14, 2024
@mganss
Copy link
Owner

mganss commented May 14, 2024

Fixed in 2.1.1141.

@juhvo
Copy link
Author

juhvo commented May 15, 2024

Thanks for the update! Now it works better in the use case I described.

However, I'd like to point out that previously it did generate restrictions as expected when elements of enumerable were of complexType but now it skips generating them on enumerables altogether.

@mganss
Copy link
Owner

mganss commented May 15, 2024

Can you show an example?

@mganss mganss reopened this May 15, 2024
@juhvo
Copy link
Author

juhvo commented May 15, 2024

Can you show an example?

I tried the previous version again and it turns out the last remark of mine was incorrect - it doesn't generate restrictions as I remembered. Sorry about the confusion and thanks for quick response!

@mganss mganss closed this as completed May 15, 2024
@Tarig0
Copy link

Tarig0 commented May 22, 2024

I have a similar issue but require/have a different fix instead of omitting the restrictions it would be preferred to have the max length be the max occurs on the collection in the XSD.

Additionally, it would be nice to tag the primitive string types with the correct annotations. Currently I have a flavor of the project that does this.

<?xml version="1.0" encoding="UTF-8"?>
<!--Generated by Standards Editor (build:R1.6.15) on 2019 Feb 14 13:31:41, ISO 20022 version : 2013-->
<xs:schema xmlns="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.08" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.08">
    <xs:element name="Document" type="PostalAddress24"/>
    <xs:simpleType name="Max70Text">
        <xs:restriction base="xs:string">
            <xs:minLength value="1"/>
            <xs:maxLength value="70"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:complexType name="PostalAddress24">
        <xs:sequence>
            <xs:element maxOccurs="7" minOccurs="0" name="AdrLine" type="Max70Text"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

actual

  [XmlElementAttribute("AdrLine")]
  public Collection<string> AdrLine
  {
    get
    {
     return _adrLine;
    }
    init
    {
      _adrLine = value;
    }
  }

expected

  [XmlElementAttribute("AdrLine")]
  [MaxLengthAttribute(7)]
  public Collection<string> AdrLine
  {
    get
    {
     return _adrLine;
    }
    init
    {
      _adrLine = value;
    }
  }

My implementation #510

  [XmlElementAttribute("AdrLine")]
  [MaxLengthAttribute(7)]
  public Collection<Max70Text> AdrLine
  {
    get
    {
     return _adrLine;
    }
    init
    {
      _adrLine = value;
    }
  }

//seperateClass for primitive string type
[GeneratedCodeAttribute("XmlSchemaClassGenerator", "2.0.864.0")]
[SerializableAttribute()]
[XmlTypeAttribute("Max70Text", Namespace="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.08")]
[XmlRootAttribute("Max70Text", Namespace="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.08")]
public partial class Max70Text
{
    
    /// <summary>
    /// <para xml:lang="en">Minimum length: 1.</para>
    /// <para xml:lang="en">Maximum length: 70.</para>
    /// </summary>
    [MinLengthAttribute(1)]
    [MaxLengthAttribute(70)]
    [XmlTextAttribute()]
    public string Value { get; set; }

   
    public static implicit operator Max70Text(string obj) => new() { Value = obj };
    public static implicit operator string(Max70Text obj) => obj.Value;
}

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

3 participants