Browse Source

Relax type check in FormHttpMessage(Reader|Writer)

Issue: SPR-16055
pull/1562/head
Sebastien Deleuze 7 years ago
parent
commit
2962f085e5
  1. 5
      spring-web/src/main/java/org/springframework/http/codec/FormHttpMessageReader.java
  2. 3
      spring-web/src/main/java/org/springframework/http/codec/FormHttpMessageWriter.java
  3. 5
      spring-web/src/test/java/org/springframework/http/codec/FormHttpMessageReaderTests.java
  4. 4
      spring-web/src/test/java/org/springframework/http/codec/FormHttpMessageWriterTests.java

5
spring-web/src/main/java/org/springframework/http/codec/FormHttpMessageReader.java

@ -77,8 +77,9 @@ public class FormHttpMessageReader implements HttpMessageReader<MultiValueMap<St @@ -77,8 +77,9 @@ public class FormHttpMessageReader implements HttpMessageReader<MultiValueMap<St
@Override
public boolean canRead(ResolvableType elementType, @Nullable MediaType mediaType) {
return (MULTIVALUE_TYPE.isAssignableFrom(elementType) &&
(mediaType == null || MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(mediaType)));
return (MULTIVALUE_TYPE.isAssignableFrom(elementType) ||
(elementType.hasUnresolvableGenerics() && MultiValueMap.class.isAssignableFrom(elementType.resolve()))) &&
(mediaType == null || MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(mediaType));
}
@Override

3
spring-web/src/main/java/org/springframework/http/codec/FormHttpMessageWriter.java

@ -77,7 +77,8 @@ public class FormHttpMessageWriter implements HttpMessageWriter<MultiValueMap<St @@ -77,7 +77,8 @@ public class FormHttpMessageWriter implements HttpMessageWriter<MultiValueMap<St
@Override
public boolean canWrite(ResolvableType elementType, @Nullable MediaType mediaType) {
return MULTIVALUE_TYPE.isAssignableFrom(elementType) &&
return (MULTIVALUE_TYPE.isAssignableFrom(elementType) ||
(elementType.hasUnresolvableGenerics() && MultiValueMap.class.isAssignableFrom(elementType.resolve()))) &&
(mediaType == null || MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(mediaType));
}

5
spring-web/src/test/java/org/springframework/http/codec/FormHttpMessageReaderTests.java

@ -27,6 +27,7 @@ import org.springframework.http.HttpHeaders; @@ -27,6 +27,7 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
/**
@ -42,6 +43,10 @@ public class FormHttpMessageReaderTests { @@ -42,6 +43,10 @@ public class FormHttpMessageReaderTests {
ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, String.class),
MediaType.APPLICATION_FORM_URLENCODED));
assertTrue(this.reader.canRead(
ResolvableType.forInstance(new LinkedMultiValueMap<String, String>()),
MediaType.APPLICATION_FORM_URLENCODED));
assertFalse(this.reader.canRead(
ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, Object.class),
MediaType.APPLICATION_FORM_URLENCODED));

4
spring-web/src/test/java/org/springframework/http/codec/FormHttpMessageWriterTests.java

@ -44,6 +44,10 @@ public class FormHttpMessageWriterTests { @@ -44,6 +44,10 @@ public class FormHttpMessageWriterTests {
ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, String.class),
MediaType.APPLICATION_FORM_URLENCODED));
assertTrue(this.writer.canWrite(
ResolvableType.forInstance(new LinkedMultiValueMap<String, String>()),
MediaType.APPLICATION_FORM_URLENCODED));
assertFalse(this.writer.canWrite(
ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, Object.class),
MediaType.APPLICATION_FORM_URLENCODED));

Loading…
Cancel
Save