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

refactor: implement schemas and all* methods in AsyncAPIDocument class #612

Merged

Conversation

magicmatatjahu
Copy link
Member

@magicmatatjahu magicmatatjahu commented Sep 15, 2022

Description

Implement schemas, allChannels, allServers, allOperations, allMessages, allSchemas methods in the AsyncAPIDocument class.

cc @smoya

Related issue(s)
Part of #481
Part of #482

@magicmatatjahu magicmatatjahu added the enhancement New feature or request label Sep 15, 2022
@magicmatatjahu magicmatatjahu marked this pull request as ready for review September 15, 2022 08:31
schemas.add(schema);
}
}
// return only schemas used in channels ("active" schemas)
Copy link
Member

Choose a reason for hiding this comment

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

This is raising a hidden behaviour I didn't think when designing the API. Are we sure this is the behavior all users will need? Once i have the "active" schemas, how do I know the ones are not active?

I think we should rather return all schemas and add a filter in the Schemas collection such as FilterByActive() and FilterByInactive or FilterByInUse()/FilterByNotInUse() or whatever.

Copy link
Member Author

Choose a reason for hiding this comment

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

If someone will need also inactive schemas can write:

    const schemas: Set<SchemaInterface> = new Set();
    function callback(schema: SchemaInterface) {
      if (!schemas.has(schema.json())) {
        schemas.add(schema);
      }
    }
    traverseAsyncApiDocument(this, callback);
    const schemas = new Schemas(Array.from(schemas));

Copy link
Member

@smoya smoya Sep 15, 2022

Choose a reason for hiding this comment

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

Sure they can but I don't think we should take this decision on return only the ones used by channels but rather all of them. Then, thanks to filters, they could filter after all.

Copy link
Member Author

@magicmatatjahu magicmatatjahu Sep 15, 2022

Choose a reason for hiding this comment

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

🤷🏼 I don't know if we should take these methods.

Copy link
Member

Choose a reason for hiding this comment

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

We had a chat on Slack about it. As it ended up making sense, I'm gonna create those filters not only for Schemas but also for Channels, Operations, Messages, and Servers, also a PR to parser-api reflecting the change.
Can you @magicmatatjahu then make this method to return all schemas?

Copy link
Member Author

Choose a reason for hiding this comment

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

@smoya After your PR, ok? 😄

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

@magicmatatjahu would you have some time to review the PR mentioned in my previous component, so we can unblock this? 🙏

Copy link
Member

Choose a reason for hiding this comment

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

I think you can adapt the code of this PR to consider #619 is gonna happen so we can merge this right? It is not really blocked in fact.

Copy link
Member Author

Choose a reason for hiding this comment

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

@magicmatatjahu magicmatatjahu changed the title refactor: implement schemas method in AsyncAPIDocument class refactor: implement schemas and all* methods in AsyncAPIDocument class Oct 4, 2022
@sonarcloud
Copy link

sonarcloud bot commented Oct 4, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@magicmatatjahu
Copy link
Member Author

@smoya I pushed latest changes with allChannels, allServers, allOperations, allMessages, allSchemas methods. :)

const operations: OperationInterface[] = [];
this.channels().forEach(channel => operations.push(...channel.operations().all()));
return new Operations(operations);
return new Operations([]);
Copy link
Member Author

Choose a reason for hiding this comment

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

We don't have operations in components in v2, so we should return empty collection.

@@ -91,7 +96,60 @@ export class AsyncAPIDocument extends BaseModel<v2.AsyncAPIObject> implements As
return this.createModel(Components, this._json.components || {}, { pointer: '/components' });
}

allServers(): ServersInterface {
Copy link
Member

Choose a reason for hiding this comment

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

This comment applies to all new all* methods.

If I understand correctly, the following servers will be considered equal and only one will be returned:

servers:
  myServer: {}
components:
  servers:
    anotherServer: {}

If I'm right, I think the behavior is wrong and the key (stored under metadata.id i think) should be considered as well when comparing.

Copy link
Member Author

Choose a reason for hiding this comment

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

In your example you will have two servers, servers.myServer and components.servers.anotherServer. We only compare the data of given object and we base on that filtering of these same "references".

Copy link
Member

Choose a reason for hiding this comment

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

So we compare the references and not the value of the json object?

Copy link
Member Author

Choose a reason for hiding this comment

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

yep, these same references == these same JSON objects

Copy link
Member Author

Choose a reason for hiding this comment

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

You can check tests for allSchemas or allMessages :)

Copy link
Member

Choose a reason for hiding this comment

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

Then, all good! Thanks for the explanation.

Copy link
Member

@smoya smoya left a comment

Choose a reason for hiding this comment

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

Awesome work @magicmatatjahu ! LGTM!

@magicmatatjahu
Copy link
Member Author

/rtm

@asyncapi-bot asyncapi-bot merged commit 562360c into asyncapi:next-major Oct 4, 2022
@magicmatatjahu magicmatatjahu deleted the next/schemas-function branch October 4, 2022 13:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request ready-to-merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants