openapi_parameters 0.3.3 → 0.4.0

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
  SHA256:
3
- metadata.gz: e010a357f13e7452f8945d95e7c277fc35c87d1cc713e41dccae68a20d37db03
4
- data.tar.gz: 763b4eed6e0e11c4f0ff839afa366cadc6569c371a4ccd5490a7b3368f8849fc
3
+ metadata.gz: c0d1127ef2e34d916ca293835143d6267d1baf9cfb6f51ca12384e712f57eef0
4
+ data.tar.gz: ced0820dba10352ab2a460b4c621c5fcbd2f79400008aef3081d442027189113
5
5
  SHA512:
6
- metadata.gz: 49a1e95d33f513346408826aa1954d9a8c58b7a656d23b1643f8f5664e3747ab04589586d8c6ffcd9392022ff85ad2f4722dd9e541f569399ff8a7bd2226ecb6
7
- data.tar.gz: 6bc8eadb1948050b12ab6561c8e412ab124d0bfd64d94414506f71d5c1549d963c2d765ce0e9ebea1908c258ef30d17b7f8c4db3bd9c81643897c0bac86ad487
6
+ metadata.gz: 1d3bbef2747984f498d259cd838ff5e9acbfd8f5417ad8718f7ac9d8ea078a72af94b3835f6307c967803c447ff3e7be0eb6b4b1cae9bb3d2afd1138226e8091
7
+ data.tar.gz: 6d17518118d5df6dc5376e8bed4b131f65077c5ed71293fe41425f788c0ecddc7fbbd476a1e93cf28e022d522fb8973b1afe0090be9ab71b3b2b08b036aa70a3
data/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.3.4] - 2024-12-17
4
+
5
+ - Add Parameter#convert(value)
6
+ - Slight performance optimization
7
+
8
+ ## [0.3.4] - 2024-06-14
9
+
10
+ - Fix handling invalid object query parameters (String vs Hash)
11
+
3
12
  ## [0.3.3] - 2023-04-13
4
13
 
5
14
  - Remove zeitwerk. It's awesome, but not needed here
data/README.md CHANGED
@@ -13,22 +13,20 @@ Note that OpenAPI supportes parameter definition on path and operation objects.
13
13
  ### Unpack query/path/header/cookie parameters from HTTP requests according to their OpenAPI definition
14
14
 
15
15
  ```ruby
16
- parameters = [{
16
+
17
+ query_parameters = OpenapiParameters::Query.new([{
17
18
  'name' => 'ids',
18
19
  'required' => true,
19
- 'in' => 'query', # or 'path', 'header', 'cookie'
20
+ 'in' => 'query', # Note that we only pass query parameters here
20
21
  'schema' => {
21
22
  'type' => 'array',
22
23
  'items' => {
23
24
  'type' => 'integer'
24
25
  }
25
26
  }
26
- }]
27
-
28
- query_parameters = OpenapiParameters::Query.new(parameters)
27
+ }])
29
28
  query_string = env['QUERY_STRING'] # => 'ids=1&ids=2'
30
29
  query_parameters.unpack(query_string) # => { 'ids' => [1, 2] }
31
- query_parameters.unpack(query_string, convert: false) # => { 'ids' => ['1', '2'] }
32
30
 
33
31
  path_parameters = OpenapiParameters::Path.new(parameters)
34
32
  route_params = env['route.params'] # This depends on the webframework you are using
@@ -2,10 +2,11 @@
2
2
 
3
3
  module OpenapiParameters
4
4
  # Tries to convert a request parameter value (string) to the type specified in the JSON Schema.
5
+ # @visibility private
5
6
  module Converter
6
7
  class << self
7
8
  ##
8
- # @param input [String, Hash, Array] the value to convert
9
+ # @param value [String, Hash, Array] the value to convert
9
10
  # @param schema [Hash] the schema to use for conversion.
10
11
  def convert(value, schema) # rubocop:disable Metrics
11
12
  return if value.nil?
@@ -43,9 +44,11 @@ module OpenapiParameters
43
44
  end
44
45
  end
45
46
 
46
- def convert_object(object, schema)
47
- object.each_with_object({}) do |(key, value), hsh|
48
- hsh[key] = convert(value, schema['properties']&.fetch(key, nil))
47
+ def convert_object(value, schema)
48
+ return value unless value.is_a?(Hash)
49
+
50
+ value.each_with_object({}) do |(key, val), hsh|
51
+ hsh[key] = convert(val, schema['properties']&.fetch(key, nil))
49
52
  end
50
53
  end
51
54
 
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenapiParameters
4
+ # Home for all converters
5
+ module Converters
6
+ @converters = {}
7
+
8
+ class << self
9
+ attr_reader :converters
10
+
11
+ def register(type, converter)
12
+ converters[type] = converter
13
+ end
14
+
15
+ def [](schema)
16
+ type = schema && schema['type']
17
+ converters.fetch(type) do
18
+ ->(value) { Converter.convert(value, schema) }
19
+ end
20
+ end
21
+ end
22
+
23
+ register('integer', lambda do |value|
24
+ Integer(value, 10)
25
+ rescue StandardError
26
+ value
27
+ end)
28
+
29
+ register('number', lambda do |value|
30
+ Float(value)
31
+ rescue StandardError
32
+ value
33
+ end)
34
+
35
+ register('boolean', lambda do |value|
36
+ if value == 'true'
37
+ true
38
+ else
39
+ value == 'false' ? false : value
40
+ end
41
+ end)
42
+ end
43
+ end
@@ -6,7 +6,7 @@ module OpenapiParameters
6
6
  # @param parameters [Array<Hash>] The OpenAPI parameter definitions.
7
7
  # @param convert [Boolean] Whether to convert the values to the correct type.
8
8
  def initialize(parameters, convert: true)
9
- @parameters = parameters
9
+ @parameters = parameters.map { Parameter.new(_1) }
10
10
  @convert = convert
11
11
  end
12
12
 
@@ -14,12 +14,11 @@ module OpenapiParameters
14
14
  def unpack(cookie_string)
15
15
  cookies = Rack::Utils.parse_cookies_header(cookie_string)
16
16
  parameters.each_with_object({}) do |parameter, result|
17
- parameter = Parameter.new(parameter)
18
17
  next unless cookies.key?(parameter.name)
19
18
 
20
19
  result[parameter.name] = catch :skip do
21
20
  value = Unpacker.unpack_value(parameter, cookies[parameter.name])
22
- @convert ? Converter.convert(value, parameter.schema) : value
21
+ @convert ? parameter.convert(value) : value
23
22
  end
24
23
  end
25
24
  end
@@ -6,19 +6,18 @@ module OpenapiParameters
6
6
  # @param parameters [Array<Hash>] The OpenAPI parameters
7
7
  # @param convert [Boolean] Whether to convert the values to the correct type.
8
8
  def initialize(parameters, convert: true)
9
- @parameters = parameters
9
+ @parameters = parameters.map { Parameter.new(_1) }
10
10
  @convert = convert
11
11
  end
12
12
 
13
13
  # @param headers [Hash] The headers from the request. Use HeadersHash to convert a Rack env to a Hash.
14
14
  def unpack(headers)
15
15
  parameters.each_with_object({}) do |parameter, result|
16
- parameter = Parameter.new(parameter)
17
16
  next unless headers.key?(parameter.name)
18
17
 
19
18
  result[parameter.name] = catch :skip do
20
19
  value = Unpacker.unpack_value(parameter, headers[parameter.name])
21
- @convert ? Converter.convert(value, parameter.schema) : value
20
+ @convert ? parameter.convert(value) : value
22
21
  end
23
22
  end
24
23
  end
@@ -9,11 +9,16 @@ module OpenapiParameters
9
9
  @definition = definition
10
10
  @name = definition['name']
11
11
  @is_deep_object = style == 'deepObject'
12
+ @converter = Converters[schema]
12
13
  check_supported!
13
14
  end
14
15
 
15
16
  attr_reader :definition, :name
16
17
 
18
+ def convert(value)
19
+ @converter.call(value)
20
+ end
21
+
17
22
  def deep_object?
18
23
  @is_deep_object
19
24
  end
@@ -8,7 +8,7 @@ module OpenapiParameters
8
8
  # @param parameters [Array<Hash>] The OpenAPI path parameters.
9
9
  # @param convert [Boolean] Whether to convert the values to the correct type.
10
10
  def initialize(parameters, convert: true)
11
- @parameters = parameters
11
+ @parameters = parameters.map { Parameter.new(_1) }
12
12
  @convert = convert
13
13
  end
14
14
 
@@ -16,13 +16,12 @@ module OpenapiParameters
16
16
 
17
17
  # @param path_params [Hash] The path parameters from the Rack request. The keys are strings.
18
18
  def unpack(path_params)
19
- parameters.each_with_object({}) do |param, result|
20
- parameter = Parameter.new(param)
19
+ parameters.each_with_object({}) do |parameter, result|
21
20
  next unless path_params.key?(parameter.name)
22
21
 
23
22
  result[parameter.name] = catch :skip do
24
23
  value = Unpacker.unpack_value(parameter, path_params[parameter.name])
25
- @convert ? Converter.convert(value, parameter.schema) : value
24
+ @convert ? parameter.convert(value) : value
26
25
  end
27
26
  end
28
27
  end
@@ -8,25 +8,24 @@ module OpenapiParameters
8
8
  # @param parameters [Array<Hash>] The OpenAPI query parameter definitions.
9
9
  # @param convert [Boolean] Whether to convert the values to the correct type.
10
10
  def initialize(parameters, convert: true)
11
- @parameters = parameters
11
+ @parameters = parameters.map { Parameter.new(_1) }
12
12
  @convert = convert
13
13
  end
14
14
 
15
15
  def unpack(query_string) # rubocop:disable Metrics/AbcSize
16
16
  parsed_query = Rack::Utils.parse_query(query_string)
17
17
  parameters.each_with_object({}) do |parameter, result|
18
- parameter = Parameter.new(parameter)
19
18
  if parameter.deep_object?
20
19
  parsed_nested_query = Rack::Utils.parse_nested_query(query_string)
21
20
  next unless parsed_nested_query.key?(parameter.name)
22
21
 
23
- result[parameter.name] = convert(parsed_nested_query[parameter.name], parameter)
22
+ value = parsed_nested_query[parameter.name]
24
23
  else
25
24
  next unless parsed_query.key?(parameter.name)
26
25
 
27
26
  value = Unpacker.unpack_value(parameter, parsed_query[parameter.name])
28
- result[parameter.name] = convert(value, parameter)
29
27
  end
28
+ result[parameter.name] = @convert ? parameter.convert(value) : value
30
29
  end
31
30
  end
32
31
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OpenapiParameters
4
- VERSION = '0.3.3'
4
+ VERSION = '0.4.0'
5
5
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'rack'
4
+ require_relative 'openapi_parameters/converters'
4
5
  require_relative 'openapi_parameters/converter'
5
6
  require_relative 'openapi_parameters/cookie'
6
7
  require_relative 'openapi_parameters/error'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openapi_parameters
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andreas Haller
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-04-13 00:00:00.000000000 Z
11
+ date: 2024-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -36,6 +36,7 @@ files:
36
36
  - README.md
37
37
  - lib/openapi_parameters.rb
38
38
  - lib/openapi_parameters/converter.rb
39
+ - lib/openapi_parameters/converters.rb
39
40
  - lib/openapi_parameters/cookie.rb
40
41
  - lib/openapi_parameters/error.rb
41
42
  - lib/openapi_parameters/header.rb