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
1 change: 1 addition & 0 deletions packages/apidom-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import defaultNamespaceInstance from './namespace';

export { default as refractorPluginElementIdentity } from './refractor/plugins/element-identity';
export { default as refractorPluginSemanticElementIdentity } from './refractor/plugins/semantic-element-identity';
export { default as MediaTypes } from './media-types';

export {
Element,
Expand Down
13 changes: 13 additions & 0 deletions packages/apidom-core/src/media-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class MediaTypes<T> extends Array<T> {
// eslint-disable-next-line class-methods-use-this
forFormat() {
throw new Error('Not implemented!');
}

// eslint-disable-next-line class-methods-use-this
latest() {
throw new Error('Not implemented!');
}
}

export default MediaTypes;
2 changes: 2 additions & 0 deletions packages/apidom-ns-asyncapi-2/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export {
isStringElement,
} from '@swagger-api/apidom-core';

export { default as mediaTypes, AsyncAPIMediaTypes } from './media-types';

// eslint-disable-next-line no-restricted-exports
export { default } from './namespace';

Expand Down
33 changes: 33 additions & 0 deletions packages/apidom-ns-asyncapi-2/src/media-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { MediaTypes } from '@swagger-api/apidom-core';

export class AsyncAPIMediaTypes extends MediaTypes<string> {
forFormat(format = 'generic') {
const effectiveFormat = format === 'generic' ? 'asyncapi;version' : format;
return this.filter((mediaType) => mediaType.includes(effectiveFormat));
}

latest(format = 'generic') {
if (format === 'json') {
return this[7];
}
if (format === 'yaml') {
return this[8];
}

return this[6];
}
}

const mediaTypes = new AsyncAPIMediaTypes(
'application/vnd.aai.asyncapi;version=2.0.0',
'application/vnd.aai.asyncapi+json;version=2.0.0',
'application/vnd.aai.asyncapi+yaml;version=2.0.0',
'application/vnd.aai.asyncapi;version=2.1.0',
'application/vnd.aai.asyncapi+json;version=2.1.0',
'application/vnd.aai.asyncapi+yaml;version=2.1.0',
'application/vnd.aai.asyncapi;version=2.2.0',
'application/vnd.aai.asyncapi+json;version=2.2.0',
'application/vnd.aai.asyncapi+yaml;version=2.2.0',
);

export default mediaTypes;
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
isArrayElement,
} from '@swagger-api/apidom-core';

import mediaTypes from '../../media-types';
/**
* AsyncApi 2.0.0 | 2.1.0 | 2.2.0 specification elements.
*/
Expand Down Expand Up @@ -261,23 +262,9 @@ const schema = {
payload(...args: any[]) {
// @ts-ignore
const { context: messageElement } = this;
const supportedSchemaFormats = [
'application/vnd.aai.asyncapi;version=2.0.0',
'application/vnd.aai.asyncapi+json;version=2.0.0',
'application/vnd.aai.asyncapi+yaml;version=2.0.0',
'application/vnd.aai.asyncapi;version=2.1.0',
'application/vnd.aai.asyncapi+json;version=2.1.0',
'application/vnd.aai.asyncapi+yaml;version=2.1.0',
'application/vnd.aai.asyncapi;version=2.2.0',
'application/vnd.aai.asyncapi+json;version=2.2.0',
'application/vnd.aai.asyncapi+yaml;version=2.2.0',
];
const schemaFormat = defaultTo(
'application/vnd.aai.asyncapi;version=2.2.0',
messageElement.schemaFormat?.toValue(),
);
const schemaFormat = defaultTo(mediaTypes.latest(), messageElement.schemaFormat?.toValue());

if (supportedSchemaFormats.includes(schemaFormat)) {
if (mediaTypes.includes(schemaFormat)) {
return new SchemaElement(...args);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import stampit from 'stampit';
import { always, defaultTo } from 'ramda';
import { ObjectElement, isObjectElement, Element } from '@swagger-api/apidom-core';

import mediaTypes from '../../../../media-types';
import MessageElement from '../../../../elements/Message';
import FallbackVisitor from '../../FallbackVisitor';
import FixedFieldsVisitor from '../../generics/FixedFieldsVisitor';
Expand All @@ -17,20 +18,7 @@ const MessageVisitor = stampit(FixedFieldsVisitor, FallbackVisitor, {
},
methods: {
refractPayload(schemaFormat: string, payload: Element) {
if (
[
'application/vnd.aai.asyncapi;version=2.0.0',
'application/vnd.aai.asyncapi+json;version=2.0.0',
'application/vnd.aai.asyncapi+yaml;version=2.0.0',
'application/vnd.aai.asyncapi;version=2.1.0',
'application/vnd.aai.asyncapi+json;version=2.1.0',
'application/vnd.aai.asyncapi+yaml;version=2.1.0',
'application/vnd.aai.asyncapi;version=2.2.0',
'application/vnd.aai.asyncapi+json;version=2.2.0',
'application/vnd.aai.asyncapi+yaml;version=2.2.0',
].includes(schemaFormat) &&
isObjectElement(payload)
) {
if (isObjectElement(payload) && mediaTypes.includes(schemaFormat)) {
this.element.payload = this.toRefractedElement(['document', 'objects', 'Schema'], payload);
}
},
Expand All @@ -49,7 +37,7 @@ const MessageVisitor = stampit(FixedFieldsVisitor, FallbackVisitor, {
} else {
// refract payload according to `schemaFormat`
const schemaFormat = defaultTo(
'application/vnd.aai.asyncapi;version=2.2.0',
mediaTypes.latest(),
objectElement.get('schemaFormat')?.toValue(),
);
this.refractPayload(schemaFormat, payload);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11497,7 +11497,7 @@ exports[`refractor given generic ApiDOM object in AsyncApi 2.1.0 shape should re
},
"value": {
"element": "string",
"content": "application/vnd.aai.asyncapi;version=2.0.0"
"content": "application/vnd.aai.asyncapi;version=2.1.0"
}
}
},
Expand Down Expand Up @@ -12397,7 +12397,7 @@ exports[`refractor given generic ApiDOM object in AsyncApi 2.1.0 shape should re
},
"value": {
"element": "string",
"content": "application/vnd.aai.asyncapi;version=2.0.0"
"content": "application/vnd.aai.asyncapi;version=2.1.0"
}
}
},
Expand Down Expand Up @@ -19647,7 +19647,7 @@ exports[`refractor given generic ApiDOM object in AsyncApi 2.2.0 shape should re
},
"value": {
"element": "string",
"content": "application/vnd.aai.asyncapi;version=2.0.0"
"content": "application/vnd.aai.asyncapi;version=2.2.0"
}
}
},
Expand Down Expand Up @@ -20630,7 +20630,7 @@ exports[`refractor given generic ApiDOM object in AsyncApi 2.2.0 shape should re
},
"value": {
"element": "string",
"content": "application/vnd.aai.asyncapi;version=2.0.0"
"content": "application/vnd.aai.asyncapi;version=2.2.0"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
"description": "correlation id description",
"location": "http://asyncapi.com/"
},
"schemaFormat": "application/vnd.aai.asyncapi;version=2.0.0",
"schemaFormat": "application/vnd.aai.asyncapi;version=2.1.0",
"contentType": "application/json",
"name": "name of the message",
"title": "title of the message",
Expand Down Expand Up @@ -176,7 +176,7 @@
"description": "correlation id description",
"location": "http://asyncapi.com/"
},
"schemaFormat": "application/vnd.aai.asyncapi;version=2.0.0",
"schemaFormat": "application/vnd.aai.asyncapi;version=2.1.0",
"contentType": "application/json",
"name": "name of the message trait",
"title": "title of the message trait",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
"description": "correlation id description",
"location": "http://asyncapi.com/"
},
"schemaFormat": "application/vnd.aai.asyncapi;version=2.0.0",
"schemaFormat": "application/vnd.aai.asyncapi;version=2.2.0",
"contentType": "application/json",
"name": "name of the message",
"title": "title of the message",
Expand Down Expand Up @@ -183,7 +183,7 @@
"description": "correlation id description",
"location": "http://asyncapi.com/"
},
"schemaFormat": "application/vnd.aai.asyncapi;version=2.0.0",
"schemaFormat": "application/vnd.aai.asyncapi;version=2.2.0",
"contentType": "application/json",
"name": "name of the message trait",
"title": "title of the message trait",
Expand Down
20 changes: 11 additions & 9 deletions packages/apidom-parser-adapter-asyncapi-json-2/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ import { propOr, omit } from 'ramda';
import { isNotUndefined } from 'ramda-adjunct';
import { ParseResultElement, createNamespace } from '@swagger-api/apidom-core';
import { parse as parseJson } from '@swagger-api/apidom-parser-adapter-json';
import asyncApiNamespace, { AsyncApi2Element } from '@swagger-api/apidom-ns-asyncapi-2';
import asyncApiNamespace, {
AsyncApi2Element,
mediaTypes,
AsyncAPIMediaTypes,
} from '@swagger-api/apidom-ns-asyncapi-2';

export const mediaTypes = [
'application/vnd.aai.asyncapi;version=2.0.0',
'application/vnd.aai.asyncapi+json;version=2.0.0',
'application/vnd.aai.asyncapi;version=2.1.0',
'application/vnd.aai.asyncapi+json;version=2.1.0',
'application/vnd.aai.asyncapi;version=2.2.0',
'application/vnd.aai.asyncapi+json;version=2.2.0',
];
const jsonMediaTypes = new AsyncAPIMediaTypes(
...mediaTypes.forFormat('generic'),
...mediaTypes.forFormat('json'),
);

export { jsonMediaTypes as mediaTypes };

export const detect = (source: string): boolean =>
!!source.match(/(["']?)asyncapi\1\s*:\s*(["']?)2\.\d+\.\d+\2/g);
Expand Down
20 changes: 11 additions & 9 deletions packages/apidom-parser-adapter-asyncapi-yaml-2/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ import { omit, propOr } from 'ramda';
import { isNotUndefined } from 'ramda-adjunct';
import { ParseResultElement, createNamespace } from '@swagger-api/apidom-core';
import { parse as parseYaml } from '@swagger-api/apidom-parser-adapter-yaml-1-2';
import asyncApiNamespace, { AsyncApi2Element } from '@swagger-api/apidom-ns-asyncapi-2';
import asyncApiNamespace, {
AsyncApi2Element,
mediaTypes,
AsyncAPIMediaTypes,
} from '@swagger-api/apidom-ns-asyncapi-2';

export const mediaTypes = [
'application/vnd.aai.asyncapi;version=2.0.0',
'application/vnd.aai.asyncapi+yaml;version=2.0.0',
'application/vnd.aai.asyncapi;version=2.1.0',
'application/vnd.aai.asyncapi+yaml;version=2.1.0',
'application/vnd.aai.asyncapi;version=2.2.0',
'application/vnd.aai.asyncapi+yaml;version=2.2.0',
];
const yamlMediaTypes = new AsyncAPIMediaTypes(
...mediaTypes.forFormat('generic'),
...mediaTypes.forFormat('yaml'),
);

export { yamlMediaTypes as mediaTypes };

export const detect = (source: string): boolean =>
!!source.match(/(["']?)asyncapi\1\s*:\s*(["']?)2\.\d+\.\d+\2/g);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const EditorControls = () => {
const canDereference = useSystemSelector('playground', 'selectCanDereference');
const source = useSystemSelector('playground', 'selectSource');
const apiDOM = useSystemSelector('playground', 'selectApiDOM');
const mediaTypes = useSystemSelector('playground', 'selectMediaTypes');
const setMediaType = useSystemActionCreatorBound('playground', 'setMediaType');
const setBaseURI = useSystemActionCreatorBound('playground', 'setBaseURI');
const parseSource = useSystemActionCreatorBound('playground', 'parseSource');
Expand Down Expand Up @@ -103,33 +104,11 @@ const EditorControls = () => {
<MenuItem value="application/vnd.oai.openapi+yaml;version=3.1.0">
application/vnd.oai.openapi+yaml;version=3.1.0
</MenuItem>
<MenuItem value="application/vnd.aai.asyncapi;version=2.0.0">
application/vnd.aai.asyncapi;version=2.0.0
</MenuItem>
<MenuItem value="application/vnd.aai.asyncapi+json;version=2.0.0">
application/vnd.aai.asyncapi+json;version=2.0.0
</MenuItem>
<MenuItem value="application/vnd.aai.asyncapi+yaml;version=2.0.0">
application/vnd.aai.asyncapi+yaml;version=2.0.0
</MenuItem>
<MenuItem value="application/vnd.aai.asyncapi;version=2.1.0">
application/vnd.aai.asyncapi;version=2.1.0
</MenuItem>
<MenuItem value="application/vnd.aai.asyncapi+json;version=2.1.0">
application/vnd.aai.asyncapi+json;version=2.1.0
</MenuItem>
<MenuItem value="application/vnd.aai.asyncapi+yaml;version=2.1.0">
application/vnd.aai.asyncapi+yaml;version=2.1.0
</MenuItem>
<MenuItem value="application/vnd.aai.asyncapi;version=2.1.0">
application/vnd.aai.asyncapi;version=2.2.0
</MenuItem>
<MenuItem value="application/vnd.aai.asyncapi+json;version=2.1.0">
application/vnd.aai.asyncapi+json;version=2.2.0
</MenuItem>
<MenuItem value="application/vnd.aai.asyncapi+yaml;version=2.1.0">
application/vnd.aai.asyncapi+yaml;version=2.2.0
</MenuItem>
{mediaTypes.map((medType) => (
<MenuItem key={medType} value={medType}>
{medType}
</MenuItem>
))}
</Select>
</FormControl>
</Grid>
Expand Down
2 changes: 2 additions & 0 deletions packages/apidom-playground/src/playground/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
selectCanParse,
selectCanResolve,
selectCanDereference,
selectMediaTypes,
} from './selectors.js';
import AppBar from './components/AppBar.jsx';
import Backdrop from './components/Backdrop.jsx';
Expand Down Expand Up @@ -97,6 +98,7 @@ const ApiDOMPlaygroundPlugin = () => ({
selectCanParse,
selectCanResolve,
selectCanDereference,
selectMediaTypes,
},
},
},
Expand Down
11 changes: 9 additions & 2 deletions packages/apidom-playground/src/playground/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { from, traverse, createNamespace, sexprs, toValue } from '@swagger-api/a
/* eslint-disable camelcase */
import openApi3_1NsPlugin from '@swagger-api/apidom-ns-openapi-3-1';
/* eslint-enable */
import asyncApi2NsPlugin from '@swagger-api/apidom-ns-asyncapi-2';
import asyncApi2NsPlugin, {
mediaTypes as asyncApi2MediaTypes,
} from '@swagger-api/apidom-ns-asyncapi-2';

export const selectSource = (state) => state.source;

Expand All @@ -31,7 +33,7 @@ export const selectApiDOMNamespace = createSelector(selectMediaType, (mediaType)
if (mediaType.includes('vnd.oai.openapi')) {
return createNamespace(openApi3_1NsPlugin);
}
if (mediaType.includes('vnd.aai.asyncapi')) {
if (asyncApi2MediaTypes.includes(mediaType)) {
return createNamespace(asyncApi2NsPlugin);
}
return createNamespace();
Expand Down Expand Up @@ -99,3 +101,8 @@ export const selectCanDereference = createSelector(
(baseURI, apiDOM, mediaType) =>
isNonEmptyString(baseURI) && isNonEmptyString(apiDOM) && isNonEmptyString(mediaType)
);

export const selectMediaTypes = (() => {
const allMediaTypes = [...asyncApi2MediaTypes];
return () => allMediaTypes;
})();
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import asyncApi2Namespace, {
getNodeType,
isAsyncApi2Element,
keyMap,
mediaTypes,
} from '@swagger-api/apidom-ns-asyncapi-2';

import DereferenceStrategy from '../DereferenceStrategy';
Expand All @@ -30,17 +31,7 @@ const AsyncApi2DereferenceStrategy: stampit.Stamp<IDereferenceStrategy> = stampi
canDereference(file: IFile): boolean {
// assert by media type
if (file.mediaType !== 'text/plain') {
return [
'application/vnd.aai.asyncapi;version=2.0.0',
'application/vnd.aai.asyncapi+json;version=2.0.0',
'application/vnd.aai.asyncapi+yaml;version=2.0.0',
'application/vnd.aai.asyncapi;version=2.1.0',
'application/vnd.aai.asyncapi+json;version=2.1.0',
'application/vnd.aai.asyncapi+yaml;version=2.1.0',
'application/vnd.aai.asyncapi;version=2.2.0',
'application/vnd.aai.asyncapi+json;version=2.2.0',
'application/vnd.aai.asyncapi+yaml;version=2.2.0',
].includes(file.mediaType);
return mediaTypes.includes(file.mediaType);
}

// assert by inspecting ApiDOM
Expand Down
Loading