Skip to content

Commit

Permalink
Merge branch 'release/2.7.0' into feat/preview_panel
Browse files Browse the repository at this point in the history
  • Loading branch information
Pakisan committed Sep 7, 2024
2 parents 6ba163f + 1e6cf2b commit e247e82
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@ import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import com.intellij.json.JsonFileType
import com.intellij.openapi.components.Service
import com.intellij.openapi.components.service
import com.intellij.openapi.util.io.FileUtil
import com.intellij.openapi.util.io.systemIndependentPath
import com.intellij.openapi.vfs.LocalFileSystem
import com.intellij.openapi.vfs.VirtualFile
import io.netty.handler.codec.http.FullHttpRequest
import org.jetbrains.yaml.YAMLFileType
import java.io.File
import java.util.function.Supplier

/**
* @author Pavel Bodiachevskii
Expand Down Expand Up @@ -47,20 +44,19 @@ class AsyncAPISpecificationHtmlRenderer {

val isJson = specificationVirtualFile.fileType is JsonFileType
val specification = replaceLocalReferences(specificationFile.readText(Charsets.UTF_8), specificationVirtualFile, isJson)
val temporalSpecificationUrl = saveAsTemporalFile(specification, isJson)

val specificationTemplate = this.javaClass.getResource(specificationTemplateUrl)
specificationTemplate ?: return "specification template not found."

val webSocket = webSocketRendererProvider.provide(
fullHttpRequest = request,
supplier = Supplier<VirtualFile?> { specificationVirtualFile }
supplier = { specificationVirtualFile }
)

return specificationTemplate.readText(Charsets.UTF_8)
.replace(
"url: '',",
"url: '${urlProvider.specification(temporalSpecificationUrl)}',"
"schema: {},",
"schema: $specification,"
)
.replace(
"<link rel=\"stylesheet\" href=\"\">",
Expand All @@ -74,7 +70,7 @@ class AsyncAPISpecificationHtmlRenderer {
)
}

private fun replaceLocalReferences(specification: String, specificationFile: VirtualFile, isJson: Boolean): String {
fun replaceLocalReferences(specification: String, specificationFile: VirtualFile, isJson: Boolean): String {
val objectMapper = if (isJson) {
ObjectMapper()
} else {
Expand All @@ -89,7 +85,7 @@ class AsyncAPISpecificationHtmlRenderer {
}
}

return objectMapper.writeValueAsString(tree)
return ObjectMapper().writeValueAsString(tree)
}

private fun localReferenceToFileUrl(localReference: String, specificationFile: VirtualFile): String {
Expand All @@ -104,17 +100,4 @@ class AsyncAPISpecificationHtmlRenderer {
return urlProvider.reference(referencedFile.path, specificationComponentReference)
}

private fun saveAsTemporalFile(specification: String, isJson: Boolean): String {
val suffix = if (isJson) {
".json"
} else {
".yaml"
}

val tempSpecification = FileUtil.createTempFile("jasyncapi-idea-plugin-${System.currentTimeMillis()}", suffix, true)
tempSpecification.writeText(specification, Charsets.UTF_8)

return tempSpecification.systemIndependentPath
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.jetbrains.io.send
import org.jetbrains.yaml.YAMLFileType
import java.io.File
import java.nio.charset.StandardCharsets
import kotlin.text.toByteArray

/**
* @author Pavel Bodiachevskii
Expand Down Expand Up @@ -128,7 +129,12 @@ class StaticServer : HttpRequestHandler() {
"application/x-yaml"
}

return Resource(contentType, requestedFile.readBytes())
val componentJson = asyncAPISpecificationHtmlRenderer.replaceLocalReferences(
requestedFile.readText(StandardCharsets.UTF_8),
referenceVirtualFile,
isJson
)
return Resource(contentType, componentJson.toByteArray(StandardCharsets.UTF_8))
}

private fun resolveResource(resourceName: String): Resource? {
Expand Down
5 changes: 1 addition & 4 deletions src/main/resources/ui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
<script src=""></script>
<script>
AsyncApiStandalone.render({
schema: {
url: '',
options: { method: "GET", mode: "cors" },
},
schema: {},
config: {
show: {
sidebar: true,
Expand Down

0 comments on commit e247e82

Please sign in to comment.