thim81 / openapi-format

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

Some filtered fields are still present after formatting #80

Closed sekaranglila closed 4 months ago

sekaranglila commented 10 months ago

I have this definition below:

/payment_requests:
    post:
      tags:
        - public
      operationId: CreatePaymentRequest
      summary: Create Payment Request
      description: Create Payment Request
      parameters:
        - $ref: '#/components/parameters/IdempotencyKey'
          x-internal-field: true
        - $ref: '#/components/parameters/BusinessID'
          x-internal-field: true
        - $ref: '#/components/parameters/CallbackURL'
          x-internal-field: true
        - $ref: '#/components/parameters/ServiceID'
          x-internal-field: true

And after running formatting, it resulted this:

/payment_requests:
    post:
      operationId: CreatePaymentRequest
      summary: Create Payment Request
      description: Create Payment Request
      parameters:
        - $ref: '#/components/parameters/BusinessID'
          x-internal-field: true
        - $ref: '#/components/parameters/ServiceID'
          x-internal-field: true

Filter: "flags": ["x-internal-field"]

As you can see there are 2 field that are left out. I was wondering if anyone knows how to fix it? I've been trying to debug it & reconfiguring my openapi spec file but still no luck

thim81 commented 10 months ago

hi @sekaranglila

The x-internal-field: true should be put in the $ref since that is how $ref behaviour works in OpenAPI. When it is a reference, the logic follows the reference and treats that content of the reference as expected.

Can you try to put x-internal-field: true within the $ref for BusinessID, and see if the parameters gets stripped?

thim81 commented 9 months ago

Hi @sekaranglila,

Did you had a chance to try out my suggestion or is this still an open issue?

TRAD-Anthony-CKO commented 4 months ago

@thim81 Sorry to hijack the issue, but I was looking to do something similar. The problem with adding the flag inside BusinessID is that it will be ignored in multiple unrelated endpoints.

Say I have 2 endpoints using the same component with a $ref, how can you filter out that same component out of the first endpoint only keeping the second intact?

thim81 commented 4 months ago

hi @TRAD-Anthony-CKO & @sekaranglila

I think I understand what you are trying to achieve. The reference is not needed for some but has to be kept for others.

Is the OpenAPI spec generated? Since you could you manually remove the $ref for the endpoints where it is not wanted.

I'll setup a test to verify the behaviour and see if it possible to add the filtering of $ref.

TRAD-Anthony-CKO commented 4 months ago

Thanks for taking a look!

Sadly the OpenAPI spec is not generated, that's why I was looking to filter out a ref block from a single endpoint using the package itself.

thim81 commented 4 months ago

hi @TRAD-Anthony-CKO @sekaranglila

This issue is solved in the latest release of openapi-format v1.16.0.

I added a test to have it covered for future releases. Thanks for providing the input so we can fix it.