Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds an option to generate spring controllers that return CompletableFutures #304

Merged
merged 2 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ This section documents the available CLI parameters for controlling what gets ge
| | CHOOSE ANY OF: |
| | `SUSPEND_MODIFIER` - This option adds the suspend modifier to the generated controller functions |
| | `AUTHENTICATION` - This option adds the authentication parameter to the generated controller functions |
| | `COMPLETION_STAGE` - This option makes generated controller functions have Type CompletionStage<T> (works only with Spring Controller generator) |
| `--http-controller-target` | Optionally select the target framework for the controllers that you want to be generated. Defaults to Spring Controllers |
| | CHOOSE ONE OF: |
| | `SPRING` - Generate for Spring framework. |
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/com/cjbooms/fabrikt/cli/CodeGenOptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ enum class ModelCodeGenOptionType(val description: String) {

enum class ControllerCodeGenOptionType(val description: String) {
SUSPEND_MODIFIER("This option adds the suspend modifier to the generated controller functions"),
AUTHENTICATION("This option adds the authentication parameter to the generated controller functions");

AUTHENTICATION("This option adds the authentication parameter to the generated controller functions"),
COMPLETION_STAGE("This option makes generated controller functions have Type CompletionStage<T> (works only with Spring Controller generator)");
override fun toString() = "`${super.toString()}` - $description"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,24 @@ class SpringControllerInterfaceGenerator(
val globalSecurity = api.openApi3.securityRequirements.securitySupport()

// Main method builder
val funcSpec = FunSpec
val baseFunSpec = FunSpec
.builder(methodName)
.addModifiers(KModifier.ABSTRACT)
.addKdoc(op.toKdoc(parameters))
.addSpringFunAnnotation(op, verb, path.pathString)
.addSuspendModifier()
.returns(SpringImports.RESPONSE_ENTITY.parameterizedBy(returnType))

parameters
val funcSpec = if (options.contains(ControllerCodeGenOptionType.COMPLETION_STAGE)) {
baseFunSpec.returns(
SpringImports.COMPLETION_STAGE.parameterizedBy(
SpringImports.RESPONSE_ENTITY.parameterizedBy(returnType)
)
)
} else {
baseFunSpec.returns(SpringImports.RESPONSE_ENTITY.parameterizedBy(returnType))
}

parameters
.map {
when (it) {
is BodyParameter ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ object SpringImports {
const val DATE_TIME_FORMAT = "org.springframework.format.annotation"

const val SPRING_AUTHENTICATION = "$SPRING_BASE.security.core"

const val JAVA_UTIL_CONCURRENT = "java.util.concurrent"
}

val CONTROLLER = ClassName(Packages.STEREOTYPE, "Controller")
Expand All @@ -41,6 +43,8 @@ object SpringImports {

val AUTHENTICATION = ClassName(Packages.SPRING_AUTHENTICATION, "Authentication")

val COMPLETION_STAGE = ClassName(Packages.JAVA_UTIL_CONCURRENT, "CompletionStage")

object DateTimeFormat {
val ISO_DATE = "${DATE_TIME_FORMAT.simpleName}.ISO.DATE"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,20 @@ class SpringControllerGeneratorTest {

assertThat(controllers.trim()).isEqualTo(expectedControllers.trim())
}

@Test
fun `controller functions are wrapped by CompletionStage`() {
val basePackage = "examples.completionStage"
val api = SourceApi(readTextResource("/examples/githubApi/api.yaml"))
val expectedControllers = readTextResource("/examples/githubApi/controllers/spring-completion-stage/Controllers.kt")

val controllers = SpringControllerInterfaceGenerator(
Packages(basePackage),
api,
JavaxValidationAnnotations,
setOf(ControllerCodeGenOptionType.COMPLETION_STAGE),
).generate().toSingleFile()

assertThat(controllers).isEqualTo(expectedControllers)
}
}
Loading