committee 5.6.4 → 5.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/lib/committee/drivers/open_api_2/driver.rb +16 -0
  3. data/lib/committee/drivers/open_api_2/link.rb +2 -0
  4. data/lib/committee/drivers/open_api_3/schema.rb +6 -1
  5. data/lib/committee/drivers.rb +15 -8
  6. data/lib/committee/middleware/response_validation.rb +19 -5
  7. data/lib/committee/middleware/stub.rb +1 -1
  8. data/lib/committee/request_unpacker.rb +19 -9
  9. data/lib/committee/schema_locations.rb +96 -0
  10. data/lib/committee/schema_validator/hyper_schema/request_validator.rb +1 -1
  11. data/lib/committee/schema_validator/hyper_schema/response_validator.rb +13 -3
  12. data/lib/committee/schema_validator/hyper_schema.rb +5 -4
  13. data/lib/committee/schema_validator/open_api_3/operation_wrapper.rb +42 -17
  14. data/lib/committee/schema_validator/open_api_3/parameter_deserializer.rb +66 -20
  15. data/lib/committee/schema_validator/open_api_3/request_validator.rb +2 -3
  16. data/lib/committee/schema_validator/open_api_3/router.rb +1 -3
  17. data/lib/committee/schema_validator/open_api_3.rb +22 -18
  18. data/lib/committee/schema_validator.rb +1 -5
  19. data/lib/committee/test/except_parameter.rb +4 -4
  20. data/lib/committee/test/methods.rb +13 -0
  21. data/lib/committee/test/schema_coverage.rb +11 -2
  22. data/lib/committee/utils.rb +6 -0
  23. data/lib/committee/validation_error.rb +1 -1
  24. data/lib/committee/version.rb +1 -1
  25. data/lib/committee.rb +1 -0
  26. data/test/drivers/open_api_2/driver_test.rb +13 -0
  27. data/test/drivers_test.rb +39 -8
  28. data/test/middleware/request_validation_open_api_3_test.rb +668 -6
  29. data/test/middleware/request_validation_test.rb +50 -0
  30. data/test/middleware/response_validation_open_api_3_test.rb +77 -0
  31. data/test/middleware/response_validation_test.rb +134 -0
  32. data/test/middleware/stub_test.rb +10 -0
  33. data/test/request_unpacker_test.rb +34 -0
  34. data/test/schema_location_test.rb +111 -0
  35. data/test/schema_validator/open_api_3/parameter_deserializer_test.rb +145 -8
  36. data/test/schema_validator_test.rb +10 -0
  37. data/test/test/methods_test.rb +188 -0
  38. data/test/test/schema_coverage_test.rb +34 -0
  39. data/test/test_helper.rb +23 -0
  40. data/test/utils_test.rb +28 -0
  41. data/test/validation_error_test.rb +1 -1
  42. metadata +20 -3
@@ -18,6 +18,122 @@ describe Committee::Middleware::RequestValidation do
18
18
  assert_equal 200, last_response.status
19
19
  end
20
20
 
21
+ describe "raw request bodies" do
22
+ it "validates text and preserves the body for the application" do
23
+ check_body = lambda { |env|
24
+ assert_equal "hello", env['committee.request_body_hash']
25
+ assert_equal({ "source" => "test" }, env['committee.params'])
26
+ assert_equal "hello", env['rack.input'].read
27
+ [200, {}, []]
28
+ }
29
+ @app = new_rack_app_with_lambda(check_body, schema: text_body_schema)
30
+
31
+ header "Content-Type", "text/plain; charset=utf-8"
32
+ post "/characters?source=test", "hello"
33
+
34
+ assert_equal 200, last_response.status, last_response.body
35
+ end
36
+
37
+ it "rejects text that violates the string schema" do
38
+ @app = new_rack_app(schema: text_body_schema)
39
+
40
+ header "Content-Type", "text/plain"
41
+ ["hi", ""].each do |body|
42
+ post "/characters", body
43
+
44
+ assert_equal 400, last_response.status
45
+ assert_match(/shorter than min length/, last_response.body)
46
+ end
47
+ end
48
+
49
+ it "accepts a binary string request body" do
50
+ content = "\x00\xFF".b
51
+ check_body = lambda { |env|
52
+ assert_equal content, env['committee.request_body_hash']
53
+ assert_equal content, env['rack.input'].read
54
+ [200, {}, []]
55
+ }
56
+ @app = new_rack_app_with_lambda(check_body, schema: open_api_3_schema)
57
+
58
+ header "Content-Type", "application/binary"
59
+ post "/validate_content_types", content
60
+
61
+ assert_equal 200, last_response.status, last_response.body
62
+ end
63
+
64
+ it "respects allow_get_body without merging text into query params" do
65
+ [false, true].each do |allow_get_body|
66
+ check_body = lambda { |env|
67
+ assert_equal(allow_get_body ? "hello" : {}, env['committee.request_body_hash'])
68
+ assert_equal({ "source" => "test" }, env['committee.params'])
69
+ [200, {}, []]
70
+ }
71
+ @app = new_rack_app_with_lambda(check_body, schema: open_api_3_schema, allow_get_body: allow_get_body)
72
+
73
+ get "/characters", { source: "test" }, { input: "hello", "CONTENT_TYPE" => "text/plain" }
74
+
75
+ assert_equal 200, last_response.status
76
+ end
77
+ end
78
+ end
79
+
80
+ it "accepts an empty optional body through Rack::Lint" do
81
+ app = Rack::Lint.new(new_rack_app(schema: open_api_3_schema))
82
+ env = Rack::MockRequest.env_for("/characters", method: "POST", input: "", "CONTENT_TYPE" => "text/plain")
83
+
84
+ status, _headers, body = app.call(env)
85
+
86
+ assert_equal 200, status
87
+ body.close if body.respond_to?(:close)
88
+ end
89
+
90
+ it "accepts a JSON array request body declared by OpenAPI3" do
91
+ check_body = lambda { |env|
92
+ assert_equal [1, 2], env['committee.request_body_hash']
93
+ assert_equal({ 'source' => 'test' }, env['committee.params'])
94
+ [200, {}, []]
95
+ }
96
+ @app = new_rack_app_with_lambda(check_body, schema: array_body_schema)
97
+
98
+ header "Content-Type", "application/json"
99
+ post "/numbers?source=test", '[1,2]'
100
+
101
+ assert_equal 200, last_response.status
102
+ end
103
+
104
+ it "validates items in a JSON array request body" do
105
+ @app = new_rack_app(schema: array_body_schema)
106
+
107
+ header "Content-Type", "application/json"
108
+ post "/numbers", '[1,"invalid"]'
109
+
110
+ assert_equal 400, last_response.status
111
+ end
112
+
113
+ [
114
+ ['application/json', '[1,2]', [1, 2], { 'type' => 'array', 'items' => { 'type' => 'integer' } }],
115
+ ['text/plain', 'hello', 'hello', { 'type' => 'string' }],
116
+ ].each do |content_type, body, expected_body, body_schema|
117
+ it "preserves query parameters with an allowed GET #{content_type} body" do
118
+ parameter = { 'name' => 'ids', 'in' => 'query', 'required' => true, 'schema' => { 'type' => 'array', 'items' => { 'type' => 'integer' } }, }
119
+ document = query_param_document(parameter)
120
+ document['paths']['/events']['get']['requestBody'] = { 'required' => true, 'content' => { content_type => { 'schema' => body_schema } } }
121
+ schema = Committee::Drivers.load_from_data(document, nil, parser_options: { strict_reference_validation: true })
122
+ check_parameter = lambda { |env|
123
+ assert_equal expected_body, env['committee.request_body_hash']
124
+ assert_equal [3, 4], env['committee.query_hash']['ids']
125
+ assert_equal({ 'ids' => [3, 4] }, env['committee.params'])
126
+ assert_equal body, env['rack.input'].read
127
+ [200, {}, []]
128
+ }
129
+ @app = new_rack_app_with_lambda(check_parameter, schema: schema, allow_get_body: true)
130
+
131
+ get '/events?ids=3&ids=4', {}, { input: body, 'CONTENT_TYPE' => content_type }
132
+
133
+ assert_equal 200, last_response.status, last_response.body
134
+ end
135
+ end
136
+
21
137
  it "not parameter request" do
22
138
  check_parameter_string = lambda { |_|
23
139
  [200, { integer: 1 }, []]
@@ -383,6 +499,141 @@ describe Committee::Middleware::RequestValidation do
383
499
  assert_match(/integer/i, last_response.body)
384
500
  end
385
501
 
502
+ describe 'serialized path parameters' do
503
+ [
504
+ ['simple', false, '1,2'],
505
+ ['label', false, '.1,2'],
506
+ ['label', true, '.1.2'],
507
+ ['matrix', false, ';ids=1,2'],
508
+ ['matrix', true, ';ids=1;ids=2'],
509
+ ].each do |style, explode, value|
510
+ it "deserializes and validates #{style} array with explode=#{explode}" do
511
+ parameter = { 'name' => 'ids', 'in' => 'path', 'required' => true, 'style' => style, 'explode' => explode, 'schema' => { 'type' => 'array', 'items' => { 'type' => 'integer' } }, }
512
+ check_parameter = lambda { |env|
513
+ assert_equal [1, 2], env['committee.path_hash']['ids']
514
+ assert_equal [1, 2], env['committee.params']['ids']
515
+ [200, {}, []]
516
+ }
517
+ @app = new_rack_app_with_lambda(check_parameter, schema: path_param_schema(parameter))
518
+
519
+ get "/paths/#{value}"
520
+
521
+ assert_equal 200, last_response.status
522
+ end
523
+ end
524
+
525
+ [
526
+ ['simple', false, 'role,admin,status,active'],
527
+ ['simple', true, 'role=admin,status=active'],
528
+ ['label', false, '.role,admin,status,active'],
529
+ ['label', true, '.role=admin.status=active'],
530
+ ['matrix', false, ';filter=role,admin,status,active'],
531
+ ['matrix', true, ';role=admin;status=active'],
532
+ ].each do |style, explode, value|
533
+ it "deserializes and validates #{style} object with explode=#{explode}" do
534
+ parameter = { 'name' => 'filter', 'in' => 'path', 'required' => true, 'style' => style, 'explode' => explode, 'schema' => { 'type' => 'object', 'required' => ['role', 'status'], 'properties' => { 'role' => { 'type' => 'string' }, 'status' => { 'type' => 'string' } } }, }
535
+ check_parameter = lambda { |env|
536
+ assert_equal({ 'role' => 'admin', 'status' => 'active' }, env['committee.path_hash']['filter'])
537
+ [200, {}, []]
538
+ }
539
+ @app = new_rack_app_with_lambda(check_parameter, schema: path_param_schema(parameter))
540
+
541
+ get "/paths/#{value}"
542
+
543
+ assert_equal 200, last_response.status
544
+ end
545
+ end
546
+
547
+ [
548
+ ['array', { 'type' => 'array', 'items' => { 'type' => 'integer' } }, '1,2,3', []],
549
+ ['object', { 'type' => 'object', 'properties' => { 'role' => { 'type' => 'string' } } }, 'role,admin', {}],
550
+ ].each do |type, schema, value, empty_value|
551
+ it "rejects a compact matrix #{type} with a missing or incorrect prefix" do
552
+ parameter = { 'name' => 'id', 'in' => 'path', 'required' => true, 'style' => 'matrix', 'explode' => false, 'schema' => schema }
553
+ @app = new_rack_app(schema: path_param_schema(parameter))
554
+
555
+ [value, ";other=#{value}", ";id-other=#{value}"].each do |invalid_value|
556
+ get "/paths/#{invalid_value}"
557
+
558
+ assert_equal 400, last_response.status, last_response.body
559
+ assert_match(/invalid matrix #{type}/, last_response.body)
560
+ end
561
+ end
562
+
563
+ it "accepts an empty compact matrix #{type}" do
564
+ parameter = { 'name' => 'id', 'in' => 'path', 'required' => true, 'style' => 'matrix', 'explode' => false, 'schema' => schema }
565
+ check_parameter = lambda { |env|
566
+ assert_equal empty_value, env['committee.path_hash']['id']
567
+ [200, {}, []]
568
+ }
569
+ @app = new_rack_app_with_lambda(check_parameter, schema: path_param_schema(parameter))
570
+
571
+ [';id', ';id='].each do |empty_parameter|
572
+ get "/paths/#{empty_parameter}"
573
+
574
+ assert_equal 200, last_response.status, last_response.body
575
+ end
576
+ end
577
+ end
578
+
579
+ it 'rejects an invalid item after deserialization' do
580
+ parameter = { 'name' => 'ids', 'in' => 'path', 'required' => true, 'style' => 'simple', 'schema' => { 'type' => 'array', 'items' => { 'type' => 'integer' } }, }
581
+ @app = new_rack_app(schema: path_param_schema(parameter))
582
+
583
+ get '/paths/1,invalid'
584
+
585
+ assert_equal 400, last_response.status
586
+ assert_match(/integer/, last_response.body)
587
+ end
588
+
589
+ it 'deserializes a path item parameter before validation' do
590
+ parameter = { 'name' => 'ids', 'in' => 'path', 'required' => true, 'schema' => { 'type' => 'array', 'items' => { 'type' => 'integer' } }, }
591
+ document = query_param_document(parameter)
592
+ path_item = document['paths'].delete('/events')
593
+ path_item['parameters'] = path_item['get'].delete('parameters')
594
+ document['paths']['/paths/{ids}'] = path_item
595
+ schema = Committee::Drivers.load_from_data(document, nil, parser_options: { strict_reference_validation: true })
596
+ check_parameter = lambda { |env|
597
+ assert_equal [1, 2], env['committee.path_hash']['ids']
598
+ [200, {}, []]
599
+ }
600
+ @app = new_rack_app_with_lambda(check_parameter, schema: schema)
601
+
602
+ get '/paths/1,2'
603
+
604
+ assert_equal 200, last_response.status
605
+ end
606
+ end
607
+
608
+ describe 'path item parameters' do
609
+ [
610
+ ['query', 'form', 'committee.query_hash'],
611
+ ['header', 'simple', 'committee.headers'],
612
+ ].each do |location, style, hash_key|
613
+ it "deserializes and validates a path item #{location} array" do
614
+ parameter = { 'name' => 'ids', 'in' => location, 'required' => true, 'style' => style, 'explode' => false, 'schema' => { 'type' => 'array', 'items' => { 'type' => 'integer' } }, }
615
+ document = query_param_document(parameter)
616
+ path_item = document['paths']['/events']
617
+ path_item['parameters'] = path_item['get'].delete('parameters')
618
+ schema = Committee::Drivers.load_from_data(document, nil, parser_options: { strict_reference_validation: true })
619
+ check_parameter = lambda { |env|
620
+ assert_equal [1, 2, 3], env[hash_key]['ids']
621
+ [200, {}, []]
622
+ }
623
+ @app = new_rack_app_with_lambda(check_parameter, schema: schema, check_header: true)
624
+
625
+ if location == 'query'
626
+ get '/events?ids=1,2,3'
627
+ else
628
+ header 'ids', '1,2,3'
629
+ get '/events'
630
+ end
631
+
632
+ assert_equal 200, last_response.status
633
+ end
634
+ end
635
+ end
636
+
386
637
  describe "overwrite same parameter (old rule)" do
387
638
  # (high priority) path_hash_key -> request_body_hash -> query_param
388
639
  it "set query parameter to committee.params and query hash" do
@@ -506,14 +757,12 @@ describe Committee::Middleware::RequestValidation do
506
757
  assert_equal 204, last_response.status
507
758
  end
508
759
 
509
- it "OpenAPI3 raise not support method" do
510
- @app = new_rack_app(schema: open_api_3_schema)
760
+ it "OpenAPI3 validates a trace request" do
761
+ @app = new_rack_app(schema: open_api_3_schema, raise: true)
511
762
 
512
- e = assert_raises(RuntimeError) {
513
- custom_request('TRACE', "/characters")
514
- }
763
+ custom_request('TRACE', "/characters")
515
764
 
516
- assert_equal 'Committee OpenAPI3 not support trace method', e.message
765
+ assert_equal 200, last_response.status
517
766
  end
518
767
 
519
768
  describe 'check header' do
@@ -550,6 +799,22 @@ describe Committee::Middleware::RequestValidation do
550
799
  end
551
800
  end
552
801
 
802
+ it 'deserializes header arrays regardless of the case used in the schema' do
803
+ parameter = { 'name' => 'X-Ids', 'in' => 'header', 'required' => true, 'schema' => { 'type' => 'array', 'items' => { 'type' => 'integer' } }, }
804
+ check_parameter = lambda { |env|
805
+ assert_equal [1, 2], env['committee.headers']['X-Ids']
806
+ refute env['committee.headers'].key?('X-IDS')
807
+ assert_equal '1,2', env['HTTP_X_IDS']
808
+ [200, {}, []]
809
+ }
810
+ @app = new_rack_app_with_lambda(check_parameter, schema: query_param_schema(parameter))
811
+
812
+ header 'X-Ids', '1,2'
813
+ get '/events'
814
+
815
+ assert_equal 200, last_response.status
816
+ end
817
+
553
818
  describe ':accept_request_filter' do
554
819
  [
555
820
  { description: 'when not specified, includes everything', accept_request_filter: nil, expected: { status: 400 } },
@@ -660,7 +925,156 @@ describe Committee::Middleware::RequestValidation do
660
925
  end
661
926
  end
662
927
 
928
+ describe 'exploded array query params' do
929
+ before do
930
+ @parameter = { 'name' => 'ids', 'in' => 'query', 'required' => true, 'schema' => { 'type' => 'array', 'items' => { 'type' => 'integer' } }, }
931
+ end
932
+
933
+ it 'preserves repeated values with the default serialization' do
934
+ check_parameter = lambda { |env|
935
+ assert_equal [1, 2], env['committee.query_hash']['ids']
936
+ assert_equal '2', env['rack.request.query_hash']['ids']
937
+ assert_equal 'last', env['committee.query_hash']['unknown']
938
+ [200, {}, []]
939
+ }
940
+ @app = new_rack_app_with_lambda(check_parameter, schema: query_param_schema(@parameter))
941
+
942
+ get '/events?ids=1&ids=2&unknown=first&unknown=last'
943
+
944
+ assert_equal 200, last_response.status
945
+ end
946
+
947
+ ['array', ['array', 'null']].each do |type|
948
+ [[nil, nil], ['form', true], ['pipeDelimited', true], ['spaceDelimited', true]].each do |style, explode|
949
+ it "preserves repeated query values for type=#{type.inspect}, style=#{style.inspect}, explode=#{explode.inspect}" do
950
+ @parameter.merge!('style' => style, 'explode' => explode)
951
+ @parameter['schema']['type'] = type
952
+ document = query_param_document(@parameter)
953
+ document['openapi'] = '3.1.0'
954
+ schema = Committee::Drivers.load_from_data(document, nil, parser_options: { strict_reference_validation: true })
955
+ check_parameter = lambda { |env|
956
+ assert_equal [1, 2], env['committee.query_hash']['ids']
957
+ assert_equal [1, 2], env['committee.params']['ids']
958
+ assert_equal '2', env['rack.request.query_hash']['ids']
959
+ [200, {}, []]
960
+ }
961
+ @app = new_rack_app_with_lambda(check_parameter, schema: schema)
962
+
963
+ get '/events?ids=1&ids=2'
964
+
965
+ assert_equal 200, last_response.status, last_response.body
966
+
967
+ get '/events?ids=invalid&ids=2'
968
+
969
+ assert_equal 400, last_response.status
970
+ end
971
+ end
972
+ end
973
+
974
+ it 'preserves decoded values with explicit form style and explode=true' do
975
+ @parameter.merge!('style' => 'form', 'explode' => true, 'schema' => { 'type' => 'array', 'items' => { 'type' => 'string' } })
976
+ check_parameter = lambda { |env|
977
+ assert_equal ['a,b', 'c&d', 'a b', ''], env['committee.query_hash']['ids']
978
+ [200, {}, []]
979
+ }
980
+ @app = new_rack_app_with_lambda(check_parameter, schema: query_param_schema(@parameter))
981
+
982
+ get '/events?%69ds=a%2Cb&ids=c%26d&ids=a+b&ids='
983
+
984
+ assert_equal 200, last_response.status
985
+ end
986
+
987
+ it 'validates every repeated value' do
988
+ @app = new_rack_app(schema: query_param_schema(@parameter))
989
+
990
+ get '/events?ids=invalid&ids=2'
991
+
992
+ assert_equal 400, last_response.status
993
+ end
994
+
995
+ it 'continues to support single values and Rack array notation' do
996
+ expected = nil
997
+ check_parameter = lambda { |env|
998
+ assert_equal expected, env['committee.query_hash']['ids']
999
+ [200, {}, []]
1000
+ }
1001
+ @app = new_rack_app_with_lambda(check_parameter, schema: query_param_schema(@parameter))
1002
+
1003
+ { 'ids=1' => [1], 'ids[]=1&ids[]=2' => [1, 2] }.each do |query, values|
1004
+ expected = values
1005
+ get "/events?#{query}"
1006
+ assert_equal 200, last_response.status
1007
+ end
1008
+ end
1009
+
1010
+ it 'does not deserialize repeated values when deserialization is disabled' do
1011
+ @app = new_rack_app(schema: query_param_schema(@parameter), deserialize_parameters: false)
1012
+
1013
+ get '/events?ids=1&ids=2'
1014
+
1015
+ assert_equal 400, last_response.status
1016
+ end
1017
+
1018
+ it 'does not restore query parameters when query parameters are disabled' do
1019
+ @app = new_rack_app(schema: query_param_schema(@parameter), allow_query_params: false)
1020
+
1021
+ get '/events?ids=1&ids=2'
1022
+
1023
+ assert_equal 400, last_response.status
1024
+ end
1025
+
1026
+ it 'preserves GET body precedence with allow_get_body' do
1027
+ check_parameter = lambda { |env|
1028
+ assert_equal [3], env['committee.query_hash']['ids']
1029
+ [200, {}, []]
1030
+ }
1031
+ @app = new_rack_app_with_lambda(check_parameter, schema: query_param_schema(@parameter), allow_get_body: true)
1032
+
1033
+ get '/events?ids=1&ids=2', {}, { input: { ids: '3' }.to_json, 'CONTENT_TYPE' => 'application/json' }
1034
+
1035
+ assert_equal 200, last_response.status
1036
+ end
1037
+
1038
+ it 'preserves form body precedence with allow_non_get_query_params' do
1039
+ document = query_param_document(@parameter)
1040
+ document['paths']['/events']['post'] = document['paths']['/events'].delete('get')
1041
+ schema = Committee::Drivers.load_from_data(document, nil, parser_options: { strict_reference_validation: true })
1042
+ check_parameter = lambda { |env|
1043
+ assert_equal [3], env['committee.query_hash']['ids']
1044
+ [200, {}, []]
1045
+ }
1046
+ @app = new_rack_app_with_lambda(check_parameter, schema: schema, allow_non_get_query_params: true)
1047
+
1048
+ post '/events?ids=1&ids=2', { ids: '3' }
1049
+
1050
+ assert_equal 200, last_response.status
1051
+ end
1052
+ end
1053
+
663
1054
  describe 'bracket-style query params' do
1055
+ it 'deserializes array params whose name ends with brackets' do
1056
+ parameter = { 'name' => 'ids[]', 'in' => 'query', 'required' => true, 'schema' => { 'type' => 'array', 'items' => { 'type' => 'integer' } }, }
1057
+ check_parameter = lambda { |env|
1058
+ assert_equal [1, 2], env['committee.query_hash']['ids[]']
1059
+ assert_equal ['1', '2'], env['rack.request.query_hash']['ids']
1060
+ [200, {}, []]
1061
+ }
1062
+ @app = new_rack_app_with_lambda(check_parameter, schema: query_param_schema(parameter))
1063
+
1064
+ get '/events?ids[]=1&ids[]=2'
1065
+
1066
+ assert_equal 200, last_response.status
1067
+ end
1068
+
1069
+ it 'returns a bad request for query parameters with invalid UTF-8 bytes' do
1070
+ parameter = { 'name' => 'query_string', 'in' => 'query', 'required' => true, 'schema' => { 'type' => 'string', 'maxLength' => 10, 'pattern' => '^[A-Z]+$' }, }
1071
+ @app = new_rack_app(schema: query_param_schema(parameter))
1072
+
1073
+ get '/events?query_string=%c0%af'
1074
+
1075
+ assert_equal 400, last_response.status
1076
+ end
1077
+
664
1078
  it 'validates query params declared with bracket notation names' do
665
1079
  check_parameter = lambda { |env|
666
1080
  assert_equal '/test', env['committee.query_hash']['filter[slug]']
@@ -698,6 +1112,214 @@ describe Committee::Middleware::RequestValidation do
698
1112
  end
699
1113
  end
700
1114
 
1115
+ describe "OpenAPI 3.1" do
1116
+ it "coerces query params with array-form type" do
1117
+ check_parameter = lambda { |env|
1118
+ assert_equal 5, env['committee.query_hash']['limit']
1119
+ assert_equal [1, 2], env['committee.query_hash']['ids']
1120
+ [200, {}, []]
1121
+ }
1122
+ @app = new_rack_app_with_lambda(check_parameter, schema: open_api_3_1_schema)
1123
+
1124
+ get "/search", limit: "5", ids: "1,2"
1125
+
1126
+ assert_equal 200, last_response.status
1127
+ end
1128
+
1129
+ it "applies constraints after coercing array-form type" do
1130
+ @app = new_rack_app(schema: open_api_3_1_schema, raise: true)
1131
+ assert_raises(Committee::InvalidRequest) { get "/search", limit: "0" }
1132
+ end
1133
+
1134
+ it "coerces path params with array-form type" do
1135
+ check_parameter = lambda { |env|
1136
+ assert_equal 42, env['committee.path_hash']['id']
1137
+ [200, {}, []]
1138
+ }
1139
+ @app = new_rack_app_with_lambda(check_parameter, schema: open_api_3_1_schema)
1140
+
1141
+ get "/items/42"
1142
+
1143
+ assert_equal 200, last_response.status
1144
+ end
1145
+
1146
+ it "accepts a valid request body" do
1147
+ @app = new_rack_app(schema: open_api_3_1_schema)
1148
+ header "Content-Type", "application/json"
1149
+ post "/items", JSON.generate(ValidItem)
1150
+ assert_equal 200, last_response.status
1151
+ end
1152
+
1153
+ it "rejects a const mismatch" do
1154
+ @app = new_rack_app(schema: open_api_3_1_schema, raise: true)
1155
+ header "Content-Type", "application/json"
1156
+ e = assert_raises(Committee::InvalidRequest) { post "/items", JSON.generate(ValidItem.merge("kind" => "gadget")) }
1157
+ assert_match(/const/, e.message)
1158
+ end
1159
+
1160
+ it "rejects a value at a numeric exclusive bound" do
1161
+ @app = new_rack_app(schema: open_api_3_1_schema, raise: true)
1162
+ header "Content-Type", "application/json"
1163
+ assert_raises(Committee::InvalidRequest) { post "/items", JSON.generate(ValidItem.merge("rating" => 5)) }
1164
+ end
1165
+
1166
+ it "rejects a prefixItems position mismatch" do
1167
+ @app = new_rack_app(schema: open_api_3_1_schema, raise: true)
1168
+ header "Content-Type", "application/json"
1169
+ assert_raises(Committee::InvalidRequest) { post "/items", JSON.generate(ValidItem.merge("point" => ["a", "b"])) }
1170
+ end
1171
+
1172
+ it "rejects a non-null value outside array-form type" do
1173
+ @app = new_rack_app(schema: open_api_3_1_schema, raise: true)
1174
+ header "Content-Type", "application/json"
1175
+ assert_raises(Committee::InvalidRequest) { post "/items", JSON.generate(ValidItem.merge("name" => 1)) }
1176
+ end
1177
+
1178
+ it "routes a path item referenced from components.pathItems" do
1179
+ @app = new_rack_app(schema: open_api_3_1_schema, strict: true)
1180
+ get "/reusable"
1181
+ assert_equal 200, last_response.status
1182
+ end
1183
+
1184
+ it "does not route webhooks" do
1185
+ @app = new_rack_app(schema: open_api_3_1_schema, strict: true)
1186
+ header "Content-Type", "application/json"
1187
+ post "/newItem", JSON.generate(ValidItem)
1188
+ assert_equal 404, last_response.status
1189
+ end
1190
+
1191
+ # array-form type is a 3.1 form, but openapi_parser validates it in any version
1192
+ it "deserializes array-form type in 3.0 documents" do
1193
+ check_parameter = lambda { |env|
1194
+ assert_equal [1, 2], env['committee.query_hash']['ids']
1195
+ [200, {}, []]
1196
+ }
1197
+ parameter = { 'name' => 'ids', 'in' => 'query', 'style' => 'form', 'explode' => false, 'schema' => { 'type' => ['array', 'null'], 'items' => { 'type' => 'integer' } } }
1198
+ @app = new_rack_app_with_lambda(check_parameter, schema: query_param_schema(parameter))
1199
+
1200
+ get "/events?ids=1,2"
1201
+
1202
+ assert_equal 200, last_response.status
1203
+ end
1204
+ end
1205
+
1206
+ describe "OpenAPI 3.2" do
1207
+ it "accepts a valid query request body" do
1208
+ @app = new_rack_app(schema: open_api_3_2_schema, raise: true)
1209
+ custom_request("QUERY", "/items", {}, { input: JSON.generate("name" => "a", "limit" => 1), "CONTENT_TYPE" => "application/json" })
1210
+ assert_equal 200, last_response.status
1211
+ end
1212
+
1213
+ it "rejects an invalid query request body" do
1214
+ @app = new_rack_app(schema: open_api_3_2_schema, raise: true)
1215
+ assert_raises(Committee::InvalidRequest) do
1216
+ custom_request("QUERY", "/items", {}, { input: JSON.generate("name" => "a", "limit" => 0), "CONTENT_TYPE" => "application/json" })
1217
+ end
1218
+ end
1219
+
1220
+ it "checks the Content-Type of a query request" do
1221
+ @app = new_rack_app(schema: open_api_3_2_schema, raise: true)
1222
+ e = assert_raises(Committee::InvalidRequest) do
1223
+ custom_request("QUERY", "/items", {}, { input: "name=a", "CONTENT_TYPE" => "text/plain" })
1224
+ end
1225
+ assert_match(/Content-Type/, e.message)
1226
+ end
1227
+
1228
+ it "routes additionalOperations by exact method case" do
1229
+ @app = new_rack_app(schema: open_api_3_2_schema, raise: true, strict: true)
1230
+ custom_request("PURGE", "/items?older_than=5")
1231
+ assert_equal 200, last_response.status
1232
+ # rack-test upcases the method unless REQUEST_METHOD is set directly
1233
+ custom_request("purge", "/items?scope=all", {}, { "REQUEST_METHOD" => "purge" })
1234
+ assert_equal 200, last_response.status
1235
+ assert_raises(Committee::InvalidRequest) { custom_request("purge", "/items?older_than=5", {}, { "REQUEST_METHOD" => "purge" }) }
1236
+ end
1237
+
1238
+ it "does not route a custom method that differs only in case" do
1239
+ @app = new_rack_app(schema: open_api_3_2_schema, strict: true)
1240
+ custom_request("search", "/items", {}, { "REQUEST_METHOD" => "search" })
1241
+ assert_equal 404, last_response.status
1242
+ end
1243
+
1244
+ it "routes a custom method that differs from a standard method only in case" do
1245
+ @app = new_rack_app(schema: open_api_3_2_schema, raise: true, strict: true)
1246
+ custom_request("Get", "/versions?custom=a", {}, { "REQUEST_METHOD" => "Get" })
1247
+ assert_equal 200, last_response.status
1248
+ assert_raises(Committee::InvalidRequest) { custom_request("Get", "/versions?standard=a", {}, { "REQUEST_METHOD" => "Get" }) }
1249
+ get "/versions?standard=a"
1250
+ assert_equal 200, last_response.status
1251
+ assert_raises(Committee::InvalidRequest) { get "/versions?custom=a" }
1252
+ end
1253
+
1254
+ it "ignores a trace requestBody" do
1255
+ @app = new_rack_app(schema: open_api_3_2_schema, raise: true, strict: true)
1256
+ custom_request("TRACE", "/items")
1257
+ assert_equal 200, last_response.status
1258
+ end
1259
+
1260
+ it "validates additionalOperations parameters" do
1261
+ @app = new_rack_app(schema: open_api_3_2_schema, raise: true)
1262
+ assert_raises(Committee::InvalidRequest) { custom_request("PURGE", "/items?older_than=x") }
1263
+ end
1264
+
1265
+ it "validates the body of an additionalOperations request" do
1266
+ @app = new_rack_app(schema: open_api_3_2_schema, raise: true)
1267
+ custom_request("SEARCH", "/items", {}, { input: JSON.generate("name" => "a"), "CONTENT_TYPE" => "application/json" })
1268
+ assert_equal 200, last_response.status
1269
+ assert_raises(Committee::InvalidRequest) do
1270
+ custom_request("SEARCH", "/items", {}, { input: JSON.generate("name" => "a", "limit" => 0), "CONTENT_TYPE" => "application/json" })
1271
+ end
1272
+ end
1273
+
1274
+ it "checks the Content-Type of an additionalOperations request" do
1275
+ @app = new_rack_app(schema: open_api_3_2_schema, raise: true)
1276
+ e = assert_raises(Committee::InvalidRequest) do
1277
+ custom_request("SEARCH", "/items", {}, { input: "name=a", "CONTENT_TYPE" => "text/plain" })
1278
+ end
1279
+ assert_match(/Content-Type/, e.message)
1280
+ end
1281
+
1282
+ it "falls back to the discriminator defaultMapping" do
1283
+ @app = new_rack_app(schema: open_api_3_2_schema, raise: true)
1284
+ header "Content-Type", "application/json"
1285
+ post "/pets", JSON.generate("name" => "felix")
1286
+ assert_equal 200, last_response.status
1287
+ assert_raises(Committee::InvalidRequest) { post "/pets", JSON.generate("bark" => true) }
1288
+ end
1289
+
1290
+ it "validates a request body referenced from components.mediaTypes" do
1291
+ @app = new_rack_app(schema: open_api_3_2_schema, raise: true)
1292
+ header "Content-Type", "application/json"
1293
+ post "/shared", JSON.generate("name" => "a")
1294
+ assert_equal 200, last_response.status
1295
+ assert_raises(Committee::InvalidRequest) { post "/shared", JSON.generate("name" => 1) }
1296
+ end
1297
+
1298
+ # openapi_parser doesn't validate in: querystring yet
1299
+ it "skips querystring parameters" do
1300
+ @app = new_rack_app(schema: open_api_3_2_schema, raise: true)
1301
+ get "/filter"
1302
+ assert_equal 200, last_response.status
1303
+ end
1304
+
1305
+ it "does not route query or additionalOperations in earlier documents" do
1306
+ @app = new_rack_app(schema: open_api_3_2_as_3_1_schema, strict: true)
1307
+ header "Content-Type", "application/json"
1308
+ custom_request("QUERY", "/items", {}, { input: JSON.generate("name" => "a") })
1309
+ assert_equal 404, last_response.status
1310
+ custom_request("PURGE", "/items")
1311
+ assert_equal 404, last_response.status
1312
+ end
1313
+
1314
+ it "routes query and additionalOperations in earlier documents with allow_3_2_features" do
1315
+ @app = new_rack_app(schema: open_api_3_2_as_3_1_schema(allow_3_2_features: true), raise: true, strict: true)
1316
+ header "Content-Type", "application/json"
1317
+ assert_raises(Committee::InvalidRequest) { custom_request("QUERY", "/items", {}, { input: JSON.generate("name" => "a", "limit" => 0) }) }
1318
+ custom_request("PURGE", "/items")
1319
+ assert_equal 200, last_response.status
1320
+ end
1321
+ end
1322
+
701
1323
  private
702
1324
 
703
1325
  def new_rack_app(options = {})
@@ -713,10 +1335,50 @@ describe Committee::Middleware::RequestValidation do
713
1335
  }
714
1336
  end
715
1337
 
1338
+ def open_api_3_2_as_3_1_schema(parser_options = {})
1339
+ Committee::Drivers.load_from_data(open_api_3_2_data.merge("openapi" => "3.1.0"), nil, parser_options: { strict_reference_validation: true }.merge(parser_options))
1340
+ end
1341
+
716
1342
  def query_param_schema(parameter)
717
1343
  Committee::Drivers.load_from_data(query_param_document(parameter), nil, parser_options: { strict_reference_validation: true })
718
1344
  end
719
1345
 
1346
+ def text_body_schema
1347
+ document = open_api_3_data
1348
+ document['paths']['/characters']['post']['requestBody']['content'] = { 'text/plain' => { 'schema' => { 'type' => 'string', 'minLength' => 3 } } }
1349
+ Committee::Drivers.load_from_data(document, open_api_3_schema_path, parser_options: { strict_reference_validation: true })
1350
+ end
1351
+
1352
+ def array_body_schema
1353
+ Committee::Drivers.load_from_data(array_body_document, nil, parser_options: { strict_reference_validation: true })
1354
+ end
1355
+
1356
+ def array_body_document
1357
+ {
1358
+ 'openapi' => '3.0.3',
1359
+ 'info' => { 'title' => 'test', 'version' => '1.0.0' },
1360
+ 'paths' => {
1361
+ '/numbers' => {
1362
+ 'post' => {
1363
+ 'requestBody' => {
1364
+ 'required' => true,
1365
+ 'content' => {
1366
+ 'application/json' => { 'schema' => { 'type' => 'array', 'items' => { 'type' => 'integer' } } },
1367
+ },
1368
+ },
1369
+ 'responses' => { '200' => { 'description' => 'ok' } },
1370
+ },
1371
+ },
1372
+ },
1373
+ }
1374
+ end
1375
+
1376
+ def path_param_schema(parameter)
1377
+ document = query_param_document(parameter)
1378
+ document['paths']["/paths/{#{parameter['name']}}"] = document['paths'].delete('/events')
1379
+ Committee::Drivers.load_from_data(document, nil, parser_options: { strict_reference_validation: true })
1380
+ end
1381
+
720
1382
  def bracket_notation_query_parameter
721
1383
  {
722
1384
  'name' => 'filter[slug]',