From 5c854b0c5883ab0b48e7502f2a06796332d45cea Mon Sep 17 00:00:00 2001 From: rsinukov Date: Mon, 2 Jan 2023 14:19:16 +0100 Subject: [PATCH] KTOR-5397 Make @Resource annotation @MetaSerializable --- .../client/plugins/resources/Resources.kt | 3 -- .../common/test/ResourcesTest.kt | 4 --- .../ktor/tests/resources/ResourcesTestJvm.kt | 1 - .../src/io/ktor/server/resources/Resources.kt | 3 -- .../io/ktor/tests/resources/ResourcesTest.kt | 32 ------------------- .../common/src/io/ktor/resources/Resource.kt | 5 +-- .../resources/PathPatternSerializationTest.kt | 10 ------ .../tests/resources/ResourceUrlBuilderTest.kt | 9 ------ 8 files changed, 1 insertion(+), 66 deletions(-) diff --git a/ktor-client/ktor-client-plugins/ktor-client-resources/common/src/io/ktor/client/plugins/resources/Resources.kt b/ktor-client/ktor-client-plugins/ktor-client-resources/common/src/io/ktor/client/plugins/resources/Resources.kt index 75806a865e..4d41495666 100644 --- a/ktor-client/ktor-client-plugins/ktor-client-resources/common/src/io/ktor/client/plugins/resources/Resources.kt +++ b/ktor-client/ktor-client-plugins/ktor-client-resources/common/src/io/ktor/client/plugins/resources/Resources.kt @@ -16,14 +16,11 @@ import io.ktor.resources.Resources as ResourcesCore * * Example: * ```kotlin - * @Serializable * @Resource("/users") * data class Users { - * @Serializable * @Resource("/{id}") * data class ById(val parent: Users = Users(), val id: Long) * - * @Serializable * @Resource("/add") * data class Add(val parent: Users = Users(), val name: String) * } diff --git a/ktor-client/ktor-client-plugins/ktor-client-resources/common/test/ResourcesTest.kt b/ktor-client/ktor-client-plugins/ktor-client-resources/common/test/ResourcesTest.kt index 6454635110..77b77f0eaf 100644 --- a/ktor-client/ktor-client-plugins/ktor-client-resources/common/test/ResourcesTest.kt +++ b/ktor-client/ktor-client-plugins/ktor-client-resources/common/test/ResourcesTest.kt @@ -15,10 +15,8 @@ import kotlin.test.* class ResourcesTest { - @Serializable @Resource("path/{id}/{method}") class Path(val id: Long, val method: String) { - @Serializable @Resource("child/{path?}") data class Child(val parent: Path, val path: String, val query: List) } @@ -108,7 +106,6 @@ class ResourcesTest { } } - @Serializable @Resource("path/{id}/{value?}") class PathWithDefault(val id: Boolean = true, val value: String? = null, val query1: Int?, val query2: Int? = 5) @@ -131,7 +128,6 @@ class ResourcesTest { } } - @Serializable @Resource("{path}") class ParametersEncoded(val path: String, val query: String, val queryList: List) diff --git a/ktor-server/ktor-server-plugins/ktor-server-resources/jvm/test/io/ktor/tests/resources/ResourcesTestJvm.kt b/ktor-server/ktor-server-plugins/ktor-server-resources/jvm/test/io/ktor/tests/resources/ResourcesTestJvm.kt index 991f462e7d..18a2aba3fb 100644 --- a/ktor-server/ktor-server-plugins/ktor-server-resources/jvm/test/io/ktor/tests/resources/ResourcesTestJvm.kt +++ b/ktor-server/ktor-server-plugins/ktor-server-resources/jvm/test/io/ktor/tests/resources/ResourcesTestJvm.kt @@ -31,7 +31,6 @@ class ResourcesTestJvm { } } - @Serializable @Resource("/") class LocationWithBigNumbers( @Serializable(with = BigDecimalSerializer::class) diff --git a/ktor-server/ktor-server-plugins/ktor-server-resources/jvmAndNix/src/io/ktor/server/resources/Resources.kt b/ktor-server/ktor-server-plugins/ktor-server-resources/jvmAndNix/src/io/ktor/server/resources/Resources.kt index b3bb3f76fc..60dacc4198 100644 --- a/ktor-server/ktor-server-plugins/ktor-server-resources/jvmAndNix/src/io/ktor/server/resources/Resources.kt +++ b/ktor-server/ktor-server-plugins/ktor-server-resources/jvmAndNix/src/io/ktor/server/resources/Resources.kt @@ -15,14 +15,11 @@ import io.ktor.resources.Resources as ResourcesCore * * Example: * ```kotlin - * @Serializable * @Resource("/users") * data class Users { - * @Serializable * @Resource("/{id}") * data class ById(val parent: Users = Users(), val id: Long) * - * @Serializable * @Resource("/add") * data class Add(val parent: Users = Users(), val name: String) * } diff --git a/ktor-server/ktor-server-plugins/ktor-server-resources/jvmAndNix/test/io/ktor/tests/resources/ResourcesTest.kt b/ktor-server/ktor-server-plugins/ktor-server-resources/jvmAndNix/test/io/ktor/tests/resources/ResourcesTest.kt index c8c8f53dd8..c770d05d98 100644 --- a/ktor-server/ktor-server-plugins/ktor-server-resources/jvmAndNix/test/io/ktor/tests/resources/ResourcesTest.kt +++ b/ktor-server/ktor-server-plugins/ktor-server-resources/jvmAndNix/test/io/ktor/tests/resources/ResourcesTest.kt @@ -29,7 +29,6 @@ internal fun withResourcesApplication(test: ApplicationTestBuilder.() -> Unit) = } class ResourcesTest { - @Serializable @Resource("/") class index @@ -47,7 +46,6 @@ class ResourcesTest { @Test fun resourceLocal() { - @Serializable @Resource("/") class indexLocal withResourcesApplication { @@ -61,7 +59,6 @@ class ResourcesTest { } } - @Serializable @Resource("/about") class about @@ -76,7 +73,6 @@ class ResourcesTest { urlShouldBeUnhandled("/about/123") } - @Serializable @Resource("/user/{id}") class user(val id: Int) @@ -93,7 +89,6 @@ class ResourcesTest { urlShouldBeUnhandled("/user?id=123") } - @Serializable @Resource("/user/{id}/{name}") class named(val id: Int, val name: String) @@ -111,7 +106,6 @@ class ResourcesTest { urlShouldBeUnhandled("/user/123") } - @Serializable @Resource("/favorite") class favorite(val id: Int) @@ -128,10 +122,8 @@ class ResourcesTest { urlShouldBeUnhandled("/favorite") } - @Serializable @Resource("/container/{id}") class pathContainer(val id: Int) { - @Serializable @Resource("/items") class items(val container: pathContainer) } @@ -150,10 +142,8 @@ class ResourcesTest { urlShouldBeUnhandled("/container/items?id=123") } - @Serializable @Resource("/container") class queryContainer(val id: Int) { - @Serializable @Resource("/items") class items(val container: queryContainer) } @@ -172,7 +162,6 @@ class ResourcesTest { urlShouldBeUnhandled("/container/123/items") } - @Serializable @Resource("/container") class optionalName(val id: Int, val optional: String? = null) @@ -190,7 +179,6 @@ class ResourcesTest { urlShouldBeUnhandled("/container/123") } - @Serializable @Resource("/container") class optionalIndex(val id: Int, val optional: Int = 42) @@ -222,10 +210,8 @@ class ResourcesTest { urlShouldBeUnhandled("/container/123") } - @Serializable @Resource("/container/{id?}") class optionalContainer(val id: Int? = null) { - @Serializable @Resource("/items") class items(val parent: optionalContainer, val optional: String? = null) } @@ -250,10 +236,8 @@ class ResourcesTest { ) } - @Serializable @Resource("/container") class simpleContainer { - @Serializable @Resource("/items") class items(val parent: simpleContainer) } @@ -273,7 +257,6 @@ class ResourcesTest { urlShouldBeUnhandled("/items") } - @Serializable @Resource("/container/{path...}") class tailCard(val path: List) @@ -289,11 +272,9 @@ class ResourcesTest { urlShouldBeHandled(tailCard(listOf("123", "items")), "/container/123/items") } - @Serializable @Resource("/") class multiquery(val value: List) - @Serializable @Resource("/") class multiquery2(val name: List) @@ -333,7 +314,6 @@ class ResourcesTest { urlShouldBeHandled(multiquery2(listOf("john, mary")), "2: /?name=john%2C+mary") } - @Serializable @Resource("/") class multiqueryWithDefault(val value: List = emptyList()) @@ -347,7 +327,6 @@ class ResourcesTest { urlShouldBeHandled(multiqueryWithDefault(listOf()), "/ []") } - @Serializable @Resource("/") class root @@ -362,7 +341,6 @@ class ResourcesTest { urlShouldBeUnhandled("/index") } - @Serializable @Resource("/help") class help @@ -377,14 +355,11 @@ class ResourcesTest { urlShouldBeUnhandled("/help/123") } - @Serializable @Resource("/users") class users { - @Serializable @Resource("/me") class me(val parent: users) - @Serializable @Resource("/{id}") class user(val parent: users, val id: Int) } @@ -408,7 +383,6 @@ class ResourcesTest { urlShouldBeUnhandled("/users/me") } - @Serializable @Resource("/items/{id}") class items @@ -423,11 +397,9 @@ class ResourcesTest { } } - @Serializable @Resource("/items/{itemId}/{extra?}") class OverlappingPath1(val itemId: Int, val extra: String?) - @Serializable @Resource("/items/{extra}") class OverlappingPath2(val extra: String) @@ -450,7 +422,6 @@ class ResourcesTest { A, B, C } - @Serializable @Resource("/") class resourceWithEnum(val e: resourceEnum) @@ -472,7 +443,6 @@ class ResourcesTest { @Test fun resourceParameterMismatchShouldLeadToBadRequestStatus() = withResourcesApplication { - @Serializable @Resource("/") data class L(val text: String, val number: Int, val longNumber: Long) @@ -506,7 +476,6 @@ class ResourcesTest { @Test fun resourceWithUInt() = withResourcesApplication { - @Serializable @Resource("/{id}/{valueParam}") data class Request(val id: UInt, val query: ULong, val valueParam: ValueClass, val valueQuery: ValueClass) @@ -522,7 +491,6 @@ class ResourcesTest { @Test fun resourceShouldReturnHttpMethodRouteObject() = withResourcesApplication { @Resource("/resource") - @Serializable class someResource routing { diff --git a/ktor-shared/ktor-resources/common/src/io/ktor/resources/Resource.kt b/ktor-shared/ktor-resources/common/src/io/ktor/resources/Resource.kt index 2b6dc91467..bbbbc28788 100644 --- a/ktor-shared/ktor-resources/common/src/io/ktor/resources/Resource.kt +++ b/ktor-shared/ktor-resources/common/src/io/ktor/resources/Resource.kt @@ -14,7 +14,6 @@ import kotlinx.serialization.* * Other properties are put into the URL query. * Example: * ``` - * @Serializable * @Resource("/users/{id}") * data class UserById(val id: Long, val properties: List) * @@ -27,14 +26,11 @@ import kotlinx.serialization.* * You can nest class for better organization, but all nested classes should have a property with an outer class type. * Example: * ```kotlin - * @Serializable * @Resource("/users") * data class Users { - * @Serializable * @Resource("/{id}") * data class ById(val parent: Users = Users(), val id: Long) * - * @Serializable * @Resource("/add") * data class Add(val parent: Users = Users(), val name: String) * } @@ -47,4 +43,5 @@ import kotlinx.serialization.* @OptIn(ExperimentalSerializationApi::class) @SerialInfo @Target(AnnotationTarget.CLASS, AnnotationTarget.TYPEALIAS) +@MetaSerializable public annotation class Resource(val path: String) diff --git a/ktor-shared/ktor-resources/common/test/io/ktor/tests/resources/PathPatternSerializationTest.kt b/ktor-shared/ktor-resources/common/test/io/ktor/tests/resources/PathPatternSerializationTest.kt index 42c00bf0b1..ea892ab29e 100644 --- a/ktor-shared/ktor-resources/common/test/io/ktor/tests/resources/PathPatternSerializationTest.kt +++ b/ktor-shared/ktor-resources/common/test/io/ktor/tests/resources/PathPatternSerializationTest.kt @@ -14,7 +14,6 @@ class PathPatternSerializationTest { private val locationsFormat = ResourcesFormat() @Resource("some/path/") - @Serializable class SimplePath @Test @@ -24,10 +23,8 @@ class PathPatternSerializationTest { } @Resource("parent/{path}") - @Serializable class NestedClass { @Resource("{child}/path") - @Serializable data class ChildClass(val parent: NestedClass) } @@ -38,22 +35,17 @@ class PathPatternSerializationTest { } @Resource("parent/{path}/") - @Serializable class NestedClassWithSlash { @Resource("{child}/path") - @Serializable data class ChildClassWithSlash(val parent: NestedClass) @Resource("/{child}/path") - @Serializable data class ChildClassWithoutSlash(val parent: NestedClass) } @Resource("parent/{path}") - @Serializable class NestedClassWithoutSlash { @Resource("/{child}/path") - @Serializable data class ChildClassWithSlash(val parent: NestedClass) } @@ -76,13 +68,11 @@ class PathPatternSerializationTest { } @Resource("/{child}/path") - @Serializable data class Container( val child: MultipleParents ) @Resource("/{child}/path") - @Serializable data class MultipleParents(val parent1: NestedClass, val value: String, val parent2: NestedClassWithSlash) @Test diff --git a/ktor-shared/ktor-resources/common/test/io/ktor/tests/resources/ResourceUrlBuilderTest.kt b/ktor-shared/ktor-resources/common/test/io/ktor/tests/resources/ResourceUrlBuilderTest.kt index 3e9fa4ccc3..db188dc018 100644 --- a/ktor-shared/ktor-resources/common/test/io/ktor/tests/resources/ResourceUrlBuilderTest.kt +++ b/ktor-shared/ktor-resources/common/test/io/ktor/tests/resources/ResourceUrlBuilderTest.kt @@ -13,7 +13,6 @@ class ResourceUrlBuilderTest { private val resourcesFormat = ResourcesFormat() - @Serializable @Resource("resource/{id}/") data class SimpleResource( val id: Int @@ -26,7 +25,6 @@ class ResourceUrlBuilderTest { assertEquals("/resource/123/", url) } - @Serializable @Resource("resource/{id}") data class SimpleResourceWithQuery( val id: Int, @@ -40,7 +38,6 @@ class ResourceUrlBuilderTest { assertEquals("/resource/123?key=456", url) } - @Serializable @Resource("resource/{id}") data class SimpleResourceWithQueryList( val id: Int, @@ -54,7 +51,6 @@ class ResourceUrlBuilderTest { assertEquals("/resource/123?key=456&key=789", url) } - @Serializable @Resource("resource/{ids...}") data class SimpleResourceWithWildcard( val ids: List @@ -71,7 +67,6 @@ class ResourceUrlBuilderTest { assertEquals("/resource", url2) } - @Serializable @Resource("resource/{id?}") data class SimpleResourceWithNullable( val id: Boolean? @@ -88,7 +83,6 @@ class ResourceUrlBuilderTest { assertEquals("/resource", url2) } - @Serializable @Resource("user/{user}") data class NestedResource( val user: String, @@ -102,7 +96,6 @@ class ResourceUrlBuilderTest { assertEquals("/resource/123/user/me", url) } - @Serializable @Resource("user/{id}") class ResourceWithoutParameter @@ -116,7 +109,6 @@ class ResourceUrlBuilderTest { } } - @Serializable @Resource("user/{id}") class ResourceWithExtraParameter( val id: List @@ -132,7 +124,6 @@ class ResourceUrlBuilderTest { } } - @Serializable @Resource("user/{id?}") class ResourceWithExtraNullableParameter( val id: List