Skip to content

Commit

Permalink
Find ResponseContentType annotations with overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
oldergod committed Jan 30, 2025
1 parent dfadd3f commit 754f54f
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 3 deletions.
5 changes: 3 additions & 2 deletions misk/src/main/kotlin/misk/Actions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package misk
import misk.web.DispatchMechanism
import misk.web.RequestContentType
import misk.web.ResponseContentType
import misk.web.actions.findAnnotationWithOverrides
import misk.web.mediatype.MediaRange
import misk.web.mediatype.MediaTypes
import okhttp3.MediaType
Expand Down Expand Up @@ -49,7 +50,7 @@ fun KFunction<*>.asAction(
acceptedMediaRanges = acceptedMediaRange,
responseContentType = when (dispatchMechanism) {
DispatchMechanism.GRPC -> {
require(findAnnotation<ResponseContentType>() == null) {
require(findAnnotationWithOverrides<ResponseContentType>() == null) {
"@Grpc cannot be used with @ResponseContentType on $this"
}
MediaTypes.APPLICATION_GRPC_MEDIA_TYPE
Expand All @@ -64,4 +65,4 @@ fun KFunction<*>.asAction(
}

private fun KFunction<*>.singleOrNullResponseMediaType() =
findAnnotation<ResponseContentType>()?.value?.singleOrNull()?.toMediaTypeOrNull()
findAnnotationWithOverrides<ResponseContentType>()?.value?.singleOrNull()?.toMediaTypeOrNull()
2 changes: 1 addition & 1 deletion misk/src/main/kotlin/misk/web/actions/WebActionFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ internal class WebActionFactory @Inject constructor(
pathPattern: String,
dispatchMechanism: DispatchMechanism
) {
val responseContentTypes = function.findAnnotation<ResponseContentType>()
val responseContentTypes = function.findAnnotationWithOverrides<ResponseContentType>()
?.value
?.toList()
// We have to have an element in the list to be able to flatMap over it below.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package misk.web.ssl

import com.google.inject.Guice
import com.google.inject.Provides
import jakarta.inject.Inject
import jakarta.inject.Singleton
import misk.MiskTestingServiceModule
import misk.client.HttpClientEndpointConfig
import misk.client.HttpClientModule
import misk.client.HttpClientsConfig
import misk.inject.KAbstractModule
import misk.inject.getInstance
import misk.testing.MiskTest
import misk.testing.MiskTestModule
import misk.web.Get
import misk.web.ResponseContentType
import misk.web.WebActionModule
import misk.web.WebServerTestingModule
import misk.web.actions.WebAction
import misk.web.jetty.JettyService
import misk.web.mediatype.MediaTypes
import okhttp3.OkHttpClient
import okhttp3.Request
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test

@MiskTest(startService = true)
class WebActionInterfaceFunctionsTest {
@MiskTestModule
val module = TestModule()

@Inject private lateinit var jetty: JettyService

private lateinit var client: OkHttpClient

@BeforeEach
fun createClient() {
val clientInjector = Guice.createInjector(ClientModule(jetty))
client = clientInjector.getInstance()
}

@Test
fun happyPath() {
val call = client.newCall(
Request.Builder()
.url(jetty.httpServerUrl.resolve("/hello")!!)
.build()
)
call.execute().use { response ->
assertThat(response.body.string()).isEqualTo("hello")
}
}

@Test
fun actionAnnotationsOnInterfaceFunction() {
val call = client.newCall(
Request.Builder()
.url(jetty.httpServerUrl.resolve("/greet")!!)
.build()
)
call.execute().use { response ->
assertThat(response.body.string()).isEqualTo("greetings")
}
}

class HelloAction @Inject constructor() : WebAction {
@Get("/hello")
@ResponseContentType(MediaTypes.TEXT_PLAIN_UTF8)
fun sayHello() = "hello"
}

interface GreetingAction : WebAction {
@Get("/greet")
@ResponseContentType(MediaTypes.TEXT_PLAIN_UTF8)
fun greet(): String
}

class RealGreetingAction @Inject constructor() : GreetingAction {
override fun greet() = "greetings"
}

class TestModule : KAbstractModule() {
override fun configure() {
install(
WebServerTestingModule(
webConfig = WebServerTestingModule.TESTING_WEB_CONFIG
)
)
install(MiskTestingServiceModule())
install(WebActionModule.create<HelloAction>())
install(WebActionModule.create<RealGreetingAction>())
}
}

// NB: The server doesn't get a port until after it starts so we create the client module
// _after_ we start the services
class ClientModule(val jetty: JettyService) : KAbstractModule() {
override fun configure() {
install(MiskTestingServiceModule())
install(HttpClientModule("default"))
}

@Provides
@Singleton
fun provideHttpClientsConfig(): HttpClientsConfig {
return HttpClientsConfig(
endpoints = mapOf(
"default" to HttpClientEndpointConfig(
url = "http://example.com/",
)
)
)
}
}
}

0 comments on commit 754f54f

Please sign in to comment.