Skip to content

Commit

Permalink
feat: add name and summary properties to message examples (#307)
Browse files Browse the repository at this point in the history
  • Loading branch information
lbroudoux authored Jun 24, 2021
1 parent 589060b commit ae07543
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 9 deletions.
2 changes: 1 addition & 1 deletion dist/bundle.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions 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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
},
"dependencies": {
"@apidevtools/json-schema-ref-parser": "^9.0.6",
"@asyncapi/specs": "2.8.0-2021-06-release.1",
"@asyncapi/specs": "2.8.0-2021-06-release.3",
"@fmvilas/pseudo-yaml-ast": "^0.3.1",
"ajv": "^6.10.1",
"js-yaml": "^3.13.1",
Expand Down
26 changes: 26 additions & 0 deletions test/good/asyncapi-messages-example-headers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
asyncapi: 2.0.0
info:
title: My API
version: '1.0.0'

channels:
myChannel:
subscribe:
message:
x-some-extension: 'some extension'
headers:
type: object
properties:
some-common-header:
type: string
payload:
type: object
properties:
name:
type: string
examples:
- name: Example1
summary: Example1 summary
headers:
some-common-header: My header

26 changes: 26 additions & 0 deletions test/good/asyncapi-messages-example-optional.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
asyncapi: 2.0.0
info:
title: My API
version: '1.0.0'

channels:
myChannel:
subscribe:
message:
x-some-extension: 'some extension'
headers:
type: object
properties:
some-common-header:
type: string
payload:
type: object
properties:
name:
type: string
examples:
- payload:
name: My name
headers:
some-common-header: My header

26 changes: 26 additions & 0 deletions test/good/asyncapi-messages-example-payload.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
asyncapi: 2.0.0
info:
title: My API
version: '1.0.0'

channels:
myChannel:
subscribe:
message:
x-some-extension: 'some extension'
headers:
type: object
properties:
some-common-header:
type: string
payload:
type: object
properties:
name:
type: string
examples:
- name: Example1
summary: Example1 summary
payload:
name: My name

28 changes: 28 additions & 0 deletions test/good/asyncapi-messages-example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
asyncapi: 2.0.0
info:
title: My API
version: '1.0.0'

channels:
myChannel:
subscribe:
message:
x-some-extension: 'some extension'
headers:
type: object
properties:
some-common-header:
type: string
payload:
type: object
properties:
name:
type: string
examples:
- name: Example1
summary: Example1 summary
payload:
name: My name
headers:
some-common-header: My header

2 changes: 1 addition & 1 deletion test/models/message-trait_test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { expect } = require('chai');
const js = { headers: { properties: { test1: { type: 'string' }, test2: { type: 'number' } } }, correlationId: { test: true }, contentType: 'application/json', name: 'test', title: 'Test', summary: 'test', description: 'testing', externalDocs: { test: true }, tags: [{ name: 'tag1' }], bindings: { amqp: { test: true } }, examples: [{ test: true }], 'x-test': 'testing' };
const js = { headers: { properties: { test1: { type: 'string' }, test2: { type: 'number' } } }, correlationId: { test: true }, contentType: 'application/json', name: 'test', title: 'Test', summary: 'test', description: 'testing', externalDocs: { test: true }, tags: [{ name: 'tag1' }], bindings: { amqp: { test: true } }, examples: [{name: 'test', summary: 'test summary', payload: {test: true}}], 'x-test': 'testing' };

const MessageTrait = require('../../lib/models/message-trait');

Expand Down
2 changes: 1 addition & 1 deletion test/models/message-traitable_test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { expect } = require('chai');
const js = { headers: { properties: { test1: { type: 'string' }, test2: { type: 'number' } } }, correlationId: { test: true }, contentType: 'application/json', name: 'test', title: 'Test', summary: 'test', description: 'testing', externalDocs: { test: true }, tags: [{ name: 'tag1' }], bindings: { amqp: { test: true } }, examples: [{ test: true }], 'x-test': 'testing' };
const js = { headers: { properties: { test1: { type: 'string' }, test2: { type: 'number' } } }, correlationId: { test: true }, contentType: 'application/json', name: 'test', title: 'Test', summary: 'test', description: 'testing', externalDocs: { test: true }, tags: [{ name: 'tag1' }], bindings: { amqp: { test: true } }, examples: [{name: 'test', summary: 'test summary', payload: {test: true}}], 'x-test': 'testing' };

const MessageTraitable = require('../../lib/models/message-traitable');

Expand Down
4 changes: 2 additions & 2 deletions test/models/message_test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { expect } = require('chai');
const js = { headers: { properties: { test1: { type: 'string' }, test2: { type: 'number' } } }, payload: { test: true }, 'x-parser-original-payload': { testing: true }, correlationId: { test: true }, 'x-parser-original-schema-format': 'application/vnd.apache.avro;version=1.9.0', contentType: 'application/json', name: 'test', title: 'Test', summary: 'test', description: 'testing', externalDocs: { test: true }, tags: [{ name: 'tag1' }], bindings: { amqp: { test: true } }, examples: [{test: true}], 'x-test': 'testing' };
const js = { headers: { properties: { test1: { type: 'string' }, test2: { type: 'number' } } }, payload: { test: true }, 'x-parser-original-payload': { testing: true }, correlationId: { test: true }, 'x-parser-original-schema-format': 'application/vnd.apache.avro;version=1.9.0', contentType: 'application/json', name: 'test', title: 'Test', summary: 'test', description: 'testing', externalDocs: { test: true }, tags: [{ name: 'tag1' }], bindings: { amqp: { test: true } }, examples: [{name: 'test', summary: 'test summary', payload: {test: true}}], 'x-test': 'testing' };

const Message = require('../../lib/models/message');

Expand All @@ -25,7 +25,7 @@ describe('Message', function() {
it('should return a string with the base64 representation of the object when x-parser-message-name extension and name are not available', function() {
const msg = { ...js, ...{ name: undefined } };
const d = new Message(msg);
expect(d.uid()).to.be.equal('eyJoZWFkZXJzIjp7InByb3BlcnRpZXMiOnsidGVzdDEiOnsidHlwZSI6InN0cmluZyJ9LCJ0ZXN0MiI6eyJ0eXBlIjoibnVtYmVyIn19fSwicGF5bG9hZCI6eyJ0ZXN0Ijp0cnVlfSwieC1wYXJzZXItb3JpZ2luYWwtcGF5bG9hZCI6eyJ0ZXN0aW5nIjp0cnVlfSwiY29ycmVsYXRpb25JZCI6eyJ0ZXN0Ijp0cnVlfSwieC1wYXJzZXItb3JpZ2luYWwtc2NoZW1hLWZvcm1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6dHJ1ZX19XSwieC10ZXN0IjoidGVzdGluZyJ9');
});
});

Expand Down
35 changes: 35 additions & 0 deletions test/parse_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,41 @@ it('should apply `x-parser-spec-parsed` extension', async function() {
await expect(parsedSpec.json()[String(xParserSpecParsed)]).to.equal(true);
});

it('should parse and include examples', async function() {
let result = await parser.parse(fs.readFileSync(path.resolve(__dirname, './good/asyncapi-messages-example.yml'), 'utf8'), { path: __filename });
expect(result.channel('myChannel').subscribe().messages()[0].examples()[0].name).to.equal('Example1');
expect(result.channel('myChannel').subscribe().messages()[0].examples()[0].summary).to.equal('Example1 summary');
expect(result.channel('myChannel').subscribe().messages()[0].examples()[0].payload.name).to.equal('My name');
expect(result.channel('myChannel').subscribe().messages()[0].examples()[0].headers['some-common-header']).to.equal('My header');

result = await parser.parse(fs.readFileSync(path.resolve(__dirname, './good/asyncapi-messages-example-payload.yml'), 'utf8'), { path: __filename });
expect(result.channel('myChannel').subscribe().messages()[0].examples()[0].name).to.equal('Example1');
expect(result.channel('myChannel').subscribe().messages()[0].examples()[0].summary).to.equal('Example1 summary');
expect(result.channel('myChannel').subscribe().messages()[0].examples()[0].payload.name).to.equal('My name');

result = await parser.parse(fs.readFileSync(path.resolve(__dirname, './good/asyncapi-messages-example-headers.yml'), 'utf8'), { path: __filename });
expect(result.channel('myChannel').subscribe().messages()[0].examples()[0].name).to.equal('Example1');
expect(result.channel('myChannel').subscribe().messages()[0].examples()[0].summary).to.equal('Example1 summary');
expect(result.channel('myChannel').subscribe().messages()[0].examples()[0].headers['some-common-header']).to.equal('My header');

result = await parser.parse(fs.readFileSync(path.resolve(__dirname, './good/asyncapi-messages-example-optional.yml'), 'utf8'), { path: __filename });
expect(result.channel('myChannel').subscribe().messages()[0].examples()[0].name).to.equal(undefined);
expect(result.channel('myChannel').subscribe().messages()[0].examples()[0].summary).to.equal(undefined);
expect(result.channel('myChannel').subscribe().messages()[0].examples()[0].payload.name).to.equal('My name');
expect(result.channel('myChannel').subscribe().messages()[0].examples()[0].headers['some-common-header']).to.equal('My header');
});

it('should fail on invalid examples', async function() {
const expectedErrorObject = {
type: 'https://github.com/asyncapi/parser-js/validation-errors',
title: 'There were errors validating the AsyncAPI document.',
};

await checkErrorWrapper(async () => {
await parser.parse(fs.readFileSync(path.resolve(__dirname, './wrong/invalid-asyncapi-messages-example.yml'), 'utf8'), { path: __filename });
}, expectedErrorObject);
});

describe('registerSchemaParser()', function() {
it('no errors can be thrown', function() {
const parserModule = {
Expand Down
24 changes: 24 additions & 0 deletions test/wrong/invalid-asyncapi-messages-example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
asyncapi: 2.0.0
info:
title: My API
version: '1.0.0'

channels:
myChannel:
subscribe:
message:
x-some-extension: 'some extension'
headers:
type: object
properties:
some-common-header:
type: string
payload:
type: object
properties:
name:
type: string
examples:
- name: Example1
summary: Example1 summary

0 comments on commit ae07543

Please sign in to comment.