Class: OpenapiFirst::ResponseValidation

Inherits:
Object
  • Object
show all
Includes:
RouterRequired
Defined in:
lib/openapi_first/response_validation.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ResponseValidation

Returns a new instance of ResponseValidation.



12
13
14
# File 'lib/openapi_first/response_validation.rb', line 12

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/openapi_first/response_validation.rb', line 16

def call(env)
  operation = env[OPERATION]
  return @app.call(env) unless operation

  response = @app.call(env)
  validate(response, operation)
  response
end

#validate(response, operation) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/openapi_first/response_validation.rb', line 25

def validate(response, operation)
  status, headers, body = response.to_a
  return validate_status_only(operation, status) if status == 204

  content_type = headers[Rack::CONTENT_TYPE]
  response_schema = operation.response_schema_for(status, content_type)
  validate_response_body(response_schema, body) if response_schema
end