Skip to content

introduce customBootRun property for customizing runtime parameter for Spring application #78

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

Merged
merged 1 commit into from
Aug 6, 2022
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
43 changes: 26 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,29 +71,36 @@ openApi {
outputDir.set(file("$buildDir/docs"))
outputFileName.set("swagger.json")
waitTimeInSeconds.set(10)
forkProperties.set("-Dspring.profiles.active=special")
groupedApiMappings.set(["https://localhost:8080/v3/api-docs/groupA" to "swagger-groupA.json",
"https://localhost:8080/v3/api-docs/groupB" to "swagger-groupB.json"])
customBootRun {
args.set(["--spring.profiles.active=special"])
}
}
```

Parameter | Description | Required | Default
--------- | ----------- | -------- | -------
`apiDocsUrl` | The URL from where the OpenAPI doc can be downloaded | No | http://localhost:8080/v3/api-docs
`outputDir` | The output directory for the generated OpenAPI file | No | $buildDir - Your project's build dir
`outputFileName` | The name of the output file with extension | No | openapi.json
`waitTimeInSeconds` | Time to wait in seconds for your Spring Boot application to start, before we make calls to `apiDocsUrl` to download the OpenAPI doc | No | 30 seconds
`forkProperties` | Any system property that you would normal need to start your spring boot application. Can either be a static string or a java Properties object | No | ""
`groupedApiMappings` | A map of URLs (from where the OpenAPI docs can be downloaded) to output file names | No | []

### Fork properties examples
Fork properties allows you to send in anything that might be necessary to allow for the forked spring boot application that gets started
to be able to start (profiles, other custom properties, etc etc)

#### Static string
| Parameter | Description | Required | Default |
|----------------------|-------------------------------------------------------------------------------------------------------------------------------------|----------|--------------------------------------|
| `apiDocsUrl` | The URL from where the OpenAPI doc can be downloaded | No | http://localhost:8080/v3/api-docs |
| `outputDir` | The output directory for the generated OpenAPI file | No | $buildDir - Your project's build dir |
| `outputFileName` | The name of the output file with extension | No | openapi.json |
| `waitTimeInSeconds` | Time to wait in seconds for your Spring Boot application to start, before we make calls to `apiDocsUrl` to download the OpenAPI doc | No | 30 seconds |
| `groupedApiMappings` | A map of URLs (from where the OpenAPI docs can be downloaded) to output file names | No | [] |
| `customBootRun` | Any bootRun property that you would normal need to start your spring boot application. | No | (N/A) |

### `customBootRun` properties examples
`customBootRun` allows you to send in the properties that might be necessary to allow for the forked spring boot application that gets started
to be able to start (profiles, other custom properties, etc.)
`customBootRun` allows you can specify bootRun style parameter, such as `args`, `jvmArgs`, `systemProperties` and `workingDir`.
If you don't specify `customBootRun` parameter, this plugin uses the parameter specified to `bootRun` in Spring Boot Gradle Plugin.

#### Passing static args
This allows for you to be able to just send the static properties when executing Spring application in `generateOpenApiDocs`.
```
openApi {
forkProperties = "-Dspring.profiles.active=special -DstringPassedInForkProperites=true"
customBootRun {
args = ["--spring.profiles.active=special"]
}
}
```

Expand All @@ -105,7 +112,9 @@ This allows for you to be able to just send in whatever you need when you genera
and as long as the config looks as follows that value will be passed into the forked spring boot application.
```
openApi {
forkProperties = System.properties
customBootRun {
systemProperties = System.properties
}
}
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package org.springdoc.openapi.gradle.plugin

import org.gradle.api.Action
import org.gradle.api.Project
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.MapProperty
import org.gradle.api.provider.Property
import javax.inject.Inject
Expand All @@ -12,4 +16,20 @@ open class OpenApiExtension @Inject constructor(project: Project) {
val outputDir: DirectoryProperty = project.objects.directoryProperty()
val waitTimeInSeconds: Property<Int> = project.objects.property(Int::class.java)
val groupedApiMappings: MapProperty<String, String> = project.objects.mapProperty(String::class.java, String::class.java)
val customBootRun: CustomBootRunAction = project.objects.newInstance(CustomBootRunAction::class.java, project)
fun customBootRun(action: Action<CustomBootRunAction>) {
action.execute(customBootRun)
}
}

open class CustomBootRunAction @Inject constructor(
project: Project,
) {
val systemProperties: MapProperty<String, Any> = project.objects.mapProperty(String::class.java, Any::class.java)
val workingDir: RegularFileProperty = project.objects.fileProperty()
val mainClass: Property<String> = project.objects.property(String::class.java)
val args: ListProperty<String> = project.objects.listProperty(String::class.java)
val classpath: ConfigurableFileCollection = project.objects.fileCollection()
val jvmArgs: ListProperty<String> = project.objects.listProperty(String::class.java)
val environment: MapProperty<String, Any> = project.objects.mapProperty(String::class.java, Any::class.java)
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ open class OpenApiGradlePlugin : Plugin<Project> {
val bootRunMainClassNameTask =
project.tasks.named(SPRING_BOOT_RUN_MAIN_CLASS_NAME_TASK_NAME)

val extension = project.extensions.findByName(EXTENSION_NAME) as OpenApiExtension
val customBootRun = extension.customBootRun
// Create a forked version spring boot run task
val forkedSpringBoot =
project.tasks.register(
Expand All @@ -36,20 +38,28 @@ open class OpenApiGradlePlugin : Plugin<Project> {
fork.onlyIf {
val bootRun = bootRunTask.get() as BootRun

val baseSystemProperties = customBootRun.systemProperties.orNull?.takeIf { it.isNotEmpty() }
?: bootRun.systemProperties
// copy all system properties, excluding those starting with `java.class.path`
fork.systemProperties =
bootRun.systemProperties.filter {
!it.key.startsWith(
CLASS_PATH_PROPERTY_NAME
)
}
fork.systemProperties = baseSystemProperties.filter {
!it.key.startsWith(
CLASS_PATH_PROPERTY_NAME
)
}

fork.workingDir = bootRun.workingDir
fork.args = bootRun.args?.toMutableList() ?: mutableListOf()
fork.classpath = bootRun.classpath
fork.main = bootRun.mainClass.get()
fork.jvmArgs = bootRun.jvmArgs
fork.environment = bootRun.environment
// use original bootRun parameter if the list-type customBootRun properties is empty
fork.workingDir = customBootRun.workingDir.asFile.orNull
?: bootRun.workingDir
fork.args = customBootRun.args.orNull?.takeIf { it.isNotEmpty() }?.toMutableList()
?: bootRun.args?.toMutableList() ?: mutableListOf()
fork.classpath = customBootRun.classpath.takeIf { !it.isEmpty }
?: bootRun.classpath
fork.main = customBootRun.mainClass.orNull
?: bootRun.mainClass.get()
fork.jvmArgs = customBootRun.jvmArgs.orNull?.takeIf { it.isNotEmpty() }
?: bootRun.jvmArgs
fork.environment = customBootRun.environment.orNull?.takeIf { it.isNotEmpty() }
?: bootRun.environment
if (org.gradle.internal.jvm.Jvm.current().toString()
.startsWith("1.8")
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package org.springdoc.openapi.gradle.plugin
import com.beust.klaxon.JsonObject
import com.beust.klaxon.Parser
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.node.TextNode
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import com.fasterxml.jackson.module.kotlin.KotlinModule
import org.gradle.internal.impldep.org.apache.commons.lang.RandomStringUtils
Expand Down Expand Up @@ -122,6 +121,23 @@ class OpenApiGradlePluginTest {
assertOpenApiJsonFile(2)
}

@Test
fun `using forked properties via System properties with customBootRun`() {
buildFile.writeText(
"""$baseBuildGradle
openApi {
customBootRun {
systemProperties = System.properties
}
}
""".trimMargin()
)

assertEquals(TaskOutcome.SUCCESS, openApiDocsTask(runTheBuild("-Dspring.profiles.active=multiple-endpoints")).outcome)
assertOpenApiJsonFile(2)
}


@Test
fun `configurable wait time`() {
buildFile.writeText(
Expand Down Expand Up @@ -156,6 +172,24 @@ class OpenApiGradlePluginTest {
assertOpenApiJsonFile(1)
}

@Test
fun `using different api url via customBootRun`() {
buildFile.writeText(
"""$baseBuildGradle
openApi{
apiDocsUrl = "http://localhost:8080/secret-api-docs"
customBootRun {
args = ["--spring.profiles.active=different-url"]
}
}
""".trimMargin()
)

assertEquals(TaskOutcome.SUCCESS, openApiDocsTask(runTheBuild()).outcome)
assertOpenApiJsonFile(1)
}


@Test
fun `yaml generation`() {
val outputYamlFileName = "openapi.yaml"
Expand Down