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

Added map property to set custom headers #144

Closed
wants to merge 2 commits into from
Closed
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
21 changes: 14 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,24 @@ openApi {
customBootRun {
args.set(["--spring.profiles.active=special"])
}

requestHeaders = [
"x-forwarded-host": "custom-host",
"x-forwarded-port": "7000"
]
}
```

| Parameter | Description | Required | Default |
|----------------------|-------------------------------------------------------------------------------------------------------------------------------------|----------|--------------------------------------|
| `apiDocsUrl` | The URL from where the OpenAPI doc can be downloaded. If the url ends with `.yaml`, output will YAML format. | No | http://localhost:8080/v3/api-docs |
| `outputDir` | The output directory for the generated OpenAPI file | No | $buildDir - Your project's build dir |
| `outputFileName` | Specifies the output file name. | No | openapi.json |
| Parameter | Description | Required | Default |
|----------------------|----------------------------------------------------------------------------------------------------------------------------|----------|--------------------------------------|
| `apiDocsUrl` | The URL from where the OpenAPI doc can be downloaded. If the url ends with `.yaml`, output will YAML format. | No | http://localhost:8080/v3/api-docs |
| `outputDir` | The output directory for the generated OpenAPI file | No | $buildDir - Your project's build dir |
| `outputFileName` | Specifies the output file name. | 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) |
| `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) |
| `requestHeaders` | customize Generated server url, relies on `server.forward-headers-strategy=framework` | No | (N/A) |


### `customBootRun` properties examples

Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group = "org.springdoc"
version = "1.8.0"
version = "1.8.1"

sonarqube {
properties {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ open class OpenApiExtension @Inject constructor(
val waitTimeInSeconds: Property<Int> = objects.property(Int::class.java)
val groupedApiMappings: MapProperty<String, String> =
objects.mapProperty(String::class.java, String::class.java)
val requestHeaders: MapProperty<String, String> =
objects.mapProperty(String::class.java, String::class.java)
val customBootRun: CustomBootRunAction =
objects.newInstance(CustomBootRunAction::class.java)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,13 @@ open class OpenApiGeneratorTask : DefaultTask() {
val groupedApiMappings: MapProperty<String, String> =
project.objects.mapProperty(String::class.java, String::class.java)

@get:Input
val requestHeaders: MapProperty<String, String> =
project.objects.mapProperty(String::class.java, String::class.java)

@get:OutputDirectory
val outputDir: DirectoryProperty = project.objects.directoryProperty()

@get:Internal
val waitTimeInSeconds: Property<Int> =
project.objects.property(Int::class.java)
Expand All @@ -56,6 +61,7 @@ open class OpenApiGeneratorTask : DefaultTask() {
groupedApiMappings.convention(extension.groupedApiMappings)
outputDir.convention(extension.outputDir)
waitTimeInSeconds.convention(extension.waitTimeInSeconds)
requestHeaders.convention(extension.requestHeaders)
}

@TaskAction
Expand All @@ -77,6 +83,10 @@ open class OpenApiGeneratorTask : DefaultTask() {
val connection: HttpURLConnection =
URL(url).openConnection() as HttpURLConnection
connection.requestMethod = "GET"
requestHeaders.get().forEach { header ->
connection.setRequestProperty(header.key, header.value)
}

connection.connect()
val statusCode = connection.responseCode
logger.trace("apiDocsUrl = {} status code = {}", url, statusCode)
Expand All @@ -86,6 +96,9 @@ open class OpenApiGeneratorTask : DefaultTask() {
val connection: HttpURLConnection =
URL(url).openConnection() as HttpURLConnection
connection.requestMethod = "GET"
requestHeaders.get().forEach { header ->
connection.setRequestProperty(header.key, header.value)
}
connection.connect()

val response = String(connection.inputStream.readBytes(), Charsets.UTF_8)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.springdoc.openapi.gradle.plugin

import com.beust.klaxon.JsonArray
import com.beust.klaxon.JsonObject
import com.beust.klaxon.Parser
import com.fasterxml.jackson.databind.ObjectMapper
Expand All @@ -12,6 +13,7 @@ import org.gradle.testkit.runner.TaskOutcome
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.slf4j.Logger
Expand Down Expand Up @@ -335,6 +337,34 @@ class OpenApiGradlePluginTest {
}
}

@Test
fun `adding headers for custom generated url`() {
val outputJsonFileName: String = DEFAULT_OPEN_API_FILE_NAME
val buildDir: File = projectBuildDir
val customHost = "custom-host"
val customPort = "7000"

buildFile.writeText(
"""$baseBuildGradle
bootRun {
args = ["--server.forward-headers-strategy=framework"]
}
openApi{
outputFileName = "$outputJsonFileName"
requestHeaders = [
"x-forwarded-host": "$customHost",
"x-forwarded-port": "$customPort"
]
}
""".trimMargin())

assertEquals(TaskOutcome.SUCCESS, openApiDocsTask(runTheBuild()).outcome)
assertOpenApiJsonFile(1, outputJsonFileName)
val openApiJson = getOpenApiJsonAtLocation(File(buildDir, outputJsonFileName))
val servers: JsonArray<Map<String, String>>? = openApiJson.array("servers")
assertTrue(servers!!.any { s -> s.get("url").equals("http://$customHost:$customPort") })
}

private fun runTheBuild(vararg additionalArguments: String = emptyArray()) =
GradleRunner.create()
.withProjectDir(projectTestDir)
Expand Down