Skip to content

Commit

Permalink
Fixed: Response media type is not extracted for @RestController annot…
Browse files Browse the repository at this point in the history
…ation (closes #22)
  • Loading branch information
cc-jhr committed Mar 8, 2019
1 parent 1292969 commit 55510e8
Show file tree
Hide file tree
Showing 12 changed files with 2,295 additions and 204 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ subprojects {
apply plugin: 'kotlin'
apply plugin: 'java-library'

version = '1.1.0-SNAPSHOT'
version = '1.1.1-SNAPSHOT'
sourceCompatibility = jvmVersion
targetCompatibility = jvmVersion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package de.codecentric.hikaku.converter.spring.extensions
import org.springframework.http.MediaType.APPLICATION_JSON_UTF8_VALUE
import org.springframework.http.MediaType.TEXT_PLAIN_VALUE
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.method.HandlerMethod
import org.springframework.web.servlet.mvc.method.RequestMappingInfo
import kotlin.reflect.full.findAnnotation
Expand All @@ -11,22 +12,9 @@ import kotlin.reflect.jvm.jvmErasure
import kotlin.reflect.jvm.kotlinFunction

internal fun Map.Entry<RequestMappingInfo, HandlerMethod>.produces(): Set<String> {
val isResponseBodyAnnotationOnClass = this.value
.method
.kotlinFunction
?.instanceParameter
?.type
?.jvmErasure
?.findAnnotation<ResponseBody>() != null

val isResponseBodyAnnotationOnFunction = this.value
.method
.kotlinFunction
?.findAnnotation<ResponseBody>() != null

val isErrorPath = this.key.patternsCondition.patterns.contains("/error")

if (!isErrorPath && !isResponseBodyAnnotationOnClass && !isResponseBodyAnnotationOnFunction) {
if (!isErrorPath && !this.value.providesResponseBodyAnnotation() && !this.value.providesRestControllerAnnotation()) {
return emptySet()
}

Expand All @@ -52,4 +40,25 @@ internal fun Map.Entry<RequestMappingInfo, HandlerMethod>.produces(): Set<String
} else {
setOf(APPLICATION_JSON_UTF8_VALUE)
}
}
}

private fun HandlerMethod.providesRestControllerAnnotation() = this.method
.kotlinFunction
?.instanceParameter
?.type
?.jvmErasure
?.findAnnotation<RestController>() != null

private fun HandlerMethod.providesResponseBodyAnnotation() = isResponseBodyAnnotationOnClass() || isResponseBodyAnnotationOnFunction()

private fun HandlerMethod.isResponseBodyAnnotationOnClass() = this.method
.kotlinFunction
?.instanceParameter
?.type
?.jvmErasure
?.findAnnotation<ResponseBody>() != null


private fun HandlerMethod.isResponseBodyAnnotationOnFunction() = this.method
.kotlinFunction
?.findAnnotation<ResponseBody>() != null
Loading

0 comments on commit 55510e8

Please sign in to comment.