openapi_parameters 0.3.4 → 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: fa336f02dcfc58d656659606710c5fed70335b7a5e101f42228a24913e12325b
4
- data.tar.gz: e81f400174546397953abe62d23c26f07fbf14f594ca2297ad129118847a4709
3
+ metadata.gz: c0d1127ef2e34d916ca293835143d6267d1baf9cfb6f51ca12384e712f57eef0
4
+ data.tar.gz: ced0820dba10352ab2a460b4c621c5fcbd2f79400008aef3081d442027189113
5
5
  SHA512:
6
- metadata.gz: b30a7b111b652c9dbc12e0c19ef5f9dc74a5ba9b6eebc0a7392ba0d5070d7c19578017a64e8948e8dd235b57e8784202e37765c62be015c5520940278def939f
7
- data.tar.gz: 1e91216f5bff01be794c5eec6a469cea42162472568c2d881afa3267aa036257d0afab0e97534edd57f074217122c96abac08e3e6d4a14c139348bb0ae81fd8e
6
+ metadata.gz: 1d3bbef2747984f498d259cd838ff5e9acbfd8f5417ad8718f7ac9d8ea078a72af94b3835f6307c967803c447ff3e7be0eb6b4b1cae9bb3d2afd1138226e8091
7
+ data.tar.gz: 6d17518118d5df6dc5376e8bed4b131f65077c5ed71293fe41425f788c0ecddc7fbbd476a1e93cf28e022d522fb8973b1afe0090be9ab71b3b2b08b036aa70a3
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.3.4] - 2024-12-17
4
+
5
+ - Add Parameter#convert(value)
6
+ - Slight performance optimization
7
+
3
8
  ## [0.3.4] - 2024-06-14
4
9
 
5
10
  - Fix handling invalid object query parameters (String vs Hash)
@@ -2,6 +2,7 @@
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
  ##
@@ -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
@@ -18,7 +18,7 @@ module OpenapiParameters
18
18
 
19
19
  result[parameter.name] = catch :skip do
20
20
  value = Unpacker.unpack_value(parameter, cookies[parameter.name])
21
- @convert ? Converter.convert(value, parameter.schema) : value
21
+ @convert ? parameter.convert(value) : value
22
22
  end
23
23
  end
24
24
  end
@@ -17,7 +17,7 @@ module OpenapiParameters
17
17
 
18
18
  result[parameter.name] = catch :skip do
19
19
  value = Unpacker.unpack_value(parameter, headers[parameter.name])
20
- @convert ? Converter.convert(value, parameter.schema) : value
20
+ @convert ? parameter.convert(value) : value
21
21
  end
22
22
  end
23
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
@@ -21,7 +21,7 @@ module OpenapiParameters
21
21
 
22
22
  result[parameter.name] = catch :skip do
23
23
  value = Unpacker.unpack_value(parameter, path_params[parameter.name])
24
- @convert ? Converter.convert(value, parameter.schema) : value
24
+ @convert ? parameter.convert(value) : value
25
25
  end
26
26
  end
27
27
  end
@@ -19,13 +19,13 @@ module OpenapiParameters
19
19
  parsed_nested_query = Rack::Utils.parse_nested_query(query_string)
20
20
  next unless parsed_nested_query.key?(parameter.name)
21
21
 
22
- result[parameter.name] = convert(parsed_nested_query[parameter.name], parameter)
22
+ value = parsed_nested_query[parameter.name]
23
23
  else
24
24
  next unless parsed_query.key?(parameter.name)
25
25
 
26
26
  value = Unpacker.unpack_value(parameter, parsed_query[parameter.name])
27
- result[parameter.name] = convert(value, parameter)
28
27
  end
28
+ result[parameter.name] = @convert ? parameter.convert(value) : value
29
29
  end
30
30
  end
31
31
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OpenapiParameters
4
- VERSION = '0.3.4'
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.4
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-06-14 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