-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KSP: Fix interceptors with parameter defaults (#11103)
- Loading branch information
Showing
5 changed files
with
169 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...est/kotlin/io/micronaut/docs/server/defaults_intercepted/DefaultsInterceptedController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package io.micronaut.docs.server.defaults_intercepted | ||
|
||
import io.micronaut.context.annotation.Requires | ||
import io.micronaut.core.async.annotation.SingleResult | ||
import io.micronaut.http.HttpResponse | ||
import io.micronaut.http.MediaType | ||
import io.micronaut.http.annotation.Body | ||
import io.micronaut.http.annotation.Controller | ||
import io.micronaut.http.annotation.Header | ||
import io.micronaut.http.annotation.Post | ||
import jakarta.validation.constraints.NotBlank | ||
import jakarta.validation.constraints.NotNull | ||
import jakarta.validation.constraints.Size | ||
import org.reactivestreams.Publisher | ||
import reactor.core.publisher.Flux | ||
import spock.lang.Specification | ||
import java.time.OffsetDateTime | ||
import java.time.ZoneId | ||
|
||
@Requires(property = "spec.name", value = "defaults-intercepted") | ||
// tag::class[] | ||
@Controller("/defaults-intercepted") | ||
open class DefaultsInterceptedController(private val timeProvider: (ZoneId) -> OffsetDateTime = OffsetDateTime::now) { | ||
// end::class[] | ||
|
||
// tag::echo[] | ||
@Post(value = "/echo", consumes = [MediaType.TEXT_PLAIN]) // <1> | ||
@NotBlank | ||
open fun echo(@Size(max = 1024) @NotNull @Body text: String, @Header("MYHEADER") someHeader : String = "THEDEFAULT"): String { // <2> | ||
return someHeader // <3> | ||
} | ||
// end::echo[] | ||
|
||
// tag::echoReactive[] | ||
@Post(value = "/echo-publisher", consumes = [MediaType.TEXT_PLAIN]) // <1> | ||
@SingleResult | ||
open fun echoFlow(@Body text: Publisher<String>, @NotNull @Header("MYHEADER") someHeader : String = "THEDEFAULT"): Publisher<HttpResponse<String>> { //<2> | ||
return Flux.from(text) | ||
.collect({ StringBuffer() }, { obj, str -> obj.append(str) }) // <3> | ||
.map { HttpResponse.ok(someHeader) } | ||
} | ||
// end::echoReactive[] | ||
|
||
// tag::endclass[] | ||
} |
56 changes: 56 additions & 0 deletions
56
...kotlin/io/micronaut/docs/server/defaults_intercepted/DefaultsInterceptedControllerSpec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package io.micronaut.docs.server.defaults_intercepted | ||
|
||
import io.kotest.core.spec.style.StringSpec | ||
import io.kotest.matchers.shouldBe | ||
import io.micronaut.context.ApplicationContext | ||
import io.micronaut.http.HttpRequest | ||
import io.micronaut.http.MediaType | ||
import io.micronaut.http.client.HttpClient | ||
import io.micronaut.runtime.server.EmbeddedServer | ||
|
||
class DefaultsInterceptedControllerSpec : StringSpec() { | ||
|
||
val embeddedServer = autoClose( | ||
ApplicationContext.run(EmbeddedServer::class.java, mapOf("spec.name" to "defaults-intercepted")) | ||
) | ||
|
||
val client = autoClose( | ||
embeddedServer.applicationContext.createBean(HttpClient::class.java, embeddedServer.getURL()) | ||
) | ||
|
||
init { | ||
"test echo response"() { | ||
val response1 = client.toBlocking().retrieve( | ||
HttpRequest.POST("/defaults-intercepted/echo", "My Text") | ||
.header("MYHEADER", "abc123") | ||
.contentType(MediaType.TEXT_PLAIN_TYPE), String::class.java | ||
) | ||
|
||
response1 shouldBe "abc123" | ||
|
||
val response2 = client.toBlocking().retrieve( | ||
HttpRequest.POST("/defaults-intercepted/echo", "My Text") | ||
.contentType(MediaType.TEXT_PLAIN_TYPE), String::class.java | ||
) | ||
|
||
response2 shouldBe "THEDEFAULT" | ||
} | ||
|
||
"test echo reactive response"() { | ||
val response1 = client.toBlocking().retrieve( | ||
HttpRequest.POST("/defaults-intercepted/echo-publisher", "My Text") | ||
.header("MYHEADER", "abc123") | ||
.contentType(MediaType.TEXT_PLAIN_TYPE), String::class.java | ||
) | ||
|
||
response1 shouldBe "abc123" | ||
|
||
val response2 = client.toBlocking().retrieve( | ||
HttpRequest.POST("/defaults-intercepted/echo-publisher", "My Text") | ||
.contentType(MediaType.TEXT_PLAIN_TYPE), String::class.java | ||
) | ||
|
||
response2 shouldBe "THEDEFAULT" | ||
} | ||
} | ||
} |