-
Notifications
You must be signed in to change notification settings - Fork 762
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(resolve): add ApiDOM OpenAPI 3.1.0 JSON parser
Refs #2717
- Loading branch information
Showing
2 changed files
with
67 additions
and
0 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
66 changes: 66 additions & 0 deletions
66
src/helpers/apidom/reference/parse/parsers/openapi-json-3-1/index.js
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,66 @@ | ||
/* eslint-disable camelcase */ | ||
import { ParseResultElement } from '@swagger-api/apidom-core'; | ||
import { ParserError, Parser } from '@swagger-api/apidom-reference/configuration/empty'; | ||
import { | ||
mediaTypes, | ||
OpenApi3_1Element, | ||
OpenAPIMediaTypes, | ||
} from '@swagger-api/apidom-ns-openapi-3-1'; | ||
|
||
// eslint-disable-next-line camelcase | ||
const OpenApiJson3_1Parser = Parser.compose(Parser, { | ||
props: { | ||
name: 'openapi-json-3-1-swagger-client', | ||
fileExtensions: ['.json'], | ||
mediaTypes: new OpenAPIMediaTypes( | ||
...mediaTypes.filterByFormat('generic'), | ||
...mediaTypes.filterByFormat('json') | ||
), | ||
detectionRegExp: /"openapi"\s*:\s*"(?<version_json>3\.1\.(?:[1-9]\d*|0))"/, | ||
}, | ||
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 { | ||
const source = file.toString(); | ||
JSON.parse(source); | ||
return this.detectionRegExp.test(source); | ||
} catch (error) { | ||
return false; | ||
} | ||
} | ||
return false; | ||
}, | ||
async parse(file) { | ||
if (this.sourceMap) { | ||
// eslint-disable-next-line no-console | ||
console.warn( | ||
"openapi-json-3-1-swagger-client parser plugin doesn't support sourceMaps option" | ||
); | ||
} | ||
|
||
const source = file.toString(); | ||
|
||
try { | ||
const pojo = JSON.parse(source); | ||
const element = OpenApi3_1Element.refract(pojo, this.refractorOpts); | ||
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 OpenApiJson3_1Parser; | ||
/* eslint-enable camelcase */ |