Skip to content

Conversation

GiovanePS
Copy link
Contributor

PR Details

Description

The problem happens due to the combination of two lines of code:

  1. xmlElement.go:58:
    • opt.Element.Push(&e)
      This line inserts an element in Element stack, to indicate that we are inside <element></element>.
  2. xmlRestriction.go:49:
    • opt.ComplexType.Peek().(*ComplexType).Elements[len(opt.ComplexType.Peek().(*ComplexType).Elements)-1] = *opt.Element.Peek().(*Element)
      This replaces the last element of ComplexType.Elements.

The problem occurs because when some element neither has a type attribute nor contains a restriction (which is valid), the elements were not popped properly (when they were pushed only because they had no name). And when a restriction is found, the second code mentioned runs, replacing the last element.

It's good to know that it only happens with an attribute because, if the restriction is an element, like this:

<element name="ElementWithRestriction">
	<simpleType>
		<restriction base="string" />
	</simpleType>
</element>

the restriction handling code runs, but the *opt.Element.Peek().(*Element) refers to itself, in other words, replacing the last element (itself) with itself.

The solution I found to solve this problem is like the solution proposed by @senekor (#5), that simply pops the element at the end:

if opt.Element.Len() > 0 && opt.ComplexType.Len() > 0 {
     opt.Element.Pop()
}

His problem is not the same as mine, but the root cause is: the opt.Element stack.
With the above solution, the code stops generating the same field twice, because there are no remnant elements.

Related Issue

#86

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@senekor
Copy link

senekor commented Mar 5, 2025

@GiovanePS stop tagging me, I don't use this library anymore.

You should've commented on the issue I opened, that way I could unsubscribe from my own issue if it is not relevant to me anymore. If you tag people directly, they have no practical way to escape the notification.

@xuri xuri added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Apr 23, 2025
xuri pushed a commit to xuri/xsd that referenced this pull request Jun 10, 2025
@xuri xuri closed this Jun 11, 2025
@xuri xuri reopened this Jun 11, 2025
Copy link
Owner

@xuri xuri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for your contribution.

Copy link

codecov bot commented Jun 11, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 81.48%. Comparing base (d0691b7) to head (d93578a).
Report is 4 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #87      +/-   ##
==========================================
- Coverage   83.57%   81.48%   -2.09%     
==========================================
  Files          36       36              
  Lines        1534     1826     +292     
==========================================
+ Hits         1282     1488     +206     
- Misses        159      245      +86     
  Partials       93       93              
Flag Coverage Δ
unittests 81.48% <100.00%> (-2.09%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@xuri xuri merged commit 286bd14 into xuri:master Jun 11, 2025
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size/XS Denotes a PR that changes 0-9 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants