Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(reference): add parser plugin for parsing dehydrated ApiDOM (#3892)
Refs #3889
- Loading branch information
Showing
6 changed files
with
268 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
packages/apidom-reference/src/parse/parsers/apidom-json/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import stampit from 'stampit'; | ||
import { | ||
ParseResultElement, | ||
isParseResultElement, | ||
namespace as baseNamespace, | ||
} from '@swagger-api/apidom-core'; | ||
|
||
import ParserError from '../../../errors/ParserError'; | ||
import { Parser as IParser, File as IFile } from '../../../types'; | ||
import Parser from '../Parser'; | ||
|
||
const ApiDOMJsonParser: stampit.Stamp<IParser> = stampit(Parser, { | ||
props: { | ||
name: 'apidom-json', | ||
fileExtensions: ['.json'], | ||
mediaTypes: ['application/vnd.apidom', 'application/vnd.apidom+json'], | ||
namespace: baseNamespace, | ||
}, | ||
init({ namespace } = {}) { | ||
this.namespace = namespace ?? this.namespace; | ||
}, | ||
methods: { | ||
canParse(file: IFile): boolean { | ||
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 { | ||
return this.namespace.fromRefract(JSON.parse(file.toString())) && true; | ||
} catch { | ||
return false; | ||
} | ||
} | ||
return false; | ||
}, | ||
parse(file: IFile): ParseResultElement { | ||
const source = file.toString(); | ||
const namespace = this['apidom-json']?.namespace ?? this.namespace; | ||
|
||
// allow empty files | ||
if (this.allowEmpty && source.trim() === '') { | ||
return new ParseResultElement(); | ||
} | ||
|
||
try { | ||
const element = namespace.fromRefract(JSON.parse(source)); | ||
|
||
if (!isParseResultElement(element)) { | ||
element.classes.push('result'); | ||
return new ParseResultElement([element]); | ||
} | ||
|
||
return element; | ||
} catch (error: unknown) { | ||
throw new ParserError(`Error parsing "${file.uri}"`, { cause: error }); | ||
} | ||
}, | ||
}, | ||
}); | ||
|
||
export default ApiDOMJsonParser; |
12 changes: 12 additions & 0 deletions
12
packages/apidom-reference/test/parse/parsers/apidom-json/fixtures/apidom.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"element": "object", | ||
"content": [ | ||
{ | ||
"element": "member", | ||
"content": { | ||
"key": { "element": "string", "content": "a" }, | ||
"value": { "element": "string", "content": "b" } | ||
} | ||
} | ||
] | ||
} |
163 changes: 163 additions & 0 deletions
163
packages/apidom-reference/test/parse/parsers/apidom-json/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
import fs from 'node:fs'; | ||
import path from 'node:path'; | ||
import { assert } from 'chai'; | ||
import { isParseResultElement } from '@swagger-api/apidom-core'; | ||
|
||
import { ParserError, File } from '../../../../src'; | ||
import ApiDOMJsonParser from '../../../../src/parse/parsers/apidom-json'; | ||
|
||
describe('parsers', function () { | ||
context('ApiDOMJsonParser', function () { | ||
context('canParse', function () { | ||
context('given file with .json extension', function () { | ||
context('and with proper media type', function () { | ||
specify('should return true', async function () { | ||
const file1 = File({ | ||
uri: '/path/to/apidom.json', | ||
mediaType: 'application/vnd.apidom', | ||
}); | ||
const file2 = File({ | ||
uri: '/path/to/apidom.json', | ||
mediaType: 'application/vnd.apidom+json', | ||
}); | ||
const parser = ApiDOMJsonParser(); | ||
|
||
assert.isTrue(await parser.canParse(file1)); | ||
assert.isTrue(await parser.canParse(file2)); | ||
}); | ||
}); | ||
|
||
context('and with improper media type', function () { | ||
specify('should return false', async function () { | ||
const file = File({ | ||
uri: '/path/to/apidom.json', | ||
mediaType: 'application/vnd.aai.asyncapi+json;version=2.6.0', | ||
}); | ||
const parser = ApiDOMJsonParser(); | ||
|
||
assert.isFalse(await parser.canParse(file)); | ||
}); | ||
}); | ||
}); | ||
|
||
context('given file with unknown extension', function () { | ||
specify('should return false', async function () { | ||
const file = File({ | ||
uri: '/path/to/apidom.yaml', | ||
mediaType: 'application/vnd.apidom', | ||
}); | ||
const parser = ApiDOMJsonParser(); | ||
|
||
assert.isFalse(await parser.canParse(file)); | ||
}); | ||
}); | ||
|
||
context('given file with no extension', function () { | ||
specify('should return false', async function () { | ||
const file = File({ | ||
uri: '/path/to/apidom', | ||
mediaType: 'application/vnd.apidom', | ||
}); | ||
const parser = ApiDOMJsonParser(); | ||
|
||
assert.isFalse(await parser.canParse(file)); | ||
}); | ||
}); | ||
|
||
context('given file with supported extension', function () { | ||
context('and file data is buffer and can be detected as ApiDOM', function () { | ||
specify('should return true', async function () { | ||
const url = path.join(__dirname, 'fixtures', 'apidom.json'); | ||
const file = File({ | ||
uri: '/path/to/apidom.json', | ||
data: fs.readFileSync(url), | ||
}); | ||
const parser = ApiDOMJsonParser(); | ||
|
||
assert.isTrue(await parser.canParse(file)); | ||
}); | ||
}); | ||
|
||
context('and file data is string and can be detected as ApiDOM', function () { | ||
specify('should return true', async function () { | ||
const url = path.join(__dirname, 'fixtures', 'apidom.json'); | ||
const file = File({ | ||
uri: '/path/to/apidom.json', | ||
data: fs.readFileSync(url).toString(), | ||
}); | ||
const parser = ApiDOMJsonParser(); | ||
|
||
assert.isTrue(await parser.canParse(file)); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
context('parse', function () { | ||
context('given ApiDOM JSON data', function () { | ||
specify('should return parse result', async function () { | ||
const uri = path.join(__dirname, 'fixtures', 'apidom.json'); | ||
const data = fs.readFileSync(uri).toString(); | ||
const file = File({ | ||
uri, | ||
data, | ||
mediaType: 'application/vnd.apidom+json', | ||
}); | ||
const parser = ApiDOMJsonParser(); | ||
const parseResult = await parser.parse(file); | ||
|
||
assert.isTrue(isParseResultElement(parseResult)); | ||
}); | ||
}); | ||
|
||
context('given ApiDOM JSON data as buffer', function () { | ||
specify('should return parse result', async function () { | ||
const uri = path.join(__dirname, 'fixtures', 'apidom.json'); | ||
const data = fs.readFileSync(uri); | ||
const file = File({ | ||
uri, | ||
data, | ||
mediaType: 'application/vnd.apidom+json', | ||
}); | ||
const parser = ApiDOMJsonParser(); | ||
const parseResult = await parser.parse(file); | ||
|
||
assert.isTrue(isParseResultElement(parseResult)); | ||
}); | ||
}); | ||
|
||
context('given data that is not an ApiDOM JSON data', function () { | ||
specify('should throw error', async function () { | ||
const file = File({ | ||
uri: '/path/to/file.json', | ||
data: 1, | ||
mediaType: 'application/vnd.apidom+json', | ||
}); | ||
const parser = ApiDOMJsonParser(); | ||
|
||
try { | ||
await parser.parse(file); | ||
assert.fail('Should throw ParserError'); | ||
} catch (e) { | ||
assert.instanceOf(e, ParserError); | ||
} | ||
}); | ||
}); | ||
|
||
context('given empty file', function () { | ||
specify('should return empty parse result', async function () { | ||
const file = File({ | ||
uri: '/path/to/file.json', | ||
data: '', | ||
mediaType: 'application/vnd.apidom+json', | ||
}); | ||
const parser = ApiDOMJsonParser(); | ||
const parseResult = await parser.parse(file); | ||
|
||
assert.isTrue(isParseResultElement(parseResult)); | ||
assert.isTrue(parseResult.isEmpty); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |