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

[Chore/#445] Swagger 생성 Task 개선 #446

Merged
merged 1 commit into from
Dec 14, 2024
Merged
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
126 changes: 72 additions & 54 deletions api/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@

import com.epages.restdocs.apispec.gradle.OpenApi3Task
import org.springframework.boot.gradle.tasks.bundling.BootJar
import java.util.Random
import org.yaml.snakeyaml.DumperOptions
import org.yaml.snakeyaml.Yaml
import java.io.FileInputStream
import java.io.FileWriter
import java.io.InputStream
import java.util.*


tasks.withType(BootJar::class.java) {
loaderImplementation = org.springframework.boot.loader.tools.LoaderImplementation.CLASSIC
Expand Down Expand Up @@ -40,64 +47,75 @@ dependencies {
implementation("org.jsoup:jsoup:1.15.3")
}

tasks.named("generateStaticSwaggerUI") {
tasks.withType(OpenApi3Task::class.java) {
val multipartformdataPaths = listOf(
"/api/v1/admin/utilities/conversion/image",
"/api/v1/admin/utilities/conversion/content"
)
doLast {
val swaggerSpecSource = "$projectDir/src/main/resources/static/docs/${project.name}/swagger-ui/swagger-spec.js"

file(swaggerSpecSource).writeText(
file(swaggerSpecSource).readText().replace(
"operationId\" : \"PutImageApi\",",
"operationId\" : \"PutImageApi\",\n" +
putImageRequestScriptSource
)
)

file(swaggerSpecSource).writeText(
file(swaggerSpecSource).readText().replace(
"operationId\" : \"ConvertContentApi\",",
"operationId\" : \"ConvertContentApi\",\n" +
putContentRequestScriptSource
val input: InputStream = FileInputStream(File("$projectDir/src/main/resources/static/openapi3.yaml"))
val options = DumperOptions().apply {
defaultFlowStyle = DumperOptions.FlowStyle.BLOCK
isPrettyFlow = true
}
val yaml = Yaml(options)
val yamlData = yaml.loadAll(input)
for (data in yamlData) {
val content = data as MutableMap<*, *>
val paths = content["paths"] as MutableMap<*, *>
paths.forEach { (path, _methods) ->
val methods = _methods as MutableMap<*, *>
// Add security to paths that require Authorization header
methods.forEach { (_, _details) ->
val details = _details as MutableMap<String, Any>
if (details.containsKey("parameters")) {
val parameters = details["parameters"] as List<Map<String, Any>>
parameters.forEach { param ->
if (param["name"] == "Authorization") {
details["security"] = listOf(mapOf("bearerAuth" to emptyList<String>()))
}
}
}
}

// Add requestBody for multipart/form-data paths
if (multipartformdataPaths.contains(path)) {
if (methods.containsKey("post")) {
val post = methods["post"] as MutableMap<String, Any>
if (!post.containsKey("requestBody")) {
post["requestBody"] = mutableMapOf(
"content" to mutableMapOf(
"multipart/form-data" to mutableMapOf(
"schema" to mutableMapOf(
"type" to "object",
"properties" to mutableMapOf(
"source" to mutableMapOf(
"type" to "string",
"format" to "binary"
)
)
)
)
)
)
}
}
}
}
val components = content["components"] as MutableMap<String, MutableMap<String, Any>>
components["securitySchemes"] = mutableMapOf(
"bearerAuth" to mutableMapOf(
"type" to "http",
"scheme" to "bearer",
"bearerFormat" to "JWT"
)
)
)
val output = File("$projectDir/src/main/resources/static/openapi3.yaml")
yaml.dump(content, FileWriter(output))
}
}
}

val putImageRequestScriptSource = "" +
" \"requestBody\" : {\n" +
" \"content\" : {\n" +
" \"multipart/form-data\" : {\n" +
" \"schema\" : {\n" +
" \"type\" : \"object\",\n" +
" \"properties\" : {\n" +
" \"source\" : {\n" +
" \"type\" : \"string\",\n" +
" \"format\" : \"binary\"\n" +
" }\n" +
" }\n" +
"\n" +
" }\n" +
" }\n" +
" }\n" +
" },"

val putContentRequestScriptSource = "" +
" \"requestBody\" : {\n" +
" \"content\" : {\n" +
" \"multipart/form-data\" : {\n" +
" \"schema\" : {\n" +
" \"type\" : \"object\",\n" +
" \"properties\" : {\n" +
" \"content\" : {\n" +
" \"type\" : \"string\",\n" +
" \"format\" : \"binary\"\n" +
" }\n" +
" }\n" +
"\n" +
" }\n" +
" }\n" +
" }\n" +
" },"

val imageName = project.hasProperty("imageName").let {
if (it) {
project.property("imageName") as String
Expand Down
Loading