diff --git a/README.md b/README.md index 35956de..ab1568e 100644 --- a/README.md +++ b/README.md @@ -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"] + } } ``` @@ -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 + } } ``` diff --git a/src/main/kotlin/org/springdoc/openapi/gradle/plugin/OpenApiExtension.kt b/src/main/kotlin/org/springdoc/openapi/gradle/plugin/OpenApiExtension.kt index fbffde7..95a587a 100644 --- a/src/main/kotlin/org/springdoc/openapi/gradle/plugin/OpenApiExtension.kt +++ b/src/main/kotlin/org/springdoc/openapi/gradle/plugin/OpenApiExtension.kt @@ -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 @@ -12,4 +16,20 @@ open class OpenApiExtension @Inject constructor(project: Project) { val outputDir: DirectoryProperty = project.objects.directoryProperty() val waitTimeInSeconds: Property = project.objects.property(Int::class.java) val groupedApiMappings: MapProperty = project.objects.mapProperty(String::class.java, String::class.java) + val customBootRun: CustomBootRunAction = project.objects.newInstance(CustomBootRunAction::class.java, project) + fun customBootRun(action: Action) { + action.execute(customBootRun) + } +} + +open class CustomBootRunAction @Inject constructor( + project: Project, +) { + val systemProperties: MapProperty = project.objects.mapProperty(String::class.java, Any::class.java) + val workingDir: RegularFileProperty = project.objects.fileProperty() + val mainClass: Property = project.objects.property(String::class.java) + val args: ListProperty = project.objects.listProperty(String::class.java) + val classpath: ConfigurableFileCollection = project.objects.fileCollection() + val jvmArgs: ListProperty = project.objects.listProperty(String::class.java) + val environment: MapProperty = project.objects.mapProperty(String::class.java, Any::class.java) } diff --git a/src/main/kotlin/org/springdoc/openapi/gradle/plugin/OpenApiGradlePlugin.kt b/src/main/kotlin/org/springdoc/openapi/gradle/plugin/OpenApiGradlePlugin.kt index 047ea30..bd44ac9 100644 --- a/src/main/kotlin/org/springdoc/openapi/gradle/plugin/OpenApiGradlePlugin.kt +++ b/src/main/kotlin/org/springdoc/openapi/gradle/plugin/OpenApiGradlePlugin.kt @@ -25,6 +25,8 @@ open class OpenApiGradlePlugin : Plugin { 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( @@ -36,20 +38,28 @@ open class OpenApiGradlePlugin : Plugin { 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") ) { diff --git a/src/test/kotlin/org/springdoc/openapi/gradle/plugin/OpenApiGradlePluginTest.kt b/src/test/kotlin/org/springdoc/openapi/gradle/plugin/OpenApiGradlePluginTest.kt index bd1e82d..1d5f5e9 100644 --- a/src/test/kotlin/org/springdoc/openapi/gradle/plugin/OpenApiGradlePluginTest.kt +++ b/src/test/kotlin/org/springdoc/openapi/gradle/plugin/OpenApiGradlePluginTest.kt @@ -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 @@ -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( @@ -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"