Polishing

See gh-28189
This commit is contained in:
rstoyanchev 2022-05-09 19:01:10 +01:00
parent 78ab4d7118
commit 8378af9e39
3 changed files with 35 additions and 22 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2021 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -60,7 +60,7 @@ public class ExchangeResult {
private static final Log logger = LogFactory.getLog(ExchangeResult.class); private static final Log logger = LogFactory.getLog(ExchangeResult.class);
private static final List<MediaType> PRINTABLE_MEDIA_TYPES = Arrays.asList( private static final List<MediaType> PRINTABLE_MEDIA_TYPES = Arrays.asList(
MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.parseMediaType("application/*+json"), MediaType.APPLICATION_XML,
MediaType.parseMediaType("text/*"), MediaType.APPLICATION_FORM_URLENCODED); MediaType.parseMediaType("text/*"), MediaType.APPLICATION_FORM_URLENCODED);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2020 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -162,6 +162,7 @@ public class ResponseBodyResultHandlerTests {
return new HandlerResult(handlerMethod, returnValue, handlerMethod.getReturnType()); return new HandlerResult(handlerMethod, returnValue, handlerMethod.getReturnType());
} }
@SuppressWarnings("SameParameterValue")
private void assertResponseBody(MockServerWebExchange exchange, @Nullable String responseBody) { private void assertResponseBody(MockServerWebExchange exchange, @Nullable String responseBody) {
StepVerifier.create(exchange.getResponse().getBody()) StepVerifier.create(exchange.getResponse().getBody())
.consumeNextWith(buf -> assertThat(buf.toString(UTF_8)).isEqualTo(responseBody)) .consumeNextWith(buf -> assertThat(buf.toString(UTF_8)).isEqualTo(responseBody))
@ -171,7 +172,7 @@ public class ResponseBodyResultHandlerTests {
@RestController @RestController
@SuppressWarnings("unused") @SuppressWarnings({"unused", "ConstantConditions"})
private static class TestRestController { private static class TestRestController {
public Mono<Void> handleToMonoVoid() { return null;} public Mono<Void> handleToMonoVoid() { return null;}
@ -200,7 +201,7 @@ public class ResponseBodyResultHandlerTests {
@Controller @Controller
@SuppressWarnings("unused") @SuppressWarnings({"unused", "ConstantConditions"})
private static class TestController { private static class TestController {
@ResponseBody @ResponseBody

View File

@ -21,6 +21,7 @@ import java.io.OutputStream;
import java.io.Serializable; import java.io.Serializable;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
@ -130,7 +131,7 @@ public class RequestResponseBodyMethodProcessorTests {
@Test @Test
public void resolveArgumentParameterizedType() throws Exception { public void resolveArgumentParameterizedType() throws Exception {
String content = "[{\"name\" : \"Jad\"}, {\"name\" : \"Robert\"}]"; String content = "[{\"name\" : \"Jad\"}, {\"name\" : \"Robert\"}]";
this.servletRequest.setContent(content.getBytes("UTF-8")); this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE); this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
List<HttpMessageConverter<?>> converters = new ArrayList<>(); List<HttpMessageConverter<?>> converters = new ArrayList<>();
@ -150,7 +151,7 @@ public class RequestResponseBodyMethodProcessorTests {
public void resolveArgumentRawTypeFromParameterizedType() throws Exception { public void resolveArgumentRawTypeFromParameterizedType() throws Exception {
String content = "fruit=apple&vegetable=kale"; String content = "fruit=apple&vegetable=kale";
this.servletRequest.setMethod("GET"); this.servletRequest.setMethod("GET");
this.servletRequest.setContent(content.getBytes("UTF-8")); this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
this.servletRequest.setContentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE); this.servletRequest.setContentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE);
List<HttpMessageConverter<?>> converters = new ArrayList<>(); List<HttpMessageConverter<?>> converters = new ArrayList<>();
@ -169,7 +170,7 @@ public class RequestResponseBodyMethodProcessorTests {
@Test @Test
public void resolveArgumentClassJson() throws Exception { public void resolveArgumentClassJson() throws Exception {
String content = "{\"name\" : \"Jad\"}"; String content = "{\"name\" : \"Jad\"}";
this.servletRequest.setContent(content.getBytes("UTF-8")); this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
this.servletRequest.setContentType("application/json"); this.servletRequest.setContentType("application/json");
List<HttpMessageConverter<?>> converters = new ArrayList<>(); List<HttpMessageConverter<?>> converters = new ArrayList<>();
@ -186,7 +187,7 @@ public class RequestResponseBodyMethodProcessorTests {
@Test @Test
public void resolveArgumentClassString() throws Exception { public void resolveArgumentClassString() throws Exception {
String content = "foobarbaz"; String content = "foobarbaz";
this.servletRequest.setContent(content.getBytes("UTF-8")); this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
this.servletRequest.setContentType("application/json"); this.servletRequest.setContentType("application/json");
List<HttpMessageConverter<?>> converters = new ArrayList<>(); List<HttpMessageConverter<?>> converters = new ArrayList<>();
@ -201,7 +202,7 @@ public class RequestResponseBodyMethodProcessorTests {
} }
@Test // SPR-9942 @Test // SPR-9942
public void resolveArgumentRequiredNoContent() throws Exception { public void resolveArgumentRequiredNoContent() {
this.servletRequest.setContent(new byte[0]); this.servletRequest.setContent(new byte[0]);
this.servletRequest.setContentType("text/plain"); this.servletRequest.setContentType("text/plain");
List<HttpMessageConverter<?>> converters = new ArrayList<>(); List<HttpMessageConverter<?>> converters = new ArrayList<>();
@ -230,7 +231,7 @@ public class RequestResponseBodyMethodProcessorTests {
MethodParameter methodParam = handlerMethod.getMethodParameters()[0]; MethodParameter methodParam = handlerMethod.getMethodParameters()[0];
String content = "{\"name\" : \"Jad\"}"; String content = "{\"name\" : \"Jad\"}";
this.servletRequest.setContent(content.getBytes("UTF-8")); this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE); this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
List<HttpMessageConverter<?>> converters = new ArrayList<>(); List<HttpMessageConverter<?>> converters = new ArrayList<>();
@ -250,7 +251,7 @@ public class RequestResponseBodyMethodProcessorTests {
MethodParameter methodParam = handlerMethod.getMethodParameters()[0]; MethodParameter methodParam = handlerMethod.getMethodParameters()[0];
String content = "[{\"name\" : \"Jad\"}, {\"name\" : \"Robert\"}]"; String content = "[{\"name\" : \"Jad\"}, {\"name\" : \"Robert\"}]";
this.servletRequest.setContent(content.getBytes("UTF-8")); this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE); this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
List<HttpMessageConverter<?>> converters = new ArrayList<>(); List<HttpMessageConverter<?>> converters = new ArrayList<>();
@ -273,7 +274,7 @@ public class RequestResponseBodyMethodProcessorTests {
MethodParameter methodParam = handlerMethod.getMethodParameters()[0]; MethodParameter methodParam = handlerMethod.getMethodParameters()[0];
String content = "{\"name\" : \"Jad\"}"; String content = "{\"name\" : \"Jad\"}";
this.servletRequest.setContent(content.getBytes("UTF-8")); this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE); this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
List<HttpMessageConverter<?>> converters = new ArrayList<>(); List<HttpMessageConverter<?>> converters = new ArrayList<>();
@ -609,7 +610,7 @@ public class RequestResponseBodyMethodProcessorTests {
@Test // SPR-12501 @Test // SPR-12501
public void resolveArgumentWithJacksonJsonView() throws Exception { public void resolveArgumentWithJacksonJsonView() throws Exception {
String content = "{\"withView1\" : \"with\", \"withView2\" : \"with\", \"withoutView\" : \"without\"}"; String content = "{\"withView1\" : \"with\", \"withView2\" : \"with\", \"withoutView\" : \"without\"}";
this.servletRequest.setContent(content.getBytes("UTF-8")); this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE); this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
Method method = JacksonController.class.getMethod("handleRequestBody", JacksonViewBean.class); Method method = JacksonController.class.getMethod("handleRequestBody", JacksonViewBean.class);
@ -634,7 +635,7 @@ public class RequestResponseBodyMethodProcessorTests {
@Test // SPR-12501 @Test // SPR-12501
public void resolveHttpEntityArgumentWithJacksonJsonView() throws Exception { public void resolveHttpEntityArgumentWithJacksonJsonView() throws Exception {
String content = "{\"withView1\" : \"with\", \"withView2\" : \"with\", \"withoutView\" : \"without\"}"; String content = "{\"withView1\" : \"with\", \"withView2\" : \"with\", \"withoutView\" : \"without\"}";
this.servletRequest.setContent(content.getBytes("UTF-8")); this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE); this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
Method method = JacksonController.class.getMethod("handleHttpEntity", HttpEntity.class); Method method = JacksonController.class.getMethod("handleHttpEntity", HttpEntity.class);
@ -664,7 +665,7 @@ public class RequestResponseBodyMethodProcessorTests {
"<withView1>with</withView1>" + "<withView1>with</withView1>" +
"<withView2>with</withView2>" + "<withView2>with</withView2>" +
"<withoutView>without</withoutView></root>"; "<withoutView>without</withoutView></root>";
this.servletRequest.setContent(content.getBytes("UTF-8")); this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
this.servletRequest.setContentType(MediaType.APPLICATION_XML_VALUE); this.servletRequest.setContentType(MediaType.APPLICATION_XML_VALUE);
Method method = JacksonController.class.getMethod("handleRequestBody", JacksonViewBean.class); Method method = JacksonController.class.getMethod("handleRequestBody", JacksonViewBean.class);
@ -692,7 +693,7 @@ public class RequestResponseBodyMethodProcessorTests {
"<withView1>with</withView1>" + "<withView1>with</withView1>" +
"<withView2>with</withView2>" + "<withView2>with</withView2>" +
"<withoutView>without</withoutView></root>"; "<withoutView>without</withoutView></root>";
this.servletRequest.setContent(content.getBytes("UTF-8")); this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
this.servletRequest.setContentType(MediaType.APPLICATION_XML_VALUE); this.servletRequest.setContentType(MediaType.APPLICATION_XML_VALUE);
Method method = JacksonController.class.getMethod("handleHttpEntity", HttpEntity.class); Method method = JacksonController.class.getMethod("handleHttpEntity", HttpEntity.class);
@ -774,7 +775,7 @@ public class RequestResponseBodyMethodProcessorTests {
@Test // SPR-14520 @Test // SPR-14520
public void resolveArgumentTypeVariableWithGenericInterface() throws Exception { public void resolveArgumentTypeVariableWithGenericInterface() throws Exception {
this.servletRequest.setContent("\"foo\"".getBytes("UTF-8")); this.servletRequest.setContent("\"foo\"".getBytes(StandardCharsets.UTF_8));
this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE); this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
Method method = MyControllerImplementingInterface.class.getMethod("handle", Object.class); Method method = MyControllerImplementingInterface.class.getMethod("handle", Object.class);
@ -794,7 +795,7 @@ public class RequestResponseBodyMethodProcessorTests {
@Test // gh-24127 @Test // gh-24127
public void resolveArgumentTypeVariableWithGenericInterfaceAndSubclass() throws Exception { public void resolveArgumentTypeVariableWithGenericInterfaceAndSubclass() throws Exception {
this.servletRequest.setContent("\"foo\"".getBytes("UTF-8")); this.servletRequest.setContent("\"foo\"".getBytes(StandardCharsets.UTF_8));
this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE); this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
Method method = SubControllerImplementingInterface.class.getMethod("handle", Object.class); Method method = SubControllerImplementingInterface.class.getMethod("handle", Object.class);
@ -836,6 +837,7 @@ public class RequestResponseBodyMethodProcessorTests {
} }
@SuppressWarnings("ConstantConditions")
String handle( String handle(
@RequestBody List<SimpleBean> list, @RequestBody List<SimpleBean> list,
@RequestBody SimpleBean simpleBean, @RequestBody SimpleBean simpleBean,
@ -845,19 +847,23 @@ public class RequestResponseBodyMethodProcessorTests {
return null; return null;
} }
@SuppressWarnings("ConstantConditions")
Resource getImage() { Resource getImage() {
return null; return null;
} }
@SuppressWarnings("ConstantConditions")
ProblemDetail handleAndReturnProblemDetail() { ProblemDetail handleAndReturnProblemDetail() {
return null; return null;
} }
@SuppressWarnings("ConstantConditions")
@RequestMapping @RequestMapping
OutputStream handleAndReturnOutputStream() { OutputStream handleAndReturnOutputStream() {
return null; return null;
} }
@SuppressWarnings("ConstantConditions")
SimpleBean getSimpleBean() { SimpleBean getSimpleBean() {
return null; return null;
} }
@ -895,7 +901,7 @@ public class RequestResponseBodyMethodProcessorTests {
} }
@SuppressWarnings({ "serial" }) @SuppressWarnings("NotNullFieldNotInitialized")
private static class SimpleBean implements Identifiable { private static class SimpleBean implements Identifiable {
private Long id; private Long id;
@ -922,7 +928,7 @@ public class RequestResponseBodyMethodProcessorTests {
} }
private final class ValidatingBinderFactory implements WebDataBinderFactory { private static final class ValidatingBinderFactory implements WebDataBinderFactory {
@Override @Override
public WebDataBinder createBinder(NativeWebRequest request, @Nullable Object target, String objectName) { public WebDataBinder createBinder(NativeWebRequest request, @Nullable Object target, String objectName) {
@ -943,6 +949,7 @@ public class RequestResponseBodyMethodProcessorTests {
return "hello"; return "hello";
} }
@SuppressWarnings("ConstantConditions")
@RequestMapping @RequestMapping
public CharSequence handleWithCharSequence() { public CharSequence handleWithCharSequence() {
return null; return null;
@ -965,6 +972,7 @@ public class RequestResponseBodyMethodProcessorTests {
private interface MyJacksonView2 {} private interface MyJacksonView2 {}
@SuppressWarnings("NotNullFieldNotInitialized")
private static class JacksonViewBean { private static class JacksonViewBean {
@JsonView(MyJacksonView1.class) @JsonView(MyJacksonView1.class)
@ -983,6 +991,7 @@ public class RequestResponseBodyMethodProcessorTests {
this.withView1 = withView1; this.withView1 = withView1;
} }
@Nullable
public String getWithView2() { public String getWithView2() {
return withView2; return withView2;
} }
@ -991,6 +1000,7 @@ public class RequestResponseBodyMethodProcessorTests {
this.withView2 = withView2; this.withView2 = withView2;
} }
@Nullable
public String getWithoutView() { public String getWithoutView() {
return withoutView; return withoutView;
} }
@ -1001,7 +1011,8 @@ public class RequestResponseBodyMethodProcessorTests {
} }
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type") @SuppressWarnings("NotNullFieldNotInitialized")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
public static class ParentClass { public static class ParentClass {
private String parentProperty; private String parentProperty;
@ -1092,6 +1103,7 @@ public class RequestResponseBodyMethodProcessorTests {
return bean; return bean;
} }
@SuppressWarnings("ConstantConditions")
@RequestMapping @RequestMapping
@ResponseBody @ResponseBody
public JacksonViewBean handleHttpEntity(@JsonView(MyJacksonView1.class) HttpEntity<JacksonViewBean> entity) { public JacksonViewBean handleHttpEntity(@JsonView(MyJacksonView1.class) HttpEntity<JacksonViewBean> entity) {