Skip to content

Commit

Permalink
Create empty produces for spring endpoint if the return type is void …
Browse files Browse the repository at this point in the history
…or Unit (#50)
  • Loading branch information
cc-jhr committed Apr 9, 2020
1 parent ae5e268 commit b919793
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,18 @@ internal fun Map.Entry<RequestMappingInfo, HandlerMethod>.produces(): Set<String
?.jvmErasure
?.java

return when(returnType) {
java.lang.String::class.java -> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Endpoint> = 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)
}
}
}

0 comments on commit b919793

Please sign in to comment.