thim81 / openapi-format

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

feat: --keepEmptySchema option #84

Closed mamatsunami closed 9 months ago

mamatsunami commented 9 months ago

Context

When filtering a OpenAPI spec, openapi-format automatically remove empty objects. I had some issues with the openapi-generator because it doesn't handle empty request body.

I wanted to format a OpenAPI spec generated by Symfony API Platform which had some empty schema response object like this :

requestBody:
  description: Create a new pet in the store
  required: true
  content:
    application/json:
      schema: {}

It was converting them to this :

requestBody:
  description: Create a new pet in the store
  required: true
  content:
    application/json: {}

Which causes a java.lang.RuntimeException: Request body cannot be null with openapi-generator when I try to generate an API client from the filtered spec file.

Solution

I added a --keepEmptySchema parameter which disable the removal of empty schema object as a workaround.

thim81 commented 9 months ago

Thanks @mamatsunami for your contribution, much appreciated.

mamatsunami commented 9 months ago

@thim81 Just realizing there is other case where it should be nice to prevent the removal of empty objects. I've seen you didn't yet create a release from my commit.

I was thinking to rework my current --keepEmptySchema option to make it generic. I think it should be more flexible to rename it --keepEmptyObjects. --keepEmptyObjects would skip the removal of all empty object, but you could also pass an array of property to only prevent the removal of empty object in some cases, for instance --keepEmptyObjects=examples,schema.

Can you wait I submit another PR before publishing a new version ?

thim81 commented 9 months ago

That would be a more generic approach, so looking forward for you PR.

thim81 commented 9 months ago

Could you also check that the new --keepEmptyObjects is also supported through the --configFile option? Since it offers more options that just a simple boolean, so it would be handy to define this in the JSON config file that can be passed along.

mamatsunami commented 9 months ago

@thim81 here it is ! https://github.com/thim81/openapi-format/pull/85 (I changed a bit my approach)