Skip to content

[7.x.x] Typed for variable no longer failson an empty return - #248

Open
Laurettta wants to merge 1 commit into
develop-7.x.xfrom
7.x.x/hotfix/for-loop-type-check
Open

Laurettta wants to merge 1 commit into
develop-7.x.xfrom
7.x.x/hotfix/for-loop-type-check

Conversation

@Laurettta

@Laurettta Laurettta commented Nov 5, 2026 •

Copy link
Copy Markdown

A for variable with a type, like for $x as xs:string in 'X' return (), no longer gives an "Invalid cardinality" error when it returns nothing.

Closes eXist-db/exist#5050

@cla-bot cla-bot Bot added the cla-signed label Nov 5, 2026
@codacy-production

Copy link
Copy Markdown

Not up to standards ⛔

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@Laurettta Laurettta changed the title [7.x.x] typed for variable no longer failson an empty return [7.x.x] Typed for variable no longer failson an empty return Nov 17, 2026
@Laurettta
Laurettta force-pushed the 7.x.x/hotfix/for-loop-type-check branch from 099215a to f4c1807 Compare November 17, 2026 23:02
@Laurettta
Laurettta requested a review from adamretter November 18, 2026 09:27
}

@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.


@Test
public void typedForVariableWithEmptyReturn_5050() throws XMLDBException {
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. Just two JUnit Test functions please, one for for and one for let.
I would also like to see the XQuery code within each 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)

Please also rename the test functions you create to make it more reflective of the GitHub issue, and what it is actually testing.

@adamretter adamretter Nov 19, 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.

Some more for tests to add please:

(: for - returning $x :)

for $x as empty-sequence() allowing empty in ()
return
  $x
,

for $x as xs:integer in ()
return
  $x
,

for $x as xs:integer? in ()
return
  $x
,

for $x as xs:integer+ in ()
return
  $x
,

for $x as xs:integer* in ()
return
  $x
,

for $x as empty-sequence() allowing empty in (1, 2, 3)
return
  $x
,

for $x as xs:integer in (1,2,3)
return
  $x
,

for $x as xs:integer? in (1,2,3)
return
  $x
,

for $x as xs:integer+ in (1,2,3)
return
  $x
,

for $x as xs:integer* in (1,2,3)
return
  $x
,

(: for - returning empty-sequence() :)
for $x as empty-sequence() allowing empty in ()
return
  ()
,

for $x as xs:integer in ()
return
  ()
,

for $x as xs:integer? in ()
return
  ()
,

for $x as xs:integer+ in ()
return
  ()
,

for $x as xs:integer* in ()
return
  ()
,

for $x as empty-sequence() allowing empty in (1,2,3)
return
  ()
,

for $x as xs:integer in (1,2,3)
return
  ()
,

for $x as xs:integer? in (1,2,3)
return
  ()
,

for $x as xs:integer+ in (1,2,3)
return
  ()
,

for $x as xs:integer* in (1,2,3)
return
  ()

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.

Some more let tests to add please:

(: let - returning $x :)
let $x as empty-sequence() := ()
return
  $x
,

let $x as xs:integer := ()
return
  $x
,

let $x as xs:integer? := ()
return
  $x
,

let $x as xs:integer+ := ()
return
  $x
,

let $x as xs:integer* := ()
return
  $x
,

let $x as xs:integer := (1,2,3)
return
  $x
,

let $x as xs:integer? := (1,2,3)
return
  $x
,

let $x as xs:integer+ := (1,2,3)
return
  $x
,

let $x as xs:integer* := (1,2,3)
return
  $x
,

(: let - returning empty-sequence() :)

let $x as empty-sequence() := ()
return
  ()
,

let $x as xs:integer := ()
return
  ()
,

let $x as xs:integer? := ()
return
  ()
,

let $x as xs:integer+ := ()
return
  ()
,

let $x as xs:integer* := ()
return
  ()
,


let $x as xs:integer := (1,2,3)
return
  ()
,

let $x as xs:integer? := (1,2,3)
return
  ()
,

let $x as xs:integer+ := (1,2,3)
return
  ()
,

let $x as xs:integer* := (1,2,3)
return
  ()

@Laurettta Laurettta Nov 19, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@adamretter while adding your queries as tests I found that for $x as xs:string in 'X' return 1 (and the same with return (1, 2, 3)) fails with XPTY0004 Invalid type for variable $x. Expected xs:string, got xs:integer. I think they should work, since $x is a string. Should I fix this in this PR or open a separate issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants