From fb6675f19eb6c09c77e05c34aa7a7786692e06b6 Mon Sep 17 00:00:00 2001 From: Sergey Mashkov Date: Tue, 26 Jan 2021 13:59:15 +0300 Subject: [PATCH] Revert "KTOR-1684 Deprecate locationOrNull" This reverts commit 89c627ac87e738994a7afcad4585461aa4755da6. --- .../ktor-locations/api/ktor-locations.api | 1 - .../jvm/src/io/ktor/locations/Location.kt | 20 ++------------ .../io/ktor/tests/locations/LocationsTest.kt | 26 ------------------- 3 files changed, 2 insertions(+), 45 deletions(-) diff --git a/ktor-features/ktor-locations/api/ktor-locations.api b/ktor-features/ktor-locations/api/ktor-locations.api index 72ee767dfa0..22b30e063d6 100644 --- a/ktor-features/ktor-locations/api/ktor-locations.api +++ b/ktor-features/ktor-locations/api/ktor-locations.api @@ -38,7 +38,6 @@ public final class io/ktor/locations/LocationKt { public static final fun href (Lio/ktor/util/pipeline/PipelineContext;Ljava/lang/Object;)Ljava/lang/String; public static final fun location (Lio/ktor/routing/Route;Lkotlin/reflect/KClass;Lkotlin/jvm/functions/Function1;)Lio/ktor/routing/Route; public static final fun locationOrNull (Lio/ktor/application/ApplicationCall;Lkotlin/reflect/KClass;)Ljava/lang/Object; - public static final fun locationOrThrow (Lio/ktor/application/ApplicationCall;Lkotlin/reflect/KClass;)Ljava/lang/Object; } public abstract class io/ktor/locations/LocationPropertyInfo { diff --git a/ktor-features/ktor-locations/jvm/src/io/ktor/locations/Location.kt b/ktor-features/ktor-locations/jvm/src/io/ktor/locations/Location.kt index 4e4f8a25767..d1e019d38de 100644 --- a/ktor-features/ktor-locations/jvm/src/io/ktor/locations/Location.kt +++ b/ktor-features/ktor-locations/jvm/src/io/ktor/locations/Location.kt @@ -228,21 +228,11 @@ public fun Route.handle(dataClass: KClass, body: suspend PipelineCo } /** - * Retrieves the current call's location or fails if it is not available (request is not handled by a location class), - * or not yet available (invoked too early before the locations feature takes place). - * - * Despite of the name, the function fails if no location found. This is why it's deprecated. - */ -@KtorExperimentalLocationsAPI -@Deprecated("Use location function instead.", ReplaceWith("location()")) -public inline fun ApplicationCall.locationOrNull(): T = location() - -/** - * Retrieves the current call's location or fails if it is not available (request is not handled by a location class), + * Retrieves the current call's location or `null` if it is not available (request is not handled by a location class), * or not yet available (invoked too early before the locations feature takes place). */ @KtorExperimentalLocationsAPI -public inline fun ApplicationCall.location(): T = locationOrThrow(T::class) +public inline fun ApplicationCall.locationOrNull(): T = locationOrNull(T::class) @PublishedApi internal fun ApplicationCall.locationOrNull(type: KClass): T = @@ -250,12 +240,6 @@ internal fun ApplicationCall.locationOrNull(type: KClass): T = type.cast(instance) } ?: error("Location instance is not available for this call.)") -@PublishedApi -internal fun ApplicationCall.locationOrThrow(type: KClass): T = - attributes.getOrNull(LocationInstanceKey)?.let { instance -> - type.cast(instance) - } ?: error("Location instance is not available for this call.)") - private val LocationInstanceKey = AttributeKey("LocationInstance") private fun KClass.cast(instance: Any): T { diff --git a/ktor-features/ktor-locations/jvm/test/io/ktor/tests/locations/LocationsTest.kt b/ktor-features/ktor-locations/jvm/test/io/ktor/tests/locations/LocationsTest.kt index 2c67d70107d..aa087d2f449 100644 --- a/ktor-features/ktor-locations/jvm/test/io/ktor/tests/locations/LocationsTest.kt +++ b/ktor-features/ktor-locations/jvm/test/io/ktor/tests/locations/LocationsTest.kt @@ -499,31 +499,5 @@ class LocationsTest { assertEquals(HttpStatusCode.BadRequest, call.response.status()) } } - - @Test - @Suppress("DEPRECATION") - fun testLocationOrNull() { - withLocationsApplication { - application.routing { - get { index -> - assertSame(index, call.locationOrNull()) - assertSame(index, call.location()) - call.respondText("OK") - } - get("/no-location") { - assertFails { - assertNull(call.locationOrNull()) - } - assertFails { - assertNull(call.location()) - } - call.respondText("OK") - } - } - - urlShouldBeHandled("/", "OK") - urlShouldBeHandled("/no-location", "OK") - } - } }