From 7d622ca5f2e709ff491b86cb503b390579bfae3f Mon Sep 17 00:00:00 2001 From: Jens Date: Sun, 24 Sep 2023 21:18:42 +0100 Subject: [PATCH] address a couple of compiler warnings and replace deprecated syntax (#240) Co-authored-by: Jens Zettelmeyer --- .../generators/client/ClientGeneratorUtils.kt | 2 +- .../controller/metadata/Micronaut.kt | 4 +-- .../generators/MicronautAuthenticationTest.kt | 30 +++++++++---------- .../MicronautControllerGeneratorTest.kt | 9 +++--- .../generators/SpringAuthenticationTest.kt | 6 ++-- .../SpringControllerGeneratorTest.kt | 10 ++++--- 6 files changed, 32 insertions(+), 29 deletions(-) diff --git a/src/main/kotlin/com/cjbooms/fabrikt/generators/client/ClientGeneratorUtils.kt b/src/main/kotlin/com/cjbooms/fabrikt/generators/client/ClientGeneratorUtils.kt index 31b98929..911b5a05 100644 --- a/src/main/kotlin/com/cjbooms/fabrikt/generators/client/ClientGeneratorUtils.kt +++ b/src/main/kotlin/com/cjbooms/fabrikt/generators/client/ClientGeneratorUtils.kt @@ -93,7 +93,7 @@ object ClientGeneratorUtils { val builder = it.toParameterSpecBuilder() if (it is RequestParameter) { if (it.defaultValue != null) OasDefault.from(it.typeInfo, it.type, it.defaultValue) - ?.let { builder.defaultValue(it.getDefault()) } + ?.let { t -> builder.defaultValue(t.getDefault()) } else if (!it.isRequired) builder.defaultValue("null") annotateRequestParameterWith?.invoke(it)?.let { annotationSpec -> builder.addAnnotation(annotationSpec) diff --git a/src/main/kotlin/com/cjbooms/fabrikt/generators/controller/metadata/Micronaut.kt b/src/main/kotlin/com/cjbooms/fabrikt/generators/controller/metadata/Micronaut.kt index f9b1c0d1..58bfb28e 100644 --- a/src/main/kotlin/com/cjbooms/fabrikt/generators/controller/metadata/Micronaut.kt +++ b/src/main/kotlin/com/cjbooms/fabrikt/generators/controller/metadata/Micronaut.kt @@ -29,8 +29,8 @@ object MicronautImports { val AUTHENTICATION = ClassName(Packages.MICRONAUT_AUTHENTICATION, "Authentication") val SECURITY_RULE = Pair(Packages.MICRONAUT_RULE, "SecurityRule") - val SECURITY_RULE_IS_AUTHENTICATED = "SecurityRule.IS_AUTHENTICATED" - val SECURITY_RULE_IS_ANONYMOUS = "SecurityRule.IS_ANONYMOUS" + const val SECURITY_RULE_IS_AUTHENTICATED = "SecurityRule.IS_AUTHENTICATED" + const val SECURITY_RULE_IS_ANONYMOUS = "SecurityRule.IS_ANONYMOUS" val CONTROLLER = ClassName(Packages.MICRONAUT_HTTP_ANNOTATION, "Controller") val BODY = ClassName(Packages.MICRONAUT_HTTP_ANNOTATION, "Body") diff --git a/src/test/kotlin/com/cjbooms/fabrikt/generators/MicronautAuthenticationTest.kt b/src/test/kotlin/com/cjbooms/fabrikt/generators/MicronautAuthenticationTest.kt index bcd4406d..a6823041 100644 --- a/src/test/kotlin/com/cjbooms/fabrikt/generators/MicronautAuthenticationTest.kt +++ b/src/test/kotlin/com/cjbooms/fabrikt/generators/MicronautAuthenticationTest.kt @@ -53,13 +53,13 @@ class MicronautAuthenticationTest { fun `ensure that global authentication set to any authentication will add the required parameter`(testCase: Pair) { val controllers = setupTest(testCase.first) val functionAnnotations = controllers.flatMap { it.members } - .flatMap { (it as TypeSpec).funSpecs.flatMap { it.annotations } } + .flatMap { (it as TypeSpec).funSpecs.flatMap { t -> t.annotations } } val functionParameters = controllers.flatMap { it.members } - .flatMap { (it as TypeSpec).funSpecs.flatMap { it.parameters } } + .flatMap { (it as TypeSpec).funSpecs.flatMap { t -> t.parameters } } assertThat(functionAnnotations).anySatisfy { annotation -> - assertThat(annotation.className.canonicalName).isEqualTo("io.micronaut.security.annotation.Secured") + assertThat(annotation.typeName.toString()).isEqualTo("io.micronaut.security.`annotation`.Secured") assertThat(annotation.members).anyMatch { it.toString() == testCase.second } } @@ -78,13 +78,13 @@ class MicronautAuthenticationTest { fun `ensure that no authentication defined will add no parameter`() { val controllers = setupTest("global_authentication_none.yml") val functionAnnotations = controllers.flatMap { it.members } - .flatMap { (it as TypeSpec).funSpecs.flatMap { it.annotations } } + .flatMap { (it as TypeSpec).funSpecs.flatMap { t -> t.annotations } } val functionParameters = controllers.flatMap { it.members } - .flatMap { (it as TypeSpec).funSpecs.flatMap { it.parameters } } + .flatMap { (it as TypeSpec).funSpecs.flatMap { t -> t.parameters } } assertThat(functionAnnotations).noneSatisfy { annotation -> - assertThat(annotation.className.simpleName).isEqualTo("Secured") + assertThat(annotation.typeName.toString()).isEqualTo("Secured") } assertThat(functionParameters).noneSatisfy { parameter -> @@ -100,7 +100,7 @@ class MicronautAuthenticationTest { val prohibitedController = (controllers.find { it.name == "ProhibitedController" }!!.members.single() as TypeSpec).funSpecs.single() assertThat(prohibitedController.annotations).anySatisfy { annotation -> - assertThat(annotation.className.canonicalName).isEqualTo("io.micronaut.security.annotation.Secured") + assertThat(annotation.typeName.toString()).isEqualTo("io.micronaut.security.`annotation`.Secured") assertThat(annotation.members).anyMatch { it.toString() == "SecurityRule.IS_ANONYMOUS" } } @@ -111,7 +111,7 @@ class MicronautAuthenticationTest { val requiredController = (controllers.find { it.name == "RequiredController" }!!.members.single() as TypeSpec).funSpecs.single() assertThat(requiredController.annotations).anySatisfy { annotation -> - assertThat(annotation.className.canonicalName).isEqualTo("io.micronaut.security.annotation.Secured") + assertThat(annotation.typeName.toString()).isEqualTo("io.micronaut.security.`annotation`.Secured") assertThat(annotation.members).anyMatch { it.toString() == "SecurityRule.IS_AUTHENTICATED" } } @@ -124,7 +124,7 @@ class MicronautAuthenticationTest { val optionalController = (controllers.find { it.name == "OptionalController" }!!.members.single() as TypeSpec).funSpecs.single() assertThat(optionalController.annotations).anySatisfy { annotation -> - assertThat(annotation.className.canonicalName).isEqualTo("io.micronaut.security.annotation.Secured") + assertThat(annotation.typeName.toString()).isEqualTo("io.micronaut.security.`annotation`.Secured") assertThat(annotation.members).anyMatch { it.toString() == "SecurityRule.IS_AUTHENTICATED, SecurityRule.IS_ANONYMOUS" } } @@ -137,7 +137,7 @@ class MicronautAuthenticationTest { val noneController = (controllers.find { it.name == "NoneController" }!!.members.single() as TypeSpec).funSpecs.single() assertThat(noneController.annotations).noneSatisfy { annotation -> - assertThat(annotation.className.simpleName).isEqualTo("Secured") + assertThat(annotation.typeName.toString()).isEqualTo("Secured") } assertThat(noneController.parameters).noneSatisfy { parameter -> @@ -147,7 +147,7 @@ class MicronautAuthenticationTest { val defaultController = (controllers.find { it.name == "DefaultController" }!!.members.single() as TypeSpec).funSpecs.single() assertThat(defaultController.annotations).anySatisfy { annotation -> - assertThat(annotation.className.canonicalName).isEqualTo("io.micronaut.security.annotation.Secured") + assertThat(annotation.typeName.toString()).isEqualTo("io.micronaut.security.`annotation`.Secured") assertThat(annotation.members).anyMatch { it.toString() == "SecurityRule.IS_AUTHENTICATED" } } @@ -164,7 +164,7 @@ class MicronautAuthenticationTest { val prohibitedController = (controllers.find { it.name == "ProhibitedController" }!!.members.single() as TypeSpec).funSpecs.single() assertThat(prohibitedController.annotations).anySatisfy { annotation -> - assertThat(annotation.className.canonicalName).isEqualTo("io.micronaut.security.annotation.Secured") + assertThat(annotation.typeName.toString()).isEqualTo("io.micronaut.security.`annotation`.Secured") assertThat(annotation.members).anyMatch { it.toString() == "SecurityRule.IS_ANONYMOUS" } } @@ -175,7 +175,7 @@ class MicronautAuthenticationTest { val requiredController = (controllers.find { it.name == "RequiredController" }!!.members.single() as TypeSpec).funSpecs.single() assertThat(requiredController.annotations).anySatisfy { annotation -> - assertThat(annotation.className.canonicalName).isEqualTo("io.micronaut.security.annotation.Secured") + assertThat(annotation.typeName.toString()).isEqualTo("io.micronaut.security.`annotation`.Secured") assertThat(annotation.members).anyMatch { it.toString() == "SecurityRule.IS_AUTHENTICATED" } } @@ -187,7 +187,7 @@ class MicronautAuthenticationTest { val optionalController = (controllers.find { it.name == "OptionalController" }!!.members.single() as TypeSpec).funSpecs.single() assertThat(optionalController.annotations).anySatisfy { annotation -> - assertThat(annotation.className.canonicalName).isEqualTo("io.micronaut.security.annotation.Secured") + assertThat(annotation.typeName.toString()).isEqualTo("io.micronaut.security.`annotation`.Secured") assertThat(annotation.members).anyMatch { it.toString() == "SecurityRule.IS_AUTHENTICATED, SecurityRule.IS_ANONYMOUS" } } @@ -200,7 +200,7 @@ class MicronautAuthenticationTest { val noneController = (controllers.find { it.name == "NoneController" }!!.members.single() as TypeSpec).funSpecs.single() assertThat(noneController.annotations).noneSatisfy { annotation -> - assertThat(annotation.className.simpleName).isEqualTo("Secured") + assertThat(annotation.typeName.toString()).isEqualTo("io.micronaut.security.annotation, Secured") } assertThat(noneController.parameters).noneSatisfy { parameter -> diff --git a/src/test/kotlin/com/cjbooms/fabrikt/generators/MicronautControllerGeneratorTest.kt b/src/test/kotlin/com/cjbooms/fabrikt/generators/MicronautControllerGeneratorTest.kt index 3afff8a8..3818200b 100644 --- a/src/test/kotlin/com/cjbooms/fabrikt/generators/MicronautControllerGeneratorTest.kt +++ b/src/test/kotlin/com/cjbooms/fabrikt/generators/MicronautControllerGeneratorTest.kt @@ -117,7 +117,7 @@ class MicronautControllerGeneratorTest { fun `ensure controller has correct annotations`() { setupGithubApiTestEnv() val controllerAnnotations = - generated.flatMap { it.members.flatMap { (it as TypeSpec).annotations.map { it.typeName.toString() } } } + generated.flatMap { it.members.flatMap { ts -> (ts as TypeSpec).annotations.map { t -> t.typeName.toString() } } } .distinct() assertThat(controllerAnnotations).containsOnly( @@ -139,9 +139,10 @@ class MicronautControllerGeneratorTest { .flatMap { it.funSpecs } .flatMap { it.parameters } .flatMap { it.annotations } - .map { it.className.canonicalName } + .map { it.typeName.toString() } .filter { ".validation." in it } .distinct() + assertThat(parameterValidationAnnotations).isNotEmpty assertThat(parameterValidationAnnotations).allMatch { it.startsWith(desiredPackagePrefix) } } @@ -164,11 +165,11 @@ class MicronautControllerGeneratorTest { val linkedFunctionNames = controllers.files .filter { it.name == "OrganisationsContributorsController" } .flatMap { it.members } - .flatMap { (it as TypeSpec).funSpecs.map { it.name } } + .flatMap { (it as TypeSpec).funSpecs.map { t -> t.name } } val ownedFunctionNames = controllers.files .filter { it.name == "RepositoriesPullRequestsController" } .flatMap { it.members } - .flatMap { (it as TypeSpec).funSpecs.map { it.name } } + .flatMap { (it as TypeSpec).funSpecs.map { t -> t.name } } assertThat(linkedFunctionNames).containsExactlyInAnyOrder( "get", "getById", diff --git a/src/test/kotlin/com/cjbooms/fabrikt/generators/SpringAuthenticationTest.kt b/src/test/kotlin/com/cjbooms/fabrikt/generators/SpringAuthenticationTest.kt index 2f32bf85..8ec0a957 100644 --- a/src/test/kotlin/com/cjbooms/fabrikt/generators/SpringAuthenticationTest.kt +++ b/src/test/kotlin/com/cjbooms/fabrikt/generators/SpringAuthenticationTest.kt @@ -51,7 +51,7 @@ class SpringAuthenticationTest { val controllers = setupTest("global_authentication_required.yml") val functionParameters = controllers.flatMap { it.members } - .flatMap { (it as TypeSpec).funSpecs.flatMap { it.parameters } } + .flatMap { (it as TypeSpec).funSpecs.flatMap { t -> t.parameters } } assertThat(functionParameters).anySatisfy { parameter -> assertThat(parameter.name).isEqualTo("authentication") @@ -63,7 +63,7 @@ class SpringAuthenticationTest { val controllers = setupTest("global_authentication_optional.yml") val functionParameters = controllers.flatMap { it.members } - .flatMap { (it as TypeSpec).funSpecs.flatMap { it.parameters } } + .flatMap { (it as TypeSpec).funSpecs.flatMap { t -> t.parameters } } assertThat(functionParameters).anySatisfy { parameter -> assertThat(parameter.name).isEqualTo("authentication") @@ -77,7 +77,7 @@ class SpringAuthenticationTest { val controllers = setupTest(testCasePath) val functionParameters = controllers.flatMap { it.members } - .flatMap { (it as TypeSpec).funSpecs.flatMap { it.parameters } } + .flatMap { (it as TypeSpec).funSpecs.flatMap { t -> t.parameters } } assertThat(functionParameters).noneSatisfy { parameter -> assertThat(parameter.name).isEqualTo("authentication") diff --git a/src/test/kotlin/com/cjbooms/fabrikt/generators/SpringControllerGeneratorTest.kt b/src/test/kotlin/com/cjbooms/fabrikt/generators/SpringControllerGeneratorTest.kt index e9149522..181760bf 100644 --- a/src/test/kotlin/com/cjbooms/fabrikt/generators/SpringControllerGeneratorTest.kt +++ b/src/test/kotlin/com/cjbooms/fabrikt/generators/SpringControllerGeneratorTest.kt @@ -113,7 +113,7 @@ class SpringControllerGeneratorTest { fun `ensure controller has correct annotations`() { setupGithubApiTestEnv() val controllerAnnotations = - generated.flatMap { it.members.flatMap { (it as TypeSpec).annotations.map { it.typeName.toString() } } } + generated.flatMap { it.members.flatMap { ts -> (ts as TypeSpec).annotations.map { t -> t.typeName.toString() } } } .distinct() assertThat(controllerAnnotations).containsOnly( @@ -137,9 +137,11 @@ class SpringControllerGeneratorTest { .flatMap { it.funSpecs } .flatMap { it.parameters } .flatMap { it.annotations } - .map { it.className.canonicalName } + .map { it.typeName.toString() } .filter { ".validation." in it } .distinct() + + assertThat(parameterValidationAnnotations).isNotEmpty assertThat(parameterValidationAnnotations).allMatch { it.startsWith(desiredPackagePrefix) } } @@ -162,11 +164,11 @@ class SpringControllerGeneratorTest { val linkedFunctionNames = controllers.files .filter { it.name == "OrganisationsContributorsController" } .flatMap { it.members } - .flatMap { (it as TypeSpec).funSpecs.map { it.name } } + .flatMap { (it as TypeSpec).funSpecs.map { t -> t.name } } val ownedFunctionNames = controllers.files .filter { it.name == "RepositoriesPullRequestsController" } .flatMap { it.members } - .flatMap { (it as TypeSpec).funSpecs.map { it.name } } + .flatMap { (it as TypeSpec).funSpecs.map { t -> t.name } } assertThat(linkedFunctionNames).containsExactlyInAnyOrder( "get", "getById",