Skip to content

Commit

Permalink
Merge pull request #45 from event-catalog/adding-new-extensions-for-i…
Browse files Browse the repository at this point in the history
…d-and-name

feat(core): added new extensions for name and id in openapi
  • Loading branch information
boyney123 authored Nov 26, 2024
2 parents 00195bc + 73edb5e commit 6e85814
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/sharp-bees-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@eventcatalog/generator-openapi": major
---

feat(core): added new extensions for name and id in openapi
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ const processMessagesForOpenAPISpec = async (pathToSpec: string, document: OpenA
}

// Write the message to the catalog
await writeMessage({ ...message, markdown: messageMarkdown }, { path: message.name, override: true });
await writeMessage({ ...message, markdown: messageMarkdown }, { path: message.id, override: true });

// If the message send or recieved by the service?
if (messageAction === 'sends') {
Expand Down
2 changes: 2 additions & 0 deletions src/test/openapi-files/petstore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ paths:
tags:
- pets
x-eventcatalog-message-type: query # This is a query operation
x-eventcatalog-message-name: List Pets # Name used in EventCatalog
x-eventcatalog-message-id: list-pets # ID value used in EventCatalog
parameters:
- name: limit
in: query
Expand Down
47 changes: 38 additions & 9 deletions src/test/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ describe('OpenAPI EventCatalog Plugin', () => {

expect(service.receives).toHaveLength(4);
expect(service.receives).toEqual([
{ id: 'listPets', version: '1.0.0' },
{ id: 'list-pets', version: '1.0.0' },
{ id: 'createPets', version: '1.0.0' },
{ id: 'showPetById', version: '1.0.0' },
{ id: 'petAdopted', version: '1.0.0' },
Expand Down Expand Up @@ -501,7 +501,7 @@ describe('OpenAPI EventCatalog Plugin', () => {
expect(service.receives).toHaveLength(5);
expect(service.receives).toEqual([
{ id: 'userloggedin', version: '1.0.0' },
{ id: 'listPets', version: '1.0.0' },
{ id: 'list-pets', version: '1.0.0' },
{ id: 'createPets', version: '1.0.0' },
{ id: 'showPetById', version: '1.0.0' },
{ id: 'petAdopted', version: '1.0.0' },
Expand All @@ -519,7 +519,7 @@ describe('OpenAPI EventCatalog Plugin', () => {
name: 'Random Name',
markdown: 'Here is my original markdown, please do not override this!',
receives: [
{ id: 'listPets', version: '1.0.0' },
{ id: 'list-pets', version: '1.0.0' },
{ id: 'createPets', version: '1.0.0' },
],
},
Expand All @@ -532,7 +532,7 @@ describe('OpenAPI EventCatalog Plugin', () => {
expect(service.receives).toHaveLength(4);

expect(service.receives).toEqual([
{ id: 'listPets', version: '1.0.0' },
{ id: 'list-pets', version: '1.0.0' },
{ id: 'createPets', version: '1.0.0' },
{ id: 'showPetById', version: '1.0.0' },
{ id: 'petAdopted', version: '1.0.0' },
Expand Down Expand Up @@ -562,16 +562,20 @@ describe('OpenAPI EventCatalog Plugin', () => {

await plugin(config, { services: [{ path: join(openAPIExamples, 'petstore.yml'), id: 'swagger-petstore' }] });

const command = await getQuery('listPets');
const command = await getQuery('list-pets');

const file = await fs.readFile(join(catalogDir, 'queries', 'listPets', 'index.md'));
const dir = await fs.readdir(join(catalogDir, 'queries'));

console.log(dir);

const file = await fs.readFile(join(catalogDir, 'queries', 'list-pets', 'index.md'));
expect(file).toBeDefined();

expect(command).toEqual(
expect.objectContaining({
id: 'listPets',
id: 'list-pets',
version: '1.0.0',
name: 'listPets',
name: 'List Pets',
summary: 'List all pets',
badges: [
{ content: 'GET', textColor: 'blue', backgroundColor: 'blue' },
Expand Down Expand Up @@ -643,6 +647,31 @@ describe('OpenAPI EventCatalog Plugin', () => {
expect(service.sends).toHaveLength(1);
expect(service.sends).toEqual([{ id: 'petVaccinated', version: '1.0.0' }]);
});

it('when messages have the `x-eventcatalog-message-name` extension defined, this value is used for the message name', async () => {
const { getQuery } = utils(catalogDir);

await plugin(config, { services: [{ path: join(openAPIExamples, 'petstore.yml'), id: 'swagger-petstore' }] });

const event = await getQuery('list-pets');

expect(event).toEqual(
expect.objectContaining({
id: 'list-pets',
name: 'List Pets',
version: '1.0.0',
summary: 'List all pets',
})
);
});
it('when messages have the `x-eventcatalog-message-id` extension defined, this value is used for the message id', async () => {
const { getQuery } = utils(catalogDir);

await plugin(config, { services: [{ path: join(openAPIExamples, 'petstore.yml'), id: 'swagger-petstore' }] });

const event = await getQuery('list-pets');
expect(event.id).toEqual('list-pets');
});
});

it('when the message already exists in EventCatalog but the versions do not match, the existing message is versioned', async () => {
Expand Down Expand Up @@ -766,7 +795,7 @@ describe('OpenAPI EventCatalog Plugin', () => {

await plugin(config, { services: [{ path: join(openAPIExamples, 'petstore.yml'), id: 'swagger-petstore' }] });

const command = await getCommand('listPets');
const command = await getCommand('list-pets');

expect(command.markdown).toContain(`### Parameters
- **limit** (query): How many items to return at one time (max 100)`);
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export type Operation = {
action: string;
externalDocs?: OpenAPIV3_1.ExternalDocumentationObject;
tags: string[];
extensions?: {
[key: string]: any;
};
};

export interface OpenAPIParameter {
Expand Down
5 changes: 3 additions & 2 deletions src/utils/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export const getSummary = (message: Operation) => {

export const buildMessage = async (pathToFile: string, document: OpenAPI.Document, operation: Operation) => {
const requestBodiesAndResponses = await getSchemasByOperationId(pathToFile, operation.operationId);
const extensions = operation.extensions || {};

const operationTags = operation.tags.map((badge) => ({
content: `tag:${badge}`,
Expand All @@ -116,9 +117,9 @@ export const buildMessage = async (pathToFile: string, document: OpenAPI.Documen
}

return {
id: uniqueIdentifier,
id: extensions['x-eventcatalog-message-id'] || uniqueIdentifier,
version: document.info.version,
name: uniqueIdentifier,
name: extensions['x-eventcatalog-message-name'] || uniqueIdentifier,
summary: getSummary(operation),
markdown: defaultMarkdown(operation, requestBodiesAndResponses),
schemaPath: requestBodiesAndResponses?.requestBody ? 'request-body.json' : '',
Expand Down
7 changes: 7 additions & 0 deletions src/utils/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ export async function getOperationsByType(openApiPath: string) {
// Check if the x-eventcatalog-message-type field is set
const messageType = openAPIOperation['x-eventcatalog-message-type'] || DEFAULT_MESSAGE_TYPE;
const messageAction = openAPIOperation['x-eventcatalog-message-action'] === 'sends' ? 'sends' : 'receives';
const extensions = Object.keys(openAPIOperation).reduce((acc: { [key: string]: any }, key) => {
if (key.startsWith('x-eventcatalog-')) {
acc[key] = openAPIOperation[key];
}
return acc;
}, {});

const operation = {
path: path,
Expand All @@ -88,6 +94,7 @@ export async function getOperationsByType(openApiPath: string) {
description: openAPIOperation.description,
summary: openAPIOperation.summary,
tags: openAPIOperation.tags || [],
extensions,
} as Operation;

operations.push(operation);
Expand Down

0 comments on commit 6e85814

Please sign in to comment.