Class: OpenapiFirst::Definition

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/openapi_first/definition.rb

Overview

Represents an OpenAPI API Description document This is returned by OpenapiFirst.load.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resolved, filepath = nil) {|@config| ... } ⇒ Definition

Returns a new instance of Definition.

Parameters:

  • resolved (Hash)

    The resolved OpenAPI document.

  • filepath (String) (defaults to: nil)

    The file path of the OpenAPI document.

Yields:



27
28
29
30
31
32
33
34
35
# File 'lib/openapi_first/definition.rb', line 27

def initialize(resolved, filepath = nil)
  @filepath = filepath
  @config = OpenapiFirst.configuration.clone
  yield @config if block_given?
  @config.freeze
  @router = Builder.build_router(resolved, @config)
  @resolved = resolved
  @paths = resolved['paths'].keys # TODO: Move into builder as well
end

Instance Attribute Details

#configConfiguration (readonly)

Returns:



19
20
21
# File 'lib/openapi_first/definition.rb', line 19

def config
  @config
end

#filepathString? (readonly)

Returns:

  • (String, nil)


17
18
19
# File 'lib/openapi_first/definition.rb', line 17

def filepath
  @filepath
end

#pathsEnumerable[String] (readonly)

Returns:

  • (Enumerable[String])


21
22
23
# File 'lib/openapi_first/definition.rb', line 21

def paths
  @paths
end

#routerRouter (readonly)

Returns:



23
24
25
# File 'lib/openapi_first/definition.rb', line 23

def router
  @router
end

Instance Method Details

#[](key) ⇒ Hash

Gives access to the raw resolved Hash. Like ‘mydefinition.dig(’schemas’, ‘Stations’)‘

Returns:

  • (Hash)


40
# File 'lib/openapi_first/definition.rb', line 40

def_delegators :@resolved, :[]

#routesEnumerable[Router::Route]

Returns an Enumerable of available Routes for this API description.

Returns:



45
# File 'lib/openapi_first/definition.rb', line 45

def_delegators :@router, :routes

#validate_request(request, raise_error: false) ⇒ ValidatedRequest

Validates the request against the API description.

Parameters:

  • request (Rack::Request)

    The Rack request object.

  • raise_error (Boolean) (defaults to: false)

    Whether to raise an error if validation fails.

Returns:



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/openapi_first/definition.rb', line 51

def validate_request(request, raise_error: false)
  route = @router.match(request.request_method, request.path, content_type: request.content_type)
  if route.error
    ValidatedRequest.new(request, error: route.error)
  else
    route.request_definition.validate(request, route_params: route.params)
  end.tap do |validated|
    @config.hooks[:after_request_validation].each { |hook| hook.call(validated, self) }
    raise validated.error.exception(validated) if validated.error && raise_error
  end
end

#validate_response(rack_request, rack_response, raise_error: false) ⇒ ValidatedResponse

Validates the response against the API description.

Parameters:

  • rack_request (Rack::Request)

    The Rack request object.

  • rack_response (Rack::Response)

    The Rack response object.

  • raise_error (Boolean) (defaults to: false)

    Whether to raise an error if validation fails.

Returns:



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/openapi_first/definition.rb', line 68

def validate_response(rack_request, rack_response, raise_error: false)
  route = @router.match(rack_request.request_method, rack_request.path, content_type: rack_request.content_type)
  return if route.error # Skip response validation for unknown requests

  response_match = route.match_response(status: rack_response.status, content_type: rack_response.content_type)
  error = response_match.error
  if error
    ValidatedResponse.new(rack_response, error:)
  else
    response_match.response.validate(rack_response)
  end.tap do |validated|
    @config.hooks[:after_response_validation]&.each { |hook| hook.call(validated, rack_request, self) }
    raise validated.error.exception(validated) if raise_error && validated.invalid?
  end
end