You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@grails.apache.org by jd...@apache.org on 2026/10/16 17:02:04 UTC
(grails-core) branch 7.0.x updated (8d707a2ff2 -> bd674957eb)
This is an automated email from the ASF dual-hosted git repository.
jdaugherty pushed a change to branch 7.0.x
in repository https://gitbox.apache.org/repos/asf/grails-core.git
from 8d707a2ff2 Merge pull request #16638 from apache/test/core-config-coverage-7.0.x
new 9ce99cde3f Return the page's property keys from the layout Content adapter
new c471280c53 Combine patterns and extract template variables as AntPathMatcher documents
new 81e4dc8373 Test File values for null instead of truth in the file system interaction
new 18d8daf59e Return the relative path of a spring script from getPathFromRoot
new e77e3cd9cc Report a misplaced endObject as such and let JSONTokener.next(n) read to the end
new bd674957eb Resolve every CompositeConfig view from the config that has the key
The 6 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../org/grails/io/support/AntPathMatcher.java | 36 ++++++-----
.../grails/io/support/AntPathMatcherSpec.groovy | 29 ++++++++-
.../commands/io/FileSystemInteractionImpl.groovy | 9 +--
.../groovy/org/grails/config/CompositeConfig.java | 59 ++++++++++-------
.../io/FileSystemInteractionImplSpec.groovy | 75 ++++++++++++++++++++++
.../org/grails/config/CompositeConfigSpec.groovy | 40 +++++++++---
.../org/grails/io/support/GrailsResourceUtils.java | 4 +-
.../io/support/GrailsResourceUtilsSpec.groovy | 8 +--
.../web/layout/TokenizedHTMLPage2Content.java | 2 +-
.../web/layout/GrailsHTMLPageParserSpec.groovy | 1 +
.../commands/io/FileSystemInteractionImpl.groovy | 9 +--
.../io/FileSystemInteractionImplSpec.groovy | 22 +++++++
.../groovy/org/grails/web/json/JSONTokener.java | 2 +-
.../groovy/org/grails/web/json/JSONWriter.java | 3 +-
.../org/grails/web/json/PrettyPrintJSONWriter.java | 6 +-
.../org/grails/web/json/JSONTokenerApiSpec.groovy | 10 +++
.../org/grails/web/json/JSONWriterApiSpec.groovy | 5 +-
.../web/json/PrettyPrintJSONWriterApiSpec.groovy | 19 +++++-
18 files changed, 263 insertions(+), 76 deletions(-)
create mode 100644 grails-core/src/test/groovy/grails/dev/commands/io/FileSystemInteractionImplSpec.groovy
(grails-core) 06/06: Resolve every CompositeConfig view from the config that has the key
Posted by jd...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
jdaugherty pushed a commit to branch 7.0.x
in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit bd674957eb81e7145f77646a39ff44b7e0b75318
Author: James Daugherty <jd...@jdresources.net>
AuthorDate: Fri Oct 16 12:58:15 2026 -0400
Resolve every CompositeConfig view from the config that has the key
A CompositeConfig answers getProperty from the highest precedence config
that has the key, but its Map views did not agree. size(), values() and
entrySet() summed the delegates, so a key shared by two configs was counted
and listed twice, and flatten() and toProperties() let the lowest precedence
config overwrite the others, the opposite of the lookup. get() and getAt()
stopped at the first config that answered at all, and a config answers a
key it does not have with a navigator, so a key only a lower config has
came back as an empty navigator instead of its value.
Every view now derives from one rule: a key is resolved by the highest
precedence config that contains it. keySet() is the ordered union of the
delegates' keys, size() is its size, values() and entrySet() resolve each
key once, containsValue() looks at those resolved values, and flatten() and
toProperties() merge the delegates from lowest to highest precedence so the
highest wins. When no config has the key, get() and getAt() fall back to the
first non-null answer as before, which keeps nested navigation working.
---
.../groovy/org/grails/config/CompositeConfig.java | 59 +++++++++++++---------
.../org/grails/config/CompositeConfigSpec.groovy | 40 +++++++++++----
2 files changed, 66 insertions(+), 33 deletions(-)
diff --git a/grails-core/src/main/groovy/org/grails/config/CompositeConfig.java b/grails-core/src/main/groovy/org/grails/config/CompositeConfig.java
index 91fa679258..03eff1b11c 100644
--- a/grails-core/src/main/groovy/org/grails/config/CompositeConfig.java
+++ b/grails-core/src/main/groovy/org/grails/config/CompositeConfig.java
@@ -18,13 +18,14 @@
*/
package org.grails.config;
+import java.util.AbstractMap;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Deque;
-import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
@@ -37,7 +38,9 @@ import grails.util.GrailsStringUtils;
import org.grails.core.exceptions.GrailsConfigurationException;
/**
- * A {@link Config} composed of other Configs
+ * A {@link Config} composed of other Configs. A lookup answers from the first config, in order of precedence,
+ * that has the key, and the {@link Map} views, {@link #flatten()} and {@link #toProperties()} resolve a key
+ * shared by several configs the same way, so each key appears once with its highest-precedence value.
*
* @author Graeme Rocher
* @since 3.0
@@ -69,8 +72,9 @@ public class CompositeConfig implements Config {
@Deprecated
public Map<String, Object> flatten() {
Map<String, Object> flattened = new LinkedHashMap<>();
- for (Config c : configs) {
- flattened.putAll(c.flatten());
+ Iterator<Config> lowestPrecedenceFirst = configs.descendingIterator();
+ while (lowestPrecedenceFirst.hasNext()) {
+ flattened.putAll(lowestPrecedenceFirst.next().flatten());
}
return flattened;
}
@@ -78,8 +82,9 @@ public class CompositeConfig implements Config {
@Override
public Properties toProperties() {
Properties properties = new Properties();
- for (Config c : configs) {
- properties.putAll(c.toProperties());
+ Iterator<Config> lowestPrecedenceFirst = configs.descendingIterator();
+ while (lowestPrecedenceFirst.hasNext()) {
+ properties.putAll(lowestPrecedenceFirst.next().toProperties());
}
return properties;
}
@@ -100,6 +105,11 @@ public class CompositeConfig implements Config {
@Override
public Object getAt(Object key) {
+ for (Config c : configs) {
+ if (c.containsKey(key)) {
+ return c.getAt(key);
+ }
+ }
for (Config c : configs) {
Object v = c.getAt(key);
if (v != null) return v;
@@ -123,11 +133,7 @@ public class CompositeConfig implements Config {
@Override
public int size() {
- int size = 0;
- for (Config config : configs) {
- size += config.size();
- }
- return size;
+ return keySet().size();
}
@Override
@@ -150,14 +156,21 @@ public class CompositeConfig implements Config {
@Override
public boolean containsValue(Object value) {
- for (Config config : configs) {
- if (config.containsValue(value)) return true;
- }
- return false;
+ return values().contains(value);
}
+ /**
+ * The value of the highest precedence config that has the key. A config answers a key it does not have with a
+ * navigator for the keys below it, so a config is only asked when it has the key, and the first answer of any
+ * config is returned only when none has it.
+ */
@Override
public Object get(Object key) {
+ for (Config config : configs) {
+ if (config.containsKey(key)) {
+ return config.get(key);
+ }
+ }
for (Config config : configs) {
Object v = config.get(key);
if (v != null) return v;
@@ -192,27 +205,27 @@ public class CompositeConfig implements Config {
@Override
public Set<String> keySet() {
- Set<String> entries = new HashSet<>();
+ Set<String> keys = new LinkedHashSet<>();
for (Config config : configs) {
- entries.addAll(config.keySet());
+ keys.addAll(config.keySet());
}
- return entries;
+ return keys;
}
@Override
public Collection<Object> values() {
Collection<Object> values = new ArrayList<>();
- for (Config config : configs) {
- values.addAll(config.values());
+ for (String key : keySet()) {
+ values.add(get(key));
}
return values;
}
@Override
public Set<Map.Entry<String, Object>> entrySet() {
- Set<Map.Entry<String, Object>> entries = new HashSet<>();
- for (Config config : configs) {
- entries.addAll(config.entrySet());
+ Set<Map.Entry<String, Object>> entries = new LinkedHashSet<>();
+ for (String key : keySet()) {
+ entries.add(new AbstractMap.SimpleImmutableEntry<>(key, get(key)));
}
return entries;
}
diff --git a/grails-core/src/test/groovy/org/grails/config/CompositeConfigSpec.groovy b/grails-core/src/test/groovy/org/grails/config/CompositeConfigSpec.groovy
index 0be4e139f6..79dd612aec 100644
--- a/grails-core/src/test/groovy/org/grails/config/CompositeConfigSpec.groovy
+++ b/grails-core/src/test/groovy/org/grails/config/CompositeConfigSpec.groovy
@@ -56,19 +56,18 @@ class CompositeConfigSpec extends Specification {
config.navigate('service', 'missing') == null
}
- void 'flatten and properties combine the keys of every config'() {
+ void 'flatten and properties resolve a shared key to the highest precedence config'() {
given:
def config = new CompositeConfig()
- config.addFirst(new PropertySourcesConfig([shared: 'first', firstOnly: 'one']))
+ config.addLast(new PropertySourcesConfig([shared: 'middle', middleOnly: 'mid']))
config.addLast(new PropertySourcesConfig([shared: 'last', lastOnly: 'two']))
+ config.addFirst(new PropertySourcesConfig([shared: 'first', firstOnly: 'one']))
expect:
- config.flatten().keySet() == ['shared', 'firstOnly', 'lastOnly'] as Set
- config.flatten().subMap(['firstOnly', 'lastOnly']) == [firstOnly: 'one', lastOnly: 'two']
- config.toProperties().keySet() == ['shared', 'firstOnly', 'lastOnly'] as Set
- config.toProperties().getProperty('firstOnly') == 'one'
- config.toProperties().getProperty('lastOnly') == 'two'
- config.getProperty('shared') == 'first'
+ config.flatten() == [shared: 'first', firstOnly: 'one', middleOnly: 'mid', lastOnly: 'two']
+ config.toProperties() == [shared: 'first', firstOnly: 'one', middleOnly: 'mid', lastOnly: 'two'] as Properties
+ config.flatten().shared == config.getProperty('shared')
+ config.toProperties().getProperty('shared') == config.getProperty('shared')
}
void 'flatten and properties resolve disjoint configs completely'() {
@@ -83,7 +82,7 @@ class CompositeConfigSpec extends Specification {
config.size() == 2
}
- void 'map views include the keys of every config'() {
+ void 'map views carry every key once with its highest precedence value'() {
given:
def first = new PropertySourcesConfig([shared: 'first', firstOnly: 'one'])
def last = new PropertySourcesConfig([shared: 'last', lastOnly: 'two'])
@@ -93,6 +92,10 @@ class CompositeConfigSpec extends Specification {
expect:
!config.isEmpty()
+ config.size() == 3
+ config.get('lastOnly') == 'two'
+ config.getAt('lastOnly') == 'two'
+ config.get('shared') == 'first'
config.keySet() == ['shared', 'firstOnly', 'lastOnly'] as Set
config.containsKey('firstOnly')
config.containsKey('lastOnly')
@@ -102,13 +105,30 @@ class CompositeConfigSpec extends Specification {
config.containsValue('first')
config.containsValue('two')
!config.containsValue('missing')
- config.values().containsAll(['first', 'one', 'two'])
+ !config.containsValue('last')
+ config.values().size() == 3
+ config.values().toSet() == ['first', 'one', 'two'] as Set
+ config.entrySet().size() == 3
config.entrySet()*.key.toSet() == config.keySet()
+ config.entrySet().find { it.key == 'shared' }.value == 'first'
config.entrySet().find { it.key == 'firstOnly' }.value == 'one'
config.entrySet().find { it.key == 'lastOnly' }.value == 'two'
config.iterator().toList().toSet() == config.entrySet()
}
+ void 'map views keep the precedence order of the configs that contribute keys'() {
+ given:
+ def config = new CompositeConfig()
+ config.addLast(new PropertySourcesConfig([lastOnly: 'two', shared: 'last']))
+ config.addFirst(new PropertySourcesConfig([shared: 'first', firstOnly: 'one']))
+
+ expect: 'the keys of the highest precedence config come first, then the keys only lower configs have'
+ config.keySet().toList().take(2).toSet() == ['shared', 'firstOnly'] as Set
+ config.keySet().toList().last() == 'lastOnly'
+ config.entrySet()*.key == config.keySet().toList()
+ config.values().toList() == config.keySet().collect { config.get(it) }
+ }
+
void 'empty composites and empty delegates have empty views'() {
given:
def config = new CompositeConfig()
(grails-core) 05/06: Report a misplaced endObject as such and let JSONTokener.next(n) read to the end
Posted by jd...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
jdaugherty pushed a commit to branch 7.0.x
in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit e77e3cd9cca965b868a8c95592440b06b52c73d6
Author: James Daugherty <jd...@jdresources.net>
AuthorDate: Fri Oct 16 12:56:25 2026 -0400
Report a misplaced endObject as such and let JSONTokener.next(n) read to the end
JSONWriter.end() chose its message by comparing the expected mode with OBJECT,
but endObject() passes KEY, so "Misplaced endObject." was unreachable and every
misplaced end was reported as endArray. The message now follows the closing
character being written. PrettyPrintJSONWriter no longer writes a newline and
indentation ahead of that check, so a misplaced end leaves the output untouched.
JSONTokener.next(n) rejected a request for exactly the remaining characters
because its bound used >= instead of >, contrary to its javadoc.
---
.../main/groovy/org/grails/web/json/JSONTokener.java | 2 +-
.../main/groovy/org/grails/web/json/JSONWriter.java | 3 +--
.../org/grails/web/json/PrettyPrintJSONWriter.java | 6 ++++--
.../org/grails/web/json/JSONTokenerApiSpec.groovy | 10 ++++++++++
.../org/grails/web/json/JSONWriterApiSpec.groovy | 5 ++---
.../web/json/PrettyPrintJSONWriterApiSpec.groovy | 19 +++++++++++++++++--
6 files changed, 35 insertions(+), 10 deletions(-)
diff --git a/grails-web-common/src/main/groovy/org/grails/web/json/JSONTokener.java b/grails-web-common/src/main/groovy/org/grails/web/json/JSONTokener.java
index 2b0018e8b9..2e72759cb5 100644
--- a/grails-web-common/src/main/groovy/org/grails/web/json/JSONTokener.java
+++ b/grails-web-common/src/main/groovy/org/grails/web/json/JSONTokener.java
@@ -122,7 +122,7 @@ public class JSONTokener {
public String next(int n) throws JSONException {
int i = this.myIndex;
int j = i + n;
- if (j >= this.mySource.length()) {
+ if (j > this.mySource.length()) {
throw syntaxError("Substring bounds error");
}
this.myIndex += n;
diff --git a/grails-web-common/src/main/groovy/org/grails/web/json/JSONWriter.java b/grails-web-common/src/main/groovy/org/grails/web/json/JSONWriter.java
index b40a42d874..98d829f044 100644
--- a/grails-web-common/src/main/groovy/org/grails/web/json/JSONWriter.java
+++ b/grails-web-common/src/main/groovy/org/grails/web/json/JSONWriter.java
@@ -161,8 +161,7 @@ public class JSONWriter {
*/
protected JSONWriter end(Mode m, char c) {
if (this.mode != m) {
- throw new JSONException(m == OBJECT ? "Misplaced endObject." :
- "Misplaced endArray.");
+ throw new JSONException(c == '}' ? "Misplaced endObject." : "Misplaced endArray.");
}
this.pop(m);
try {
diff --git a/grails-web-common/src/main/groovy/org/grails/web/json/PrettyPrintJSONWriter.java b/grails-web-common/src/main/groovy/org/grails/web/json/PrettyPrintJSONWriter.java
index 864d0daff3..19829b282d 100644
--- a/grails-web-common/src/main/groovy/org/grails/web/json/PrettyPrintJSONWriter.java
+++ b/grails-web-common/src/main/groovy/org/grails/web/json/PrettyPrintJSONWriter.java
@@ -104,8 +104,10 @@ public class PrettyPrintJSONWriter extends JSONWriter {
@Override
protected JSONWriter end(Mode m, char c) {
- newline();
- indent();
+ if (this.mode == m) {
+ newline();
+ indent();
+ }
return super.end(m, c);
}
diff --git a/grails-web-common/src/test/groovy/org/grails/web/json/JSONTokenerApiSpec.groovy b/grails-web-common/src/test/groovy/org/grails/web/json/JSONTokenerApiSpec.groovy
index 52dc8db523..902ae07a14 100644
--- a/grails-web-common/src/test/groovy/org/grails/web/json/JSONTokenerApiSpec.groovy
+++ b/grails-web-common/src/test/groovy/org/grails/web/json/JSONTokenerApiSpec.groovy
@@ -48,6 +48,16 @@ class JSONTokenerApiSpec extends Specification {
tokener.toString() == ' at character 4 of abcd'
}
+ void "next(n) returns the next n characters up to exactly the end of the source"() {
+ given:
+ def tokener = new JSONTokener('abc')
+
+ expect:
+ tokener.next(2) == 'ab'
+ tokener.next(1) == 'c'
+ new JSONTokener('abc').next(3) == 'abc'
+ }
+
void "character and substring mismatches include the source position"() {
when:
new JSONTokener('abc').next((char) 'z')
diff --git a/grails-web-common/src/test/groovy/org/grails/web/json/JSONWriterApiSpec.groovy b/grails-web-common/src/test/groovy/org/grails/web/json/JSONWriterApiSpec.groovy
index 7c0c4217cb..f2574c31f0 100644
--- a/grails-web-common/src/test/groovy/org/grails/web/json/JSONWriterApiSpec.groovy
+++ b/grails-web-common/src/test/groovy/org/grails/web/json/JSONWriterApiSpec.groovy
@@ -105,9 +105,8 @@ class JSONWriterApiSpec extends Specification {
({ JSONWriter w -> w.object() }) | ({ w -> w.array() }) | 'Misplaced array'
({ JSONWriter w -> w.object() }) | ({ w -> w.object() }) | 'Misplaced object'
({ JSONWriter w -> }) | ({ w -> w.endArray() }) | 'Misplaced endArray'
- // endObject misuse is matched on the shared prefix only: the writer currently reports it as endArray
- ({ JSONWriter w -> }) | ({ w -> w.endObject() }) | 'Misplaced end'
- ({ JSONWriter w -> w.object().key('key') }) | ({ w -> w.endObject() }) | 'Misplaced end'
+ ({ JSONWriter w -> }) | ({ w -> w.endObject() }) | 'Misplaced endObject'
+ ({ JSONWriter w -> w.object().key('key') }) | ({ w -> w.endObject() }) | 'Misplaced endObject'
({ JSONWriter w -> w.array().endArray() }) | ({ w -> w.array() }) | 'Misplaced array'
({ JSONWriter w -> w.object().endObject() }) | ({ w -> w.object() }) | 'Misplaced object'
({ JSONWriter w -> w.array().endArray() }) | ({ w -> w.value(1L) }) | 'Value out of sequence'
diff --git a/grails-web-common/src/test/groovy/org/grails/web/json/PrettyPrintJSONWriterApiSpec.groovy b/grails-web-common/src/test/groovy/org/grails/web/json/PrettyPrintJSONWriterApiSpec.groovy
index 1651f989df..84230a889c 100644
--- a/grails-web-common/src/test/groovy/org/grails/web/json/PrettyPrintJSONWriterApiSpec.groovy
+++ b/grails-web-common/src/test/groovy/org/grails/web/json/PrettyPrintJSONWriterApiSpec.groovy
@@ -73,11 +73,26 @@ class PrettyPrintJSONWriterApiSpec extends Specification {
({ w -> w.key('key') }) | 'Misplaced key'
({ w -> w.value(true) }) | 'Value out of sequence'
({ w -> w.object().valueNull() }) | 'Value out of sequence'
- // endObject misuse is matched on the shared prefix only: the writer currently reports it as endArray
- ({ w -> w.array().endObject() }) | 'Misplaced end'
+ ({ w -> w.array().endObject() }) | 'Misplaced endObject'
({ w -> w.object().endArray() }) | 'Misplaced endArray'
}
+ void "pretty writer writes nothing before rejecting a misplaced end"() {
+ given:
+ def output = new StringWriter()
+ def writer = new PrettyPrintJSONWriter(output)
+ writer.array()
+ def written = output.toString()
+
+ when:
+ writer.endObject()
+
+ then:
+ def error = thrown(JSONException)
+ error.message == 'Misplaced endObject.'
+ output.toString() == written
+ }
+
void "pretty writer wraps failures in newlines indentation keys and values"() {
given:
def output = Stub(Writer) {
(grails-core) 01/06: Return the page's property keys from the layout Content adapter
Posted by jd...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
jdaugherty pushed a commit to branch 7.0.x
in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit 9ce99cde3f23d8621b6a17e2b2745a933cf0f7fa
Author: James Daugherty <jd...@jdresources.net>
AuthorDate: Fri Oct 16 12:54:17 2026 -0400
Return the page's property keys from the layout Content adapter
TokenizedHTMLPage2Content.getPropertyKeys() called itself instead of the
wrapped page, so any caller asking a parsed layout Content for its property
keys recursed until the stack overflowed. Delegate to the page like the other
property accessors do, and assert the keys through the public Content API.
---
.../groovy/org/apache/grails/web/layout/TokenizedHTMLPage2Content.java | 2 +-
.../groovy/org/apache/grails/web/layout/GrailsHTMLPageParserSpec.groovy | 1 +
2 files changed, 2 insertions(+), 1 deletion(-)
diff --git a/grails-gsp/grails-layout/src/main/groovy/org/apache/grails/web/layout/TokenizedHTMLPage2Content.java b/grails-gsp/grails-layout/src/main/groovy/org/apache/grails/web/layout/TokenizedHTMLPage2Content.java
index 3f0deb3f9c..15d211fef3 100644
--- a/grails-gsp/grails-layout/src/main/groovy/org/apache/grails/web/layout/TokenizedHTMLPage2Content.java
+++ b/grails-gsp/grails-layout/src/main/groovy/org/apache/grails/web/layout/TokenizedHTMLPage2Content.java
@@ -59,7 +59,7 @@ final class TokenizedHTMLPage2Content implements Content {
@Override
public String[] getPropertyKeys() {
- return getPropertyKeys();
+ return page.getPropertyKeys();
}
@Override
diff --git a/grails-gsp/grails-layout/src/test/groovy/org/apache/grails/web/layout/GrailsHTMLPageParserSpec.groovy b/grails-gsp/grails-layout/src/test/groovy/org/apache/grails/web/layout/GrailsHTMLPageParserSpec.groovy
index c83e75e33b..38a772905d 100644
--- a/grails-gsp/grails-layout/src/test/groovy/org/apache/grails/web/layout/GrailsHTMLPageParserSpec.groovy
+++ b/grails-gsp/grails-layout/src/test/groovy/org/apache/grails/web/layout/GrailsHTMLPageParserSpec.groovy
@@ -76,6 +76,7 @@ class GrailsHTMLPageParserSpec extends Specification {
body.toString() == 'Body'
content.title == 'Title'
content.getProperty('page.extra') == 'value'
+ content.propertyKeys.toList().containsAll(['title', 'meta.layout', 'page.extra'])
GSPGrailsLayoutPage.content2htmlPage(content).body == 'Body'
}
}
(grails-core) 04/06: Return the relative path of a spring script from getPathFromRoot
Posted by jd...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
jdaugherty pushed a commit to branch 7.0.x
in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit 18d8daf59e5e9ab6a66d811b740013fd18cf54e4
Author: James Daugherty <jd...@jdresources.net>
AuthorDate: Fri Oct 16 12:56:14 2026 -0400
Return the relative path of a spring script from getPathFromRoot
getPathFromRoot picked m.group(m.groupCount() - 1) for whichever compiler
root pattern matched. That is the file group of the three-group resource
pattern, but for the single-group spring scripts pattern it is group(0), the
whole match, so /app/grails-app/conf/spring/resources.groovy came back
unchanged instead of as resources.groovy. Select the single group when the
pattern has only one, and cover the spring script, a nested spring script and
a Windows path in the model spec's data table.
---
.../main/groovy/org/grails/io/support/GrailsResourceUtils.java | 4 +++-
.../groovy/org/grails/io/support/GrailsResourceUtilsSpec.groovy | 8 +++-----
2 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/grails-gradle/model/src/main/groovy/org/grails/io/support/GrailsResourceUtils.java b/grails-gradle/model/src/main/groovy/org/grails/io/support/GrailsResourceUtils.java
index e0141a999a..2861fea39e 100644
--- a/grails-gradle/model/src/main/groovy/org/grails/io/support/GrailsResourceUtils.java
+++ b/grails-gradle/model/src/main/groovy/org/grails/io/support/GrailsResourceUtils.java
@@ -856,7 +856,9 @@ public class GrailsResourceUtils {
for (Pattern COMPILER_ROOT_PATTERN : COMPILER_ROOT_PATTERNS) {
Matcher m = COMPILER_ROOT_PATTERN.matcher(path);
if (m.find()) {
- return m.group(m.groupCount() - 1);
+ // the spring scripts pattern captures the relative path as its only group; the resource pattern
+ // captures the artefact directory before it and the extension after it
+ return m.group(m.groupCount() == 1 ? 1 : m.groupCount() - 1);
}
}
return null;
diff --git a/grails-gradle/model/src/test/groovy/org/grails/io/support/GrailsResourceUtilsSpec.groovy b/grails-gradle/model/src/test/groovy/org/grails/io/support/GrailsResourceUtilsSpec.groovy
index bbcb2b2f8c..62970a0552 100644
--- a/grails-gradle/model/src/test/groovy/org/grails/io/support/GrailsResourceUtilsSpec.groovy
+++ b/grails-gradle/model/src/test/groovy/org/grails/io/support/GrailsResourceUtilsSpec.groovy
@@ -62,16 +62,14 @@ class GrailsResourceUtilsSpec extends Specification {
path | relativePath | directory
'/app/grails-app/domain/example/Book.groovy' | 'example/Book.groovy' | 'domain'
'/app/grails-app/conf/BootStrap.groovy' | 'BootStrap.groovy' | 'conf'
+ '/app/grails-app/conf/spring/resources.groovy' | 'resources.groovy' | 'conf'
+ '/app/grails-app/conf/spring/nested/beans.groovy' | 'nested/beans.groovy' | 'conf'
+ 'C:\\app\\grails-app\\conf\\spring\\resources.groovy' | 'resources.groovy' | 'conf'
'C:\\app\\grails-app\\domain\\example\\Book.java' | 'example\\Book.java' | 'domain'
'/app/src/main/groovy/example/Book.groovy' | null | null
'/app/grails-app/views/book/show.gsp' | null | null
}
- void 'a spring script resolves to the conf artefact directory'() {
- expect:
- GrailsResourceUtils.getArtefactDirectory('/app/grails-app/conf/spring/resources.groovy') == 'conf'
- }
-
void 'domain detection distinguishes source directories and extensions'() {
expect:
GrailsResourceUtils.isDomainClass(new URL(location)) == expected
(grails-core) 03/06: Test File values for null instead of truth in the file system interaction
Posted by jd...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
jdaugherty pushed a commit to branch 7.0.x
in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit 81e4dc83731d28dd05ed56ac5e5b7106fdc80024
Author: James Daugherty <jd...@jdresources.net>
AuthorDate: Fri Oct 16 12:55:58 2026 -0400
Test File values for null instead of truth in the file system interaction
Groovy 5 and later evaluate a java.io.File that does not exist as false,
where Groovy 4 treated every File as true. The guards in both copies of
FileSystemInteractionImpl relied on the old meaning, so on Grails 8 and later
projectPath returned an empty string for a file that is not there yet, which
is exactly the state a template destination is in after a failed render has
deleted it: the TemplateRendererImpl error message then read "Error rendering
template to destination : ..." with no path. The same guards decided the base
directory in file(), the copy spec destination and the empty-path check in
resource(). Each one now tests for null (or an empty path) explicitly.
Covered by a feature on the shell-cli FileSystemInteractionImplSpec and a new
spec for the grails.dev.commands.io copy, both using files that do not exist.
---
.../commands/io/FileSystemInteractionImpl.groovy | 9 +--
.../io/FileSystemInteractionImplSpec.groovy | 75 ++++++++++++++++++++++
.../commands/io/FileSystemInteractionImpl.groovy | 9 +--
.../io/FileSystemInteractionImplSpec.groovy | 22 +++++++
4 files changed, 107 insertions(+), 8 deletions(-)
diff --git a/grails-core/src/main/groovy/grails/dev/commands/io/FileSystemInteractionImpl.groovy b/grails-core/src/main/groovy/grails/dev/commands/io/FileSystemInteractionImpl.groovy
index f57e4ffa81..0f37ff555d 100644
--- a/grails-core/src/main/groovy/grails/dev/commands/io/FileSystemInteractionImpl.groovy
+++ b/grails-core/src/main/groovy/grails/dev/commands/io/FileSystemInteractionImpl.groovy
@@ -85,7 +85,7 @@ class FileSystemInteractionImpl implements FileSystemInteraction {
FileSystemInteraction.CopySpec spec = new FileSystemInteraction.CopySpec()
callable.delegate = spec
callable.call()
- if (spec.from && spec.into) {
+ if (spec.from && spec.into != null) {
if (spec.from instanceof Iterable) {
copyAll((Iterable) spec.from, spec.into)
}
@@ -158,7 +158,7 @@ class FileSystemInteractionImpl implements FileSystemInteraction {
if (path instanceof File) return (File) path
else if (path instanceof Resource) return ((Resource) path).file
else {
- new File(baseDir ?: new File('.'), path.toString())
+ new File(baseDir != null ? baseDir : new File('.'), path.toString())
}
}
@@ -213,7 +213,7 @@ class FileSystemInteractionImpl implements FileSystemInteraction {
*/
@Override
Resource resource(Object path) {
- if (!path) return null
+ if (path == null || path.toString().isEmpty()) return null
if (path instanceof Resource) return (Resource) path
def f = file(path)
if (f?.exists() && f.isFile()) {
@@ -261,7 +261,8 @@ class FileSystemInteractionImpl implements FileSystemInteraction {
@Override
String projectPath(Object path) {
def file = file(path)
- if (file) {
+ // a File that does not exist is false on Groovy 5 and later, so test for null explicitly
+ if (file != null) {
def basePath = baseDir.canonicalPath
return (file.canonicalPath - basePath).substring(1)
}
diff --git a/grails-core/src/test/groovy/grails/dev/commands/io/FileSystemInteractionImplSpec.groovy b/grails-core/src/test/groovy/grails/dev/commands/io/FileSystemInteractionImplSpec.groovy
new file mode 100644
index 0000000000..ddd22a28f0
--- /dev/null
+++ b/grails-core/src/test/groovy/grails/dev/commands/io/FileSystemInteractionImplSpec.groovy
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package grails.dev.commands.io
+
+import org.grails.io.support.FileSystemResource
+import spock.lang.Specification
+import spock.lang.TempDir
+
+class FileSystemInteractionImplSpec extends Specification {
+
+ @TempDir
+ File directory
+
+ FileSystemInteractionImpl interaction
+
+ void setup() {
+ interaction = new FileSystemInteractionImpl(directory)
+ }
+
+ def 'files and resources are resolved relative to the project and existing objects are preserved'() {
+ given:
+ def file = new File(directory, 'example.txt')
+ file.text = 'example'
+ def resource = new FileSystemResource(file)
+
+ expect:
+ interaction.file('example.txt') == file
+ interaction.file(file).is(file)
+ interaction.file(resource) == file
+ interaction.resource(resource).is(resource)
+ interaction.resource('example.txt').file == file
+ interaction.resource(null) == null
+ interaction.resource('') == null
+ !interaction.resource('missing.txt').exists()
+ interaction.projectPath(file) == 'example.txt'
+ }
+
+ def 'files that do not exist yet are resolved like existing ones'() {
+ given: 'a project directory that has not been created and files below it'
+ def pending = new File(directory, 'pending')
+ def pendingInteraction = new FileSystemInteractionImpl(pending)
+ def missing = new File(directory, 'nested/missing.txt')
+ def existing = interaction.file('present.txt')
+ existing.text = 'present'
+
+ when:
+ interaction.copy {
+ from(existing)
+ into(new File(directory, 'target'))
+ }
+
+ then:
+ interaction.projectPath(missing) == 'nested/missing.txt'
+ interaction.projectPath('missing.txt') == 'missing.txt'
+ !interaction.resource(missing).exists()
+ pendingInteraction.file('build.gradle') == new File(pending, 'build.gradle')
+ interaction.file('target/present.txt').text == 'present'
+ }
+}
diff --git a/grails-shell-cli/src/main/groovy/org/grails/cli/profile/commands/io/FileSystemInteractionImpl.groovy b/grails-shell-cli/src/main/groovy/org/grails/cli/profile/commands/io/FileSystemInteractionImpl.groovy
index 6dbd0aadb2..53f8b5f6b0 100644
--- a/grails-shell-cli/src/main/groovy/org/grails/cli/profile/commands/io/FileSystemInteractionImpl.groovy
+++ b/grails-shell-cli/src/main/groovy/org/grails/cli/profile/commands/io/FileSystemInteractionImpl.groovy
@@ -87,7 +87,7 @@ class FileSystemInteractionImpl implements FileSystemInteraction {
FileSystemInteraction.CopySpec spec = new FileSystemInteraction.CopySpec()
callable.delegate = spec
callable.call()
- if (spec.from && spec.into) {
+ if (spec.from && spec.into != null) {
if (spec.from instanceof Iterable) {
copyAll((Iterable) spec.from, spec.into)
}
@@ -161,7 +161,7 @@ class FileSystemInteractionImpl implements FileSystemInteraction {
else if (path instanceof Resource) return ((Resource) path).file
else {
def baseDir = executionContext.baseDir
- new File(baseDir ?: new File('.'), path.toString())
+ new File(baseDir != null ? baseDir : new File('.'), path.toString())
}
}
@@ -216,7 +216,7 @@ class FileSystemInteractionImpl implements FileSystemInteraction {
*/
@Override
Resource resource(Object path) {
- if (!path) return null
+ if (path == null || path.toString().isEmpty()) return null
if (path instanceof Resource) return (Resource) path
def f = file(path)
if (f?.exists() && f.isFile()) {
@@ -264,7 +264,8 @@ class FileSystemInteractionImpl implements FileSystemInteraction {
@Override
String projectPath(Object path) {
def file = file(path)
- if (file) {
+ // a File that does not exist is false on Groovy 5 and later, so test for null explicitly
+ if (file != null) {
def basePath = executionContext.baseDir.canonicalPath
return (file.canonicalPath - basePath).substring(1)
}
diff --git a/grails-shell-cli/src/test/groovy/org/grails/cli/profile/commands/io/FileSystemInteractionImplSpec.groovy b/grails-shell-cli/src/test/groovy/org/grails/cli/profile/commands/io/FileSystemInteractionImplSpec.groovy
index 953b976bee..c7acbb161d 100644
--- a/grails-shell-cli/src/test/groovy/org/grails/cli/profile/commands/io/FileSystemInteractionImplSpec.groovy
+++ b/grails-shell-cli/src/test/groovy/org/grails/cli/profile/commands/io/FileSystemInteractionImplSpec.groovy
@@ -65,6 +65,28 @@ class FileSystemInteractionImplSpec extends Specification {
interaction.projectPath(file) == 'example.txt'
}
+ def 'files that do not exist yet are resolved like existing ones'() {
+ given: 'a project directory that has not been created and files below it'
+ def pending = new File(directory, 'pending')
+ def pendingInteraction = new FileSystemInteractionImpl(Stub(ExecutionContext) { getBaseDir() >> pending })
+ def missing = new File(directory, 'nested/missing.txt')
+ def existing = interaction.file('present.txt')
+ existing.text = 'present'
+
+ when:
+ interaction.copy {
+ from(existing)
+ into(new File(directory, 'target'))
+ }
+
+ then:
+ interaction.projectPath(missing) == 'nested/missing.txt'
+ interaction.projectPath('missing.txt') == 'missing.txt'
+ !interaction.resource(missing).exists()
+ pendingInteraction.file('build.gradle') == new File(pending, 'build.gradle')
+ interaction.file('target/present.txt').text == 'present'
+ }
+
def 'mkdir and delete operate on project files and return the interaction for chaining'() {
when:
def result = interaction.mkdir('nested/child')
(grails-core) 02/06: Combine patterns and extract template variables as AntPathMatcher documents
Posted by jd...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
jdaugherty pushed a commit to branch 7.0.x
in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit c471280c537a8d682fe6f1deeb38db810ea9933a
Author: James Daugherty <jd...@jdresources.net>
AuthorDate: Fri Oct 16 12:55:04 2026 -0400
Combine patterns and extract template variables as AntPathMatcher documents
combine() keyed its file-extension branch off any dot in the first pattern,
so "/report.html" + "/other.txt" returned "/report.html" and two wildcard
extensions such as "/*.html" + "/*.txt" never raised the documented
IllegalArgumentException. It now takes that branch only for a "*." pattern,
as Spring's AntPathMatcher does, concatenates everything else, treats ".*"
and a missing extension as "any", and rejects two specific extensions.
AntPathStringMatcher.matchStrings assumed one capturing group per template
variable and threw IndexOutOfBoundsException when a variable regex added a
group of its own. It now rejects that with Spring's IllegalArgumentException
advising non-capturing groups; matching without extraction is unchanged.
---
.../org/grails/io/support/AntPathMatcher.java | 36 ++++++++++++----------
.../grails/io/support/AntPathMatcherSpec.groovy | 29 ++++++++++++++++-
2 files changed, 47 insertions(+), 18 deletions(-)
diff --git a/grails-bootstrap/src/main/groovy/org/grails/io/support/AntPathMatcher.java b/grails-bootstrap/src/main/groovy/org/grails/io/support/AntPathMatcher.java
index 78dbf8d42f..5609ac9c6a 100644
--- a/grails-bootstrap/src/main/groovy/org/grails/io/support/AntPathMatcher.java
+++ b/grails-bootstrap/src/main/groovy/org/grails/io/support/AntPathMatcher.java
@@ -306,7 +306,8 @@ public class AntPathMatcher {
if (!hasText(pattern2)) {
return pattern1;
}
- if (!pattern1.contains("{") && match(pattern1, pattern2)) {
+ boolean pattern1ContainsUriVar = pattern1.indexOf('{') != -1;
+ if (!pattern1ContainsUriVar && match(pattern1, pattern2)) {
return pattern2;
}
if (pattern1.endsWith("/*")) {
@@ -325,31 +326,26 @@ public class AntPathMatcher {
// /hotels/** + booking -> /hotels/**/booking
return pattern1 + "/" + pattern2;
}
- int dotPos1 = pattern1.indexOf('.');
- if (dotPos1 == -1) {
+ int starDotPos1 = pattern1.indexOf("*.");
+ if (pattern1ContainsUriVar || starDotPos1 == -1 || pathSeparator.equals(".")) {
// simply concatenate the two patterns
if (pattern1.endsWith("/") || pattern2.startsWith("/")) {
return pattern1 + pattern2;
}
return pattern1 + "/" + pattern2;
}
- String fileName1 = pattern1.substring(0, dotPos1);
- String extension1 = pattern1.substring(dotPos1);
- String fileName2;
- String extension2;
+ String extension1 = pattern1.substring(starDotPos1 + 1);
int dotPos2 = pattern2.indexOf('.');
- if (dotPos2 != -1) {
- fileName2 = pattern2.substring(0, dotPos2);
- extension2 = pattern2.substring(dotPos2);
+ String fileName2 = dotPos2 == -1 ? pattern2 : pattern2.substring(0, dotPos2);
+ String extension2 = dotPos2 == -1 ? "" : pattern2.substring(dotPos2);
+ boolean extension1All = extension1.equals(".*") || extension1.isEmpty();
+ boolean extension2All = extension2.equals(".*") || extension2.isEmpty();
+ if (!extension1All && !extension2All) {
+ throw new IllegalArgumentException("Cannot combine patterns: " + pattern1 + " vs " + pattern2);
}
- else {
- fileName2 = pattern2;
- extension2 = "";
- }
- String fileName = fileName1.endsWith("*") ? fileName2 : fileName1;
- String extension = extension1.startsWith("*") ? extension2 : extension1;
+ String extension = extension1All ? extension2 : extension1;
- return fileName + extension;
+ return fileName2 + extension;
}
private boolean hasText(String txt) {
@@ -538,6 +534,12 @@ public class AntPathMatcher {
return false;
}
if (uriTemplateVariables != null) {
+ if (variableNames.size() != matcher.groupCount()) {
+ throw new IllegalArgumentException("The number of capturing groups in the pattern segment " +
+ pattern + " does not match the number of URI template variables it defines, " +
+ "which can occur if capturing groups are used in a URI template regex. " +
+ "Use non-capturing groups instead.");
+ }
for (int i = 1, count = matcher.groupCount(); i <= count; i++) {
String name = variableNames.get(i - 1);
String value = matcher.group(i);
diff --git a/grails-bootstrap/src/test/groovy/org/grails/io/support/AntPathMatcherSpec.groovy b/grails-bootstrap/src/test/groovy/org/grails/io/support/AntPathMatcherSpec.groovy
index 98c8919939..0f1da5a781 100644
--- a/grails-bootstrap/src/test/groovy/org/grails/io/support/AntPathMatcherSpec.groovy
+++ b/grails-bootstrap/src/test/groovy/org/grails/io/support/AntPathMatcherSpec.groovy
@@ -199,10 +199,28 @@ class AntPathMatcherSpec extends Specification {
'/{hotel}' | '/booking' | '/{hotel}/booking'
'/*.html' | '/hotels.html' | '/hotels.html'
'/*.html' | '/hotels' | '/hotels.html'
+ '/*.html' | '/hotels.*' | '/hotels.html'
+ '/*.*' | '/*.html' | '/*.html'
+ '/report.html' | '/other.txt' | '/report.html/other.txt'
'/users/{id:[}' | '/users/42' | '/users/{id:[}/users/42'
' ' | 'booking' | ' /booking'
}
+ @Unroll
+ void 'combine rejects #first and #second because both name a wildcard extension'() {
+ when:
+ matcher.combine(first, second)
+
+ then:
+ def error = thrown(IllegalArgumentException)
+ error.message == "Cannot combine patterns: $first vs $second"
+
+ where:
+ first | second
+ '/*.html' | '/*.txt'
+ '/*.html' | '/hotels.txt'
+ }
+
@Unroll
void 'recognize wildcard patterns without treating URI templates as wildcards'() {
expect:
@@ -271,10 +289,19 @@ class AntPathMatcherSpec extends Specification {
operation << ['match', 'matchStart', 'extractUriTemplateVariables']
}
- void 'capturing groups in a template regex still match'() {
+ void 'capturing groups in a template regex match but cannot be extracted as variables'() {
expect:
matcher.match('/users/{id:(ab|cd)}', '/users/ab')
!matcher.match('/users/{id:(ab|cd)}', '/users/ef')
+ matcher.extractUriTemplateVariables('/users/{id:(?:ab|cd)}', '/users/ab') == [id: 'ab']
+
+ when:
+ matcher.extractUriTemplateVariables('/users/{id:(ab|cd)}', '/users/ab')
+
+ then:
+ def error = thrown(IllegalArgumentException)
+ error.message.contains('does not match the number of URI template variables')
+ error.message.endsWith('Use non-capturing groups instead.')
}
@Unroll