Skip to content

Commit

Permalink
v2.0.2 Fixes
Browse files Browse the repository at this point in the history
Fixed issues with parsing yaml due in yamljs library. Changed to js-yaml library.
Fixed issue where validation errors are not cleared in yaml file.
  • Loading branch information
arjun-g committed Aug 12, 2018
1 parent 4265b62 commit 98e324c
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 81 deletions.
12 changes: 11 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,19 @@
"args": ["--extensionDevelopmentPath=${workspaceRoot}" ],
"stopOnEntry": false,
"sourceMaps": true,
"outFiles": ["${workspaceRoot}/out/src/**/*.js"],
"outFiles": ["${workspaceRoot}/out/**/*.js"],
"preLaunchTask": "npm compile"
},
{
"type": "node",
"request": "attach",
"name": "Attach to Server",
"address": "localhost",
"protocol": "inspector",
"port": 6009,
"sourceMaps": true,
"outFiles": ["${workspaceRoot}/out/language/server.js"]
},
{
"name": "Launch Tests",
"type": "extensionHost",
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Swagger Viewer - v2.0.0
# Swagger Viewer - v2.0.2
**Swagger Viewer lets you preview and validate Swagger 2.0 and OpenAPI files as you type in Visual Studio Code.**

It works on swagger files in json and yaml format. Preview and validation happens in real time as you type.
Expand Down Expand Up @@ -53,6 +53,10 @@ Swagger Viewer validates your documents against Swagger 2.0 and OpenAPI specific

## Releases

**v2.0.2 Changes**
* Fixed issues with parsing yaml due in yamljs library. Changed to js-yaml library.
* Fixed issue where validation errors are not cleared in yaml file.

**v2.0.0 Changes**
* Code base changed to TypeScript
* Partial validation support added
Expand Down
26 changes: 9 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 12 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,22 @@
"theme": "dark"
},
"license": "SEE LICENSE IN LICENSE.md",
"version": "2.0.1",
"version": "2.0.2",
"publisher": "Arjun",
"engines": {
"vscode": "^1.23.0"
},
"categories": [
"Other"
],
"keywords": [
"swagger",
"openapi",
"api",
"api documentation",
"json",
"yaml"
],
"activationEvents": [
"onLanguage:yaml",
"onLanguage:json",
Expand Down Expand Up @@ -81,21 +89,21 @@
},
"devDependencies": {
"@types/express": "^4.16.0",
"@types/js-yaml": "^3.11.2",
"@types/node": "^10.5.5",
"@types/socket.io": "^1.4.36",
"@types/swagger-parser": "^4.0.2",
"@types/yamljs": "^0.2.30",
"typescript": "^2.8.3"
},
"dependencies": {
"express": "^4.16.3",
"js-yaml": "^3.12.0",
"socket.io": "^2.1.1",
"swagger-parser": "^5.0.2",
"swagger-ui-dist": "^3.17.5",
"vscode": "^1.1.18",
"vscode-languageclient": "^4.1.4",
"vscode-languageserver": "^4.1.3",
"yamljs": "^0.3.0"
"vscode-languageserver": "^4.1.3"
},
"bugs": {
"url": "https://github.com/arjun-g/vs-swagger-viewer/issues"
Expand Down
94 changes: 46 additions & 48 deletions src/language/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
TextDocumentPositionParams
} from 'vscode-languageserver';
import * as SwaggerParser from 'swagger-parser';
import * as YAML from 'yamljs';
import * as YAML from 'js-yaml';

const SWAGGER_CODE_COMPLTE_DEFS = [
/** TODO */
Expand Down Expand Up @@ -111,70 +111,69 @@ documents.onDidChangeContent(change => {
function getParsedContent(document: TextDocument){
let fileContent = document.getText();
let fileObject: Object = null;
try{
if(fileContent.indexOf('swagger') >= 0 || fileContent.indexOf('openapi') >= 0){
if (document.languageId === "json") {
fileObject = JSON.parse(fileContent);
} else if (document.languageId === "yaml" || document.languageId === "yml") {
fileObject = YAML.parse(fileContent);
}
if(fileContent.indexOf('swagger') >= 0 || fileContent.indexOf('openapi') >= 0){
if (document.languageId === "json") {
fileObject = JSON.parse(fileContent);
} else if (document.languageId === "yaml" || document.languageId === "yml") {
fileObject = YAML.safeLoad(fileContent);// YAML.safeLoad(fileContent);
}
}
catch(err){
let diagnostics: Diagnostic[] = [];
let diagnostic: Diagnostic = {
severity: DiagnosticSeverity.Warning,
code: 0,
message: err.message,
range: {
start: {
line: 0,
character: 1
},
end: {
line: 0,
character: 1
}
},
source: "Swagger Viewer"
};

if (err.mark) {
diagnostic.range.start = diagnostic.range.end = {
line: err.mark.line,
character: err.mark.column
};
}

diagnostics.push(diagnostic);

connection.sendDiagnostics({
uri: document.uri,
diagnostics
});

connection.sendRequest("validated", err);
}
if(fileObject && typeof fileObject === 'object' && (fileObject.hasOwnProperty('swagger') || fileObject.hasOwnProperty('openapi'))){
return fileObject;
}
return null;
}

async function validateTextDocument(textDocument: TextDocument): Promise<void> {
let swaggerObject = getParsedContent(textDocument);
let swaggerObject = null;
try{
swaggerObject = getParsedContent(textDocument);
}
catch(ex){
let diagnostics: Diagnostic[] = [];
let diagnostic: Diagnostic = {
severity: DiagnosticSeverity.Warning,
code: 0,
message: ex.message,
range: {
start: {
line: 0,
character: 1
},
end: {
line: 0,
character: 1
}
},
source: "Swagger Viewer Parse"
};

if (ex.mark) {
diagnostic.range.start = diagnostic.range.end = {
line: ex.mark.line,
character: ex.mark.column
};
}

diagnostics.push(diagnostic);

connection.sendDiagnostics({
uri: textDocument.uri,
diagnostics
});

return;
}

if(!swaggerObject) return;

let obj = JSON.parse(textDocument.getText());
SwaggerParser.validate(obj)
SwaggerParser.validate(swaggerObject)
.then(api => {
let diagnostics: Diagnostic[] = [];
connection.sendDiagnostics({
uri: textDocument.uri,
diagnostics
});
connection.sendRequest("validated", null);
})
.catch(err => {
let diagnostics: Diagnostic[] = [];
Expand Down Expand Up @@ -209,7 +208,6 @@ async function validateTextDocument(textDocument: TextDocument): Promise<void> {
diagnostics
});

connection.sendRequest("validated", err);
})

}
Expand Down
24 changes: 14 additions & 10 deletions src/preview/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as vscode from 'vscode';
import * as YAML from 'yamljs';
import * as YAML from 'js-yaml';
import * as SwaggerParser from 'swagger-parser';

import { PreviewServer } from './server';
Expand Down Expand Up @@ -58,18 +58,22 @@ function hashString(str: string): string{

function getParsedContent(document: vscode.TextDocument){
const fileContent = document.getText();
console.log('LANGUAGE', document.languageId)
if (document.languageId === "json") {
return JSON.parse(fileContent);
} else if (document.languageId === "yaml") {
return YAML.parse(fileContent);
} else if (document.languageId === "plaintext") {
if (fileContent.match(/^\s*[{[]/)) {
try{
if (document.languageId === "json") {
return JSON.parse(fileContent);
} else {
return YAML.parse(fileContent);
} else if (document.languageId === "yaml") {
return YAML.safeLoad(fileContent);
} else if (document.languageId === "plaintext") {
if (fileContent.match(/^\s*[{[]/)) {
return JSON.parse(fileContent);
} else {
return YAML.safeLoad(fileContent);
}
}
}
catch(ex){
return null;
}
}

let previewServer: PreviewServer = null;
Expand Down

0 comments on commit 98e324c

Please sign in to comment.