From 08d10305b60e5a264404c570131458250b863433 Mon Sep 17 00:00:00 2001 From: Anton Shabouta Date: Thu, 15 Aug 2019 11:39:04 +0300 Subject: [PATCH] Bug fixes and refactoring --- README.md | 2 +- build.gradle | 7 ++- .../phpinnacle/redoc/RedocApplication.java | 46 +++++++++++++------ .../com/phpinnacle/redoc/RedocServer.java | 13 +++--- src/main/resources/META-INF/plugin.xml | 8 ++++ 5 files changed, 53 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 3cf89ea..155fa5b 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ Please see [CONTRIBUTING](CONTRIBUTING.md) and [CONDUCT](CONDUCT.md) for details The MIT License (MIT). Please see [License File](LICENSE.md) for more information. -[ico-version]: https://img.shields.io/jetbrains/plugin/v/intellij-redoc?style=flat-square +[ico-version]: https://img.shields.io/jetbrains/plugin/v/12822-redoc?style=flat-square [ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square [link-redoc]: https://github.com/Redocly/redoc diff --git a/build.gradle b/build.gradle index 45a03f1..1273f62 100644 --- a/build.gradle +++ b/build.gradle @@ -31,11 +31,14 @@ javafx { } publishPlugin { - token pluginToken + token intellijPluginToken } patchPluginXml { changeNotes """ - Refactoring and CS fixes + Swagger 2.0 specification support + + Refactoring + CS fixes """ } diff --git a/src/main/java/com/phpinnacle/redoc/RedocApplication.java b/src/main/java/com/phpinnacle/redoc/RedocApplication.java index f156b31..ec56f75 100644 --- a/src/main/java/com/phpinnacle/redoc/RedocApplication.java +++ b/src/main/java/com/phpinnacle/redoc/RedocApplication.java @@ -25,7 +25,7 @@ import org.jetbrains.yaml.psi.impl.YAMLDocumentImpl; import org.jetbrains.yaml.psi.impl.YAMLKeyValueImpl; -import java.util.Objects; +import java.util.*; class RedocApplication implements Disposable { private RedocServer server = RedocServer.getInstance(); @@ -33,6 +33,13 @@ class RedocApplication implements Disposable { private FileDocumentManager manager = FileDocumentManager.getInstance(); private Application application = ApplicationManager.getApplication(); + private Map openApiFields = new HashMap<>(); + + RedocApplication() { + openApiFields.put("swagger", "2.0"); + openApiFields.put("openapi", "3.0.0"); + } + boolean isAcceptable(@NotNull Project project, @NotNull VirtualFile file) { PsiFile psiFile = PsiManager.getInstance(project).findFile(file); @@ -46,11 +53,11 @@ boolean isAcceptable(@NotNull Project project, @NotNull VirtualFile file) { @NotNull FileEditor createEditor(@NotNull VirtualFile file) { RedocEditor editor = new RedocEditor(server, settings, file.getPath()); - Document document = manager.getDocument(file); + Document document = Objects.requireNonNull(manager.getDocument(file)); - editor.setup(Objects.requireNonNull(document)); + editor.setup(document); - server.attach(file.getPath(), document.getText()); + server.attach(file.getPath(), document); application.getMessageBus() .connect(editor) @@ -76,11 +83,13 @@ private boolean isAcceptableJSON(@NotNull PsiFile psiFile) { continue; } - JsonProperty schema = ((JsonObjectImpl) element).findProperty("openapi"); - JsonValue value = schema != null ? schema.getValue() : null; + for (String key : openApiFields.keySet()) { + JsonProperty property = ((JsonObjectImpl) element).findProperty(key); + JsonValue value = property != null ? property.getValue() : null; - if (value != null) { - return value.textMatches("\"3.0.0\""); + if (value != null) { + return matchValue(value, key); + } } } @@ -110,14 +119,16 @@ private boolean isAcceptableYAML(@NotNull PsiFile psiFile) { continue; } - if (!"openapi".equals(child.getName())) { - continue; - } + for (String key : openApiFields.keySet()) { + if (!key.equals(child.getName())) { + continue; + } - YAMLValue value = ((YAMLKeyValueImpl) child).getValue(); + YAMLValue value = ((YAMLKeyValueImpl) child).getValue(); - if (value != null) { - return value.textMatches("3.0.0"); + if (value != null) { + return matchValue(value, key); + } } } } @@ -131,4 +142,11 @@ public void dispose() { server.dispose(); } + + private boolean matchValue(PsiElement element, String key) + { + String version = openApiFields.get(key); + + return element.textMatches(version) || element.textMatches("\"" + version + "\""); + } } diff --git a/src/main/java/com/phpinnacle/redoc/RedocServer.java b/src/main/java/com/phpinnacle/redoc/RedocServer.java index 790ea78..9ce89f2 100644 --- a/src/main/java/com/phpinnacle/redoc/RedocServer.java +++ b/src/main/java/com/phpinnacle/redoc/RedocServer.java @@ -2,6 +2,7 @@ import com.intellij.openapi.Disposable; import com.intellij.openapi.components.ServiceManager; +import com.intellij.openapi.editor.Document; import com.intellij.openapi.util.Disposer; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; @@ -29,8 +30,8 @@ public static RedocServer getInstance() { return ServiceManager.getService(RedocServer.class); } - void attach(@NotNull String spec, @NotNull String text) { - handler.attach(spec, text); + void attach(@NotNull String spec, @NotNull Document document) { + handler.attach(spec, document); } void detach(@NotNull String spec) { @@ -50,10 +51,10 @@ public void dispose() { } private static class RootHandler implements HttpHandler { - private Map documents = new HashMap<>(); + private Map documents = new HashMap<>(); - void attach(@NotNull String spec, @NotNull String text) { - documents.put(spec, text); + void attach(@NotNull String spec, @NotNull Document document) { + documents.put(spec, document); } void detach(@NotNull String spec) { @@ -77,7 +78,7 @@ public void handle(HttpExchange httpExchange) throws IOException { String path = httpExchange.getRequestURI().getPath(); if (documents.containsKey(path)) { - text = documents.get(path); + text = documents.get(path).getText(); } else { status = 404; } diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 0426f0b..8030bca 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -5,6 +5,14 @@ Redoc JS library + +

Features:

+ +
    +
  1. OpenAPI 3.0 and Swagger 2.0 specifications
  2. +
  3. Specifications in YAML or JSON
  4. +
  5. Redoc settings supported
  6. +
]]>