Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(editor-preview-asyncapi): add initial support for AsyncAPI 3.0.0 #4886

Merged
merged 1 commit into from
Mar 29, 2024
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
22 changes: 22 additions & 0 deletions src/plugins/editor-content-type/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,28 @@ export const detectContentType = (content) => {
return editorActions.detectContentTypeSuccess({ contentType, content, requestId });
}

const asyncApi3JSONMatch = content.match(
/"asyncapi"\s*:\s*"(?<version_json>3\.(?:[1-9]\d*|0)\.(?:[1-9]\d*|0))"/
);
if (asyncApi3JSONMatch !== null && fn.isValidJSONObject(content)) {
const { groups } = asyncApi3JSONMatch;
const version = groups?.version_json;
const contentType = `application/vnd.aai.asyncapi+json;version=${version}`;

return editorActions.detectContentTypeSuccess({ contentType, content, requestId });
}

const asyncApi3YAMLMatch = content.match(
/(?<YAML>^(["']?)asyncapi\2\s*:\s*(["']?)(?<version_yaml>3\.(?:[1-9]\d*|0)\.(?:[1-9]\d*|0))\3(?:\s+|$))|(?<JSON>"asyncapi"\s*:\s*"(?<version_json>3\.(?:[1-9]\d*|0)\.(?:[1-9]\d*|0))")/m
);
if (asyncApi3YAMLMatch !== null && fn.isValidYAMLObject(content)) {
const { groups } = asyncApi3YAMLMatch;
const version = groups?.version_json ?? groups?.version_yaml;
const contentType = `application/vnd.aai.asyncapi+yaml;version=${version}`;

return editorActions.detectContentTypeSuccess({ contentType, content, requestId });
}

const openApi30xJSONMatch = content.match(detectionRegExpOpenAPIJSON30x);
if (openApi30xJSONMatch !== null && fn.isValidJSONObject(content)) {
const { groups } = openApi30xJSONMatch;
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/editor-preview-asyncapi/monaco.contribution.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const lazyMonacoContribution = ({ system }) => {
const modelValue = model.getValue();
const modelVersionId = model.getVersionId();
const conditionFn = () =>
editorSelectors.selectIsContentTypeAsyncAPI2() &&
editorSelectors.selectIsContentTypeAsyncAPI() &&
modelValue === editorSelectors.selectContent() &&
!editorPreviewAsyncAPISelectors.selectIsParseInProgress();

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/editor-preview-asyncapi/wrap-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const detectContentTypeSuccess = createSafeActionWrapper(
({ content }) => {
const { editorSelectors, editorPreviewAsyncAPIActions } = system;

if (editorSelectors.selectIsContentTypeAsyncAPI2()) {
if (editorSelectors.selectIsContentTypeAsyncAPI()) {
editorPreviewAsyncAPIActions.parse(content);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ import PropTypes from 'prop-types';
const EditorPreviewWrapper = (Original, system) => {
const EditorPreview = ({ getComponent, editorSelectors }) => {
const EditorPreviewAsyncAPI = getComponent('EditorPreviewAsyncAPI', true);
const isAsyncAPI = editorSelectors.selectIsContentTypeAsyncAPI();

return editorSelectors.selectIsContentTypeAsyncAPI2() ? (
<EditorPreviewAsyncAPI />
) : (
<Original {...system} /> // eslint-disable-line react/jsx-props-no-spreading
);
if (isAsyncAPI) {
return <EditorPreviewAsyncAPI />;
}

return <Original {...system} />; // eslint-disable-line react/jsx-props-no-spreading
};

EditorPreview.propTypes = {
getComponent: PropTypes.func.isRequired,
editorSelectors: PropTypes.shape({
selectIsContentTypeAsyncAPI2: PropTypes.func.isRequired,
selectIsContentTypeAsyncAPI: PropTypes.func.isRequired,
}).isRequired,
};

Expand Down