Skip to content

Replace StrictHashConfiguration by simpler Grape::Util::ApiDescription #2575

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

Merged

Conversation

ericproulx
Copy link
Contributor

Refactor: Replace StrictHashConfiguration with ApiDescription

This pull request refactors the mechanism for handling API endpoint descriptions by replacing the overly complex Grape::Util::StrictHashConfiguration with a new, more straightforward class, Grape::Util::ApiDescription.

Problem

The previous implementation, StrictHashConfiguration, used significant metaprogramming (class_eval, define_method, const_set) to dynamically build configuration containers. This made the code:

  • Difficult to understand: The dynamic nature obscured the simple goal of collecting description attributes.
  • Hard to debug: Errors were often cryptic and disconnected from the original desc block.
  • Inflexible: Adding new description attributes was cumbersome and required modifying the dynamic builder.

Solution

The new Grape::Util::ApiDescription class provides a clear and explicit DSL for defining endpoint metadata.

Key Changes:

  • Added Grape::Util::ApiDescription: A simple PORO (Plain Old Ruby Object) that uses an instance_eval block to provide a clean DSL for setting description attributes. All available attributes (:summary, :detail, :params, etc.) are explicitly defined.
  • Updated Grape::DSL::Desc: The desc method in the DSL now instantiates ApiDescription instead of using the StrictHashConfiguration module.
  • Removed Grape::Util::StrictHashConfiguration: The complex, metaprogrammed module and its specs have been completely removed from the codebase.

Benefits

  • Improved Readability & Maintainability: The new implementation is simple, explicit, and easy for any developer to understand and extend.
  • Simplified Codebase: Removed over 100 lines of complex metaprogramming in favor of a ~50 line PORO.
  • Better Developer Experience: Debugging is now much easier, as the code directly reflects the DSL's behavior.
  • No Functional Change: This is purely a refactoring. It has no impact on the public API or runtime behavior of Grape. All existing APIs will continue to work as before.

This change makes a core part of Grape's DSL significantly more robust and easier to maintain for the future.

@ericproulx ericproulx requested a review from dblock June 22, 2025 17:34
@grape-bot
Copy link

grape-bot commented Jun 22, 2025

1 Warning
⚠️ There're library changes, but not tests. That's OK as long as you're refactoring existing code.

Generated by 🚫 Danger

@ericproulx ericproulx force-pushed the remove_strict_hash_configuration branch from 161b4ad to b79c77b Compare June 22, 2025 17:36
Add Grape::Util::ApiDescription

Remove options from ApiDescription

Remove const

Update api_description.rb

FIx rubocop

Renamed `to_h` by `settings`
@ericproulx ericproulx force-pushed the remove_strict_hash_configuration branch from b79c77b to 017ac62 Compare June 23, 2025 08:00
@ericproulx ericproulx marked this pull request as ready for review June 23, 2025 08:00
@dblock dblock merged commit 5dca095 into ruby-grape:master Jun 26, 2025
47 checks passed
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.

3 participants