Skip to content

Commit

Permalink
Polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed May 9, 2022
1 parent 78ab4d7 commit 8378af9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -60,7 +60,7 @@ public class ExchangeResult {
private static final Log logger = LogFactory.getLog(ExchangeResult.class);

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);


Expand Down
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -162,6 +162,7 @@ private HandlerResult getHandlerResult(Object controller, @Nullable Object retur
return new HandlerResult(handlerMethod, returnValue, handlerMethod.getReturnType());
}

@SuppressWarnings("SameParameterValue")
private void assertResponseBody(MockServerWebExchange exchange, @Nullable String responseBody) {
StepVerifier.create(exchange.getResponse().getBody())
.consumeNextWith(buf -> assertThat(buf.toString(UTF_8)).isEqualTo(responseBody))
Expand All @@ -171,7 +172,7 @@ private void assertResponseBody(MockServerWebExchange exchange, @Nullable String


@RestController
@SuppressWarnings("unused")
@SuppressWarnings({"unused", "ConstantConditions"})
private static class TestRestController {

public Mono<Void> handleToMonoVoid() { return null;}
Expand Down Expand Up @@ -200,7 +201,7 @@ public ProblemDetail handleToProblemDetail() {


@Controller
@SuppressWarnings("unused")
@SuppressWarnings({"unused", "ConstantConditions"})
private static class TestController {

@ResponseBody
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.Serializable;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -130,7 +131,7 @@ public void setup() throws Exception {
@Test
public void resolveArgumentParameterizedType() throws Exception {
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);

List<HttpMessageConverter<?>> converters = new ArrayList<>();
Expand All @@ -150,7 +151,7 @@ public void resolveArgumentParameterizedType() throws Exception {
public void resolveArgumentRawTypeFromParameterizedType() throws Exception {
String content = "fruit=apple&vegetable=kale";
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);

List<HttpMessageConverter<?>> converters = new ArrayList<>();
Expand All @@ -169,7 +170,7 @@ public void resolveArgumentRawTypeFromParameterizedType() throws Exception {
@Test
public void resolveArgumentClassJson() throws Exception {
String content = "{\"name\" : \"Jad\"}";
this.servletRequest.setContent(content.getBytes("UTF-8"));
this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
this.servletRequest.setContentType("application/json");

List<HttpMessageConverter<?>> converters = new ArrayList<>();
Expand All @@ -186,7 +187,7 @@ public void resolveArgumentClassJson() throws Exception {
@Test
public void resolveArgumentClassString() throws Exception {
String content = "foobarbaz";
this.servletRequest.setContent(content.getBytes("UTF-8"));
this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
this.servletRequest.setContentType("application/json");

List<HttpMessageConverter<?>> converters = new ArrayList<>();
Expand All @@ -201,7 +202,7 @@ public void resolveArgumentClassString() throws Exception {
}

@Test // SPR-9942
public void resolveArgumentRequiredNoContent() throws Exception {
public void resolveArgumentRequiredNoContent() {
this.servletRequest.setContent(new byte[0]);
this.servletRequest.setContentType("text/plain");
List<HttpMessageConverter<?>> converters = new ArrayList<>();
Expand Down Expand Up @@ -230,7 +231,7 @@ public void resolveArgumentTypeVariable() throws Exception {
MethodParameter methodParam = handlerMethod.getMethodParameters()[0];

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);

List<HttpMessageConverter<?>> converters = new ArrayList<>();
Expand All @@ -250,7 +251,7 @@ public void resolveParameterizedWithTypeVariableArgument() throws Exception {
MethodParameter methodParam = handlerMethod.getMethodParameters()[0];

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);

List<HttpMessageConverter<?>> converters = new ArrayList<>();
Expand All @@ -273,7 +274,7 @@ public void resolveArgumentTypeVariableWithNonGenericConverter() throws Exceptio
MethodParameter methodParam = handlerMethod.getMethodParameters()[0];

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);

List<HttpMessageConverter<?>> converters = new ArrayList<>();
Expand Down Expand Up @@ -609,7 +610,7 @@ public void jacksonJsonViewWithResponseEntityAndXmlMessageConverter() throws Exc
@Test // SPR-12501
public void resolveArgumentWithJacksonJsonView() throws Exception {
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);

Method method = JacksonController.class.getMethod("handleRequestBody", JacksonViewBean.class);
Expand All @@ -634,7 +635,7 @@ public void resolveArgumentWithJacksonJsonView() throws Exception {
@Test // SPR-12501
public void resolveHttpEntityArgumentWithJacksonJsonView() throws Exception {
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);

Method method = JacksonController.class.getMethod("handleHttpEntity", HttpEntity.class);
Expand Down Expand Up @@ -664,7 +665,7 @@ public void resolveArgumentWithJacksonJsonViewAndXmlMessageConverter() throws Ex
"<withView1>with</withView1>" +
"<withView2>with</withView2>" +
"<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);

Method method = JacksonController.class.getMethod("handleRequestBody", JacksonViewBean.class);
Expand Down Expand Up @@ -692,7 +693,7 @@ public void resolveHttpEntityArgumentWithJacksonJsonViewAndXmlMessageConverter()
"<withView1>with</withView1>" +
"<withView2>with</withView2>" +
"<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);

Method method = JacksonController.class.getMethod("handleHttpEntity", HttpEntity.class);
Expand Down Expand Up @@ -774,7 +775,7 @@ public void jacksonSubTypeList() throws Exception {

@Test // SPR-14520
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);

Method method = MyControllerImplementingInterface.class.getMethod("handle", Object.class);
Expand All @@ -794,7 +795,7 @@ public void resolveArgumentTypeVariableWithGenericInterface() throws Exception {

@Test // gh-24127
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);

Method method = SubControllerImplementingInterface.class.getMethod("handle", Object.class);
Expand Down Expand Up @@ -836,6 +837,7 @@ private void assertContentDisposition(RequestResponseBodyMethodProcessor process
}


@SuppressWarnings("ConstantConditions")
String handle(
@RequestBody List<SimpleBean> list,
@RequestBody SimpleBean simpleBean,
Expand All @@ -845,19 +847,23 @@ String handle(
return null;
}

@SuppressWarnings("ConstantConditions")
Resource getImage() {
return null;
}

@SuppressWarnings("ConstantConditions")
ProblemDetail handleAndReturnProblemDetail() {
return null;
}

@SuppressWarnings("ConstantConditions")
@RequestMapping
OutputStream handleAndReturnOutputStream() {
return null;
}

@SuppressWarnings("ConstantConditions")
SimpleBean getSimpleBean() {
return null;
}
Expand Down Expand Up @@ -895,7 +901,7 @@ private static class MySimpleParameterizedControllerWithList extends MyParameter
}


@SuppressWarnings({ "serial" })
@SuppressWarnings("NotNullFieldNotInitialized")
private static class SimpleBean implements Identifiable {

private Long id;
Expand All @@ -922,7 +928,7 @@ public void setName(String name) {
}


private final class ValidatingBinderFactory implements WebDataBinderFactory {
private static final class ValidatingBinderFactory implements WebDataBinderFactory {

@Override
public WebDataBinder createBinder(NativeWebRequest request, @Nullable Object target, String objectName) {
Expand All @@ -943,6 +949,7 @@ public String handle() {
return "hello";
}

@SuppressWarnings("ConstantConditions")
@RequestMapping
public CharSequence handleWithCharSequence() {
return null;
Expand All @@ -965,6 +972,7 @@ private interface MyJacksonView1 {}
private interface MyJacksonView2 {}


@SuppressWarnings("NotNullFieldNotInitialized")
private static class JacksonViewBean {

@JsonView(MyJacksonView1.class)
Expand All @@ -983,6 +991,7 @@ public void setWithView1(String withView1) {
this.withView1 = withView1;
}

@Nullable
public String getWithView2() {
return withView2;
}
Expand All @@ -991,6 +1000,7 @@ public void setWithView2(String withView2) {
this.withView2 = withView2;
}

@Nullable
public String getWithoutView() {
return withoutView;
}
Expand All @@ -1001,7 +1011,8 @@ public void setWithoutView(String withoutView) {
}


@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@SuppressWarnings("NotNullFieldNotInitialized")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
public static class ParentClass {

private String parentProperty;
Expand Down Expand Up @@ -1092,6 +1103,7 @@ public JacksonViewBean handleRequestBody(@JsonView(MyJacksonView1.class) @Reques
return bean;
}

@SuppressWarnings("ConstantConditions")
@RequestMapping
@ResponseBody
public JacksonViewBean handleHttpEntity(@JsonView(MyJacksonView1.class) HttpEntity<JacksonViewBean> entity) {
Expand Down

0 comments on commit 8378af9

Please sign in to comment.