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

fix: apply traits to standalone messages from components section #214

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
df0c326
fix: NOT YET WORKING apply traits to standalone messages
c-pius Dec 10, 2020
f4a59f5
applied linting rules
c-pius Dec 10, 2020
7769b6e
updated output json 1
c-pius Dec 13, 2020
7ebd02d
updated output json 2
c-pius Dec 13, 2020
c5e5ede
guard on components and components.messages
c-pius Dec 13, 2020
f190db1
test for asyncapi without components object
c-pius Dec 13, 2020
148a2a4
Merge remote-tracking branch 'upstream/master' into fix/apply-traits-…
c-pius Dec 13, 2020
7cb782e
fixed test to include anonymous schema ids in schema objects
c-pius Dec 13, 2020
fe01eb7
adding check for not validating messages twice
c-pius Dec 15, 2020
88011c2
convert to string instead of disabling eslint
c-pius Dec 15, 2020
069a0bf
Merge remote-tracking branch 'upstream/master' into fix/apply-traits-…
c-pius Dec 21, 2020
9c71814
applying x-parser-original-schema-format in default parser
c-pius Dec 21, 2020
207c80a
added comment on x-parser-original-schema-format
c-pius Dec 21, 2020
6b4d8b8
default parser adding x-parser-original-payload
c-pius Dec 23, 2020
341aa20
Revert "default parser adding x-parser-original-payload"
c-pius Jan 5, 2021
5d191b0
Revert "added comment on x-parser-original-schema-format"
c-pius Jan 5, 2021
159e574
Revert "applying x-parser-original-schema-format in default parser"
c-pius Jan 5, 2021
8bb2263
added x-parser-message-parsed flag
c-pius Jan 5, 2021
ec9710c
Update lib/parser.js
c-pius Jan 5, 2021
e6acb00
Update lib/parser.js
c-pius Jan 5, 2021
7b8db16
Update lib/parser.js
c-pius Jan 5, 2021
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
38 changes: 35 additions & 3 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const OPERATIONS = ['publish', 'subscribe'];
const SPECIAL_SECURITY_TYPES = ['oauth2', 'openIdConnect'];
const PARSERS = {};
const xParserCircle = 'x-parser-circular';
const xParserMessageParsed = 'x-parser-message-parsed';

/**
* @module @asyncapi/parser
Expand Down Expand Up @@ -169,14 +170,18 @@ async function customDocumentOperations(parsedJSON, asyncapiYAMLorJSON, initialF
validateServerSecurity(parsedJSON, asyncapiYAMLorJSON, initialFormat, SPECIAL_SECURITY_TYPES);

if (!parsedJSON.channels) return;

validateChannels(parsedJSON, asyncapiYAMLorJSON, initialFormat);
validateOperationId(parsedJSON, asyncapiYAMLorJSON, initialFormat, OPERATIONS);

await customComponentsMsgOperations(parsedJSON, asyncapiYAMLorJSON, initialFormat, options);
await customChannelsOperations(parsedJSON, asyncapiYAMLorJSON, initialFormat, options);
}

async function validateAndConvertMessage(msg, originalAsyncAPIDocument, fileFormat, parsedAsyncAPIDocument, pathToPayload) {
//check if the message has been parsed before
if (xParserMessageParsed in msg && msg[String(xParserMessageParsed)] === true) return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's only a question. (xParserMessageParsed in msg) === true will work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in general yes, if (xParserMessageParsed in msg) would work as well as of now. the second check is to check whether the value of x-parser-message-parsed is also set to boolean value of true. if we omit this we would check the sheer presence of the flag and skip validation. would be okay for me as well. just thought the check for a boolean true value would make it more resilient


const schemaFormat = msg.schemaFormat || DEFAULT_SCHEMA_FORMAT;

await PARSERS[String(schemaFormat)]({
Expand All @@ -190,6 +195,7 @@ async function validateAndConvertMessage(msg, originalAsyncAPIDocument, fileForm
});

msg.schemaFormat = DEFAULT_SCHEMA_FORMAT;
msg[String(xParserMessageParsed)] = true;
}

/**
Expand Down Expand Up @@ -231,7 +237,7 @@ function applyTraits(js) {
*
* @param {Object} parsedJSON parsed AsyncAPI document
* @param {String} asyncapiYAMLorJSON AsyncAPI document in string
* @param {String} initialFormat information of the document was oryginally JSON or YAML
* @param {String} initialFormat information of the document was originally JSON or YAML
* @param {Object} options Configuration options.
*/
async function customChannelsOperations(parsedJSON, asyncapiYAMLorJSON, initialFormat, options) {
Expand All @@ -253,4 +259,30 @@ async function customChannelsOperations(parsedJSON, asyncapiYAMLorJSON, initialF
}));
});
await Promise.all(promisesArray);
}
}

/**
* Triggers additional operations on the AsyncAPI messages located in the components section of the document. It triggers operations like traits application, validation and conversion
*
* @private
*
* @param {Object} parsedJSON parsed AsyncAPI document
* @param {String} asyncapiYAMLorJSON AsyncAPI document in string
* @param {String} initialFormat information of the document was originally JSON or YAML
* @param {Object} options Configuration options.
*/
async function customComponentsMsgOperations(parsedJSON, asyncapiYAMLorJSON, initialFormat, options) {
if (!parsedJSON.components || !parsedJSON.components.messages) return;

const promisesArray = [];

Object.entries(parsedJSON.components.messages).forEach(([messageName, message]) => {
derberg marked this conversation as resolved.
Show resolved Hide resolved
if (options.applyTraits) {
applyTraits(message);
}
const pathToPayload = `/components/messages/${messageName}/payload`;
promisesArray.push(validateAndConvertMessage(message, asyncapiYAMLorJSON, initialFormat, parsedJSON, pathToPayload));
derberg marked this conversation as resolved.
Show resolved Hide resolved
});

await Promise.all(promisesArray);
}
35 changes: 35 additions & 0 deletions test/good/asyncapi-messages-channels.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
asyncapi: 2.0.0
info:
title: My API
version: '1.0.0'

channels:
mychannel:
publish:
message:
$ref: '#/components/messages/channelMessage'

components:
messages:
channelMessage:
traits:
- $ref: '#/components/messageTraits/extension'
testMessage:
traits:
- $ref: '#/components/messageTraits/extension'
payload:
$ref: '#/components/schemas/testSchema'
schemas:
testSchema:
type: object
properties:
name:
type: string
messageTraits:
extension:
x-some-extension: 'some extension'
headers:
type: object
properties:
some-common-header:
type: string
27 changes: 27 additions & 0 deletions test/good/asyncapi-no-channels.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
asyncapi: 2.0.0
info:
title: My API
version: '1.0.0'
channels: {}

components:
messages:
testMessage:
traits:
- $ref: '#/components/messageTraits/extension'
payload:
$ref: '#/components/schemas/testSchema'
schemas:
testSchema:
type: object
properties:
name:
type: string
messageTraits:
extension:
x-some-extension: 'some extension'
headers:
type: object
properties:
some-common-header:
type: string
13 changes: 13 additions & 0 deletions test/good/asyncapi-no-components.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
asyncapi: 2.0.0
info:
title: My API
version: 1.0.0
channels:
"/test/tester":
subscribe:
message:
payload:
type: object
properties:
name:
type: string
Loading