Module: OpenapiFirst
- Defined in:
- lib/openapi_first.rb,
lib/openapi_first/test.rb,
lib/openapi_first/errors.rb,
lib/openapi_first/header.rb,
lib/openapi_first/router.rb,
lib/openapi_first/builder.rb,
lib/openapi_first/failure.rb,
lib/openapi_first/request.rb,
lib/openapi_first/version.rb,
lib/openapi_first/response.rb,
lib/openapi_first/definition.rb,
lib/openapi_first/file_loader.rb,
lib/openapi_first/schema/hash.rb,
lib/openapi_first/ref_resolver.rb,
lib/openapi_first/test/methods.rb,
lib/openapi_first/test/observe.rb,
lib/openapi_first/configuration.rb,
lib/openapi_first/test/callable.rb,
lib/openapi_first/test/coverage.rb,
lib/openapi_first/test/registry.rb,
lib/openapi_first/error_response.rb,
lib/openapi_first/request_parser.rb,
lib/openapi_first/response_parser.rb,
lib/openapi_first/request_validator.rb,
lib/openapi_first/validated_request.rb,
lib/openapi_first/response_validator.rb,
lib/openapi_first/test/configuration.rb,
lib/openapi_first/test/coverage/plan.rb,
lib/openapi_first/test/plain_helpers.rb,
lib/openapi_first/validated_response.rb,
lib/openapi_first/router/find_content.rb,
lib/openapi_first/request_body_parsers.rb,
lib/openapi_first/router/find_response.rb,
lib/openapi_first/router/path_template.rb,
lib/openapi_first/response_body_parsers.rb,
lib/openapi_first/test/minitest_helpers.rb,
lib/openapi_first/error_responses/default.rb,
lib/openapi_first/error_responses/jsonapi.rb,
lib/openapi_first/schema/validation_error.rb,
lib/openapi_first/validators/request_body.rb,
lib/openapi_first/schema/validation_result.rb,
lib/openapi_first/test/coverage/route_task.rb,
lib/openapi_first/test/observer_middleware.rb,
lib/openapi_first/validators/response_body.rb,
lib/openapi_first/test/coverage/request_task.rb,
lib/openapi_first/test/coverage/response_task.rb,
lib/openapi_first/validators/response_headers.rb,
lib/openapi_first/validators/request_parameters.rb,
lib/openapi_first/middlewares/request_validation.rb,
lib/openapi_first/middlewares/response_validation.rb,
lib/openapi_first/test/coverage/terminal_formatter.rb
Overview
OpenapiFirst is a toolchain to build HTTP APIS based on OpenAPI API descriptions.
Defined Under Namespace
Modules: ErrorResponse, ErrorResponses, Middlewares, Schema, Test, Validators Classes: Builder, Configuration, Definition, Error, Failure, FileNotFoundError, NotFoundError, ParseError, Request, RequestInvalidError, RequestParser, RequestValidator, Response, ResponseInvalidError, ResponseNotFoundError, ResponseParser, ResponseValidator, Router, ValidatedRequest, ValidatedResponse
Constant Summary collapse
- REQUEST =
Key in rack to find instance of Request
'openapi.request'
- FAILURE =
:openapi_first_validation_failure
- Header =
Data.define(:name, :required?, :schema, :node) do def resolved_schema node['schema']&.resolved end end
- VERSION =
'2.11.1'
- ParsedRequest =
Data.define(:path, :query, :headers, :body, :cookies)
- ParsedResponse =
Data.define(:body, :headers)
Class Method Summary collapse
- .configuration ⇒ Configuration
- .configure {|Configuration| ... } ⇒ Configuration
-
.find_error_response(name) ⇒ Class
The error response class.
-
.load(filepath_or_definition, only: nil) ⇒ Definition
Load and dereference an OpenAPI spec file or return the Definition if it’s already loaded.
-
.parse(contents, only: nil, filepath: nil) ⇒ Definition
Parse a dereferenced Hash TODO: This needs to work with unresolved contents as well.
-
.register_error_response(name, klass) ⇒ Object
Register an error response class.
Class Method Details
.configuration ⇒ Configuration
22 23 24 |
# File 'lib/openapi_first.rb', line 22 def self.configuration @configuration ||= Configuration.new end |
.configure {|Configuration| ... } ⇒ Configuration
28 29 30 |
# File 'lib/openapi_first.rb', line 28 def self.configure yield configuration end |
.find_error_response(name) ⇒ Class
Returns The error response class.
44 45 46 47 48 49 50 |
# File 'lib/openapi_first.rb', line 44 def self.find_error_response(name) ERROR_RESPONSES.fetch(name) do raise "Unknown error response: #{name}. " / 'Register your error response class via `OpenapiFirst.register_error_response(name, klass)`. ' / "Registered error responses are: #{ERROR_RESPONSES.keys.join(', ')}." end end |
.load(filepath_or_definition, only: nil) ⇒ Definition
Load and dereference an OpenAPI spec file or return the Definition if it’s already loaded
55 56 57 58 59 60 61 62 63 |
# File 'lib/openapi_first.rb', line 55 def self.load(filepath_or_definition, only: nil, &) return filepath_or_definition if filepath_or_definition.is_a?(Definition) filepath = filepath_or_definition raise FileNotFoundError, "File not found: #{filepath}" unless File.exist?(filepath) contents = FileLoader.load(filepath) parse(contents, only:, filepath:, &) end |
.parse(contents, only: nil, filepath: nil) ⇒ Definition
Parse a dereferenced Hash TODO: This needs to work with unresolved contents as well
68 69 70 71 72 |
# File 'lib/openapi_first.rb', line 68 def self.parse(contents, only: nil, filepath: nil, &) contents = ::JSON.parse(::JSON.generate(contents)) # Deeply stringify keys, because of YAML. See https://github.com/ahx/openapi_first/issues/367 contents['paths'].filter!(&->(key, _) { only.call(key) }) if only Definition.new(contents, filepath, &) end |
.register_error_response(name, klass) ⇒ Object
Register an error response class
38 39 40 |
# File 'lib/openapi_first.rb', line 38 def self.register_error_response(name, klass) ERROR_RESPONSES[name.to_sym] = klass end |