Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions exist-core/src/main/java/org/exist/xquery/ForExpr.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,12 @@ public Sequence eval(Sequence contextSequence, Item contextItem)
if (sequenceType != null) {
//Type.EMPTY is *not* a subtype of other types ; checking cardinality first
//only a check on empty sequence is accurate here
if (resultSequence.isEmpty() &&
!sequenceType.getCardinality().isSuperCardinalityOrEqualOf(Cardinality.EMPTY_SEQUENCE))
{throw new XPathException(this, ErrorCodes.XPTY0004,
"Invalid cardinality for variable $" + varName + ". Expected " +
sequenceType.getCardinality().getHumanDescription() +
", got " + Cardinality.EMPTY_SEQUENCE.getHumanDescription());}
// if (resultSequence.isEmpty() &&
// !sequenceType.getCardinality().isSuperCardinalityOrEqualOf(Cardinality.EMPTY_SEQUENCE))
// {throw new XPathException(this, ErrorCodes.XPTY0004,
// "Invalid cardinality for variable $" + varName + ". Expected " +
// sequenceType.getCardinality().getHumanDescription() +
// ", got " + Cardinality.EMPTY_SEQUENCE.getHumanDescription());}
//TODO : ignore nodes right now ; they are returned as xs:untypedAtomicType
if (!Type.subTypeOf(sequenceType.getPrimaryType(), Type.NODE)) {
if (!resultSequence.isEmpty() &&
Expand Down
12 changes: 12 additions & 0 deletions exist-core/src/test/java/org/exist/xquery/XQueryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2452,6 +2452,18 @@ public void wrongInvalidTypeError_1787285() throws XMLDBException {
}
}

@Test
public void typedForVariableWithEmptyReturn_5050() throws XMLDBException {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please don't name this _5050. Instead link to the GitHub issue in the Javadoc for the function.

final String query = "for $x as xs:string in 'X' return ()";

@adamretter adamretter Nov 18, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would like to see more tests added to the same test function, and the XQuery code within that function formatted a bit more nicely please:

  1. let $x := 'X'
    return
      $x
  2. let $x as xs:string := 'X'
    return
      $x
  3. let $result :=
        let $x as xs:string := 'X'
        return
          $x
    return
      empty($result)
  4. let $x := 'X'
    return
      ()
  5. let $x as xs:string := 'X'
    return
      ()
  6. let $result :=
        let $x as xs:string := 'X'
        return
          ()
    return
      empty($result)
  7. let $x := 'X'
    return
      1
  8. let $x as xs:string := 'X'
    return
      1
  9. let $result :=
        let $x as xs:string := 'X'
        return
          1
    return
      empty($result)
  10. let $x := 'X'
    return
      (1, 2, 3)
  11. let $x as xs:string := 'X'
    return
      (1, 2, 3)
  12. let $result :=
        let $x as xs:string := 'X'
        return
          (1, 2, 3)
    return
      empty($result)
  13. for $x in 'X'
    return
      $x
  14. for $x as xs:string in 'X'
    return
      $x
  15. let $result :=
        for $x as xs:string in 'X'
        return
          $x
    return
      empty($result)
  16. for $x in 'X'
    return
      ()
  17. for $x as xs:string in 'X'
    return
      ()
  18. let $result :=
        for $x as xs:string in 'X'
        return
          ()
    return
      empty($result)
  19. for $x in 'X'
    return
      1
  20. for $x as xs:string in 'X'
    return
      1
  21. let $result :=
        for $x as xs:string in 'X'
        return
          1
    return
      empty($result)
  22. for $x in 'X'
    return
      (1, 2, 3)
  23. for $x as xs:string in 'X'
    return
      (1, 2, 3)
  24. let $result :=
        for $x as xs:string in 'X'
        return
          (1, 2, 3)
    return
      empty($result)

You should then also rename the test function to make it more reflective of the GitHub issue, and what it is actually testing.

try (final Collection testCollection = getTestCollection()) {
final XPathQueryService service = testCollection.getService(XPathQueryService.class);
try (final EXistResourceSet result = (EXistResourceSet) service.query(query)) {
assertEquals(0, result.getSize());
}
}
}


/**
* Regression
*
Expand Down
Loading