Skip to content

Commit

Permalink
Show more informative error message on parsing error
Browse files Browse the repository at this point in the history
If the API documentation JSON file is incorrect, it only shows GSON's
error message, which is quite difficult to understand at first.

Here is the old error message:

Caused by: com.google.gson.JsonSyntaxException:
Expected a com.google.gson.JsonObject but was com.google.gson.JsonPrimitive
  • Loading branch information
sh1nj1 committed Apr 5, 2023
1 parent bb1a68a commit 67af4ef
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.springdoc.openapi.gradle.plugin

import com.google.gson.GsonBuilder
import com.google.gson.JsonObject
import com.google.gson.JsonSyntaxException
import org.awaitility.Durations
import org.awaitility.core.ConditionTimeoutException
import org.awaitility.kotlin.*
Expand Down Expand Up @@ -95,7 +96,12 @@ open class OpenApiGeneratorTask : DefaultTask() {

private fun prettifyJson(response: String): String {
val gson = GsonBuilder().setPrettyPrinting().create()
val googleJsonObject = gson.fromJson(response, JsonObject::class.java)
return gson.toJson(googleJsonObject)
try {
val googleJsonObject = gson.fromJson(response, JsonObject::class.java)
return gson.toJson(googleJsonObject)
} catch (e: RuntimeException) {
throw JsonSyntaxException("Failed to parse the API docs response string. " +
"Please ensure that the response is in the correct format. response=$response", e)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import org.gradle.testkit.runner.GradleRunner
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.BeforeEach
import org.junit.jupiter.api.Test
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.io.File
import java.io.FileReader
import java.nio.file.Files
Expand Down Expand Up @@ -47,6 +50,10 @@ class OpenApiGradlePluginTest {
}
""".trimIndent()

companion object {
val logger: Logger = LoggerFactory.getLogger(OpenApiGradlePluginTest::class.java)
}

@BeforeEach
fun createTemporaryAcceptanceProjectFromTemplate() {
File(javaClass.classLoader.getResource("acceptance-project")!!.path).copyRecursively(projectTestDir)
Expand Down Expand Up @@ -278,6 +285,26 @@ class OpenApiGradlePluginTest {
assertOpenApiJsonFile(2, outputJsonFileNameGroupB)
}

@Test
fun `using invalid doc url`() {
buildFile.writeText(
"""$baseBuildGradle
openApi{
apiDocsUrl = "http://localhost:8080/hello/world"
}
""".trimMargin()
)

try {
openApiDocsTask(runTheBuild())
} catch (e: RuntimeException) {
logger.error(e.message)
assertNotNull(e.message?.lines()?.find { it.contains(
"Failed to parse the API docs response string. " +
"Please ensure that the response is in the correct format.") })
}
}

private fun runTheBuild(vararg additionalArguments: String = emptyArray()) = GradleRunner.create()
.withProjectDir(projectTestDir)
.withArguments("clean", "generateOpenApiDocs", *additionalArguments)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.example.demo.endpoints;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController("/hello")
@RequestMapping("/hello")
public class HelloWorldController {

@GetMapping("/world")
@ResponseBody
public String helloWorld() {
return "Hello World!";
}
Expand Down

0 comments on commit 67af4ef

Please sign in to comment.