From b9197933ed8ec114812eef7e48478629af2bf46d Mon Sep 17 00:00:00 2001 From: Jannes Heinrich Date: Thu, 9 Apr 2020 16:48:38 +0200 Subject: [PATCH] Create empty produces for spring endpoint if the return type is void or Unit (#50) --- .../extensions/ProducesSpringExtension.kt | 12 ++++++---- .../redirect/RedirectTestController.kt | 12 ++++++++++ .../redirect/SpringControllerRedirectTest.kt | 24 +++++++++++++++++++ 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/spring/src/main/kotlin/de/codecentric/hikaku/converters/spring/extensions/ProducesSpringExtension.kt b/spring/src/main/kotlin/de/codecentric/hikaku/converters/spring/extensions/ProducesSpringExtension.kt index fa05c143..efe9c551 100644 --- a/spring/src/main/kotlin/de/codecentric/hikaku/converters/spring/extensions/ProducesSpringExtension.kt +++ b/spring/src/main/kotlin/de/codecentric/hikaku/converters/spring/extensions/ProducesSpringExtension.kt @@ -44,14 +44,18 @@ internal fun Map.Entry.produces(): Set setOf(TEXT_PLAIN_VALUE) - RedirectView::class.java -> emptySet() + return when { + returnType == java.lang.String::class.java -> setOf(TEXT_PLAIN_VALUE) + returnType == String::class.java -> setOf(TEXT_PLAIN_VALUE) + returnType == RedirectView::class.java -> emptySet() + returnType != null && isVoid(returnType) -> emptySet() else -> setOf(APPLICATION_JSON_VALUE) } } -private fun Method.hasNoReturnType() = this.returnType.name == "void" || this.returnType.name == "java.lang.Void" +private fun Method.hasNoReturnType() = isVoid(this.returnType) + +private fun isVoid(returnType: Class<*>) = returnType.name == "void" || returnType.name == "java.lang.Void" || returnType.name == "kotlin.Unit" private fun HandlerMethod.providesRestControllerAnnotation() = this.method .kotlinFunction diff --git a/spring/src/test/kotlin/de/codecentric/hikaku/converters/spring/produces/redirect/RedirectTestController.kt b/spring/src/test/kotlin/de/codecentric/hikaku/converters/spring/produces/redirect/RedirectTestController.kt index 137ced25..7e390445 100644 --- a/spring/src/test/kotlin/de/codecentric/hikaku/converters/spring/produces/redirect/RedirectTestController.kt +++ b/spring/src/test/kotlin/de/codecentric/hikaku/converters/spring/produces/redirect/RedirectTestController.kt @@ -2,8 +2,10 @@ package de.codecentric.hikaku.converters.spring.produces.redirect import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.ResponseBody import org.springframework.web.bind.annotation.RestController import org.springframework.web.servlet.view.RedirectView +import javax.servlet.http.HttpServletResponse @SpringBootApplication open class DummyApp @@ -14,3 +16,13 @@ open class RedirectTestController { @GetMapping("/todos") fun todos(): RedirectView = RedirectView() } + +@RestController +open class RedirectUsingHttpServletResponseTestController { + + @GetMapping("/todos") + @ResponseBody + fun getTest(response: HttpServletResponse) { + response.sendRedirect("http://localhost:8080/other") + } +} diff --git a/spring/src/test/kotlin/de/codecentric/hikaku/converters/spring/produces/redirect/SpringControllerRedirectTest.kt b/spring/src/test/kotlin/de/codecentric/hikaku/converters/spring/produces/redirect/SpringControllerRedirectTest.kt index 81c0782d..266460fa 100644 --- a/spring/src/test/kotlin/de/codecentric/hikaku/converters/spring/produces/redirect/SpringControllerRedirectTest.kt +++ b/spring/src/test/kotlin/de/codecentric/hikaku/converters/spring/produces/redirect/SpringControllerRedirectTest.kt @@ -38,4 +38,28 @@ class SpringControllerRedirectTest { assertThat(implementation.conversionResult).containsExactlyInAnyOrderElementsOf(specification) } } + + @Nested + @WebMvcTest(RedirectUsingHttpServletResponseTestController::class, excludeAutoConfiguration = [ErrorMvcAutoConfiguration::class]) + inner class RedirectUsingHttpServletResponseTest { + + @Autowired + lateinit var context: ConfigurableApplicationContext + + @Test + fun `produces not set if the return type is RedirectView`() { + //given + val specification: Set = setOf( + Endpoint("/todos", GET, produces = emptySet()), + Endpoint("/todos", HEAD, produces = emptySet()), + Endpoint("/todos", OPTIONS, produces = emptySet()) + ) + + //when + val implementation = SpringConverter(context) + + //then + assertThat(implementation.conversionResult).containsExactlyInAnyOrderElementsOf(specification) + } + } }