Skip to content

Commit

Permalink
spec
Browse files Browse the repository at this point in the history
  • Loading branch information
supermomonga committed Jan 7, 2024
1 parent 3f9bab5 commit b2f1c5f
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 64 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ inherit_mode:
- Exclude

AllCops:
TargetRubyVersion: 3.0
TargetRubyVersion: 3.1.1
DisabledByDefault: true
Exclude:
- 'bin/**/*'
Expand Down
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ source 'https://rubygems.org'
# Specify your gem's dependencies in openapi_rails_typed_parameters.gemspec
gemspec

gem 'rake', '>= 13.0.0'
gem 'rake'

group :test do
gem 'debug'
gem 'rspec'
gem 'rspec-rails'
end
Expand Down
67 changes: 27 additions & 40 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ PATH
openapi_rails_typed_parameters (0.1.0)
actionpack
activesupport
openapi_first
openapi_first (~> 1.0)
railties

GEM
remote: https://rubygems.org/
specs:
abbrev (0.1.1)
abbrev (0.1.2)
actionpack (7.1.2)
actionview (= 7.1.2)
activesupport (= 7.1.2)
Expand Down Expand Up @@ -45,45 +45,30 @@ GEM
connection_pool (2.4.1)
crass (1.0.6)
csv (3.2.8)
deep_merge (1.2.2)
debug (1.9.1)
irb (~> 1.10)
reline (>= 0.3.8)
diff-lcs (1.5.0)
drb (2.2.0)
ruby2_keywords
dry-core (1.0.1)
concurrent-ruby (~> 1.0)
zeitwerk (~> 2.6)
dry-transformer (1.0.1)
zeitwerk (~> 2.6)
ecma-re-validator (0.4.0)
regexp_parser (~> 2.2)
erubi (1.12.0)
ffi (1.16.3)
fileutils (1.7.2)
hana (1.3.7)
hanami-router (2.0.2)
mustermann (~> 3.0)
mustermann-contrib (~> 3.0)
rack (~> 2.0)
hanami-utils (2.0.3)
concurrent-ruby (~> 1.0)
dry-core (~> 1.0, < 2)
dry-transformer (~> 1.0, < 2)
hansi (0.2.1)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
io-console (0.7.0)
irb (1.10.1)
io-console (0.7.1)
irb (1.11.0)
rdoc
reline (>= 0.3.8)
json (2.7.1)
json_refs (0.1.8)
hana
json_schemer (0.2.25)
ecma-re-validator (~> 0.3)
json_schemer (2.1.1)
hana (~> 1.3)
regexp_parser (~> 2.0)
simpleidn (~> 0.2)
uri_template (~> 0.7)
language_server-protocol (3.17.0.3)
listen (3.8.0)
rb-fsevent (~> 0.10, >= 0.10.3)
Expand All @@ -100,21 +85,23 @@ GEM
hansi (~> 0.2.0)
mustermann (= 3.0.0)
mutex_m (0.2.0)
nokogiri (1.15.5-x86_64-linux)
nokogiri (1.16.0-x86_64-linux)
racc (~> 1.4)
openapi_first (0.21.0)
deep_merge (>= 1.2.1)
hanami-router (~> 2.0.0)
hanami-utils (~> 2.0.0)
openapi_first (1.0.0)
json_refs (~> 0.1, >= 0.1.7)
json_schemer (~> 0.2.16)
multi_json (~> 1.14)
rack (~> 2.2)
parallel (1.23.0)
parser (3.2.2.4)
json_schemer (~> 2.1.0)
multi_json (~> 1.15)
mustermann-contrib (~> 3.0.0)
openapi_parameters (>= 0.3.2, < 2.0)
rack (>= 2.2, < 4.0)
openapi_parameters (0.3.2)
rack (>= 2.2)
zeitwerk (~> 2.6)
parallel (1.24.0)
parser (3.3.0.0)
ast (~> 2.4.1)
racc
psych (5.1.1.1)
psych (5.1.2)
stringio
racc (1.7.3)
rack (2.2.8)
Expand Down Expand Up @@ -145,12 +132,12 @@ GEM
rb-fsevent (0.11.2)
rb-inotify (0.10.1)
ffi (~> 1.0)
rbs (3.3.2)
rbs (3.4.1)
abbrev
rdoc (6.6.1)
rdoc (6.6.2)
psych (>= 4.0.0)
regexp_parser (2.8.3)
reline (0.4.1)
reline (0.4.2)
io-console (~> 0.5)
rexml (3.2.6)
rspec (3.12.0)
Expand Down Expand Up @@ -189,7 +176,7 @@ GEM
parser (>= 3.2.1.0)
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.5)
securerandom (0.3.0)
securerandom (0.3.1)
simpleidn (0.2.1)
unf (~> 0.1.4)
steep (1.6.0)
Expand Down Expand Up @@ -218,16 +205,16 @@ GEM
unf_ext
unf_ext (0.0.9.1)
unicode-display_width (2.5.0)
uri_template (0.7.0)
webrick (1.8.1)
zeitwerk (2.6.12)

PLATFORMS
x86_64-linux

DEPENDENCIES
debug
openapi_rails_typed_parameters!
rake (>= 13.0.0)
rake
rspec
rspec-rails
rubocop
Expand Down
32 changes: 17 additions & 15 deletions lib/openapi_rails_typed_parameters/action_controller_base_refine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,34 @@

module OpenapiRailsTypedParameters
class TypedParameters
attr_reader :path, :query, :body
attr_reader :request

def initialize(path: {}, query: {}, body: {})
@path = path
@query = query
@body = body
delegate :body, to: :request
delegate :validate, to: :request
delegate :validate!, to: :request

def initialize(request:)
@request = request
end

def path_params() = request.path_parameters&.with_indifferent_access
def query_params() = request.query_parameters&.with_indifferent_access
def valid?() = validate.nil?

def to_h
{
path: path,
query: query,
body: body
}
path_params: path_params,
query_params: query_params,
body: body,
valid: valid?
}.with_indifferent_access
end
end

refine ActionController::Base do
def typed_parameters
copied_request = request.dup
validator = Handler.validator
validator.request_validate(copied_request)
TypedParameters.new(
path: copied_request.env[OpenapiFirst::REQUEST],
query: copied_request.env[OpenapiFirst::REQUEST],
body: copied_request.env[OpenapiFirst::REQUEST]
request: Handler.validator.request(request)
)
end
end
Expand Down
4 changes: 2 additions & 2 deletions openapi_rails_typed_parameters.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
spec.description = 'Statically typed request parameters using OpenAPI Specification'
spec.homepage = 'https://github.com/supermomonga/openapi_rails_typed_parameters'
spec.license = 'MIT'
spec.required_ruby_version = '>= 3.0.0'
spec.required_ruby_version = '>= 3.1.1'

spec.metadata['allowed_push_host'] = "TODO: Set to your gem server 'https://example.com'"

Expand All @@ -32,7 +32,7 @@ Gem::Specification.new do |spec|

spec.add_dependency 'actionpack'
spec.add_dependency 'activesupport'
spec.add_dependency 'openapi_first'
spec.add_dependency 'openapi_first', '~> 1.0'
spec.add_dependency 'railties'

spec.metadata['rubygems_mfa_required'] = 'true'
Expand Down
44 changes: 40 additions & 4 deletions spec/openapi_rails_typed_parameters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,45 @@
require 'spec_helper'

RSpec.describe 'a', type: :request do
it 'returns an object' do
get '/users?role=admin&minimum=1'
expected = []
expect(response.body).to eq expected
describe 'Query parameters' do
context 'with valid query' do
it 'returns response' do
get '/users?role=admin&minimum=1'
expected = {
path_params: {},
query_params: {
role: 'admin',
minimum: 1
},
body: nil,
valid: true
}
actual = JSON.parse(response.body, symbolize_names: true)
expect(actual).to eq expected
end
end

context 'with invalid query' do
context 'unknown enum value' do
it 'returns error' do
get '/users?role=foo&minimum=1'
expected = {
message: 'Query parameter is invalid: value at `/role` is not one of: ["admin", "maintainer"]'
}
actual = JSON.parse(response.body, symbolize_names: true)
expect(actual).to eq expected
end
end
context 'invalid value type' do
it 'returns error' do
get '/users?role=admin&minimum=foo'
expected = {
message: 'Query parameter is invalid: value at `/minimum` is not an integer'
}
actual = JSON.parse(response.body, symbolize_names: true)
expect(actual).to eq expected
end
end
end
end
end
10 changes: 9 additions & 1 deletion spec/sample_app/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'action_controller/railtie'

class SampleApp < Rails::Application
config.active_support.cache_format_version = 7.0
config.logger = ActiveSupport::Logger.new($stdout)
config.eager_load = true
config.hosts << proc { true } if config.respond_to? :hosts
Expand All @@ -23,7 +24,14 @@ class UsersController < ApplicationController

def index
tp = typed_parameters
render json: tp
begin
tp.validate!
render json: tp.to_h
rescue OpenapiFirst::RequestInvalidError => e
render json: {
message: e.message
}, status: :bad_request
end
end

def show
Expand Down
26 changes: 26 additions & 0 deletions spec/sample_app/schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
openapi: 3.0.3
info:
version: 1.0.0
title: Sample App
servers:
- url: https://example.com/
paths:
/users:
get:
parameters:
- name: role
in: query
required: true
schema:
type: string
enum: [ admin, maintainer ]
- name: minimum
in: query
required: false
schema:
type: integer
- name: maximum
in: query
required: false
schema:
type: integer

0 comments on commit b2f1c5f

Please sign in to comment.