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: support for 2.1.0 version of spec #362

Merged
merged 4 commits into from
Jun 29, 2021
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
14 changes: 7 additions & 7 deletions library/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"dependencies": {
"@asyncapi/avro-schema-parser": "^0.2.0",
"@asyncapi/openapi-schema-parser": "^2.0.0",
"@asyncapi/parser": "^1.5.2",
"@asyncapi/parser": "^1.7.0",
"highlight.js": "^10.7.2",
"isomorphic-dompurify": "^0.13.0",
"marked": "^2.1.1",
Expand Down
18 changes: 13 additions & 5 deletions library/src/containers/Messages/MessageExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Message, Schema } from '@asyncapi/parser';

import { CollapseButton, JSONSnippet } from '../../components';
import { MessageHelpers } from '../../helpers/message';
import { MessageExample as MessageExampleType } from '../../types';

interface Props {
message: Message;
Expand Down Expand Up @@ -40,7 +41,7 @@ export const MessageExample: React.FunctionComponent<Props> = ({ message }) => {
interface ExampleProps {
type: 'Payload' | 'Headers';
schema: Schema;
examples?: any[];
examples?: MessageExampleType[];
}

export const Example: React.FunctionComponent<ExampleProps> = ({
Expand Down Expand Up @@ -71,12 +72,19 @@ export const Example: React.FunctionComponent<ExampleProps> = ({
<ul>
{examples.map((example, idx) => (
<li className="mt-4" key={idx}>
<h5 className="text-xs font-bold text-gray-700">
Example #{idx + 1}
<h5 className="text-xs font-bold text-gray-500">
{example.name
? `#${idx + 1} Example - ${example.name}`
: `#${idx + 1} Example`}
</h5>
{example.summary && (
<p className="text-xs font-bold text-gray-500">
{example.summary}
</p>
)}
<div className="mt-1">
<JSONSnippet
snippet={MessageHelpers.sanitizeExample(example)}
snippet={MessageHelpers.sanitizeExample(example.example)}
/>
</div>
</li>
Expand All @@ -87,7 +95,7 @@ export const Example: React.FunctionComponent<ExampleProps> = ({
<JSONSnippet
snippet={MessageHelpers.generateExample(schema.json())}
/>
<h6 className="text-xs font-bold text-gray-700 italic mt-2">
<h6 className="text-xs font-bold text-gray-600 italic mt-2">
This example has been generated automatically.
</h6>
</div>
Expand Down
182 changes: 176 additions & 6 deletions library/src/helpers/__tests__/message.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,13 @@ describe('MessageHelpers', () => {
new Message({
examples: [
{
name: 'example name',
summary: 'example summary',
headers: { foo: 'bar' },
},
{
name: 'example name',
summary: 'example summary',
headers: { bar: 'foo' },
},
],
Expand All @@ -64,15 +68,30 @@ describe('MessageHelpers', () => {
new Message({
examples: [
{
name: 'example name',
summary: 'example summary',
payload: { foo: 'bar' },
},
{
name: 'example name',
summary: 'example summary',
payload: { bar: 'foo' },
},
],
}),
);
expect(result).toEqual([{ foo: 'bar' }, { bar: 'foo' }]);
expect(result).toEqual([
{
name: 'example name',
summary: 'example summary',
example: { foo: 'bar' },
},
{
name: 'example name',
summary: 'example summary',
example: { bar: 'foo' },
},
]);
});

test('should return examples from payload schema', () => {
Expand All @@ -83,7 +102,73 @@ describe('MessageHelpers', () => {
},
}),
);
expect(result).toEqual([{ foo: 'bar' }, { bar: 'foo' }]);
expect(result).toEqual([
{
example: { foo: 'bar' },
},
{
example: { bar: 'foo' },
},
]);
});

test('should return examples from payload schema - case when only headers examples are defined in `examples` field', () => {
const result = MessageHelpers.getPayloadExamples(
new Message({
payload: {
examples: [{ foo: 'bar' }, { bar: 'foo' }],
},
examples: [
{
name: 'example name',
summary: 'example summary',
headers: { foo: 'bar' },
},
{
name: 'example name',
summary: 'example summary',
headers: { bar: 'foo' },
},
],
}),
);
expect(result).toEqual([
{
example: { foo: 'bar' },
},
{
example: { bar: 'foo' },
},
]);
});

test('should return examples for payload - case when at least one item in `examples` array has `payload` field with existing `payload.examples`', () => {
const result = MessageHelpers.getPayloadExamples(
new Message({
payload: {
examples: [{ foo: 'bar' }, { bar: 'foo' }],
},
examples: [
{
name: 'example name',
summary: 'example summary',
headers: { foo: 'bar' },
},
{
name: 'example name',
summary: 'example summary',
payload: { bar: 'foo' },
},
],
}),
);
expect(result).toEqual([
{
name: 'example name',
summary: 'example summary',
example: { bar: 'foo' },
},
]);
});
});

Expand All @@ -93,9 +178,13 @@ describe('MessageHelpers', () => {
new Message({
examples: [
{
name: 'example name',
summary: 'example summary',
payload: { foo: 'bar' },
},
{
name: 'example name',
summary: 'example summary',
payload: { bar: 'foo' },
},
],
Expand All @@ -104,31 +193,112 @@ describe('MessageHelpers', () => {
expect(result).toEqual(undefined);
});

test('should return payload examples', () => {
test('should return headers examples', () => {
const result = MessageHelpers.getHeadersExamples(
new Message({
examples: [
{
name: 'example name',
summary: 'example summary',
headers: { foo: 'bar' },
},
{
name: 'example name',
summary: 'example summary',
headers: { bar: 'foo' },
},
],
}),
);
expect(result).toEqual([{ foo: 'bar' }, { bar: 'foo' }]);
expect(result).toEqual([
{
name: 'example name',
summary: 'example summary',
example: { foo: 'bar' },
},
{
name: 'example name',
summary: 'example summary',
example: { bar: 'foo' },
},
]);
});

test('should return examples from heaers schema', () => {
test('should return examples from headers schema', () => {
const result = MessageHelpers.getHeadersExamples(
new Message({
headers: {
examples: [{ foo: 'bar' }, { bar: 'foo' }],
},
}),
);
expect(result).toEqual([{ foo: 'bar' }, { bar: 'foo' }]);
expect(result).toEqual([
{
example: { foo: 'bar' },
},
{
example: { bar: 'foo' },
},
]);
});

test('should return examples from headers schema - case when only payload examples are defined in `examples` field', () => {
const result = MessageHelpers.getHeadersExamples(
new Message({
headers: {
examples: [{ foo: 'bar' }, { bar: 'foo' }],
},
examples: [
{
name: 'example name',
summary: 'example summary',
payload: { foo: 'bar' },
},
{
name: 'example name',
summary: 'example summary',
payload: { bar: 'foo' },
},
],
}),
);
expect(result).toEqual([
{
example: { foo: 'bar' },
},
{
example: { bar: 'foo' },
},
]);
});

test('should return examples for headers - case when at least one item in `examples` array has `headers` field with existing `headers.examples`', () => {
const result = MessageHelpers.getHeadersExamples(
new Message({
headers: {
examples: [{ foo: 'bar' }, { bar: 'foo' }],
},
examples: [
{
name: 'example name',
summary: 'example summary',
payload: { foo: 'bar' },
},
{
name: 'example name',
summary: 'example summary',
headers: { bar: 'foo' },
},
],
}),
);
expect(result).toEqual([
{
name: 'example name',
summary: 'example summary',
example: { bar: 'foo' },
},
]);
});
});
});
Loading