[
https://issues.apache.org/jira/browse/CXF-7765?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16524208#comment-16524208
]
ASF GitHub Bot commented on CXF-7765:
-------------------------------------
coheigea closed pull request #425: CXF-7765 - URITemplate.compareTemplates
returns inconsistent results
URL: https://github.com/apache/cxf/pull/425
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git
a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/URITemplate.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/URITemplate.java
index b5059f3ddca..b9f0232d1a9 100644
---
a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/URITemplate.java
+++
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/URITemplate.java
@@ -377,28 +377,24 @@ public static URITemplate createExactTemplate(String
pathValue) {
}
public static int compareTemplates(URITemplate t1, URITemplate t2) {
- String l1 = t1.getLiteralChars();
- String l2 = t2.getLiteralChars();
- if (!l1.equals(l2)) {
- // descending order
- return l1.length() < l2.length() ? 1 : -1;
- }
-
- int g1 = t1.getVariables().size();
- int g2 = t2.getVariables().size();
+ int l1 = t1.getLiteralChars().length();
+ int l2 = t2.getLiteralChars().length();
// descending order
- int result = g1 < g2 ? 1 : g1 > g2 ? -1 : 0;
+ int result = l1 < l2 ? 1 : l1 > l2 ? -1 : 0;
if (result == 0) {
- int gCustom1 = t1.getCustomVariables().size();
- int gCustom2 = t2.getCustomVariables().size();
- if (gCustom1 != gCustom2) {
- // descending order
- return gCustom1 < gCustom2 ? 1 : -1;
+ int g1 = t1.getVariables().size();
+ int g2 = t2.getVariables().size();
+ // descending order
+ result = g1 < g2 ? 1 : g1 > g2 ? -1 : 0;
+ if (result == 0) {
+ int gCustom1 = t1.getCustomVariables().size();
+ int gCustom2 = t2.getCustomVariables().size();
+ result = gCustom1 < gCustom2 ? 1 : gCustom1 > gCustom2 ? -1 :
0;
+ if (result == 0) {
+ result =
t1.getPatternValue().compareTo(t2.getPatternValue());
+ }
}
}
- if (result == 0) {
- result = t1.getPatternValue().compareTo(t2.getPatternValue());
- }
return result;
}
diff --git
a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/URITemplateTest.java
b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/URITemplateTest.java
index 97e53637479..7d2ac299ba0 100644
---
a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/URITemplateTest.java
+++
b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/URITemplateTest.java
@@ -740,4 +740,17 @@ public void testEncodeLiteralCharactersNotVariable() {
//System.out.println(ut.encodeLiteralCharacters());
assertEquals("a%20{digit:[0-9]}%20b",
ut.encodeLiteralCharacters(false));
}
+
+ @Test
+ public void testCompareNumberOfLiteralCharacters() {
+ URITemplate t1 = new URITemplate("/foo");
+ URITemplate t2 = new URITemplate("/bar");
+ URITemplate t3 = new URITemplate("/foo/bar");
+ assertEquals(0, URITemplate.compareTemplates(t1, t1));
+ assertTrue(URITemplate.compareTemplates(t1, t3) > 0);
+ assertTrue(URITemplate.compareTemplates(t3, t1) < 0);
+ assertEquals(Integer.signum(URITemplate.compareTemplates(t1, t2)),
+ -Integer.signum(URITemplate.compareTemplates(t2, t1)));
+ }
+
}
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> URITemplate.compareTemplates returns inconsistent results
> ---------------------------------------------------------
>
> Key: CXF-7765
> URL: https://issues.apache.org/jira/browse/CXF-7765
> Project: CXF
> Issue Type: Bug
> Components: JAX-RS
> Affects Versions: 3.1.15
> Reporter: Shon Vella
> Assignee: Colm O hEigeartaigh
> Priority: Major
> Fix For: 3.2.6, 3.1.17
>
>
> When org.apache.cxf.jaxrs.model.URITemplate.compareTemplates() is passed two
> templates with the same number of literal characters, it returns -1
> regardless of the order the templates a passed in. I suppose this may be on
> purpose, but the result is that if compareTemplates() is used as the basis of
> a Comparator<URITemplate> that is used by java.util.Collections.sort() it can
> result in:
> {{java.lang.IllegalArgumentException: Comparison method violates its general
> contract!}}
> I would also expect that this would result in some degree of unpredictability
> of the prioritization of when selecting the appropriate JAX-RS method to call
> for a given request.
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)