thim81 / openapi-format

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

error with inverseOperationIds and example values of null #56

Closed pf-lkobbe closed 1 year ago

pf-lkobbe commented 2 years ago

An error is thrown if filter option "inverseOperationIds" is used against a json schema file that contains a null value in any of the examples (not necessarily in the operation with the to-be-filtered operationID). Obviously, I was able to fix this by adjusting the example value, but it took me a while to identify the issue, so it'd be great if this error could be caught with a better description than UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'operationId' of null.

The problematic part in the json: "primaryColor": { "type": "string", "example": null }

thim81 commented 2 years ago

thank @pf-lkobbe, I'll try to reproduce it and foresee a better error message.

thim81 commented 2 years ago

@pf-lkobbe I tried to reproduce the issue in the test that was added in #58 , but I was not able to reproduce the issue.

Could you provide me some more info so that I can reproduce the issue?

thim81 commented 2 years ago

@pf-lkobbe Would it be possible to provide me with an OpenAPI example and OpenAPI-format configuration that results in the error?

Since I seem unable to reproduce it.

saoco commented 1 year ago

Hi, I was facing the same error, but my OpenAPI specification is quite big, so it's hard (with my knowledge and skills) to track down the root cause.

In order to fix it, I simply added node !== null to line 362 in openapi-format.js :

// Filter out fields without operationIds, when Inverse operationIds is set
if (node !== null && inverseFilterProps.length > 0 && this.path[0] === 'paths' && node.operationId === undefined
  && httpVerbs.includes(this.key)
) {
  // debugFilterStep = 'Filter - Single field - Inverse operationIds without operationIds'
  this.remove();
}

Best

Christian

thim81 commented 1 year ago

@saoco Your solution proposed is very simple and quite risk free, so I'll include that in a PR.

I created a test in an PR https://github.com/thim81/openapi-format/pull/58/files#diff-0d93bd3e8e1dcc8c388140ffab6fec8c5aeb39c7ffbec60e0929edc8f688f404 to try to reproduce the issue, but I was not able to do so.

Would it be possible to share the big OpenAPI so that I at least can use that for a manual validation?

thim81 commented 1 year ago

@saoco @pf-lkobbe I just released openapi-format version 1.10.3 which has the fix included.

Could you verify if the issue is solved?

saoco commented 1 year ago

@thim81 I just updated my local version to 1.10.3 and I can "build" my API without any issues. The fix seems to work. Please excuse that it took so long to verify for fix.

Best Christian