thim81 / openapi-format

Format an OpenAPI document by ordering, formatting and filtering fields.
MIT License
77 stars 14 forks source link

YAML empty objects get sripped away #72

Closed a-waider closed 1 year ago

a-waider commented 1 year ago

I've experienced that empty objects in the yaml gets stripped away. An example for a request body in openapi with an epmty object gets stripped away.

E.g.

example:
  default:
    key1: "value1"
    key: {}

becomes

example:
  default:
    key1: "value1"

Is this related to the parsing library used in openapi-format? Do you have any suggestions how to preserve the empty object?

a-waider commented 1 year ago

So, I found the location responsible for removing empty objects: openapi-format.js#L443. When commenting out, it works as intended.

What are the reasons to remove empty objects? Can we just delete lines 440 to 444 in openapi-format.js?

thim81 commented 1 year ago

@a-waider This was there to clean up empty structures as a result of filtering.

If you would filter a certain number of properties or endpoints, it could be that there were empty parent elements, and that is what this removes.

Could you explain why you want to keep the empty key: {}? Just for my understanding of your use-case.

a-waider commented 1 year ago

In my use case there is an example for a request body which contains a key with an empty map.

thim81 commented 1 year ago

I have to think a bit more about this but potentially we could make the line440 more specific to skip it for request body elements

a-waider commented 1 year ago

That would be awesome

a-waider commented 1 year ago

Any news on this issue?

thim81 commented 1 year ago

I just haven't found the time to pick this up.

thim81 commented 1 year ago

hi @a-waider There is a fix ready #73 If the empty object is in a default example, it gets skipped from being deleted. Would that solve your case?

thim81 commented 1 year ago

hi @a-waider

I improved the logic to delete empty objects, like you suggested and released a new version 1.13.1 of openapi-format. Let me know, if it behaves as you expect.

a-waider commented 1 year ago

Thanks for you effort. The new version solves this issue :)