Treat Kotlin nullable as non-required

Where `isOptional` is used, also check for `isNullable` i.e.
values are not considered required if they are Kotlin nullables:
- spring-messaging: named value method arguments
- spring-web: named value method arguments
- spring-webmvc: request parts

This means that Kotlin client code no longer has to explicity specify
"required=false" for Kotlin nullables -- this information is inferred
automatically by the framework.

Issue: SPR-14165
This commit is contained in:
Raman Gupta
2016-09-08 14:06:01 -04:00
committed by Sebastien Deleuze
parent 735e288d46
commit fada91e538
7 changed files with 155 additions and 3 deletions

View File

@@ -0,0 +1,60 @@
package org.springframework.util
import org.junit.Before
import org.junit.Test
import java.lang.reflect.Method
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.springframework.core.MethodParameter
import org.springframework.util.KotlinUtils.*
/**
* Unit tests for [KotlinUtils].
* @author Raman Gupta
*/
class KotlinUtilsTests {
private lateinit var methodNullable: Method
private lateinit var methodNonNullable: Method
@Before
@Throws(NoSuchMethodException::class)
fun setUp() {
methodNullable = javaClass.getMethod("methodNullable", String::class.java, java.lang.Long.TYPE)
methodNonNullable = javaClass.getMethod("methodNonNullable", String::class.java, java.lang.Long.TYPE)
}
@Test
fun `Is kotlin present`() {
// we'd have to change the build to test the opposite
assertTrue(isKotlinPresent())
}
@Test
fun `Are kotlin classes detected`() {
assertFalse(isKotlinClass(null))
assertFalse(isKotlinClass(MethodParameter::class.java))
assertTrue(isKotlinClass(javaClass))
}
@Test
fun `Obtains method return type nullability`() {
assertTrue(isNullable(-1, methodNullable, null))
assertFalse(isNullable(-1, methodNonNullable, null))
}
@Test
fun `Obtains method parameter nullability`() {
assertTrue(isNullable(0, methodNullable, null))
assertFalse(isNullable(1, methodNullable, null))
}
@Suppress("unused", "unused_parameter")
fun methodNullable(p1: String?, p2: Long): Int? = 42
@Suppress("unused", "unused_parameter")
fun methodNonNullable(p1: String?, p2: Long): Int = 42
}