parser-js icon indicating copy to clipboard operation
parser-js copied to clipboard

Handle in proper way empty object/array in has* like functions

Open magicmatatjahu opened this issue 5 years ago • 7 comments

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

magicmatatjahu avatar Nov 19 '20 12:11 magicmatatjahu

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:

jonaslagoni avatar Nov 19 '20 12:11 jonaslagoni

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

derberg avatar Nov 19 '20 12:11 derberg

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:

github-actions[bot] avatar Jan 19 '21 01:01 github-actions[bot]

I'm wondering what is the point of keeping it open 😄

derberg avatar Oct 05 '21 09:10 derberg

@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.

magicmatatjahu avatar Oct 05 '21 09:10 magicmatatjahu

I think you might be interested in joining https://github.com/asyncapi/parser-js/pull/453

smoya avatar Mar 07 '22 16:03 smoya

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.

smoya avatar Jul 11 '22 14:07 smoya

Issue is resolved in v2 ParserJS - now in https://github.com/asyncapi/parser-js/tree/next-major branch

magicmatatjahu avatar Oct 25 '22 08:10 magicmatatjahu