Refine ModelAttributeMethodProcessor Kotlin exception handling

This commit refines ModelAttributeMethodProcessor Kotlin exception
handling in order to throw a proper MethodArgumentNotValidException
instead of a NullPointerException when Kotlin null-safety constraints
are not fulfilled, translating to an HTTP error with 400 status code
(Bad Request) instead of 500 (Internal Server Error).

Closes gh-23846
This commit is contained in:
Sébastien Deleuze
2023-02-14 14:07:41 +01:00
parent 979118c1eb
commit 569df6eecc
3 changed files with 135 additions and 6 deletions

View File

@@ -0,0 +1,87 @@
/*
* Copyright 2002-2023 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.method.annotation
import org.assertj.core.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.mockito.ArgumentMatchers.*
import org.mockito.BDDMockito
import org.mockito.Mockito
import org.springframework.core.MethodParameter
import org.springframework.core.annotation.SynthesizingMethodParameter
import org.springframework.web.bind.MethodArgumentNotValidException
import org.springframework.web.bind.support.WebDataBinderFactory
import org.springframework.web.bind.support.WebRequestDataBinder
import org.springframework.web.context.request.ServletWebRequest
import org.springframework.web.method.support.ModelAndViewContainer
import org.springframework.web.testfixture.servlet.MockHttpServletRequest
import kotlin.annotation.AnnotationTarget.*
/**
* Kotlin test fixture for [ModelAttributeMethodProcessor].
*
* @author Sebastien Deleuze
*/
class ModelAttributeMethodProcessorKotlinTests {
private lateinit var container: ModelAndViewContainer
private lateinit var processor: ModelAttributeMethodProcessor
private lateinit var param: MethodParameter
@BeforeEach
fun setup() {
container = ModelAndViewContainer()
processor = ModelAttributeMethodProcessor(false)
var method = ModelAttributeHandler::class.java.getDeclaredMethod("test",Param::class.java)
param = SynthesizingMethodParameter(method, 0)
}
@Test
fun resolveArgumentWithValue() {
val mockRequest = MockHttpServletRequest().apply { addParameter("a", "b") }
val requestWithParam = ServletWebRequest(mockRequest)
val factory = Mockito.mock<WebDataBinderFactory>()
BDDMockito.given(factory.createBinder(any(), any(), eq("param")))
.willAnswer { WebRequestDataBinder(it.getArgument(1)) }
Assertions.assertThat(processor.resolveArgument(this.param, container, requestWithParam, factory)).isEqualTo(Param("b"))
}
@Test
fun throwMethodArgumentNotValidExceptionWithNull() {
val mockRequest = MockHttpServletRequest().apply { addParameter("a", null) }
val requestWithParam = ServletWebRequest(mockRequest)
val factory = Mockito.mock<WebDataBinderFactory>()
BDDMockito.given(factory.createBinder(any(), any(), eq("param")))
.willAnswer { WebRequestDataBinder(it.getArgument(1)) }
Assertions.assertThatThrownBy {
processor.resolveArgument(this.param, container, requestWithParam, factory)
}.isInstanceOf(MethodArgumentNotValidException::class.java)
.hasMessageContaining("parameter a")
}
private data class Param(val a: String)
@Suppress("UNUSED_PARAMETER")
private class ModelAttributeHandler {
fun test(param: Param) { }
}
}