easy-jsonapi 1.0.4 → 1.0.9

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.
data/.rubocop.yml CHANGED
@@ -1,6 +1,6 @@
1
1
  AllCops:
2
2
  NewCops: enable
3
- TargetRubyVersion: '3.0.0'
3
+ TargetRubyVersion: '3.0.1'
4
4
  SuggestExtensions: false
5
5
 
6
6
  Metrics/AbcSize:
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 3.0.0
1
+ 3.0.1
data/.travis.yml CHANGED
@@ -1,7 +1,7 @@
1
1
  language: ruby
2
2
  cache: bundler
3
3
  rvm:
4
- - 3.0.0
4
+ - 3.0.1
5
5
  before_install:
6
6
  - gem install bundler
7
7
  - bundler install
data/CHANGELOG.md CHANGED
@@ -1,5 +1,28 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 1.0.9 - 2020-05-04
4
+
5
+ - Updated dependencies to fix security vulnerability in rexml
6
+
7
+ ## 1.0.8 - 2020-05-04
8
+
9
+ - Updated dependencies to fix security vulnerability in rexml
10
+
11
+ ## 1.0.7 - 2020-03-31
12
+
13
+ - Fixed bug in JSONAPI::Parser::JSONParser that would serialize hashes with symbol key values instead of string
14
+
15
+ ## 1.0.6 - 2020-03-30
16
+
17
+ - Fixed bug in JSONAPI::Middleware that was not checking for environment variables properly
18
+
19
+ ## 1.0.5 - 2020-03-30
20
+
21
+ - Fixed bug in JSONAPI::Exceptions::HeadersExceptions that didn't check for user required headers requirements
22
+ - Fixed bug in JSONAPI::Exceptions::QueryParamExceptions that didn't check for user required query param requirements
23
+ - Added more tests to the middleware
24
+ - Updated Documentation
25
+
3
26
  ## 1.0.4 - 2020-03-28
4
27
 
5
28
  - Fixed JSONAPI::ExceptionsHeadersExceptions bug
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- easy-jsonapi (1.0.4)
4
+ easy-jsonapi (1.0.9)
5
5
  oj (~> 3.10)
6
6
 
7
7
  GEM
@@ -24,7 +24,7 @@ GEM
24
24
  racc (~> 1.4)
25
25
  nokogiri (1.11.2-x86_64-linux)
26
26
  racc (~> 1.4)
27
- oj (3.11.3)
27
+ oj (3.11.5)
28
28
  parallel (1.20.1)
29
29
  parser (3.0.0.0)
30
30
  ast (~> 2.4.1)
@@ -36,7 +36,7 @@ GEM
36
36
  regexp_parser (2.1.1)
37
37
  reverse_markdown (2.0.0)
38
38
  nokogiri
39
- rexml (3.2.4)
39
+ rexml (3.2.5)
40
40
  rspec (3.10.0)
41
41
  rspec-core (~> 3.10.0)
42
42
  rspec-expectations (~> 3.10.0)
data/README.md CHANGED
@@ -21,7 +21,7 @@ Ever wanted the benefits of [JSONAPI](https://jsonapi.org/) without the learning
21
21
  2. A `parser` to interact with requests in a typical Object-Oriented Fashion, providing convenient and efficient access to headers, query parameters, and document members.
22
22
  3. A `validator` to check your serialized responses for [JSONAPI](https://jsonapi.org/) compliance.
23
23
 
24
- With its only gem dependency being [Oj](https://github.com/ohler55/oj), ***easy-jsonapi*** is a lightweight, dependable tool, featuring comprehensive error messages and over ***500 unit tests*** allowing developers to spend less time debugging and more time creating.
24
+ With its only gem dependency being [Oj](https://github.com/ohler55/oj), ***easy-jsonapi*** is a lightweight, dependable tool, featuring comprehensive error messages and over ***525 unit tests*** allowing developers to spend less time debugging and more time creating.
25
25
 
26
26
  As a bonus, flexible user configurations can be added to the middleware providing custom screening on all requests or individual requests depending on the resource type of the endpoint and the user-defined document, header, or query param restrictions.
27
27
 
@@ -31,6 +31,8 @@ As a bonus, flexible user configurations can be added to the middleware providin
31
31
 
32
32
  - [*RubyGems* *Repository*](https://rubygems.org/search?query=easy-jsonapi)
33
33
 
34
+ - [*Class Diagram*](https://lucid.app/lucidchart/invitations/accept/3d543b29-4171-43cb-94db-9aeb346cdcac)
35
+
34
36
  ## Installation
35
37
 
36
38
  Add this line to your applications' Gemfile:
@@ -126,7 +128,7 @@ use JSONAPI::Middleware
126
128
 
127
129
  The easy-jsonapi middleware can operate in development or production mode.
128
130
 
129
- If `ENV['RACK_ENV']` is set to `:development` or not set at all, the middleware will be operating in development mode.
131
+ If `ENV['RACK_ENV']` is set to `'development'` or not set at all, the middleware will be operating in development mode.
130
132
 
131
133
  When the middleware is in development mode it will raise an exception wherever it finds the http request to be non JSONAPI compliant.
132
134
 
@@ -137,7 +139,7 @@ The types of exceptions it will raise are:
137
139
  - `JSONAPI::Exceptions::QueryParamExceptions::InvalidQueryParam` when an included query parameter is non-compliant
138
140
  - `JSONAPI::Exceptions::DocumentExceptions::InvalidDocument` when the body is included and non-compliant
139
141
 
140
- If `ENV['RACK_ENV']` is set to something other than `:development`, then the middleware will return the appropriate status code error given the JSON:API clause the headers, query params, or document violates.
142
+ If `ENV['RACK_ENV']` is set to something other than `'development'`, then the middleware will return the appropriate status code error given the JSON:API clause the headers, query params, or document violates.
141
143
 
142
144
  ### User Configurations
143
145
 
data/easy-jsonapi.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = 'easy-jsonapi'
5
- spec.version = '1.0.4'
5
+ spec.version = '1.0.9'
6
6
  spec.authors = ['Joshua DeMoss, Joe Viscomi']
7
7
  spec.email = ['demoss.joshua@gmail.com']
8
8
 
@@ -25,9 +25,11 @@ module JSONAPI
25
25
 
26
26
  # Check http verb vs included headers
27
27
  # @param env [Hash] The rack environment variable
28
- def self.check_request(env, body, config_manager = nil, opts = {})
28
+ # @param config_manager [JSONAPI::ConfigManager] The manager of user configurations
29
+ # @param opts [Hash] Includes http_method, path, and contains_body values
30
+ def self.check_request(env, config_manager = nil, opts = {})
29
31
  check_compliance(env, config_manager, opts)
30
- check_http_method_against_headers(env, body)
32
+ check_http_method_against_headers(env, opts[:contains_body])
31
33
  end
32
34
 
33
35
  # Check jsonapi compliance
@@ -39,7 +41,7 @@ module JSONAPI
39
41
  hdrs = JSONAPI::Parser::HeadersParser.parse(env)
40
42
  usr_opts = { http_method: opts[:http_method], path: opts[:path] }
41
43
  err_msg = JSONAPI::Exceptions::UserDefinedExceptions.check_user_header_requirements(hdrs, config_manager, usr_opts)
42
- return err_msg unless err_msg.nil?
44
+ raise err_msg unless err_msg.nil?
43
45
  end
44
46
 
45
47
  class << self
@@ -78,30 +80,30 @@ module JSONAPI
78
80
  # error if the combination doesn't make sense
79
81
  # @param (see #compliant?)
80
82
  # @raise InvalidHeader the invalid header incombination with the http verb
81
- def check_http_method_against_headers(env, body)
83
+ def check_http_method_against_headers(env, contains_body)
82
84
  case env['REQUEST_METHOD']
83
85
  when 'GET'
84
- check_get_against_hdrs(env, body)
86
+ check_get_against_hdrs(env, contains_body)
85
87
  when 'POST' || 'PATCH' || 'PUT'
86
- check_post_against_hdrs(env, body)
88
+ check_post_against_hdrs(env, contains_body)
87
89
  when 'DELETE'
88
- check_delete_against_hdrs(env, body)
90
+ check_delete_against_hdrs(env, contains_body)
89
91
  end
90
92
  end
91
93
 
92
94
  # Raise error if a GET request has a body or a content type header
93
95
  # @param (see #compliant?)
94
- def check_get_against_hdrs(env, body)
95
- raise_error('GET requests cannot have a body.') unless body == ""
96
+ def check_get_against_hdrs(env, contains_body)
97
+ raise_error('GET requests cannot have a body.') if contains_body
96
98
  raise_error("GET request cannot have a 'CONTENT_TYPE' http header.") unless env['CONTENT_TYPE'].nil?
97
99
  end
98
100
 
99
101
  # POST, PUT, and PATCH request must have a content type header,
100
102
  # a body, and a content-type and accept header that accepts jsonapi
101
103
  # @param (see #compliant?)
102
- def check_post_against_hdrs(env, body)
104
+ def check_post_against_hdrs(env, contains_body)
103
105
  raise_error("POST, PUT, and PATCH requests must have a 'CONTENT_TYPE' header.") unless env['CONTENT_TYPE']
104
- raise_error('POST, PUT, and PATCH requests must have a body.') if body == ""
106
+ raise_error('POST, PUT, and PATCH requests must have a body.') unless contains_body
105
107
 
106
108
  return if env['CONTENT_TYPE'] == 'application/vnd.api+json' && accepts_jsonapi?(env)
107
109
 
@@ -110,8 +112,8 @@ module JSONAPI
110
112
  end
111
113
 
112
114
  # Raise error if DELETE hdr has a body or a content type header
113
- def check_delete_against_hdrs(env, body)
114
- raise_error('DELETE requests cannot have a body.') unless body == ""
115
+ def check_delete_against_hdrs(env, contains_body)
116
+ raise_error('DELETE requests cannot have a body.') if contains_body
115
117
  raise_error("DELETE request cannot have a 'CONTENT_TYPE' http header.") unless env['CONTENT_TYPE'].nil?
116
118
  end
117
119
 
@@ -33,7 +33,7 @@ module JSONAPI
33
33
  end
34
34
 
35
35
  err_msg = JSONAPI::Exceptions::UserDefinedExceptions.check_user_query_param_requirements(rack_req_params, config_manager, opts)
36
- return err_msg unless err_msg.nil?
36
+ raise err_msg unless err_msg.nil?
37
37
 
38
38
  nil
39
39
  end
@@ -62,8 +62,10 @@ module JSONAPI
62
62
  # with underscores instead of dashes.
63
63
  # @param config (see #check_user_document_requirements)
64
64
  def check_user_header_requirements(headers, config_manager, opts)
65
- return if config_manager.nil? || config_manager.default?
65
+ return if config_manager.nil?
66
+
66
67
  config = get_config(config_manager, opts[:http_method], opts[:path])
68
+ return if config.default? && config_manager.size.positive?
67
69
 
68
70
  err =
69
71
  check_for_required_headers(headers, config.required_headers)
@@ -77,8 +79,10 @@ module JSONAPI
77
79
  # @param rack_req_params [Hash] The hash of the query parameters given by Rack::Request
78
80
  # @param config (see #check_user_document_requirements)
79
81
  def check_user_query_param_requirements(rack_req_params, config_manager, opts)
80
- return if config_manager.nil? || config_manager.default?
82
+ return if config_manager.nil?
83
+
81
84
  config = get_config(config_manager, opts[:http_method], opts[:path])
85
+ return if config.default? && config_manager.size.positive?
82
86
 
83
87
  err =
84
88
  check_for_required_params(rack_req_params, config.required_query_params)
@@ -23,8 +23,8 @@ module JSONAPI
23
23
  # and error if any section is found to be non-compliant.
24
24
  # @param env The rack envirornment hash
25
25
  def call(env)
26
- if in_maintenance_mode?(env)
27
- return maintenance_response(env)
26
+ if in_maintenance_mode?
27
+ return maintenance_response
28
28
  end
29
29
 
30
30
  if jsonapi_request?(env)
@@ -38,17 +38,15 @@ module JSONAPI
38
38
  private
39
39
 
40
40
  # Checks the 'MAINTENANCE' environment variable
41
- # @param (see #call)
42
41
  # @return [TrueClass | FalseClass]
43
- def in_maintenance_mode?(env)
44
- !env['MAINTENANCE'].nil?
42
+ def in_maintenance_mode?
43
+ !ENV['MAINTENANCE'].nil?
45
44
  end
46
45
 
47
46
  # Return 503 with or without msg depending on environment
48
- # @param (see #call)
49
47
  # @return [Array] Http Error Responses
50
- def maintenance_response(env)
51
- if environment_development?(env)
48
+ def maintenance_response
49
+ if environment_development?
52
50
  [503, {}, ['MAINTENANCE envirornment variable set']]
53
51
  else
54
52
  [503, {}, []]
@@ -91,28 +89,28 @@ module JSONAPI
91
89
  # Store separately so you can rewind for next middleware or app
92
90
  body = env['rack.input'].read
93
91
  env['rack.input'].rewind
94
- opts = { http_method: env['REQUEST_METHOD'], path: env['PATH_INFO'] }
92
+ opts = { http_method: env['REQUEST_METHOD'], path: env['PATH_INFO'], contains_body: body != "" }
95
93
 
96
- header_error = check_headers_compliance(env, body, config_manager, opts)
94
+ header_error = check_headers_compliance(env, config_manager, opts)
97
95
  return header_error unless header_error.nil?
98
96
 
99
97
  req = Rack::Request.new(env)
100
- param_error = check_query_param_compliance(env, req.GET, config_manager, opts)
98
+ param_error = check_query_param_compliance(req.GET, config_manager, opts)
101
99
  return param_error unless param_error.nil?
102
100
 
103
101
  return unless env['CONTENT_TYPE']
104
102
 
105
- body_error = check_req_body_compliance(env, body, config_manager, opts)
103
+ body_error = check_body_compliance(body, config_manager, opts)
106
104
  return body_error unless body_error.nil?
107
105
  end
108
106
 
109
107
  # Checks whether the http headers are jsonapi compliant
110
108
  # @param (see #call)
111
109
  # @return [NilClass | Array] Nil meaning no error or a 400 level http response
112
- def check_headers_compliance(env, body, config_manager, opts)
113
- JSONAPI::Exceptions::HeadersExceptions.check_request(env, body, config_manager, opts)
114
- rescue JSONAPI::Exceptions::HeadersExceptions::InvalidHeader || JSONAPI::Exceptions::UserDefinedExceptions::InvalidHeader => e
115
- raise if environment_development?(env)
110
+ def check_headers_compliance(env, config_manager, opts)
111
+ JSONAPI::Exceptions::HeadersExceptions.check_request(env, config_manager, opts)
112
+ rescue JSONAPI::Exceptions::HeadersExceptions::InvalidHeader, JSONAPI::Exceptions::UserDefinedExceptions::InvalidHeader => e
113
+ raise if environment_development?
116
114
 
117
115
  [e.status_code, {}, []]
118
116
  end
@@ -120,10 +118,10 @@ module JSONAPI
120
118
  # @param query_params [Hash] The rack request query_param hash
121
119
  # @raise If the query parameters are not JSONAPI compliant
122
120
  # @return [NilClass | Array] Nil meaning no error or a 400 level http response
123
- def check_query_param_compliance(env, query_params, config_manager, opts)
121
+ def check_query_param_compliance(query_params, config_manager, opts)
124
122
  JSONAPI::Exceptions::QueryParamsExceptions.check_compliance(query_params, config_manager, opts)
125
- rescue JSONAPI::Exceptions::QueryParamsExceptions::InvalidQueryParameter || JSONAPI::Exceptions::UserDefinedExceptions::InvalidQueryParam => e
126
- raise if environment_development?(env)
123
+ rescue JSONAPI::Exceptions::QueryParamsExceptions::InvalidQueryParameter, JSONAPI::Exceptions::UserDefinedExceptions::InvalidQueryParam => e
124
+ raise if environment_development?
127
125
 
128
126
  [e.status_code, {}, []]
129
127
  end
@@ -131,14 +129,14 @@ module JSONAPI
131
129
  # @param env (see #call)
132
130
  # @param req (see #check_query_param_compliance)
133
131
  # @raise If the document body is not JSONAPI compliant
134
- def check_req_body_compliance(env, body, config_manager, opts)
132
+ def check_body_compliance(body, config_manager, opts)
135
133
  JSONAPI::Exceptions::DocumentExceptions.check_compliance(body, config_manager, opts)
136
- rescue JSONAPI::Exceptions::DocumentExceptions::InvalidDocument || JSONAPI::Exceptions::UserDefinedExceptions::InvalidDocument => e
137
- raise if environment_development?(env)
134
+ rescue JSONAPI::Exceptions::DocumentExceptions::InvalidDocument, JSONAPI::Exceptions::UserDefinedExceptions::InvalidDocument => e
135
+ raise if environment_development?
138
136
 
139
137
  [e.status_code, {}, []]
140
138
  rescue JSONAPI::Exceptions::JSONParseError
141
- raise if environment_development?(env)
139
+ raise if environment_development?
142
140
 
143
141
  [400, {}, []]
144
142
  end
@@ -151,8 +149,8 @@ module JSONAPI
151
149
  end
152
150
 
153
151
  # @param (see #call)
154
- def environment_development?(env)
155
- env['RACK_ENV'].to_s.downcase == 'development' || env['RACK_ENV'].nil?
152
+ def environment_development?
153
+ ENV['RACK_ENV'].to_s.downcase == 'development' || ENV['RACK_ENV'].nil?
156
154
  end
157
155
  end
158
156
  end
@@ -21,7 +21,7 @@ module JSONAPI
21
21
  # Convert ruby hash into JSON
22
22
  # @param ruby_hash [Hash] THe hash to convert into JSON
23
23
  def self.dump(ruby_hash)
24
- Oj.dump(ruby_hash)
24
+ Oj.dump(ruby_hash, mode: :compat)
25
25
  end
26
26
 
27
27
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy-jsonapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua DeMoss, Joe Viscomi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-03-29 00:00:00.000000000 Z
11
+ date: 2021-05-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -249,7 +249,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
249
249
  - !ruby/object:Gem::Version
250
250
  version: '0'
251
251
  requirements: []
252
- rubygems_version: 3.2.3
252
+ rubygems_version: 3.2.15
253
253
  signing_key:
254
254
  specification_version: 4
255
255
  summary: Middleware, Parser, and Validator for JSONAPI requests and serialized resopnses