Skip to content

Commit

Permalink
feat(resolve): add ApiDOM YAML 1.2 parser
Browse files Browse the repository at this point in the history
Refs #2717
  • Loading branch information
char0n committed Jan 23, 2023
1 parent c3de393 commit db4737f
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 48 deletions.
48 changes: 0 additions & 48 deletions src/helpers/apidom/openapi-3-1/parsers/json/index.js

This file was deleted.

50 changes: 50 additions & 0 deletions src/helpers/apidom/reference/parse/parsers/json/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { from, ParseResultElement } from '@swagger-api/apidom-core';
import { ParserError, Parser } from '@swagger-api/apidom-reference/configuration/empty';

const JsonParser = Parser.compose(Parser, {
props: {
name: 'json-swagger-client',
fileExtensions: ['.json'],
mediaTypes: ['application/json'],
},
methods: {
async canParse(file) {
const hasSupportedFileExtension =
this.fileExtensions.length === 0 ? true : this.fileExtensions.includes(file.extension);
const hasSupportedMediaType = this.mediaTypes.includes(file.mediaType);

if (!hasSupportedFileExtension) return false;
if (hasSupportedMediaType) return true;
if (!hasSupportedMediaType) {
try {
JSON.parse(file.toString());
return true;
} catch (error) {
return false;
}
}
return false;
},

async parse(file) {
if (this.sourceMap) {
// eslint-disable-next-line no-console
console.warn("json-swagger-client parser plugin doesn't support sourceMaps option");
}

const source = file.toString();

try {
const element = from(JSON.parse(source));
const parseResultElement = new ParseResultElement();

element.classes.push('result');
parseResultElement.push(element);
return parseResultElement;
} catch (error) {
throw new ParserError(`Error parsing "${file.uri}"`, error);
}
},
},
});
export default JsonParser;
52 changes: 52 additions & 0 deletions src/helpers/apidom/reference/parse/parsers/yaml-1-2/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import YAML, { JSON_SCHEMA } from 'js-yaml';
import { from, ParseResultElement } from '@swagger-api/apidom-core';
import { ParserError, Parser } from '@swagger-api/apidom-reference/configuration/empty';

const YamlParser = Parser.compose(Parser, {
props: {
name: 'yaml-1-2-swagger-client',
fileExtensions: ['.yaml', '.yml'],
mediaTypes: ['text/yaml', 'application/yaml'],
},
methods: {
async canParse(file) {
const hasSupportedFileExtension =
this.fileExtensions.length === 0 ? true : this.fileExtensions.includes(file.extension);
const hasSupportedMediaType = this.mediaTypes.includes(file.mediaType);

if (!hasSupportedFileExtension) return false;
if (hasSupportedMediaType) return true;
if (!hasSupportedMediaType) {
try {
YAML.load(file.toString(), { schema: JSON_SCHEMA });
return true;
} catch (error) {
return false;
}
}
return false;
},

async parse(file) {
if (this.sourceMap) {
// eslint-disable-next-line no-console
console.warn("yaml-1-2-swagger-client parser plugin doesn't support sourceMaps option");
}

const source = file.toString();

try {
const element = from(YAML.load(source, { schema: JSON_SCHEMA }));
const parseResultElement = new ParseResultElement();

element.classes.push('result');
parseResultElement.push(element);
return parseResultElement;
} catch (error) {
throw new ParserError(`Error parsing "${file.uri}"`, error);
}
},
},
});

export default YamlParser;

0 comments on commit db4737f

Please sign in to comment.