Skip to content

feat: iterate through all the schemas and handle circular references - #391

Merged
magicmatatjahu merged 21 commits into
asyncapi:masterfrom
magicmatatjahu:improve-traverse-functions
Nov 23, 2021
Merged

magicmatatjahu merged 21 commits into
asyncapi:masterfrom
magicmatatjahu:improve-traverse-functions

Conversation

@magicmatatjahu

@magicmatatjahu magicmatatjahu commented Oct 18, 2021 •

Copy link
Copy Markdown
Member

Description

Iterate through all the schemas:

IMPORTANT: This PR handles also the circular references #293 because at the moment tests for circular refs are broken due to fact that "handling" of circular refs are done by traverseSchema function (please see logs of issue asyncapi/generator#666 to check that traverseCombinedSchemas function cannot handle such a situation, so I fixed it in PR). So I wanna also fix it by my suggestion here - #293 (comment) but I need feedback for that, cc @jonaslagoni @derberg @fmvilas I am pinging you because you know the most the parser's codebase :)

Related issue(s)
Resolves #293
Resolves #229

Comment thread lib/iterators.js Resolved
@magicmatatjahu
magicmatatjahu marked this pull request as ready for review October 19, 2021 14:56
@magicmatatjahu magicmatatjahu changed the title fix: iterate through all the schemas fix: iterate through all the schemas and handle circular references Oct 19, 2021

@jonaslagoni jonaslagoni left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The tests are starting to get a bit ridiculous 😆

Only two small changes 👏 Looking good 👍

Comment thread lib/iterators.js Resolved
Comment thread lib/iterators.js Resolved
Comment thread lib/iterators.js Resolved
@derberg

derberg commented Oct 25, 2021

Copy link
Copy Markdown
Member

@magicmatatjahu I see @jonaslagoni did a good review so I will not really engage more than just saying it should be feat: as you are adding new features. Other than that, great work to make this happen!

@magicmatatjahu magicmatatjahu changed the title fix: iterate through all the schemas and handle circular references feat: iterate through all the schemas and handle circular references Oct 25, 2021
@magicmatatjahu

Copy link
Copy Markdown
Member Author

@jonaslagoni Thanks for review! Changes are propagated.

@derberg Thanks! Title is changed. However I missed to update Readme's section about circular references - could you check https://github.com/asyncapi/parser-js/pull/391/files#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5R212 ?

Comment thread README.md
- `x-parser-original-schema-format` holds information about the original schema format of the payload. You can use different schema formats with the AsyncAPI documents and the parser converts them to AsyncAPI schema. This is why different schema format is set, and the original one is preserved in the extension.
- `x-parser-original-payload` holds the original payload of the message. You can use different formats for payloads with the AsyncAPI documents and the parser converts them to. For example, it converts payload described with Avro schema to AsyncAPI schema. The original payload is preserved in the extension.
- [`x-parser-circular`](#circular-references)
- [`x-parser-circular-props`](#circular-references)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

why do you remove those? these extensions are still there, right?

@magicmatatjahu magicmatatjahu Oct 25, 2021 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes and no. The old implementation had this to itself, that it marked extensions in places where they should not and gave false information what is circular and what is not - I described it in more detail in issue #293 (case with items in RecursiveAncestor schema).

This PR no longer uses these extensions to determine what is circular and what is not, however it has made fallbacks to return identical values if someone uses extensions - you can see the fallbacks here, so templates which use the function like circularProps(), hasCircularProps() in theory shouldn't be broken :)

The only exception may be when someone uses those extensions inline and not through functions, then there is a problem, but apart from html-template and markdown-template I don't know any other that use information what is circular and what is not.

Also x-parser-circular is still injected in AsyncAPI root document, so I will revert this extension :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

can you explain how is it better to call this operation every time you want to get a list of circular props? instead of having this info always on extension level and just read an array?

I also see you remove from docs info about hasCircularProps and cCircularProps but these are still there

@magicmatatjahu magicmatatjahu Oct 25, 2021 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

ok :) The circularProps() function itself does not currently return any needed information - it was needed in the previous implementation, but no longer is, but I left this function in case some template uses it.

To check why it's not needed (and it didn't work as expected) here is an example:

schemas:
  Circular1:
    props:
      circular2:
        $ref: '#/schemas/Circular2'
  Circular2:
    props:
      circular1:
        $ref: '#/schemas/Circular1'

so we have case when we have circular references:

  1. Circular1 -> Circular2 -> Circular1
  2. Circular2 -> Circular1 -> Circular2

Problem with old implementation for x-circular-props is that it injects that extension to Circular1 with values ['circular2'] and in Circular2 it injects with values ['circular1']. With the above statement of what is circular this information for developer is wrong - the information that property circular1/2 is circular should appear in at least 2 second sub-tree of JSON and not, as now, in 1 sub-tree.

So it is better to define dynamically what is circular and what is not - have in mind that after dereferencing you operate on this same object in places where you used reference to it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ok so you basically do not want to document hasCircularProps and circularProps as they are there for legacy, to not be forced to release 2.0 of the library, right?

shouldn't we mark them as @deprecated or something like that?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

do we need an issue to discuss it? isn't it how it should be just clearly stated in this PR, that all properties, including parser extensions, MUST be accessed by parser helpers and never directly unless helper doesn't exist?

Or that 😄

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

https://github.com/search?q=x-parser-circular-props&type=code so we have usage of it only in our tooling, html-template (in tests), markdown-template (in tests) and in event gateway @smoya 😅 Rest are generated html-docs and pushed to repos.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@smoya I created PR asyncapi/parser-api#23

@derberg @jonaslagoni in latest commit I added note:

NOTE: All extensions added by the parser (including all properties) should be retrieved using special functions. Names of extensions and their location may change, and their eventual changes will not be announced.

Is it enough or you wanna add something more?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@derberg Jonas accepted my changes. Do you have any other objections (including with NOTE)?

@derberg

derberg commented Oct 27, 2021

Copy link
Copy Markdown
Member

@magicmatatjahu only some code smells are there 😉

@magicmatatjahu

Copy link
Copy Markdown
Member Author

@derberg please look at what these smell codes are :) I'm not going to shorten this function, because splitting it will only make it more confusing - identical to the test file.

@derberg

derberg commented Oct 27, 2021

Copy link
Copy Markdown
Member

@magicmatatjahu sure, make sense, but we should not merge with code smells as then we start ignoring them, as simple as that. Just ignore them with proper comments

@magicmatatjahu

Copy link
Copy Markdown
Member Author

@derberg Done, thanks!

derberg
derberg previously approved these changes Oct 27, 2021

@jonaslagoni jonaslagoni left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM 👍

@sonarqubecloud

Copy link
Copy Markdown

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
0.0% 0.0% Duplication

@magicmatatjahu
magicmatatjahu merged commit 3834186 into asyncapi:master Nov 23, 2021
@magicmatatjahu
magicmatatjahu deleted the improve-traverse-functions branch November 23, 2021 14:31
@asyncapi-bot

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 1.12.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request released

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Handling of circular refs in spec is broken Some schemas are never iterated

5 participants