Skip to content

Commit 3eda02d

Browse files
committed
Update MediaType's includes method
An additional update (after the last commit) of the "includes" and "isCompatibleWith" methods of MediaType to accomodate wildcards in media types with a suffix. Issue: SPR-9841
1 parent 7718936 commit 3eda02d

File tree

2 files changed

+41
-22
lines changed

2 files changed

+41
-22
lines changed

spring-web/src/main/java/org/springframework/http/MediaType.java

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -482,19 +482,27 @@ public boolean includes(MediaType other) {
482482
return true;
483483
}
484484
else if (this.type.equals(other.type)) {
485-
if (this.subtype.equals(other.subtype) || this.isWildcardSubtype()) {
485+
if (this.subtype.equals(other.subtype)) {
486486
return true;
487487
}
488-
// application/*+xml includes application/soap+xml
489-
int thisPlusIdx = this.subtype.indexOf('+');
490-
int otherPlusIdx = other.subtype.indexOf('+');
491-
if (thisPlusIdx != -1 && otherPlusIdx != -1) {
492-
String thisSubtypeNoSuffix = this.subtype.substring(0, thisPlusIdx);
493-
String thisSubtypeSuffix = this.subtype.substring(thisPlusIdx + 1);
494-
String otherSubtypeSuffix = other.subtype.substring(otherPlusIdx + 1);
495-
if (thisSubtypeSuffix.equals(otherSubtypeSuffix) && WILDCARD_TYPE.equals(thisSubtypeNoSuffix)) {
488+
if (this.isWildcardSubtype()) {
489+
// wildcard with suffix, e.g. application/*+xml
490+
int thisPlusIdx = this.subtype.indexOf('+');
491+
if (thisPlusIdx == -1) {
496492
return true;
497493
}
494+
else {
495+
// application/*+xml includes application/soap+xml
496+
int otherPlusIdx = other.subtype.indexOf('+');
497+
if (otherPlusIdx != -1) {
498+
String thisSubtypeNoSuffix = this.subtype.substring(0, thisPlusIdx);
499+
String thisSubtypeSuffix = this.subtype.substring(thisPlusIdx + 1);
500+
String otherSubtypeSuffix = other.subtype.substring(otherPlusIdx + 1);
501+
if (thisSubtypeSuffix.equals(otherSubtypeSuffix) && WILDCARD_TYPE.equals(thisSubtypeNoSuffix)) {
502+
return true;
503+
}
504+
}
505+
}
498506
}
499507
}
500508
return false;
@@ -515,23 +523,30 @@ public boolean isCompatibleWith(MediaType other) {
515523
return true;
516524
}
517525
else if (this.type.equals(other.type)) {
518-
if (this.subtype.equals(other.subtype) || this.isWildcardSubtype() || other.isWildcardSubtype()) {
526+
if (this.subtype.equals(other.subtype)) {
519527
return true;
520528
}
521-
// application/*+xml is compatible with application/soap+xml, and vice-versa
522-
int thisPlusIdx = this.subtype.indexOf('+');
523-
int otherPlusIdx = other.subtype.indexOf('+');
524-
if (thisPlusIdx != -1 && otherPlusIdx != -1) {
525-
String thisSubtypeNoSuffix = this.subtype.substring(0, thisPlusIdx);
526-
String otherSubtypeNoSuffix = other.subtype.substring(0, otherPlusIdx);
527-
528-
String thisSubtypeSuffix = this.subtype.substring(thisPlusIdx + 1);
529-
String otherSubtypeSuffix = other.subtype.substring(otherPlusIdx + 1);
530-
531-
if (thisSubtypeSuffix.equals(otherSubtypeSuffix) &&
532-
(WILDCARD_TYPE.equals(thisSubtypeNoSuffix) || WILDCARD_TYPE.equals(otherSubtypeNoSuffix))) {
529+
// wildcard with suffix? e.g. application/*+xml
530+
if (this.isWildcardSubtype() || other.isWildcardSubtype()) {
531+
532+
int thisPlusIdx = this.subtype.indexOf('+');
533+
int otherPlusIdx = other.subtype.indexOf('+');
534+
535+
if (thisPlusIdx == -1 && otherPlusIdx == -1) {
533536
return true;
534537
}
538+
else if (thisPlusIdx != -1 && otherPlusIdx != -1) {
539+
String thisSubtypeNoSuffix = this.subtype.substring(0, thisPlusIdx);
540+
String otherSubtypeNoSuffix = other.subtype.substring(0, otherPlusIdx);
541+
542+
String thisSubtypeSuffix = this.subtype.substring(thisPlusIdx + 1);
543+
String otherSubtypeSuffix = other.subtype.substring(otherPlusIdx + 1);
544+
545+
if (thisSubtypeSuffix.equals(otherSubtypeSuffix) &&
546+
(WILDCARD_TYPE.equals(thisSubtypeNoSuffix) || WILDCARD_TYPE.equals(otherSubtypeNoSuffix))) {
547+
return true;
548+
}
549+
}
535550
}
536551
}
537552
return false;

spring-web/src/test/java/org/springframework/http/MediaTypeTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public void includes() throws Exception {
5959

6060
assertTrue(applicationWildcardXml.includes(applicationSoapXml));
6161
assertFalse(applicationSoapXml.includes(applicationWildcardXml));
62+
63+
assertFalse(applicationWildcardXml.includes(MediaType.APPLICATION_JSON));
6264
}
6365

6466
@Test
@@ -84,6 +86,8 @@ public void isCompatible() throws Exception {
8486

8587
assertTrue(applicationWildcardXml.isCompatibleWith(applicationSoapXml));
8688
assertTrue(applicationSoapXml.isCompatibleWith(applicationWildcardXml));
89+
90+
assertFalse(applicationWildcardXml.isCompatibleWith(MediaType.APPLICATION_JSON));
8791
}
8892

8993
@Test

0 commit comments

Comments
 (0)