Skip to content
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

Show more informative error message on parsing error #108

Merged
merged 1 commit into from
Aug 15, 2023
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
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