Class: OpenapiFirst::ResponseValidation

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

Instance Method Summary collapse

Constructor Details

#initialize(app, _options = {}) ⇒ ResponseValidation

Returns a new instance of ResponseValidation.



10
11
12
# File 'lib/openapi_first/response_validation.rb', line 10

def initialize(app, _options = {})
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



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

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

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/openapi_first/response_validation.rb', line 23

def validate(response, operation)
  status, headers, body = response.to_a
  response_definition = response_for(operation, status)

  validate_response_headers(response_definition.headers, headers, openapi_version: operation.openapi_version)

  return if no_content?(response_definition)

  content_type = Rack::Response[status, headers, body].content_type
  raise ResponseInvalid, "Response has no content-type for '#{operation.name}'" unless content_type

  response_schema = response_definition.schema_for(content_type)
  unless response_schema
    message = "Response content type not found '#{content_type}' for '#{operation.name}'"
    raise ResponseContentTypeNotFoundError, message
  end
  validate_response_body(response_schema, body)
end