-
Notifications
You must be signed in to change notification settings - Fork 0
ApiDoc-1 RestDoc 적용하기 #11
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
Changes from all commits
6498237
75a5f1e
1aefc2d
5c32dc6
bc9e03e
e0ade36
3c029a4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,71 @@ | ||
import groovy.lang.Closure | ||
import io.swagger.v3.oas.models.servers.Server | ||
import org.hidetake.gradle.swagger.generator.GenerateSwaggerUI | ||
import org.springframework.boot.gradle.tasks.bundling.BootJar | ||
|
||
plugins { | ||
id("com.epages.restdocs-api-spec") version "0.18.0" | ||
id("org.hidetake.swagger.generator") version "2.19.2" | ||
} | ||
|
||
dependencies { | ||
val springVersion by properties | ||
|
||
implementation(project(":support:yaml")) | ||
implementation(project(":core")) | ||
|
||
implementation("org.springframework.boot:spring-boot-starter-web:$springVersion") | ||
|
||
testImplementation("org.springframework.boot:spring-boot-starter-test:$springVersion") | ||
testImplementation("org.springframework.restdocs:spring-restdocs-mockmvc:3.0.0") | ||
testImplementation("com.epages:restdocs-api-spec-mockmvc:0.18.0") | ||
swaggerCodegen("io.swagger.codegen.v3:swagger-codegen-cli:3.0.42") | ||
swaggerUI("org.webjars:swagger-ui:4.18.2") | ||
} | ||
|
||
tasks { | ||
withType<Jar> { enabled = false } | ||
withType<BootJar> { | ||
enabled = true | ||
dependsOn("copySwaggerUI") | ||
mainClass.set("yapp.rating.boardgame.WebApplicationKt") | ||
} | ||
|
||
val generateSwaggerUIPrefix = "Api" | ||
openapi3 { | ||
setServers( | ||
listOf( | ||
toServer("http://localhost:8080"), | ||
) | ||
) | ||
title = "My API" | ||
description = "My API description" | ||
// tagDescriptionsPropertiesFile = "src/docs/tag-descriptions.yaml" | ||
version = "0.1.0" | ||
format = "yaml" | ||
} | ||
postman { | ||
title = "My API" | ||
version = "0.1.0" | ||
baseUrl = "https://localhost:8080" | ||
} | ||
swaggerSources { | ||
create(generateSwaggerUIPrefix).apply { | ||
setInputFile(file("${project.buildDir}/api-spec/openapi3.yaml")) | ||
} | ||
} | ||
withType<GenerateSwaggerUI> { | ||
dependsOn("openapi3") | ||
} | ||
register<Copy>("copySwaggerUI") { | ||
dependsOn("generateSwaggerUI$generateSwaggerUIPrefix") | ||
|
||
val generateSwaggerUISampleTask = | ||
[email protected]<GenerateSwaggerUI>("generateSwaggerUI$generateSwaggerUIPrefix").get() | ||
|
||
from("${generateSwaggerUISampleTask.outputDir}") | ||
into("src/main/resources/static/swagger") | ||
} | ||
} | ||
|
||
fun toServer(url: String): Closure<Server> = closureOf<Server> { this.url = url } as Closure<Server> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package yapp.rating | ||
|
||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
|
||
// TODO: Delete 테스트 코드 전용 클래스 | ||
@RestController | ||
@RequestMapping("/v1/test") | ||
class TestController { | ||
|
||
@GetMapping | ||
fun testGet(): TestResponse { | ||
return TestResponse("Good!") | ||
} | ||
|
||
data class TestResponse( | ||
val value: String | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package yapp.rating | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import io.kotest.core.spec.style.FunSpec | ||
import org.springframework.restdocs.ManualRestDocumentation | ||
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation | ||
import org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint | ||
import org.springframework.test.web.servlet.MockMvc | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders | ||
import org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder | ||
|
||
// @ExtendWith(RestDocumentationExtension::class) | ||
abstract class ControllerTest : FunSpec() { | ||
protected abstract val controller: Any | ||
protected lateinit var mockMvc: MockMvc | ||
private set | ||
|
||
private val restDocumentation = ManualRestDocumentation() | ||
|
||
init { | ||
beforeSpec { | ||
setUpMockMvc() | ||
} | ||
beforeEach { | ||
restDocumentation.beforeTest(javaClass, it.name.testName) | ||
} | ||
afterEach { | ||
restDocumentation.afterTest() | ||
} | ||
} | ||
|
||
private fun setUpMockMvc() { | ||
mockMvc = MockMvcBuilders.standaloneSetup(controller) | ||
.apply<StandaloneMockMvcBuilder>( | ||
MockMvcRestDocumentation.documentationConfiguration(restDocumentation) | ||
.uris().withScheme("http").withHost("localhost").and() | ||
.operationPreprocessors() | ||
.withRequestDefaults(prettyPrint()) | ||
.withResponseDefaults(prettyPrint()) | ||
) | ||
// .setControllerAdvice(ExceptionHandler()) | ||
// .setCustomArgumentResolvers() | ||
// .setMessageConverters() | ||
.build() | ||
} | ||
|
||
companion object { | ||
@JvmStatic | ||
protected val objectMapper = ObjectMapper() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package yapp.rating | ||
|
||
import com.epages.restdocs.apispec.MockMvcRestDocumentationWrapper.document | ||
import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status | ||
|
||
class TestControllerTest : ControllerTest() { | ||
override val controller = TestController() | ||
|
||
init { | ||
test("Get Test") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기서 RestDoc의 Kotlin DSL 을 사용 하면 Spring 과 호환이 되지 않아 정상적으로 작동하지 않습니다. 참고 Kotlin DSL 은 아래와 같은 코드를 말합니다.
|
||
mockMvc.perform(get("/v1/test")) | ||
.andExpect(status().isOk) | ||
.andDo( | ||
document("test") | ||
) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,11 @@ | ||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile | ||
import org.springframework.boot.gradle.tasks.bundling.BootJar | ||
|
||
plugins { | ||
kotlin("jvm") version "1.7.22" | ||
kotlin("kapt") version "1.7.22" | ||
id("org.jlleitschuh.gradle.ktlint") version "10.2.0" | ||
|
||
id("io.spring.dependency-management") version "1.1.0" | ||
|
||
id("org.springframework.boot") | ||
} | ||
|
||
|
@@ -33,18 +32,21 @@ subprojects { | |
dependencies { | ||
val kotestVersion: String by properties | ||
|
||
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.15.0") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Kotlin 에서는 Jackson이 정상적으로 작동하지 않아 추가했습니다. Jackson은 기본 생성자를 필요로 하는데 |
||
|
||
testImplementation("io.kotest:kotest-runner-junit5-jvm:$kotestVersion") | ||
testImplementation("io.kotest:kotest-assertions-core:$kotestVersion") | ||
} | ||
|
||
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> { | ||
kotlinOptions { | ||
freeCompilerArgs = listOf("-Xjsr305=strict") | ||
jvmTarget = "17" | ||
tasks { | ||
withType<KotlinCompile> { | ||
kotlinOptions { | ||
freeCompilerArgs = listOf("-Xjsr305=strict") | ||
jvmTarget = "17" | ||
} | ||
} | ||
test { | ||
useJUnitPlatform() | ||
} | ||
} | ||
|
||
tasks.withType<Test> { | ||
useJUnitPlatform() | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
추후에 필요한 값들이라 주석으로 명시해뒀습니다.