Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions apidom/packages/apidom-ls/src/parser-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,8 @@ import * as openapi3_1Adapter_Yaml from 'apidom-parser-adapter-openapi-yaml-3-1'
// @ts-ignore
import * as asyncapi2_0Adapter_Yaml from 'apidom-parser-adapter-asyncapi-yaml-2-0';

// @ts-ignore
import { ParseResultElement } from 'apidom';

import { TextDocument } from 'vscode-languageserver-textdocument';

export interface Parser {
parse(source: string, options: ParserOptions): PromiseLike<ParseResultElement>;
}

export interface ParserOptions {
sourceMap?: boolean;
specObj?: string;
Expand Down Expand Up @@ -58,7 +51,7 @@ export function isJsonDoc(document: TextDocument | string): boolean {
return jsonStart != null && JSON_ENDS[jsonStart[0]].test(text);
}

export function getParser(document: TextDocument): Parser {
export function getParser(document: TextDocument): ApiDOMParser {
const async = isAsyncDoc(document);
const json = isJsonDoc(document);
if (async && json) {
Expand Down
9 changes: 7 additions & 2 deletions apidom/packages/apidom-ls/test/openapi-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,13 @@ describe('apidom-ls', function () {
// parser.use(asyncapi2_0Adapter);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const parseResult = await parser.parse(value, { sourceMap: true });
addMetadataMapping(parseResult.api);

assert.deepEqual(parseResult.api.meta.get('metadataMap').toValue(), metadataMap);
if (parseResult.api !== undefined) {
addMetadataMapping(parseResult.api);

assert.deepEqual(parseResult.api.meta.get('metadataMap').toValue(), metadataMap);
} else {
assert.fail('parserResult.api should return OpenApi Element');
}
});
});
14 changes: 10 additions & 4 deletions apidom/packages/apidom-parser/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { head } from 'ramda';
import { isArray, isFunction, isString, isUndefined } from 'ramda-adjunct';
import { ParseResultElement, Namespace } from 'apidom';

interface ParserOptions {
interface ParserOptions extends Record<string, any> {
mediaType?: string;
}

type Detect = (source: string) => boolean;
type Parse = (source: string, options: ParserOptions) => ParseResultElement;
type Parse = (source: string, options: ParserOptions) => Promise<ParseResultElement>;

interface ApiDOMParserAdapter {
detect?: Detect;
Expand All @@ -17,7 +17,13 @@ interface ApiDOMParserAdapter {
namespace: Namespace;
}

const ApiDOMParser = stampit().init(function ApiDOMParser() {
interface ApiDOMParser {
use(adapter: ApiDOMParserAdapter): ApiDOMParser;
findNamespace(source: string, options?: ParserOptions): Namespace;
parse(source: string, options?: ParserOptions): Promise<ParseResultElement>;
}

const ApiDOMParser: stampit.Stamp<ApiDOMParser> = stampit().init(function ApiDOMParser() {
const adapters: ApiDOMParserAdapter[] = [];

const detectAdapterCandidates = (source: string) => {
Expand Down Expand Up @@ -45,7 +51,7 @@ const ApiDOMParser = stampit().init(function ApiDOMParser() {
return this;
};

this.namespace = function namespace(source: string, options: ParserOptions = {}) {
this.findNamespace = function findNamespace(source: string, options: ParserOptions = {}) {
const adapter = findAdapter(source, options.mediaType);

return adapter?.namespace;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import SpeedDialAction from '@material-ui/lab/SpeedDialAction';
import DescriptionIcon from '@material-ui/icons/Description';
import AttachFileIcon from '@material-ui/icons/AttachFile';
import ImportExportIcon from '@material-ui/icons/ImportExport';
import { setSource } from 'features/app/slice';
import { setSource, setBaseURI } from 'features/app/slice';
import UrlImportDialog from 'features/app/file-importer/url-import-dialog/components/UrlImportDialog';

const useStyles = makeStyles((theme) => ({
Expand Down Expand Up @@ -46,6 +46,7 @@ const FileImporter = () => {
const reader = new FileReader();
const onloadend = (onloadendEvent) => {
const source = onloadendEvent.target.result;
dispatch(setBaseURI(window.location.href));
dispatch(setSource(source));
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const EditorControls = () => {
dispatch(parseSource({ source, mediaType }));
};
const handleApiDOMResolve = () => {
dispatch(resolveApiDOM({ apiDOM, mediaType, baseURI }));
dispatch(resolveApiDOM({ source, apiDOM, mediaType, baseURI }));
};

return (
Expand Down
6 changes: 3 additions & 3 deletions experiments/apidom-playground/src/features/app/slice.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export const importURL = createAsyncThunk('importURLStatus', async (url) => {
});

export const parseSource = createAsyncThunk('parseSourceStatus', async ({ source, mediaType }) => {
const namespace = parser.namespace(source, { sourceMap: true, mediaType });
const namespace = parser.findNamespace(source, { sourceMap: true, mediaType });
const parseResult = await parser.parse(source, { sourceMap: true, mediaType });
const refract = dehydrate(parseResult, namespace);

Expand All @@ -76,8 +76,8 @@ export const parseSource = createAsyncThunk('parseSourceStatus', async ({ source

export const resolveApiDOM = createAsyncThunk(
'resolveApiDOMStatus',
async ({ apiDOM, mediaType, baseURI }) => {
const namespace = parser.namespace('', { mediaType });
async ({ source, apiDOM, mediaType, baseURI }) => {
const namespace = parser.findNamespace(source, { mediaType });
const parseResult = from(apiDOM, namespace);

return resolveApiDOMReferences(parseResult, { parse: { mediaType }, resolve: { baseURI } });
Expand Down