Skip to content

Commit 848f3b7

Browse files
committed
[bugfix] Ensure backwards compatibility with eXist-db URL Rewrite Controllers (controller.xq) that use exist: variables but do not declare them as external variables
Closes eXist-db/exist#2060
1 parent b794672 commit 848f3b7

File tree

8 files changed

+107
-20
lines changed

8 files changed

+107
-20
lines changed

exist-core/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,6 +1067,7 @@
10671067
<include>src/test/java/org/exist/xquery/ForwardReferenceTest.java</include>
10681068
<include>src/main/java/org/exist/xquery/Function.java</include>
10691069
<include>src/main/java/org/exist/xquery/FunctionFactory.java</include>
1070+
<include>src/test/java/org/exist/xquery/InternalModuleTest.java</include>
10701071
<include>src/main/java/org/exist/xquery/LocationStep.java</include>
10711072
<include>src/main/java/org/exist/xquery/Module.java</include>
10721073
<include>src/main/java/org/exist/xquery/NamedFunctionReference.java</include>
@@ -1082,6 +1083,7 @@
10821083
<include>src/main/java/org/exist/xquery/Variable.java</include>
10831084
<include>src/main/java/org/exist/xquery/VariableDeclaration.java</include>
10841085
<include>src/main/java/org/exist/xquery/VariableImpl.java</include>
1086+
<include>src/main/java/org/exist/xquery/VariableReference.java</include>
10851087
<include>src/test/java/org/exist/xquery/XmldbBinariesTest.java</include>
10861088
<include>src/test/java/org/exist/xquery/XPathOpOrSpecialCaseTest.java</include>
10871089
<include>src/test/java/org/exist/xquery/XPathQueryTest.java</include>
@@ -1720,6 +1722,7 @@
17201722
<exclude>src/test/resources-filtered/org/exist/xquery/import-from-pkg-test.conf.xml</exclude>
17211723
<exclude>src/test/java/org/exist/xquery/ImportFromPkgTest.java</exclude>
17221724
<exclude>src/test/java/org/exist/xquery/ImportModuleTest.java</exclude>
1725+
<exclude>src/test/java/org/exist/xquery/InternalModuleTest.java</exclude>
17231726
<exclude>src/main/java/org/exist/xquery/JavaBinding.java</exclude>
17241727
<exclude>src/test/resources-filtered/org/exist/xquery/JavaBindingTest.conf.xml</exclude>
17251728
<exclude>src/test/java/org/exist/xquery/JavaBindingTest.java</exclude>
@@ -1739,6 +1742,7 @@
17391742
<exclude>src/main/java/org/exist/xquery/Variable.java</exclude>
17401743
<exclude>src/main/java/org/exist/xquery/VariableDeclaration.java</exclude>
17411744
<exclude>src/main/java/org/exist/xquery/VariableImpl.java</exclude>
1745+
<exclude>src/main/java/org/exist/xquery/VariableReference.java</exclude>
17421746
<exclude>src/test/java/org/exist/xquery/WatchdogTest.java</exclude>
17431747
<exclude>src/test/java/org/exist/xquery/XmldbBinariesTest.java</exclude>
17441748
<exclude>src/test/java/org/exist/xquery/XPathOpOrSpecialCaseTest.java</exclude>

exist-core/src/main/java/org/exist/xquery/VariableReference.java

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
/*
2+
* Elemental
3+
* Copyright (C) 2024, Evolved Binary Ltd
4+
*
5+
6+
* https://www.evolvedbinary.com | https://www.elemental.xyz
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; version 2.1.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* NOTE: Parts of this file contain code from 'The eXist-db Authors'.
22+
* The original license header is included below.
23+
*
24+
* =====================================================================
25+
*
226
* eXist-db Open Source Native XML Database
327
* Copyright (C) 2001 The eXist-db Authors
428
*
@@ -21,6 +45,7 @@
2145
*/
2246
package org.exist.xquery;
2347

48+
import org.exist.Namespaces;
2449
import org.exist.dom.QName;
2550
import org.exist.dom.persistent.DocumentSet;
2651
import org.exist.xquery.util.ExpressionDumper;
@@ -59,16 +84,22 @@ public void analyze(final AnalyzeContextInfo contextInfo) throws XPathException
5984
// ignore: variable might not be known yet
6085
return;
6186
}
62-
if (var == null) {
63-
throw new XPathException(this, ErrorCodes.XPDY0002,
64-
"variable '$" + qname + "' is not set.");
87+
88+
// TODO(AR) this `if` guard statement is a workaround for controller.xq files that use controller variables (e.g. exist:path) but do not declare them as external
89+
if (var == null && !Namespaces.EXIST_NS.equals(qname.getNamespaceURI())) {
90+
throw new XPathException(this, ErrorCodes.XPST0008,
91+
"Variable '$" + qname + "' is not declared.");
6592
}
66-
if (!var.isInitialized()) {
93+
94+
if (var != null && !var.isInitialized()) {
6795
throw new XPathException(this, ErrorCodes.XQST0054,
68-
"variable declaration of '$" + qname + "' cannot " +
69-
"be executed because of a circularity.");
96+
"variable declaration of '$" + qname + "' cannot " +
97+
"be executed because of a circularity.");
98+
}
99+
100+
if (var != null) {
101+
contextInfo.setStaticReturnType(var.getStaticType());
70102
}
71-
contextInfo.setStaticReturnType(var.getStaticType());
72103
}
73104

74105
@Override

exist-core/src/test/java/org/exist/xquery/InternalModuleTest.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
/*
2+
* Elemental
3+
* Copyright (C) 2024, Evolved Binary Ltd
4+
*
5+
6+
* https://www.evolvedbinary.com | https://www.elemental.xyz
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; version 2.1.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* NOTE: Parts of this file contain code from 'The eXist-db Authors'.
22+
* The original license header is included below.
23+
*
24+
* =====================================================================
25+
*
226
* eXist-db Open Source Native XML Database
327
* Copyright (C) 2001 The eXist-db Authors
428
*
@@ -19,7 +43,6 @@
1943
* License along with this library; if not, write to the Free Software
2044
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2145
*/
22-
2346
package org.exist.xquery;
2447

2548
import org.exist.dom.QName;
@@ -104,9 +127,7 @@ public void requestResponseSessionVariables_4_x_x_Api() throws XMLDBException {
104127
} catch (final XMLDBException e) {
105128
assertTrue(e.getCause() instanceof XPathException);
106129
final XPathException xpe = (XPathException)e.getCause();
107-
108-
//TODO(AR) should be XPST0008, eXist-db has a bug with the error code, see: https://github.com/eXist-db/exist/issues/2060
109-
assertEquals(ErrorCodes.XPDY0002, xpe.getErrorCode());
130+
assertEquals(ErrorCodes.XPST0008, xpe.getErrorCode());
110131
}
111132
}
112133

exist-core/src/test/java/org/exist/xquery/XQueryTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,25 +1065,25 @@ public void module() throws XMLDBException {
10651065
assertEquals("XQuery: " + query, "bar1", result.getResource(1).getContent());
10661066
assertEquals("XQuery: " + query, "bar2", result.getResource(2).getContent());
10671067

1068-
// Non-heritance check
1068+
// Non-transitive inheritance check
10691069
query = "xquery version \"1.0\";\n" + "import module namespace foo=\"foo\" at \"" + URI + "/test/" + FATHER_MODULE_NAME + "\";\n" + "declare namespace foo1=\"foo1\"; \n" + "$foo1:bar";
10701070
try {
10711071
message = "";
10721072
result = service.query(query);
10731073
} catch (XMLDBException e) {
10741074
message = e.getMessage();
10751075
}
1076-
assertTrue(message.indexOf("XPDY0002") > -1);
1076+
assertTrue(message.indexOf("XPST0008") > -1);
10771077

1078-
// Non-heritance check
1078+
// Non-transitive inheritance check
10791079
query = "xquery version \"1.0\";\n" + "import module namespace foo=\"foo\" at \"" + URI + "/test/" + FATHER_MODULE_NAME + "\";\n" + "declare namespace foo2=\"foo2\"; \n" + "$foo2:bar";
10801080
try {
10811081
message = "";
10821082
result = service.query(query);
10831083
} catch (XMLDBException e) {
10841084
message = e.getMessage();
10851085
}
1086-
assertTrue(message.indexOf("XPDY0002") > -1);
1086+
assertTrue(message.indexOf("XPST0008") > -1);
10871087
query = "xquery version \"1.0\";\n" + "import module namespace foo1=\"foo\" at \"" + URI + "/test/" + CHILD1_MODULE_NAME + "\";\n" + "import module namespace foo2=\"foo\" at \"" + URI + "/test/" + CHILD1_MODULE_NAME + "\";\n" + "$foo1:bar";
10881088
try {
10891089
message = "";

exist-jetty-config/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
<includes>
105105
<include>pom.xml</include>
106106
<include>src/main/resources/org/exist/jetty/etc/webapps/portal/index.html</include>
107-
<include>src/main/resources/webapp/controller.xql</include>
107+
<include>src/main/resources/webapp/controller.xq</include>
108108
<include>src/main/resources/webapp/404.html</include>
109109
</includes>
110110
</licenseSet>

exist-jetty-config/src/main/resources/webapp/controller.xql renamed to exist-jetty-config/src/main/resources/webapp/controller.xq

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,14 @@ xquery version "3.0";
5252
sub-controllers.
5353
------------------------------------------------------- :)
5454

55-
declare namespace c="http://exist-db.org/xquery/controller";
56-
declare namespace expath="http://expath.org/ns/pkg";
55+
declare namespace exist = "http://exist.sourceforge.net/NS/exist";
56+
declare namespace expath = "http://expath.org/ns/pkg";
5757

58-
import module namespace request="http://exist-db.org/xquery/request";
59-
import module namespace xdb = "http://exist-db.org/xquery/xmldb";
58+
import module namespace repo = "http://exist-db.org/xquery/repo";
59+
import module namespace request = "http://exist-db.org/xquery/request";
60+
import module namespace util = "http://exist-db.org/xquery/util";
61+
62+
declare variable $exist:path external;
6063

6164
declare function local:get-dashboard() {
6265
let $path := collection(repo:get-root())//expath:package[@name = "http://exist-db.org/apps/dashboard"]

extensions/modules/expathrepo/expathrepo-trigger-test/pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@
179179
<include>pom.xml</include>
180180
<include>src/main/java/org/exist/repo/ExampleModule.java</include>
181181
<include>src/test/resources/conf.xml</include>
182+
<include>src/main/xar-resources/controller.xq</include>
182183
<include>xar-assembly.xml</include>
183184
</includes>
184185
</licenseSet>
@@ -192,6 +193,7 @@
192193
<exclude>pom.xml</exclude>
193194
<exclude>src/main/java/org/exist/repo/ExampleModule.java</exclude>
194195
<exclude>src/test/resources/conf.xml</exclude>
196+
<exclude>src/main/xar-resources/controller.xq</exclude>
195197
<exclude>xar-assembly.xml</exclude>
196198
</excludes>
197199
</licenseSet>

extensions/modules/expathrepo/expathrepo-trigger-test/src/main/xar-resources/controller.xql renamed to extensions/modules/expathrepo/expathrepo-trigger-test/src/main/xar-resources/controller.xq

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
(:
2+
: Elemental
3+
: Copyright (C) 2024, Evolved Binary Ltd
4+
:
5+
6+
: https://www.evolvedbinary.com | https://www.elemental.xyz
7+
:
8+
: This library is free software; you can redistribute it and/or
9+
: modify it under the terms of the GNU Lesser General Public
10+
: License as published by the Free Software Foundation; version 2.1.
11+
:
12+
: This library is distributed in the hope that it will be useful,
13+
: but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
: Lesser General Public License for more details.
16+
:
17+
: You should have received a copy of the GNU Lesser General Public
18+
: License along with this library; if not, write to the Free Software
19+
: Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
:
21+
: NOTE: Parts of this file contain code from 'The eXist-db Authors'.
22+
: The original license header is included below.
23+
:
24+
: =====================================================================
25+
:
226
: eXist-db Open Source Native XML Database
327
: Copyright (C) 2001 The eXist-db Authors
428
:
@@ -23,6 +47,8 @@ xquery version "3.1";
2347

2448
declare namespace exist = "http://exist.sourceforge.net/NS/exist";
2549

50+
import module namespace request = "http://exist-db.org/xquery/request";
51+
2652
declare variable $exist:path external;
2753
declare variable $exist:resource external;
2854
declare variable $exist:controller external;

0 commit comments

Comments
 (0)