Introduce Post-Processing Hook for OpenAPI Spec Customization #206
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR introduces a new feature: a post-processing hook that allows for custom modifications of the OpenAPI spec generated by our RSpec tests. It enables users to apply transformations or additions to the spec, such as unrandomizing IDs or adding custom fields, after the spec has been generated but before it is finalised.
Changes
post_process_hook
to apply custom modifications to the OpenAPI spec.@post_process_hook
initialized asnil
and made it accessible viaattr_accessor
.cleanup_schema!
and addedexecute_post_process_hook
, which calls the user-definedpost_process_hook
if it's provided."custom_field": "custom_value"
to the OpenAPI JSON and YAML docs as a proof of concept.Motivation
This enhancement was motivated by the need for greater flexibility in generating OpenAPI specs. Users often have specific requirements for their OpenAPI documentation, such as consistent example values, additional metadata, or custom formatting. By introducing a post-processing hook, we empower users to tailor the generated specs to their precise needs without modifying the core logic of rspec-openapi.
Usage
To utilize this feature, set the
RSpec::OpenAPI.post_process_hook
to a lambda that acceptspath
,records
, andspec
as arguments. Within this lambda, users can manipulate thespec
as needed. For example:This PR provides a foundational step towards more customizable and user-friendly OpenAPI spec generation, opening the door for numerous extensions and customizations based on user feedback and evolving requirements.