Skip to content

Commit ebd3743

Browse files
committed
Added map property to set custom headers Fixes #144
1 parent d6f3d3e commit ebd3743

File tree

4 files changed

+56
-6
lines changed

4 files changed

+56
-6
lines changed

README.md

+9-4
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,24 @@ openApi {
8686
customBootRun {
8787
args.set(["--spring.profiles.active=special"])
8888
}
89+
requestHeaders = [
90+
"x-forwarded-host": "custom-host",
91+
"x-forwarded-port": "7000"
92+
]
8993
}
9094
```
9195

9296
| Parameter | Description | Required | Default |
9397
|----------------------|-------------------------------------------------------------------------------------------------------------------------------------|----------|--------------------------------------|
94-
| `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 |
98+
| `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 |
9599
| `outputDir` | The output directory for the generated OpenAPI file | No | $buildDir - Your project's build dir |
96-
| `outputFileName` | Specifies the output file name. | No | openapi.json |
100+
| `outputFileName` | Specifies the output file name. | No | openapi.json |
97101
| `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 |
98102
| `trustStore` | Path to a trust store that contains custom trusted certificates. | No | `<None>` |
99103
| `trustStorePassword` | Password to open Trust Store | No | `<None>` |
100104
| `groupedApiMappings` | A map of URLs (from where the OpenAPI docs can be downloaded) to output file names | No | [] |
101105
| `customBootRun` | Any bootRun property that you would normal need to start your spring boot application. | No | (N/A) |
106+
| `requestHeaders` | customize Generated server url, relies on `server.forward-headers-strategy=framework` | No | (N/A) |
102107

103108
### `customBootRun` properties examples
104109

@@ -178,7 +183,7 @@ OpenAPI doc.
178183
in `build.gradle.kts`
179184

180185
```
181-
id("org.springdoc.openapi-gradle-plugin") version "1.9.0"
186+
id("org.springdoc.openapi-gradle-plugin") version "1.8.0"
182187
```
183188
184189
3. Add the following to the spring boot apps `settings.gradle`
@@ -197,4 +202,4 @@ OpenAPI doc.
197202
* Thanks a lot [JetBrains](https://www.jetbrains.com/?from=springdoc-openapi) for
198203
supporting springdoc-openapi project.
199204
200-
![JetBrains logo](https://springdoc.org/img/jetbrains.svg)
205+
![JetBrains logo](https://springdoc.org/img/jetbrains.svg)

src/main/kotlin/org/springdoc/openapi/gradle/plugin/OpenApiExtension.kt

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ open class OpenApiExtension @Inject constructor(
2323

2424
val groupedApiMappings: MapProperty<String, String> =
2525
objects.mapProperty(String::class.java, String::class.java)
26+
val requestHeaders: MapProperty<String, String> =
27+
objects.mapProperty(String::class.java, String::class.java)
2628
val customBootRun: CustomBootRunAction =
2729
objects.newInstance(CustomBootRunAction::class.java)
2830

src/main/kotlin/org/springdoc/openapi/gradle/plugin/OpenApiGeneratorTask.kt

+15-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ open class OpenApiGeneratorTask : DefaultTask() {
4444
val outputFileName: Property<String> = project.objects.property(String::class.java)
4545

4646
@get:Input
47-
val groupedApiMappings: MapProperty<String, String> = project.objects.mapProperty(String::class.java, String::class.java)
47+
val groupedApiMappings: MapProperty<String, String> =
48+
project.objects.mapProperty(String::class.java, String::class.java)
49+
50+
@get:Input
51+
val requestHeaders: MapProperty<String, String> =
52+
project.objects.mapProperty(String::class.java, String::class.java)
4853

4954
@get:OutputDirectory
5055
val outputDir: DirectoryProperty = project.objects.directoryProperty()
@@ -74,6 +79,7 @@ open class OpenApiGeneratorTask : DefaultTask() {
7479
waitTimeInSeconds.convention(extension.waitTimeInSeconds)
7580
trustStore.convention(extension.trustStore)
7681
trustStorePassword.convention(extension.trustStorePassword)
82+
requestHeaders.convention(extension.requestHeaders)
7783
}
7884

7985
@TaskAction
@@ -97,16 +103,23 @@ open class OpenApiGeneratorTask : DefaultTask() {
97103
val connection: HttpURLConnection =
98104
URL(url).openConnection() as HttpURLConnection
99105
connection.requestMethod = "GET"
106+
requestHeaders.get().forEach { header ->
107+
connection.setRequestProperty(header.key, header.value)
108+
}
109+
100110
connection.connect()
101111
val statusCode = connection.responseCode
102-
logger.debug("apiDocsUrl = {} status code = {}", url, statusCode)
112+
logger.trace("apiDocsUrl = {} status code = {}", url, statusCode)
103113
statusCode < MAX_HTTP_STATUS_CODE
104114
}
105115
logger.info("Generating OpenApi Docs..")
106116
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.socketFactory)
107117
val connection: HttpURLConnection =
108118
URL(url).openConnection() as HttpURLConnection
109119
connection.requestMethod = "GET"
120+
requestHeaders.get().forEach { header ->
121+
connection.setRequestProperty(header.key, header.value)
122+
}
110123
connection.connect()
111124

112125
val response = String(connection.inputStream.readBytes(), Charsets.UTF_8)

src/test/kotlin/org/springdoc/openapi/gradle/plugin/OpenApiGradlePluginTest.kt

+30
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.springdoc.openapi.gradle.plugin
22

3+
import com.beust.klaxon.JsonArray
34
import com.beust.klaxon.JsonObject
45
import com.beust.klaxon.Parser
56
import com.fasterxml.jackson.databind.ObjectMapper
@@ -12,6 +13,7 @@ import org.gradle.testkit.runner.TaskOutcome
1213
import org.junit.jupiter.api.Assertions.assertEquals
1314
import org.junit.jupiter.api.Assertions.assertFalse
1415
import org.junit.jupiter.api.Assertions.assertNotNull
16+
import org.junit.jupiter.api.Assertions.assertTrue
1517
import org.junit.jupiter.api.BeforeEach
1618
import org.junit.jupiter.api.Test
1719
import org.slf4j.Logger
@@ -356,6 +358,34 @@ class OpenApiGradlePluginTest {
356358
}
357359
}
358360

361+
@Test
362+
fun `adding headers for custom generated url`() {
363+
val outputJsonFileName: String = DEFAULT_OPEN_API_FILE_NAME
364+
val buildDir: File = projectBuildDir
365+
val customHost = "custom-host"
366+
val customPort = "7000"
367+
368+
buildFile.writeText(
369+
"""$baseBuildGradle
370+
bootRun {
371+
args = ["--server.forward-headers-strategy=framework"]
372+
}
373+
openApi{
374+
outputFileName = "$outputJsonFileName"
375+
requestHeaders = [
376+
"x-forwarded-host": "$customHost",
377+
"x-forwarded-port": "$customPort"
378+
]
379+
}
380+
""".trimMargin())
381+
382+
assertEquals(TaskOutcome.SUCCESS, openApiDocsTask(runTheBuild()).outcome)
383+
assertOpenApiJsonFile(1, outputJsonFileName)
384+
val openApiJson = getOpenApiJsonAtLocation(File(buildDir, outputJsonFileName))
385+
val servers: JsonArray<Map<String, String>>? = openApiJson.array("servers")
386+
assertTrue(servers!!.any { s -> s.get("url").equals("http://$customHost:$customPort") })
387+
}
388+
359389
private fun runTheBuild(vararg additionalArguments: String = emptyArray()) =
360390
GradleRunner.create()
361391
.withProjectDir(projectTestDir)

0 commit comments

Comments
 (0)