Skip to content

Commit

Permalink
fix security annotations and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fellmann committed Oct 27, 2023
1 parent 0d5754a commit e156d9e
Show file tree
Hide file tree
Showing 9 changed files with 417 additions and 47 deletions.
8 changes: 7 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ tasks {
attributes["Built-JDK"] = System.getProperty("java.version")
attributes["Built-Gradle"] = gradle.gradleVersion
}
archiveBaseName.set(executableName)
archiveFileName.set(executableName + ".jar")
archiveClassifier.set("")
}

Expand Down Expand Up @@ -175,3 +175,9 @@ signing {

sign(publishing.publications["fabrikt"])
}

tasks {
test {
jvmArgs("--add-opens=java.base/java.lang=ALL-UNNAMED")
}
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ class MicronautControllerInterfaceGenerator(
val systemRoleTypeName =
ClassName("de.flipnext.core.usermanagement.users.domain", "SystemRole")
val requirements =
op.securityRequirements.getOrNull(0)?.requirements?.values?.first()?.parameters
op.securityRequirements.getOrNull(0)?.requirements?.values?.firstOrNull()?.parameters
val spec = AnnotationSpec.builder(MicronautImports.SECURED)
// **warning** this is a hack to get the system role names into the annotation
// since this is already custom flip code we use the system role name directly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.MethodSource
import java.io.File
import java.util.stream.Stream

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
Expand Down Expand Up @@ -64,6 +65,13 @@ class MicronautControllerGeneratorTest {
api,
).generate().toSingleFile()

/*
Use this to update the expected output
File("src/test/resources/examples/$testCaseName/controllers/micronaut/Controllers.kt").writeText(
controllers
)
*/

assertThat(controllers).isEqualTo(expectedControllers)
}

Expand Down Expand Up @@ -118,6 +126,7 @@ class MicronautControllerGeneratorTest {

assertThat(controllerAnnotations).containsOnly(
"io.micronaut.http.annotation.Controller",
"io.micronaut.validation.Validated",
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ import io.micronaut.http.annotation.QueryValue
import io.micronaut.security.annotation.Secured
import io.micronaut.security.authentication.Authentication
import io.micronaut.security.rules.SecurityRule
import io.micronaut.validation.Validated
import kotlin.String
import kotlin.Unit

@Controller
interface RequiredController {
@Validated
class RequiredController(
val testPathDelegate: TestPathDelegate
) {
/**
*
*
Expand All @@ -22,23 +26,41 @@ interface RequiredController {
fun testPath(
@QueryValue(value = "testString") testString: String,
authentication: Authentication
): HttpResponse<Unit>
): HttpResponse<Unit> = testPathDelegate.testPath(
testString,
authentication
)

interface TestPathDelegate {
fun testPath(testString: String, authentication: Authentication): HttpResponse<Unit>
}
}

@Controller
interface ProhibitedController {
@Validated
class ProhibitedController(
val testPathDelegate: TestPathDelegate
) {
/**
*
*
* @param testString
*/
@Get(uri = "/prohibited")
@Secured(SecurityRule.IS_ANONYMOUS)
fun testPath(@QueryValue(value = "testString") testString: String): HttpResponse<Unit>
fun testPath(@QueryValue(value = "testString") testString: String): HttpResponse<Unit> =
testPathDelegate.testPath(testString)

interface TestPathDelegate {
fun testPath(testString: String): HttpResponse<Unit>
}
}

@Controller
interface OptionalController {
@Validated
class OptionalController(
val testPathDelegate: TestPathDelegate
) {
/**
*
*
Expand All @@ -49,22 +71,40 @@ interface OptionalController {
fun testPath(
@QueryValue(value = "testString") testString: String,
authentication: Authentication?
): HttpResponse<Unit>
): HttpResponse<Unit> = testPathDelegate.testPath(
testString,
authentication
)

interface TestPathDelegate {
fun testPath(testString: String, authentication: Authentication?): HttpResponse<Unit>
}
}

@Controller
interface NoneController {
@Validated
class NoneController(
val testPathDelegate: TestPathDelegate
) {
/**
*
*
* @param testString
*/
@Get(uri = "/none")
fun testPath(@QueryValue(value = "testString") testString: String): HttpResponse<Unit>
fun testPath(@QueryValue(value = "testString") testString: String): HttpResponse<Unit> =
testPathDelegate.testPath(testString)

interface TestPathDelegate {
fun testPath(testString: String): HttpResponse<Unit>
}
}

@Controller
interface DefaultController {
@Validated
class DefaultController(
val testPathDelegate: TestPathDelegate
) {
/**
*
*
Expand All @@ -75,5 +115,12 @@ interface DefaultController {
fun testPath(
@QueryValue(value = "testString") testString: String,
authentication: Authentication
): HttpResponse<Unit>
): HttpResponse<Unit> = testPathDelegate.testPath(
testString,
authentication
)

interface TestPathDelegate {
fun testPath(testString: String, authentication: Authentication): HttpResponse<Unit>
}
}
Loading

0 comments on commit e156d9e

Please sign in to comment.