Skip to content

Commit

Permalink
Remove Operation
Browse files Browse the repository at this point in the history
  • Loading branch information
ahx committed May 30, 2024
1 parent a3aed35 commit a18a5ad
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 227 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
- after_request_body_property_validation
- after_request_parameter_property_validation

### Breaking Changes

- `Definition` was renamed to `Doc`
- `Operation` was removed
- Instead of `Doc#operations` you can use `Doc#routes`, which returns a list of routes. Routes have a `#path`, `#request_method`, `#requests` and `#responses`.

## 1.4.3

- Allow using json_schemer 2...3
Expand Down
10 changes: 5 additions & 5 deletions lib/openapi_first/doc.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# frozen_string_literal: true

require_relative 'operation'
require_relative 'failure'
require_relative 'router'
require_relative 'request'
Expand All @@ -11,8 +10,6 @@ module OpenapiFirst
# This is returned by OpenapiFirst.load.
class Doc
attr_reader :filepath, :openapi_version, :config, :paths
# Openapi 3 specific
attr_reader :operations

REQUEST_METHODS = %w[get head post put patch delete trace options].freeze
private_constant :REQUEST_METHODS
Expand All @@ -25,7 +22,7 @@ def initialize(resolved, filepath = nil)
@openapi_version = detect_version(resolved)
@router = Router.new

@operations = resolved['paths'].flat_map do |path, path_item_object|
resolved['paths'].each do |path, path_item_object|
path_item_object.slice(*REQUEST_METHODS).keys.map do |request_method|
operation_object = path_item_object[request_method]
path_item_parameters = path_item_object['parameters']
Expand All @@ -46,7 +43,6 @@ def initialize(resolved, filepath = nil)
response_content_type: response.content_type
)
end
Operation.new(path, request_method, operation_object, path_item_parameters:)
end
end
@paths = resolved['paths'].keys
Expand All @@ -55,6 +51,10 @@ def initialize(resolved, filepath = nil)
@config.freeze
end

def routes
@router.routes
end

# Validates the request against the API description.
# @param rack_request [Rack::Request] The Rack request object.
# @param raise_error [Boolean] Whether to raise an error if validation fails.
Expand Down
104 changes: 0 additions & 104 deletions lib/openapi_first/operation.rb

This file was deleted.

8 changes: 1 addition & 7 deletions lib/openapi_first/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def initialize(path:, request_method:, operation_id:, parameters:, content_type:
@validator = RequestValidator.new(self, hooks:, openapi_version:)
end

attr_reader :content_type, :content_schema, :operation_id, :request_method
attr_reader :content_type, :content_schema, :operation_id, :request_method, :path

def validate(request, route_params:)
parsed = @parser.parse(request, route_params:)
Expand All @@ -47,12 +47,6 @@ def required_request_body?
@required_request_body
end

def inspect
result = "Request:#{request_method} #{path}"
result << ":#{content_type}" if content_type
result
end

private

IGNORED_HEADERS = Set['Content-Type', 'Accept', 'Authorization'].freeze
Expand Down
11 changes: 5 additions & 6 deletions spec/doc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,12 @@ def build_request(path, method: 'GET')
end
end

describe '#operations' do
let(:definition) { OpenapiFirst.load('./spec/data/petstore.yaml') }
describe '#routes' do
let(:definition) { OpenapiFirst.load('./spec/data/train-travel-api/openapi.yaml') }

it 'returns a list of operations' do
expect(definition.operations.length).to eq 3
expected_ids = %w[listPets createPets showPetById]
expect(definition.operations.map(&:operation_id)).to eq expected_ids
it 'returns routes' do
routes = definition.routes.map { |route| "#{route.request_method} #{route.path}" }
expect(routes).to match_array ['GET /stations', 'GET /trips', 'GET /bookings', 'POST /bookings', 'GET /bookings/{bookingId}', 'DELETE /bookings/{bookingId}', 'POST /bookings/{bookingId}/payment']
end
end

Expand Down
105 changes: 0 additions & 105 deletions spec/operation_spec.rb

This file was deleted.

0 comments on commit a18a5ad

Please sign in to comment.