@@ -2,6 +2,10 @@ package org.springdoc.openapi.gradle.plugin
2
2
3
3
import com.beust.klaxon.JsonObject
4
4
import com.beust.klaxon.Parser
5
+ import com.fasterxml.jackson.databind.ObjectMapper
6
+ import com.fasterxml.jackson.databind.node.TextNode
7
+ import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
8
+ import com.fasterxml.jackson.module.kotlin.KotlinModule
5
9
import org.gradle.internal.impldep.org.apache.commons.lang.RandomStringUtils
6
10
import org.gradle.testkit.runner.BuildResult
7
11
import org.gradle.testkit.runner.GradleRunner
@@ -20,6 +24,9 @@ class OpenApiGradlePluginTest {
20
24
private val buildFile = File (projectTestDir, " build.gradle" )
21
25
private val projectBuildDir = File (projectTestDir, " build" )
22
26
27
+ private val pathsField = " paths"
28
+ private val openapiField = " openapi"
29
+
23
30
private val baseBuildGradle = """ plugins {
24
31
id 'java'
25
32
id 'org.springframework.boot' version '2.4.5'
@@ -149,6 +156,24 @@ class OpenApiGradlePluginTest {
149
156
assertOpenApiJsonFile(1 )
150
157
}
151
158
159
+ @Test
160
+ fun `yaml generation` () {
161
+ val outputYamlFileName = " openapi.yaml"
162
+
163
+ buildFile.writeText(
164
+ """ $baseBuildGradle
165
+
166
+ openApi{
167
+ apiDocsUrl = "http://localhost:8080/v3/api-docs.yaml"
168
+ outputFileName = "$outputYamlFileName "
169
+ }
170
+ """ .trimMargin()
171
+ )
172
+
173
+ assertEquals(TaskOutcome .SUCCESS , openApiDocsTask(runTheBuild()).outcome)
174
+ assertOpenApiYamlFile(1 , outputYamlFileName)
175
+ }
176
+
152
177
@Test
153
178
fun `using multiple grouped apis` () {
154
179
val outputJsonFileNameGroupA = " openapi-groupA.json"
@@ -171,6 +196,28 @@ class OpenApiGradlePluginTest {
171
196
assertOpenApiJsonFile(2 , outputJsonFileNameGroupB)
172
197
}
173
198
199
+ @Test
200
+ fun `using multiple grouped apis with yaml` () {
201
+ val outputYamlFileNameGroupA = " openapi-groupA.yaml"
202
+ val outputYamlFileNameGroupB = " openapi-groupB.yaml"
203
+
204
+ buildFile.writeText(
205
+ """ $baseBuildGradle
206
+ bootRun {
207
+ args = ["--spring.profiles.active=multiple-grouped-apis"]
208
+ }
209
+ openApi{
210
+ groupedApiMappings = ["http://localhost:8080/v3/api-docs.yaml/groupA": "$outputYamlFileNameGroupA ",
211
+ "http://localhost:8080/v3/api-docs.yaml/groupB": "$outputYamlFileNameGroupB "]
212
+ }
213
+ """ .trimMargin()
214
+ )
215
+
216
+ assertEquals(TaskOutcome .SUCCESS , openApiDocsTask(runTheBuild()).outcome)
217
+ assertOpenApiYamlFile(1 , outputYamlFileNameGroupA)
218
+ assertOpenApiYamlFile(2 , outputYamlFileNameGroupB)
219
+ }
220
+
174
221
@Test
175
222
fun `using multiple grouped apis should ignore single api properties` () {
176
223
val outputJsonFileNameSingleGroupA = " openapi-single-groupA.json"
@@ -209,11 +256,23 @@ class OpenApiGradlePluginTest {
209
256
buildDir : File = projectBuildDir
210
257
) {
211
258
val openApiJson = getOpenApiJsonAtLocation(File (buildDir, outputJsonFileName))
212
- assertEquals(" 3.0.1" , openApiJson.string(" openapi " ))
213
- assertEquals(expectedPathCount, openApiJson.obj(" paths " )!! .size)
259
+ assertEquals(" 3.0.1" , openApiJson.string(openapiField ))
260
+ assertEquals(expectedPathCount, openApiJson.obj(pathsField )!! .size)
214
261
}
215
262
216
263
private fun getOpenApiJsonAtLocation (path : File ) = Parser .default().parse(FileReader (path)) as JsonObject
217
264
265
+ private fun assertOpenApiYamlFile (
266
+ expectedPathCount : Int ,
267
+ outputJsonFileName : String = DEFAULT_OPEN_API_FILE_NAME ,
268
+ buildDir : File = projectBuildDir
269
+ ) {
270
+ val mapper = ObjectMapper (YAMLFactory ())
271
+ mapper.registerModule(KotlinModule .Builder ().build())
272
+ val node = mapper.readTree(File (buildDir, outputJsonFileName))
273
+ assertEquals(" 3.0.1" , node.get(openapiField).asText())
274
+ assertEquals(expectedPathCount, node.get(pathsField)!! .size())
275
+ }
276
+
218
277
private fun openApiDocsTask (result : BuildResult ) = result.tasks.find { it.path.contains(" generateOpenApiDocs" ) }!!
219
278
}
0 commit comments