Search code examples
ruby-on-railsrubyruby-on-rails-5

Inspect RoR json API


Is any way (I mean RoR code) to enumerate

  • exising API endpoints
  • input data with data types
  • output data with data types
  • something else

Let suppose we use ruby on rails and our api is based on models and its types.

( Something like with schema here )

What is possible and how? What is not possible and why?


Solution

  • This is generally not possible.

    The controllers (and models) in a Rails app define on various layers what data to accept. This is generally not defined in a static format but through a layered validation process (e.g. on the controller with strong_parameters and the models with their validations.

    Since those validations can define arbitrarily complex business rules using Ruby code, usually, you can thus only check if a given data structure is accepted by trying to pass it to the app and checking if it accepts it without any errors.

    With that being said, there are gems which allow you to define "abstract" API schemas which might be consumed by external clients and used to validate data in your app. Examples here are trailblazer, dry-validation, json-schema and others. Note that these approaches usually require to follow the architectural requirements of these gems which might heavily affect the way you design your application.