Redocly / redocly-cli

⚒️ Redocly CLI makes OpenAPI easy. Lint/validate to any standard, generate beautiful docs, and more.
https://redocly.com/docs/cli/
MIT License
912 stars 139 forks source link

Ignore validating example with externalValue against schema #1509

Closed xeptore closed 6 months ago

xeptore commented 6 months ago

What/Why/How?

Currently, when an example object has externalValue property no-invalid-media-type-examples lint rule fails as it is validating the example object (the parent object) against the schema definition.

I could assume there are couple of ways to handle this case. However, as there is already another lint rule (no-example-value-and-externalValue), which is kind of related to this one, and its default severity level causes either one of value, or externalValue properties to exist for a specific example object, I decided to go with the simple exclusion condition, which basically deactivates the validation for example objects that has externalValue set. I also intentionally didn't implement the logic to verify the specified URL set for externalValue to check whether the target resource actually matches the schema definition or not (by issuing HTTP request for example) as it's both complex to implement properly due to various cases including relative URLs, and for external resources being unpredictable by nature, e.g., the server might be unavailable at the time of linting, or returing a temporary failure due to network issues.

Reference

I faced the issue while I was trying to add file examples for request, and response bodies of an API according to the official documentation (here).

Testing

Relevant tests are updated to cover this scenario.

Screenshots (optional)

It can be tested by disabling the condition I added to the lint rule.

Check yourself

Security

changeset-bot[bot] commented 6 months ago

🦋 Changeset detected

Latest commit: f3ae8ef13afc52f4c6b33b0c5adddfea1b5bcf3a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages | Name | Type | | --------------------- | ----- | | @redocly/cli | Patch | | @redocly/openapi-core | Patch |

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

xeptore commented 6 months ago

Also, please add a patch changeset for both core and cli packages describing the changes. Thanks!

Done in 82869150e676cf1d59d35e2f09cc6faf86027474.

P.S. Please don't forget to run prettier 🙂

Done in cf02cec879c8e9e557fae2f2f7093f802741da80.

tatomyr commented 6 months ago

Thank you @xeptore for spending your time on this! 👍

xeptore commented 6 months ago

Thank you @xeptore for spending your time on this! 👍

Happy to help! Cheers