openapi_parser 1.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yaml +9 -2
  3. data/CHANGELOG.md +9 -0
  4. data/Rakefile +1 -1
  5. data/Steepfile +3 -3
  6. data/lib/openapi_parser/errors.rb +11 -0
  7. data/lib/openapi_parser/{schema_validators → schema_validator}/all_of_validator.rb +4 -0
  8. data/lib/openapi_parser/{schema_validators → schema_validator}/any_of_validator.rb +3 -0
  9. data/lib/openapi_parser/{schema_validators → schema_validator}/array_validator.rb +9 -0
  10. data/lib/openapi_parser/{schema_validators → schema_validator}/base.rb +1 -1
  11. data/lib/openapi_parser/{schema_validators → schema_validator}/one_of_validator.rb +3 -0
  12. data/lib/openapi_parser/{schema_validators → schema_validator}/string_validator.rb +5 -1
  13. data/lib/openapi_parser/{schema_validators → schema_validator}/unspecified_type_validator.rb +1 -1
  14. data/lib/openapi_parser/schema_validator.rb +15 -15
  15. data/lib/openapi_parser/schemas/classes.rb +1 -0
  16. data/lib/openapi_parser/schemas/info.rb +6 -0
  17. data/lib/openapi_parser/schemas/openapi.rb +4 -0
  18. data/lib/openapi_parser/schemas.rb +1 -0
  19. data/lib/openapi_parser/version.rb +1 -1
  20. data/lib/openapi_parser.rb +6 -2
  21. data/openapi_parser.gemspec +1 -1
  22. data/sig/openapi_parser/config.rbs +2 -0
  23. data/sig/openapi_parser/errors.rbs +22 -0
  24. data/sig/openapi_parser/reference_expander.rbs +3 -0
  25. data/sig/openapi_parser/schema_validators/base.rbs +4 -3
  26. data/sig/types.rbs +5 -0
  27. data/sig/wip_types.rbs +0 -3
  28. metadata +25 -22
  29. /data/lib/openapi_parser/{schema_validators → schema_validator}/boolean_validator.rb +0 -0
  30. /data/lib/openapi_parser/{schema_validators → schema_validator}/enumable.rb +0 -0
  31. /data/lib/openapi_parser/{schema_validators → schema_validator}/float_validator.rb +0 -0
  32. /data/lib/openapi_parser/{schema_validators → schema_validator}/integer_validator.rb +0 -0
  33. /data/lib/openapi_parser/{schema_validators → schema_validator}/minimum_maximum.rb +0 -0
  34. /data/lib/openapi_parser/{schema_validators → schema_validator}/nil_validator.rb +0 -0
  35. /data/lib/openapi_parser/{schema_validators → schema_validator}/object_validator.rb +0 -0
  36. /data/lib/openapi_parser/{schema_validators → schema_validator}/options.rb +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3019c8458c35d159f7c8eccc8f8203d39826438804460cccd1383f9e29b95299
4
- data.tar.gz: 3471fed71a9d1f81688d87a0c907a0ab4c0529ca3e4a9d40159403b88238d1a6
3
+ metadata.gz: 46f9ea4e608b98aadd42d6da1ad0f3ab83f8b87c14fee89d287bf7d92d40a38a
4
+ data.tar.gz: 84aadee40dbb76d7332fa222d05e447a3070c57311dc6d872267b36b68183e39
5
5
  SHA512:
6
- metadata.gz: d9f7ba65682079456e26ff03d4be816ad6f61b9bb9224c4cf37157a0bb4d4455d17c3e36fbadd0f557700d9926f69a50ae8ebdc9041412304b12b05feb91df4e
7
- data.tar.gz: fd611630b787af1e69a003de92c01f8c83d9e9d6e8be68fe66fdba6a93ce2a8ca412c89a00fa35bcf4d7441fe10c309ec9eb9f3e91659957429b0375629a3671
6
+ metadata.gz: dea21c51e1744e593cd110b4b697c212e31d8834e12f148b73ff53e9777cabe83250db73b00102d3cab6f865b30b682f427adc39f7f22c71698965af76fb1cee
7
+ data.tar.gz: 98e5c2e15c264918477a36c94788c9ae6c3054c0c86bc2b2b98e1eb5effec77698c00e412682065f2fc0f8538588fde298f54a4083843673de574947dc7e917f
@@ -5,8 +5,15 @@ jobs:
5
5
  strategy:
6
6
  fail-fast: false
7
7
  matrix:
8
- os: [ubuntu-latest, macos-latest]
9
- ruby: [2.6, 2.7, '3.0', '3.1']
8
+ os:
9
+ - ubuntu-latest
10
+ - macos-latest
11
+ ruby:
12
+ - "2.7"
13
+ - "3.0"
14
+ - "3.1"
15
+ - "3.2"
16
+ - ruby-head
10
17
  runs-on: ${{ matrix.os }}
11
18
  steps:
12
19
  - uses: actions/checkout@v2
data/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  ## Unreleased
2
2
 
3
+ ## 2.1.0 (2024-04-03)
4
+ ### Fixed
5
+ * Add full-date compliant date format validation #164
6
+
7
+ ## 1.1.0 (2023-10-15)
8
+ ### Added
9
+ * Support for uniqueItems in array #154
10
+ * Fix nullable field does not work with allOf, anyOf and oneOf keyword #128
11
+
3
12
  ## 1.0.0 (2021-02-03)
4
13
  ### Added
5
14
  * Add date-time format validation #126
data/Rakefile CHANGED
@@ -4,7 +4,7 @@ require 'rspec/core/rake_task'
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
6
  task :steep do
7
- sh 'steep check'
7
+ sh 'steep check'
8
8
  end
9
9
 
10
10
  task :default => [:steep, :spec]
data/Steepfile CHANGED
@@ -4,8 +4,8 @@ target :lib do
4
4
 
5
5
  check "lib/openapi_parser.rb"
6
6
  check "lib/openapi_parser/config.rb"
7
- check "lib/openapi_parser/schema_validators/options.rb"
8
- check "lib/openapi_parser/schema_validators/base.rb"
7
+ check "lib/openapi_parser/schema_validator/options.rb"
8
+ check "lib/openapi_parser/schema_validator/base.rb"
9
9
 
10
- library 'uri'
10
+ library 'uri', 'json', 'pathname'
11
11
  end
@@ -272,4 +272,15 @@ module OpenAPIParser
272
272
  "#{@reference} #{@value.inspect} contains fewer than min items"
273
273
  end
274
274
  end
275
+
276
+ class NotUniqueItems < OpenAPIError
277
+ def initialize(value, reference)
278
+ super(reference)
279
+ @value = value
280
+ end
281
+
282
+ def message
283
+ "#{@reference} #{@value.inspect} contains duplicate items"
284
+ end
285
+ end
275
286
  end
@@ -5,6 +5,10 @@ class OpenAPIParser::SchemaValidator
5
5
  # @param [Object] value
6
6
  # @param [OpenAPIParser::Schemas::Schema] schema
7
7
  def coerce_and_validate(value, schema, **keyword_args)
8
+ if value.nil? && schema.nullable
9
+ return [value, nil]
10
+ end
11
+
8
12
  # if any schema return error, it's not valida all of value
9
13
  remaining_keys = value.kind_of?(Hash) ? value.keys : []
10
14
  nested_additional_properties = false
@@ -3,6 +3,9 @@ class OpenAPIParser::SchemaValidator
3
3
  # @param [Object] value
4
4
  # @param [OpenAPIParser::Schemas::Schema] schema
5
5
  def coerce_and_validate(value, schema, **_keyword_args)
6
+ if value.nil? && schema.nullable
7
+ return [value, nil]
8
+ end
6
9
  if schema.discriminator
7
10
  return validate_discriminator_schema(schema.discriminator, value)
8
11
  end
@@ -8,6 +8,9 @@ class OpenAPIParser::SchemaValidator
8
8
  value, err = validate_max_min_items(value, schema)
9
9
  return [nil, err] if err
10
10
 
11
+ value, err = validate_unique_items(value, schema)
12
+ return [nil, err] if err
13
+
11
14
  # array type have an schema in items property
12
15
  items_schema = schema.items
13
16
  coerced_values = value.map do |v|
@@ -28,5 +31,11 @@ class OpenAPIParser::SchemaValidator
28
31
 
29
32
  [value, nil]
30
33
  end
34
+
35
+ def validate_unique_items(value, schema)
36
+ return [nil, OpenAPIParser::NotUniqueItems.new(value, schema.object_reference)] if schema.uniqueItems && value.length != value.uniq.length
37
+
38
+ [value, nil]
39
+ end
31
40
  end
32
41
  end
@@ -14,7 +14,7 @@ class OpenAPIParser::SchemaValidator
14
14
 
15
15
  def validate_discriminator_schema(discriminator, value, parent_discriminator_schemas: [])
16
16
  property_name = discriminator.property_name
17
- unless (property_name && value.key?(property_name))
17
+ if property_name.nil? || !value.key?(property_name)
18
18
  return [nil, OpenAPIParser::NotExistDiscriminatorPropertyName.new(discriminator.property_name, value, discriminator.object_reference)]
19
19
  end
20
20
  mapping_key = value[property_name]
@@ -3,6 +3,9 @@ class OpenAPIParser::SchemaValidator
3
3
  # @param [Object] value
4
4
  # @param [OpenAPIParser::Schemas::Schema] schema
5
5
  def coerce_and_validate(value, schema, **_keyword_args)
6
+ if value.nil? && schema.nullable
7
+ return [value, nil]
8
+ end
6
9
  if schema.discriminator
7
10
  return validate_discriminator_schema(schema.discriminator, value)
8
11
  end
@@ -75,11 +75,15 @@ class OpenAPIParser::SchemaValidator
75
75
  return [value, nil] unless schema.format == 'date'
76
76
 
77
77
  begin
78
- Date.strptime(value, "%Y-%m-%d")
78
+ parsed_date = Date.iso8601(value)
79
79
  rescue ArgumentError
80
80
  return [nil, OpenAPIParser::InvalidDateFormat.new(value, schema.object_reference)]
81
81
  end
82
82
 
83
+ unless parsed_date.strftime('%Y-%m-%d') == value
84
+ return [nil, OpenAPIParser::InvalidDateFormat.new(value, schema.object_reference)]
85
+ end
86
+
83
87
  return [value, nil]
84
88
  end
85
89
 
@@ -2,7 +2,7 @@ class OpenAPIParser::SchemaValidator
2
2
  class UnspecifiedTypeValidator < Base
3
3
  # @param [Object] value
4
4
  def coerce_and_validate(value, _schema, **_keyword_args)
5
- value
5
+ [value, nil]
6
6
  end
7
7
  end
8
8
  end
@@ -1,18 +1,18 @@
1
- require_relative 'schema_validators/options'
2
- require_relative 'schema_validators/enumable'
3
- require_relative 'schema_validators/minimum_maximum'
4
- require_relative 'schema_validators/base'
5
- require_relative 'schema_validators/string_validator'
6
- require_relative 'schema_validators/integer_validator'
7
- require_relative 'schema_validators/float_validator'
8
- require_relative 'schema_validators/boolean_validator'
9
- require_relative 'schema_validators/object_validator'
10
- require_relative 'schema_validators/array_validator'
11
- require_relative 'schema_validators/any_of_validator'
12
- require_relative 'schema_validators/all_of_validator'
13
- require_relative 'schema_validators/one_of_validator'
14
- require_relative 'schema_validators/nil_validator'
15
- require_relative 'schema_validators/unspecified_type_validator'
1
+ require_relative 'schema_validator/options'
2
+ require_relative 'schema_validator/enumable'
3
+ require_relative 'schema_validator/minimum_maximum'
4
+ require_relative 'schema_validator/base'
5
+ require_relative 'schema_validator/string_validator'
6
+ require_relative 'schema_validator/integer_validator'
7
+ require_relative 'schema_validator/float_validator'
8
+ require_relative 'schema_validator/boolean_validator'
9
+ require_relative 'schema_validator/object_validator'
10
+ require_relative 'schema_validator/array_validator'
11
+ require_relative 'schema_validator/any_of_validator'
12
+ require_relative 'schema_validator/all_of_validator'
13
+ require_relative 'schema_validator/one_of_validator'
14
+ require_relative 'schema_validator/nil_validator'
15
+ require_relative 'schema_validator/unspecified_type_validator'
16
16
 
17
17
  class OpenAPIParser::SchemaValidator
18
18
  # validate value by schema
@@ -16,4 +16,5 @@ module OpenAPIParser::Schemas
16
16
  class Schema < Base; end
17
17
  class Components < Base; end
18
18
  class Header < Base; end
19
+ class Info < Base; end
19
20
  end
@@ -0,0 +1,6 @@
1
+ module OpenAPIParser::Schemas
2
+ class Info < Base
3
+
4
+ openapi_attr_values :title, :version
5
+ end
6
+ end
@@ -29,6 +29,10 @@ module OpenAPIParser::Schemas
29
29
  # @return [Components, nil]
30
30
  openapi_attr_object :components, Components, reference: false
31
31
 
32
+ # @!attribute [r] info
33
+ # @return [Info, nil]
34
+ openapi_attr_object :info, Info, reference: false
35
+
32
36
  # @return [OpenAPIParser::RequestOperation, nil]
33
37
  def request_operation(http_method, request_path)
34
38
  OpenAPIParser::RequestOperation.create(http_method, request_path, @path_item_finder, @config)
@@ -15,3 +15,4 @@ require_relative 'schemas/components'
15
15
  require_relative 'schemas/media_type'
16
16
  require_relative 'schemas/schema'
17
17
  require_relative 'schemas/header'
18
+ require_relative 'schemas/info'
@@ -1,3 +1,3 @@
1
1
  module OpenAPIParser
2
- VERSION = '1.0.0'.freeze
2
+ VERSION = '2.1.0'.freeze
3
3
  end
@@ -43,13 +43,16 @@ module OpenAPIParser
43
43
  def load_uri(uri, config:, schema_registry:)
44
44
  # Open-uri doesn't open file scheme uri, so we try to open file path directly
45
45
  # File scheme uri which points to a remote file is not supported.
46
+ uri_path = uri.path
47
+ raise "file not found" if uri_path.nil?
48
+
46
49
  content = if uri.scheme == 'file'
47
- open(uri.path)&.read
50
+ open(uri_path)&.read
48
51
  elsif uri.is_a?(OpenURI::OpenRead)
49
52
  uri.open()&.read
50
53
  end
51
54
 
52
- extension = Pathname.new(uri.path).extname
55
+ extension = Pathname.new(uri_path).extname
53
56
  load_hash(parse_file(content, extension), config: config, uri: uri, schema_registry: schema_registry)
54
57
  end
55
58
 
@@ -85,6 +88,7 @@ module OpenAPIParser
85
88
  end
86
89
 
87
90
  def parse_json(content)
91
+ raise "json content is nil" unless content
88
92
  JSON.parse(content)
89
93
  end
90
94
 
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
12
12
  spec.description = 'parser for OpenAPI 3.0 or later'
13
13
  spec.homepage = 'https://github.com/ota42y/openapi_parser'
14
14
  spec.license = 'MIT'
15
- spec.required_ruby_version = ">= 2.6.0"
15
+ spec.required_ruby_version = ">= 2.7.0"
16
16
 
17
17
  # Specify which files should be added to the gem when it is released.
18
18
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
@@ -2,6 +2,8 @@
2
2
  module OpenAPIParser
3
3
  class Config
4
4
  @config: untyped
5
+ @request_validator_options: OpenAPIParser::SchemaValidator::Options
6
+ @response_validate_options: OpenAPIParser::SchemaValidator::ResponseValidateOptions
5
7
  alias request_body_options request_validator_options
6
8
  alias path_params_options request_validator_options
7
9
 
@@ -0,0 +1,22 @@
1
+ module OpenAPIParser
2
+ class OpenAPIError < StandardError
3
+ def initialize: (untyped reference) -> untyped
4
+ end
5
+
6
+ class ValidateError < OpenAPIError
7
+ def initialize: (untyped data, (String | nil) type, untyped reference) -> untyped
8
+ def message: -> String
9
+
10
+ def self.build_error_result: (Object value, OpenAPIParser::Schemas::Schema schema) -> [nil, OpenAPIParser::ValidateError]
11
+ end
12
+
13
+ class NotExistDiscriminatorMappedSchema < OpenAPIError
14
+ def initialize: (untyped mapped_schema_reference, untyped reference) -> untyped
15
+ def message: -> String
16
+ end
17
+
18
+ class NotExistDiscriminatorPropertyName < OpenAPIError
19
+ def initialize: (untyped mapped_schema_reference, untyped value, untyped reference) -> untyped
20
+ def message: -> String
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ class OpenAPIParser::ReferenceExpander
2
+ def self.expand: (OpenAPIParser::Schemas::OpenAPI openapi, untyped validate_references) -> nil
3
+ end
@@ -4,14 +4,15 @@ module OpenAPIParser
4
4
  class Base
5
5
  @coerce_value: bool | nil
6
6
 
7
- def initialize: (OpenAPIParser::SchemaValidator::Validatable validatable, (bool | nil) coerce_value) -> untyped
8
7
  attr_reader validatable: OpenAPIParser::SchemaValidator::Validatable
9
- def coerce_and_validate: (Object _value, OpenAPIParser::Schemas::Schema _schema, **untyped) -> bot
8
+
9
+ def initialize: (OpenAPIParser::SchemaValidator::Validatable validatable, (bool | nil) coerce_value) -> untyped
10
+ def coerce_and_validate: (Object _value, OpenAPIParser::Schemas::Schema _schema, **untyped) -> [untyped, (ValidateError | NotExistDiscriminatorMappedSchema | nil)]
10
11
  def validate_discriminator_schema: (
11
12
  OpenAPIParser::Schemas::Discriminator discriminator,
12
13
  Hash[String, bot] value,
13
14
  ?parent_discriminator_schemas: Array[OpenAPIParser::Schemas::Schema]
14
- ) -> [Object | nil, OpenAPIParser::validate_error]
15
+ ) -> [Object | nil, OpenAPIParser::OpenAPIError]
15
16
  end
16
17
  end
17
18
  end
data/sig/types.rbs CHANGED
@@ -6,3 +6,8 @@ module OpenURI
6
6
  end
7
7
  end
8
8
 
9
+
10
+ module Psych
11
+ def self.safe_load: (untyped content, untyped parmitted_classes) -> Hash[bot, bot]
12
+ end
13
+
data/sig/wip_types.rbs CHANGED
@@ -62,6 +62,3 @@ class OpenAPIParser::Schemas::Discriminator < OpenAPIParser::Schemas::Base
62
62
  attr_reader property_name: (String | nil)
63
63
  attr_reader mapping: Hash[String, String]
64
64
  end
65
-
66
- class OpenAPIParser::OpenAPIError
67
- end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openapi_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ota42y
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-02-13 00:00:00.000000000 Z
11
+ date: 2024-04-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -200,27 +200,28 @@ files:
200
200
  - lib/openapi_parser/reference_expander.rb
201
201
  - lib/openapi_parser/request_operation.rb
202
202
  - lib/openapi_parser/schema_validator.rb
203
- - lib/openapi_parser/schema_validators/all_of_validator.rb
204
- - lib/openapi_parser/schema_validators/any_of_validator.rb
205
- - lib/openapi_parser/schema_validators/array_validator.rb
206
- - lib/openapi_parser/schema_validators/base.rb
207
- - lib/openapi_parser/schema_validators/boolean_validator.rb
208
- - lib/openapi_parser/schema_validators/enumable.rb
209
- - lib/openapi_parser/schema_validators/float_validator.rb
210
- - lib/openapi_parser/schema_validators/integer_validator.rb
211
- - lib/openapi_parser/schema_validators/minimum_maximum.rb
212
- - lib/openapi_parser/schema_validators/nil_validator.rb
213
- - lib/openapi_parser/schema_validators/object_validator.rb
214
- - lib/openapi_parser/schema_validators/one_of_validator.rb
215
- - lib/openapi_parser/schema_validators/options.rb
216
- - lib/openapi_parser/schema_validators/string_validator.rb
217
- - lib/openapi_parser/schema_validators/unspecified_type_validator.rb
203
+ - lib/openapi_parser/schema_validator/all_of_validator.rb
204
+ - lib/openapi_parser/schema_validator/any_of_validator.rb
205
+ - lib/openapi_parser/schema_validator/array_validator.rb
206
+ - lib/openapi_parser/schema_validator/base.rb
207
+ - lib/openapi_parser/schema_validator/boolean_validator.rb
208
+ - lib/openapi_parser/schema_validator/enumable.rb
209
+ - lib/openapi_parser/schema_validator/float_validator.rb
210
+ - lib/openapi_parser/schema_validator/integer_validator.rb
211
+ - lib/openapi_parser/schema_validator/minimum_maximum.rb
212
+ - lib/openapi_parser/schema_validator/nil_validator.rb
213
+ - lib/openapi_parser/schema_validator/object_validator.rb
214
+ - lib/openapi_parser/schema_validator/one_of_validator.rb
215
+ - lib/openapi_parser/schema_validator/options.rb
216
+ - lib/openapi_parser/schema_validator/string_validator.rb
217
+ - lib/openapi_parser/schema_validator/unspecified_type_validator.rb
218
218
  - lib/openapi_parser/schemas.rb
219
219
  - lib/openapi_parser/schemas/base.rb
220
220
  - lib/openapi_parser/schemas/classes.rb
221
221
  - lib/openapi_parser/schemas/components.rb
222
222
  - lib/openapi_parser/schemas/discriminator.rb
223
223
  - lib/openapi_parser/schemas/header.rb
224
+ - lib/openapi_parser/schemas/info.rb
224
225
  - lib/openapi_parser/schemas/media_type.rb
225
226
  - lib/openapi_parser/schemas/openapi.rb
226
227
  - lib/openapi_parser/schemas/operation.rb
@@ -236,6 +237,8 @@ files:
236
237
  - openapi_parser.gemspec
237
238
  - sig/openapi_parser.rbs
238
239
  - sig/openapi_parser/config.rbs
240
+ - sig/openapi_parser/errors.rbs
241
+ - sig/openapi_parser/reference_expander.rbs
239
242
  - sig/openapi_parser/schema_validator.rbs
240
243
  - sig/openapi_parser/schema_validators/base.rbs
241
244
  - sig/openapi_parser/schema_validators/options.rbs
@@ -247,7 +250,7 @@ homepage: https://github.com/ota42y/openapi_parser
247
250
  licenses:
248
251
  - MIT
249
252
  metadata: {}
250
- post_install_message:
253
+ post_install_message:
251
254
  rdoc_options: []
252
255
  require_paths:
253
256
  - lib
@@ -255,15 +258,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
255
258
  requirements:
256
259
  - - ">="
257
260
  - !ruby/object:Gem::Version
258
- version: 2.6.0
261
+ version: 2.7.0
259
262
  required_rubygems_version: !ruby/object:Gem::Requirement
260
263
  requirements:
261
264
  - - ">="
262
265
  - !ruby/object:Gem::Version
263
266
  version: '0'
264
267
  requirements: []
265
- rubygems_version: 3.3.3
266
- signing_key:
268
+ rubygems_version: 3.1.2
269
+ signing_key:
267
270
  specification_version: 4
268
271
  summary: OpenAPI3 parser
269
272
  test_files: []