Replies: 5 comments 4 replies
-
Hi Bijan, If you have a shared controller for your API routes one way could be: module Api
class ApplicationController < ActionController::API
if Rails.env.test?
use OpenapiFirst::Middlewares::ResponseValidation,
spec: Rails.root.join('public', 'openapi.yaml')
end
use OpenapiFirst::Middlewares::RequestValidation, spec: Rails.root.join('public', 'openapi.yaml'),
raise_error: Rails.env.test?
# ...
#
end
end What do you think about that variant? Could you share why you don't use the request validation middleware in production? Are you concerned about performance? I know that several people have the use case you described, so I am very open to add a more integrated solution to solve this. |
Beta Was this translation helpful? Give feedback.
-
Hi again, another option would be to subclass the build in middlewares like this: class MyResponseValidation < OpenapiFirst::Middlewares::ResponseValidation
def call(env)
if Rack::Request.new(env).path == '/health'
super
else
@app.call(env)
end
end
end Or what do you think about an option to skip validation for certain requests (not implemented)? config.middleware.use OpenapiFirst::Middlewares::ResponseValidation,
spec: 'api/openapi.yaml',
except: ->(env) { env[Rack::PATH_INFO].start_with?('/assets') } |
Beta Was this translation helpful? Give feedback.
-
Hi Andreas, |
Beta Was this translation helpful? Give feedback.
-
One thing using the request validation middleware on production enables is accessing the parsed request parameters. The middleware has to parse parameters and request body for validation and adds If you happen to find the time and resources to test this out, any feedback would be appreciated. |
Beta Was this translation helpful? Give feedback.
-
I have created an issue to validate the OpenAPI file so one can track the status of that feature there. I like the idea. An API about skipping non-API routes while using the built-in middlewares globally is still up for debate. |
Beta Was this translation helpful? Give feedback.
-
I am currently figuring out an easy way to make use of both validation middlewares in my test for an app that has api routes where the middlewares make sense and some normal oldschool rails routes that should be excluded.
What is the recommended way here?
I thought about disabling the middleware and build a helper to mark tests that should be openapi_first validated.
Another approach could be a middleware that checks if the path starts for example /api/ and only then loads the validation middlewares.
What do you recommend? How you handle this?
Currently I load in test.rb which makes normal controller tests fail of course (these routes should not be included in openapi.yml ofc):
Beta Was this translation helpful? Give feedback.
All reactions