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
@Override @Override
public boolean canRead(ResolvableType elementType, @Nullable MediaType mediaType) { public boolean canRead(ResolvableType elementType, @Nullable MediaType mediaType) {
return (MULTIVALUE_TYPE.isAssignableFrom(elementType) && return (MULTIVALUE_TYPE.isAssignableFrom(elementType) ||
(mediaType == null || MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(mediaType))); (elementType.hasUnresolvableGenerics() && MultiValueMap.class.isAssignableFrom(elementType.resolve()))) &&
(mediaType == null || MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(mediaType));
} }
@Override @Override

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

@ -77,7 +77,8 @@ public class FormHttpMessageWriter implements HttpMessageWriter<MultiValueMap<St
@Override @Override
public boolean canWrite(ResolvableType elementType, @Nullable MediaType mediaType) { 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)); (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;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
/** /**
@ -42,6 +43,10 @@ public class FormHttpMessageReaderTests {
ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, String.class), ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, String.class),
MediaType.APPLICATION_FORM_URLENCODED)); MediaType.APPLICATION_FORM_URLENCODED));
assertTrue(this.reader.canRead(
ResolvableType.forInstance(new LinkedMultiValueMap<String, String>()),
MediaType.APPLICATION_FORM_URLENCODED));
assertFalse(this.reader.canRead( assertFalse(this.reader.canRead(
ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, Object.class), ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, Object.class),
MediaType.APPLICATION_FORM_URLENCODED)); MediaType.APPLICATION_FORM_URLENCODED));

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

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

Loading…
Cancel
Save