Skip to content

Commit 1792fd7

Browse files
authored
Merge pull request #108 from sh1nj1/enhance/clearer-error-message
Show more informative error message on parsing error
2 parents c29bdb1 + 67af4ef commit 1792fd7

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.springdoc.openapi.gradle.plugin
22

33
import com.google.gson.GsonBuilder
44
import com.google.gson.JsonObject
5+
import com.google.gson.JsonSyntaxException
56
import org.awaitility.Durations
67
import org.awaitility.core.ConditionTimeoutException
78
import org.awaitility.kotlin.*
@@ -95,7 +96,12 @@ open class OpenApiGeneratorTask : DefaultTask() {
9596

9697
private fun prettifyJson(response: String): String {
9798
val gson = GsonBuilder().setPrettyPrinting().create()
98-
val googleJsonObject = gson.fromJson(response, JsonObject::class.java)
99-
return gson.toJson(googleJsonObject)
99+
try {
100+
val googleJsonObject = gson.fromJson(response, JsonObject::class.java)
101+
return gson.toJson(googleJsonObject)
102+
} catch (e: RuntimeException) {
103+
throw JsonSyntaxException("Failed to parse the API docs response string. " +
104+
"Please ensure that the response is in the correct format. response=$response", e)
105+
}
100106
}
101107
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ import org.gradle.testkit.runner.GradleRunner
1111
import org.gradle.testkit.runner.TaskOutcome
1212
import org.junit.jupiter.api.Assertions.assertEquals
1313
import org.junit.jupiter.api.Assertions.assertFalse
14+
import org.junit.jupiter.api.Assertions.assertNotNull
1415
import org.junit.jupiter.api.BeforeEach
1516
import org.junit.jupiter.api.Test
17+
import org.slf4j.Logger
18+
import org.slf4j.LoggerFactory
1619
import java.io.File
1720
import java.io.FileReader
1821
import java.nio.file.Files
@@ -47,6 +50,10 @@ class OpenApiGradlePluginTest {
4750
}
4851
""".trimIndent()
4952

53+
companion object {
54+
val logger: Logger = LoggerFactory.getLogger(OpenApiGradlePluginTest::class.java)
55+
}
56+
5057
@BeforeEach
5158
fun createTemporaryAcceptanceProjectFromTemplate() {
5259
File(javaClass.classLoader.getResource("acceptance-project")!!.path).copyRecursively(projectTestDir)
@@ -278,6 +285,26 @@ class OpenApiGradlePluginTest {
278285
assertOpenApiJsonFile(2, outputJsonFileNameGroupB)
279286
}
280287

288+
@Test
289+
fun `using invalid doc url`() {
290+
buildFile.writeText(
291+
"""$baseBuildGradle
292+
openApi{
293+
apiDocsUrl = "http://localhost:8080/hello/world"
294+
}
295+
""".trimMargin()
296+
)
297+
298+
try {
299+
openApiDocsTask(runTheBuild())
300+
} catch (e: RuntimeException) {
301+
logger.error(e.message)
302+
assertNotNull(e.message?.lines()?.find { it.contains(
303+
"Failed to parse the API docs response string. " +
304+
"Please ensure that the response is in the correct format.") })
305+
}
306+
}
307+
281308
private fun runTheBuild(vararg additionalArguments: String = emptyArray()) = GradleRunner.create()
282309
.withProjectDir(projectTestDir)
283310
.withArguments("clean", "generateOpenApiDocs", *additionalArguments)

src/test/resources/acceptance-project/src/main/java/com/example/demo/endpoints/HelloWorldController.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package com.example.demo.endpoints;
22

33
import org.springframework.web.bind.annotation.GetMapping;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
import org.springframework.web.bind.annotation.ResponseBody;
46
import org.springframework.web.bind.annotation.RestController;
57

68
@RestController("/hello")
9+
@RequestMapping("/hello")
710
public class HelloWorldController {
811

912
@GetMapping("/world")
13+
@ResponseBody
1014
public String helloWorld() {
1115
return "Hello World!";
1216
}

0 commit comments

Comments
 (0)