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

XmlSchema reports error on a valid XML schema regarding <xs:restriction> #51778

Closed
lovettchris opened this issue Apr 24, 2021 · 1 comment · Fixed by #51779
Closed

XmlSchema reports error on a valid XML schema regarding <xs:restriction> #51778

lovettchris opened this issue Apr 24, 2021 · 1 comment · Fixed by #51779

Comments

@lovettchris
Copy link
Contributor

Description

Got following description from my customer…

"... The" System.Xml.XmlReader.Create () "method incorrectly throws an exception in certain situations with 'choice' / 'sequences' in xsds. I've attached an example with two xsd files, one xml file and some source code. Both xsd files conform to the W3C standard. Both xsd files can be used successfully with the attached xml file using the online validator.

The System.Xml.XmlReader.Create () method throws an exception with "Example.1.xsd" and that shouldn't actually happen. If I remove a further element from each of the two sequences in the xsd (as in "Example.0.xsd"), then the
System.Xml.XmlReader.Create () also works

So I've attached an example with only 66 lines of source code to understand the error. Two xsd files are used to generate an XmlReader each. I would expect it to work either way. It also works for "Example.0.xsd". However, it does not work for "Example.1.xsd". The only difference between the two xsd files is that "Example.1.xsd" has an additional can element in the defined sequences.

This is the exception that shouldn't come:

System.Xml.Schema.XmlSchemaValidationException: Invalid particle derivation by restriction - 'The derived sequence particle at (8, 6) is not a valid restriction of the base choice particle at (17, 4) according to Sequence: Choice - MapAndSum. ' ... "

Beispiel.zip

Configuration

Happens in all versions of .NET framework and is not specific to OS, platform, or configuration.

Regression?

This is not a regression.

Other information

The simplest fix is to remove the check below

 || derivdSequence.Items.Count > baseChoice.Items.Count)

from the following method as it is actually bogus. In the xsd:restriction case the number of immediate child particles in the sequence or in the choice cannot be compared like this since GetMappingParticle is actually recursive.

        private bool IsSequenceFromChoice(XmlSchemaSequence derivedSequence, XmlSchemaChoice baseChoice)
        {
            decimal minOccurs, maxOccurs;
            minOccurs = derivedSequence.MinOccurs * derivedSequence.Items.Count;
            if (derivedSequence.MaxOccurs == decimal.MaxValue)
            {
                maxOccurs = decimal.MaxValue;
            }
            else
            {
                maxOccurs = derivedSequence.MaxOccurs * derivedSequence.Items.Count;
            }
            if (!IsValidOccurrenceRangeRestriction(minOccurs, maxOccurs, baseChoice.MinOccurs, baseChoice.MaxOccurs)
                 || derivdSequence.Items.Count > baseChoice.Items.Count)
            {
                return false;
            }
            for (int i = 0; i < derivedSequence.Items.Count; ++i)
            {
                if (GetMappingParticle((XmlSchemaParticle)derivedSequence.Items[i], baseChoice.Items) < 0)
                    return false;
            }
            return true;
        }
@ghost
Copy link

ghost commented Apr 24, 2021

Tagging subscribers to this area: @buyaa-n, @krwq
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

Got following description from my customer…

"... The" System.Xml.XmlReader.Create () "method incorrectly throws an exception in certain situations with 'choice' / 'sequences' in xsds. I've attached an example with two xsd files, one xml file and some source code. Both xsd files conform to the W3C standard. Both xsd files can be used successfully with the attached xml file using the online validator.

The System.Xml.XmlReader.Create () method throws an exception with "Example.1.xsd" and that shouldn't actually happen. If I remove a further element from each of the two sequences in the xsd (as in "Example.0.xsd"), then the
System.Xml.XmlReader.Create () also works

So I've attached an example with only 66 lines of source code to understand the error. Two xsd files are used to generate an XmlReader each. I would expect it to work either way. It also works for "Example.0.xsd". However, it does not work for "Example.1.xsd". The only difference between the two xsd files is that "Example.1.xsd" has an additional can element in the defined sequences.

This is the exception that shouldn't come:

System.Xml.Schema.XmlSchemaValidationException: Invalid particle derivation by restriction - 'The derived sequence particle at (8, 6) is not a valid restriction of the base choice particle at (17, 4) according to Sequence: Choice - MapAndSum. ' ... "

Beispiel.zip

Configuration

Happens in all versions of .NET framework and is not specific to OS, platform, or configuration.

Regression?

This is not a regression.

Other information

The simplest fix is to remove the check below

 || derivdSequence.Items.Count > baseChoice.Items.Count)

from the following method as it is actually bogus. In the xsd:restriction case the number of immediate child particles in the sequence or in the choice cannot be compared like this since GetMappingParticle is actually recursive.

        private bool IsSequenceFromChoice(XmlSchemaSequence derivedSequence, XmlSchemaChoice baseChoice)
        {
            decimal minOccurs, maxOccurs;
            minOccurs = derivedSequence.MinOccurs * derivedSequence.Items.Count;
            if (derivedSequence.MaxOccurs == decimal.MaxValue)
            {
                maxOccurs = decimal.MaxValue;
            }
            else
            {
                maxOccurs = derivedSequence.MaxOccurs * derivedSequence.Items.Count;
            }
            if (!IsValidOccurrenceRangeRestriction(minOccurs, maxOccurs, baseChoice.MinOccurs, baseChoice.MaxOccurs)
                 || derivdSequence.Items.Count > baseChoice.Items.Count)
            {
                return false;
            }
            for (int i = 0; i < derivedSequence.Items.Count; ++i)
            {
                if (GetMappingParticle((XmlSchemaParticle)derivedSequence.Items[i], baseChoice.Items) < 0)
                    return false;
            }
            return true;
        }
Author: lovettchris
Assignees: -
Labels:

area-System.Xml

Milestone: -

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Apr 24, 2021
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Apr 28, 2021
krwq pushed a commit that referenced this issue Apr 28, 2021
)

* Fix issue #51778 and add more test coverage for xs:restrictions.

* Fix issue #51778

* CR feedback
@ghost ghost locked as resolved and limited conversation to collaborators May 28, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
No open projects
Development

Successfully merging a pull request may close this issue.

2 participants