Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Makes header & request params optional and handle empty files gracefully #236

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

muhammadnawzad
Copy link

@muhammadnawzad muhammadnawzad commented Sep 11, 2024

Hello good people, I hope all is well.

This PR introduces three minor changes to the rspec-openapi gem:

  1. Header params can now be optional and by default required:
    In the previous implementation, all header params were marked as required (required: true). Now, header params are optional, and by default required. The logic has been updated to check the optional_headers field, and any headers not present in that list are considered required (record.optional_headers.exclude?(key))

  2. Requets body params can now be optional and by default required:
    In the previous implementation, all request body params were marked as required from the enrich_with_required_keys method, (obj[:required] = obj[:properties]&.keys || []). Now, request body params are optional, and by default required. The logic has been updated to check the optional_request_params field in the record, and any params not present in that list are considered required ((obj[:properties]&.keys - optional_request_params) || [])

  3. Gracefully handle empty files during YAML/JSON parsing:
    The previous implementation would raise errors when attempting to load an empty YAML/JSON file. The update adds a check to ensure that if the content is nil (i.e., the file is empty), an empty hash {} is returned, avoiding any errors. (Check the provided error at the bottom).

These changes improve flexibility in defining required headers and prevent errors when dealing with empty files. Please note that I have tested these on a Rails API, provided changes should be tested in other frameworks.

P.S. Automated unit test is not provided since the changes doesn't modify the intended behavior of these methods by default current tests should cover these as well.

Raised Error mentioned in 3:

Failure/Error: base.replace(RSpec::OpenAPI::KeyTransformer.symbolize(base))

NoMethodError:
  undefined method `replace' for nil

@muhammadnawzad muhammadnawzad changed the title Makes header params optional and handle empty files gracefully Makes header & request params optional and handle empty files gracefully Sep 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant