committee 2.0.0.pre3 → 2.0.0.pre4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1e120c18b8e980fa98259dcb4d874b0dd095224b
4
- data.tar.gz: de67a03b9afba89cbb0dce7b5654fedfb02a5dc7
3
+ metadata.gz: ba1386b19ed18bfad94a7306dc91adecd1df8986
4
+ data.tar.gz: 65543870a13c61dd5077f06865690c5f21fdae94
5
5
  SHA512:
6
- metadata.gz: 59339f5504e9945a8cdc1248eaf47187d0eafb7cfe90c802a3f1e0b391b40154266d157cc3c16b48992ca39fe783c27ef8b8b3f7eeaeff39961c24d7a6e95052
7
- data.tar.gz: d5252e28f9e64e2e8fd2812189ed425a32d01e55e1949588cf94d98ec537ee9fad7a0aa93be8672d8affd0946fc411ad00abe1d36e0c3a4b11db9b32d94cc5ef
6
+ metadata.gz: e9ffac6d7c7f874b7e15701ad1a20a531a9bc6204aa61337c410f8d3fb0db077f30d5bc8e8c1aadb65ba37944399affbd7522ea6439638648d05c3bcd363ba66
7
+ data.tar.gz: 29b5db2813226fd23f7db76aa76cc01a744b64d2f6c00e317cadfc52dddad76ef6cd48dd8f68781bdbdde414295431bedf7c8f9f79648a3f592a1a65583cf183
@@ -8,6 +8,7 @@ module Committee::Middleware
8
8
  @check_content_type = options.fetch(:check_content_type, true)
9
9
  @optimistic_json = options.fetch(:optimistic_json, false)
10
10
  @strict = options[:strict]
11
+ @coerce_date_times = options.fetch(:coerce_date_times, false)
11
12
 
12
13
  @coerce_form_params = options.fetch(:coerce_form_params,
13
14
  @schema.driver.default_coerce_form_params)
@@ -62,6 +63,14 @@ module Committee::Middleware
62
63
  if link
63
64
  validator = Committee::RequestValidator.new(link, check_content_type: @check_content_type)
64
65
  validator.call(request, request.env[@params_key])
66
+
67
+ request.env["rack.request.query_hash"].merge!(
68
+ Committee::ParameterCoercer
69
+ .new(request.env["rack.request.query_hash"], link.schema,
70
+ coerce_date_times: @coerce_date_times)
71
+ .call
72
+ )
73
+
65
74
  @app.call(request.env)
66
75
  elsif @strict
67
76
  raise Committee::NotFound
@@ -0,0 +1,39 @@
1
+ module Committee
2
+ class ParameterCoercer
3
+ def initialize(params, schema, options = {})
4
+ @params = params
5
+ @schema = schema
6
+
7
+ @coerce_date_times = options.fetch(:coerce_date_times, false)
8
+ end
9
+
10
+ def call
11
+ coerced = {}
12
+ @schema.properties.each do |k, s|
13
+ original_val = @params[k]
14
+
15
+ unless original_val.nil?
16
+ s.type.each do |to_type|
17
+ if @coerce_date_times && to_type == "string" && s.format == "date-time"
18
+ coerce_val = parse_date_time(original_val)
19
+ coerced[k] = coerce_val if coerce_val
20
+ end
21
+
22
+ break if coerced.key?(k)
23
+ end
24
+ end
25
+ end
26
+
27
+ coerced
28
+ end
29
+
30
+ private
31
+ def parse_date_time(original_val)
32
+ begin
33
+ DateTime.parse(original_val)
34
+ rescue ArgumentError => e
35
+ raise ::Committee::InvalidResponse unless e.message =~ /invalid date/
36
+ end
37
+ end
38
+ end
39
+ end
data/lib/committee.rb CHANGED
@@ -4,6 +4,7 @@ require "rack"
4
4
 
5
5
  require_relative "committee/errors"
6
6
  require_relative "committee/string_params_coercer"
7
+ require_relative "committee/parameter_coercer"
7
8
  require_relative "committee/request_unpacker"
8
9
  require_relative "committee/request_validator"
9
10
  require_relative "committee/response_generator"
@@ -17,6 +17,27 @@ describe Committee::Middleware::RequestValidation do
17
17
  assert_equal 200, last_response.status
18
18
  end
19
19
 
20
+ it "pass datetime string and coerce_date_times option enable" do
21
+ @app = new_rack_app(coerce_date_times: true, schema: hyper_schema)
22
+ params = {
23
+ "update_time" => "2016-04-01T16:00:00.000+09:00"
24
+ }
25
+ header "Content-Type", "application/json"
26
+ post "/apps", JSON.generate(params)
27
+ assert_equal 200, last_response.status
28
+ end
29
+
30
+ it "pass invalid datetime string and coerce_date_times option enable" do
31
+ @app = new_rack_app(coerce_date_times: true, schema: hyper_schema)
32
+ params = {
33
+ "update_time" => "invalid_datetime_format"
34
+ }
35
+ header "Content-Type", "application/json"
36
+ post "/apps", JSON.generate(params)
37
+ assert_equal 400, last_response.status
38
+ assert_match /invalid request/i, last_response.body
39
+ end
40
+
20
41
  it "detects an invalid request" do
21
42
  @app = new_rack_app(schema: hyper_schema)
22
43
  header "Content-Type", "application/json"
@@ -0,0 +1,33 @@
1
+ require_relative "test_helper"
2
+
3
+ require "stringio"
4
+
5
+ describe Committee::ParameterCoercer do
6
+ before do
7
+ @schema = JsonSchema.parse!(hyper_schema_data)
8
+ @schema.expand_references!
9
+ # POST /apps/:id
10
+ @link = @link = @schema.properties["app"].links[0]
11
+ end
12
+
13
+ it "pass datetime string" do
14
+ params = { "update_time" => "2016-04-01T16:00:00.000+09:00"}
15
+ converted_params = call(params, coerce_date_times: true)
16
+
17
+ assert_kind_of DateTime, converted_params["update_time"]
18
+ end
19
+
20
+ it "pass invalid datetime string, not convert" do
21
+ params = { "update_time" => "llmfllmf"}
22
+ converted_params = call(params, coerce_date_times: true)
23
+
24
+ assert_nil converted_params["update_time"]
25
+ end
26
+
27
+ private
28
+
29
+ def call(params, options={})
30
+ link = Committee::Drivers::HyperSchema::Link.new(@link)
31
+ Committee::ParameterCoercer.new(params, link.schema, options).call
32
+ end
33
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: committee
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.pre3
4
+ version: 2.0.0.pre4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandur
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-10-19 00:00:00.000000000 Z
12
+ date: 2016-12-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json_schema
@@ -163,6 +163,7 @@ files:
163
163
  - lib/committee/middleware/request_validation.rb
164
164
  - lib/committee/middleware/response_validation.rb
165
165
  - lib/committee/middleware/stub.rb
166
+ - lib/committee/parameter_coercer.rb
166
167
  - lib/committee/request_unpacker.rb
167
168
  - lib/committee/request_validator.rb
168
169
  - lib/committee/response_generator.rb
@@ -181,6 +182,7 @@ files:
181
182
  - test/middleware/request_validation_test.rb
182
183
  - test/middleware/response_validation_test.rb
183
184
  - test/middleware/stub_test.rb
185
+ - test/parameter_coercer_test.rb
184
186
  - test/request_unpacker_test.rb
185
187
  - test/request_validator_test.rb
186
188
  - test/response_generator_test.rb