rspec-openapi icon indicating copy to clipboard operation
rspec-openapi copied to clipboard

Makes header & request params optional and handle empty files gracefully

Open muhammadnawzad opened this issue 5 months ago • 0 comments

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 avatar Sep 11 '24 11:09 muhammadnawzad