parser-js
parser-js copied to clipboard
Handle in proper way empty object/array in has* like functions
Reason/Context
After discussion in this PR https://github.com/asyncapi/parser-js/pull/192 (comment https://github.com/asyncapi/parser-js/pull/192#issuecomment-729031318) we should consider in which way we should go with handling empty object/array in has* like functions. At this moment, parser-js returns true if given field has object/array with values, and also true if object/array is empty - in those functions is used !! statement to convert value to boolean. Unfortunately JS treats empty object/array as true value. In opposite, empty object/array should treat as false value.
Description
If we decide to change default behavior, then changing logic will be huge breaking change to handle schema in templates.
Also we should discuss point of view from this comment: https://github.com/asyncapi/parser-js/issues/173#issuecomment-699637697
I still have the point of view that has* implies something has been defined i.e. it has values or has been defined with values in which empty arrays/objects does not :smile:
I like has* functions (didn't think if they can have better name) as they imply clearly what function is for. We should have those functions all around and use them only to determine if given part of spec is present or not. This way, in the future, it will be easy to change the logic inside those, depending how we understand that something is there or isn't. Function like properties should be only to extract properties, but I'd like to easily check if they are there with hasProperties.
For me, if object or array are empty, has* function should return false
This issue has been automatically marked as stale because it has not had recent activity :sleeping: It will be closed in 60 days if no further activity occurs. To unstale this issue, add a comment with detailed explanation. Thank you for your contributions :heart:
I'm wondering what is the point of keeping it open 😄
@derberg
At this moment, parser-js returns true if given field has object/array with values, and also true if object/array is empty - in those functions is used !! statement to convert value to boolean. Unfortunately JS treats empty object/array as true value. In opposite, empty object/array should treat as false value
for that :) Probably when we will release 3.0.0 version of AsyncAPI ParserJS will have breaking changes and we can think about handling such a problem.
I think you might be interested in joining https://github.com/asyncapi/parser-js/pull/453
This is already being handled in the future v2.0.0 release, which follows the Intent-Driven API.
We could close this ticket once the v2.0.0 version of the parser is fixed.
Issue is resolved in v2 ParserJS - now in https://github.com/asyncapi/parser-js/tree/next-major branch