Skip to content

fix: #76 yaml group export #77

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
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
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ dependencies {
testImplementation(platform("org.junit:junit-bom:5.7.1"))
testImplementation("org.junit.jupiter:junit-jupiter")
testImplementation("com.beust:klaxon:5.5")
testImplementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.13.2")
testImplementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.13.2")

detektPlugins("io.gitlab.arturbosch.detekt:detekt-formatting:1.16.0")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ open class OpenApiGeneratorTask : DefaultTask() {
logger.info("Generating OpenApi Docs..")
val response: Response = khttp.get(url)

val isYaml = url.toLowerCase().matches(Regex(".+[./]yaml$"))
val isYaml = url.toLowerCase().matches(Regex(".+[./]yaml(/.+)*"))
val apiDocs = if (isYaml) response.text else prettifyJson(response)

val outputFile = outputDir.file(fileName).get().asFile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ 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
import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.GradleRunner
Expand All @@ -20,6 +24,9 @@ class OpenApiGradlePluginTest {
private val buildFile = File(projectTestDir, "build.gradle")
private val projectBuildDir = File(projectTestDir, "build")

private val pathsField = "paths"
private val openapiField = "openapi"

private val baseBuildGradle = """plugins {
id 'java'
id 'org.springframework.boot' version '2.4.5'
Expand Down Expand Up @@ -149,6 +156,24 @@ class OpenApiGradlePluginTest {
assertOpenApiJsonFile(1)
}

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

buildFile.writeText(
"""$baseBuildGradle

openApi{
apiDocsUrl = "http://localhost:8080/v3/api-docs.yaml"
outputFileName = "$outputYamlFileName"
}
""".trimMargin()
)

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

@Test
fun `using multiple grouped apis`() {
val outputJsonFileNameGroupA = "openapi-groupA.json"
Expand All @@ -171,6 +196,28 @@ class OpenApiGradlePluginTest {
assertOpenApiJsonFile(2, outputJsonFileNameGroupB)
}

@Test
fun `using multiple grouped apis with yaml`() {
val outputYamlFileNameGroupA = "openapi-groupA.yaml"
val outputYamlFileNameGroupB = "openapi-groupB.yaml"

buildFile.writeText(
"""$baseBuildGradle
bootRun {
args = ["--spring.profiles.active=multiple-grouped-apis"]
}
openApi{
groupedApiMappings = ["http://localhost:8080/v3/api-docs.yaml/groupA": "$outputYamlFileNameGroupA",
"http://localhost:8080/v3/api-docs.yaml/groupB": "$outputYamlFileNameGroupB"]
}
""".trimMargin()
)

assertEquals(TaskOutcome.SUCCESS, openApiDocsTask(runTheBuild()).outcome)
assertOpenApiYamlFile(1, outputYamlFileNameGroupA)
assertOpenApiYamlFile(2, outputYamlFileNameGroupB)
}

@Test
fun `using multiple grouped apis should ignore single api properties`() {
val outputJsonFileNameSingleGroupA = "openapi-single-groupA.json"
Expand Down Expand Up @@ -209,11 +256,23 @@ class OpenApiGradlePluginTest {
buildDir: File = projectBuildDir
) {
val openApiJson = getOpenApiJsonAtLocation(File(buildDir, outputJsonFileName))
assertEquals("3.0.1", openApiJson.string("openapi"))
assertEquals(expectedPathCount, openApiJson.obj("paths")!!.size)
assertEquals("3.0.1", openApiJson.string(openapiField))
assertEquals(expectedPathCount, openApiJson.obj(pathsField)!!.size)
}

private fun getOpenApiJsonAtLocation(path: File) = Parser.default().parse(FileReader(path)) as JsonObject

private fun assertOpenApiYamlFile(
expectedPathCount: Int,
outputJsonFileName: String = DEFAULT_OPEN_API_FILE_NAME,
buildDir: File = projectBuildDir
) {
val mapper = ObjectMapper(YAMLFactory())
mapper.registerModule(KotlinModule.Builder().build())
val node = mapper.readTree(File(buildDir, outputJsonFileName))
assertEquals("3.0.1", node.get(openapiField).asText())
assertEquals(expectedPathCount, node.get(pathsField)!!.size())
}

private fun openApiDocsTask(result: BuildResult) = result.tasks.find { it.path.contains("generateOpenApiDocs") }!!
}