openapi_parser 2.4.0 → 2.6.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 (55) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yaml +2 -3
  3. data/CHANGELOG.md +43 -0
  4. data/README.md +44 -1
  5. data/lib/openapi_parser/concerns/media_type_selectable.rb +18 -0
  6. data/lib/openapi_parser/concerns/schema_loader/creator.rb +7 -2
  7. data/lib/openapi_parser/concerns/schema_loader/hash_body_loader.rb +1 -1
  8. data/lib/openapi_parser/config.rb +5 -0
  9. data/lib/openapi_parser/errors.rb +31 -0
  10. data/lib/openapi_parser/schema_validator/any_of_validator.rb +1 -1
  11. data/lib/openapi_parser/schema_validator/base.rb +35 -9
  12. data/lib/openapi_parser/schema_validator/float_validator.rb +4 -0
  13. data/lib/openapi_parser/schema_validator/integer_validator.rb +19 -1
  14. data/lib/openapi_parser/schema_validator/multiple_of.rb +26 -0
  15. data/lib/openapi_parser/schema_validator/nil_validator.rb +4 -0
  16. data/lib/openapi_parser/schema_validator/one_of_validator.rb +1 -1
  17. data/lib/openapi_parser/schema_validator.rb +45 -7
  18. data/lib/openapi_parser/schemas/components.rb +4 -0
  19. data/lib/openapi_parser/schemas/discriminator.rb +4 -0
  20. data/lib/openapi_parser/schemas/media_type.rb +4 -0
  21. data/lib/openapi_parser/schemas/openapi.rb +40 -3
  22. data/lib/openapi_parser/schemas/path_item.rb +32 -6
  23. data/lib/openapi_parser/schemas/request_body.rb +1 -1
  24. data/lib/openapi_parser/schemas/response.rb +5 -1
  25. data/lib/openapi_parser/schemas/schema.rb +16 -1
  26. data/lib/openapi_parser/spec_validator/rule.rb +24 -3
  27. data/lib/openapi_parser/spec_validator/rules/additional_operations_before_32.rb +26 -0
  28. data/lib/openapi_parser/spec_validator/rules/allow_empty_value_deprecation.rb +26 -0
  29. data/lib/openapi_parser/spec_validator/rules/boolean_schema_in_30.rb +26 -0
  30. data/lib/openapi_parser/spec_validator/rules/cookie_style_before_32.rb +26 -0
  31. data/lib/openapi_parser/spec_validator/rules/default_mapping_before_32.rb +29 -0
  32. data/lib/openapi_parser/spec_validator/rules/defs_in_30.rb +25 -0
  33. data/lib/openapi_parser/spec_validator/rules/example_value_fields_before_32.rb +48 -0
  34. data/lib/openapi_parser/spec_validator/rules/media_types_before_32.rb +24 -0
  35. data/lib/openapi_parser/spec_validator/rules/query_method_before_32.rb +26 -0
  36. data/lib/openapi_parser/spec_validator/rules/querystring_before_32.rb +26 -0
  37. data/lib/openapi_parser/spec_validator/rules/response_summary_before_32.rb +25 -0
  38. data/lib/openapi_parser/spec_validator/rules/security_scheme_fields_before_32.rb +41 -0
  39. data/lib/openapi_parser/spec_validator/rules/self_before_32.rb +20 -0
  40. data/lib/openapi_parser/spec_validator/rules/server_name_before_32.rb +35 -0
  41. data/lib/openapi_parser/spec_validator/rules/streaming_fields_before_32.rb +56 -0
  42. data/lib/openapi_parser/spec_validator/rules/tag_fields_before_32.rb +34 -0
  43. data/lib/openapi_parser/spec_validator/rules/xml_attribute_deprecation.rb +29 -0
  44. data/lib/openapi_parser/spec_validator/rules/xml_node_type_before_32.rb +29 -0
  45. data/lib/openapi_parser/spec_validator/rules/xml_wrapped_deprecation.rb +29 -0
  46. data/lib/openapi_parser/spec_validator.rb +38 -0
  47. data/lib/openapi_parser/version.rb +1 -1
  48. data/lib/openapi_parser.rb +5 -4
  49. data/sig/openapi_parser/config.rbs +1 -0
  50. data/sig/openapi_parser/schema_validator.rbs +1 -0
  51. data/sig/openapi_parser/schema_validators/base.rbs +4 -2
  52. data/sig/openapi_parser/spec_validator.rbs +85 -0
  53. data/sig/openapi_parser.rbs +5 -3
  54. data/sig/wip_types.rbs +6 -0
  55. metadata +21 -1
@@ -5,11 +5,14 @@
5
5
 
6
6
  module OpenAPIParser::Schemas
7
7
  class OpenAPI < Base
8
- def initialize(raw_schema, config, uri: nil, schema_registry: {})
8
+ # @param [OpenAPIParser::Schemas::OpenAPI, nil] referrer the document whose $ref loads this one
9
+ def initialize(raw_schema, config, uri: nil, schema_registry: {}, referrer: nil)
10
+ # set before super: child objects consult use_3_2_features? while they're built
11
+ @config = config
12
+ @referrer = referrer
9
13
  super('#', nil, self, raw_schema)
10
14
  @find_object_cache = {}
11
15
  @path_item_finder = OpenAPIParser::PathItemFinder.new(paths) if paths # invalid definition
12
- @config = config
13
16
  @uri = uri
14
17
  @schema_registry = schema_registry
15
18
 
@@ -33,6 +36,33 @@ module OpenAPIParser::Schemas
33
36
  Gem::Version.new(openapi).release
34
37
  end
35
38
 
39
+ # Whether OpenAPI 3.1 runtime behavior applies where 3.0 and 3.1 define
40
+ # the same thing differently: the document declares 3.1 or later. A
41
+ # referenced file that declares no version follows the document that
42
+ # loaded it.
43
+ # @return [Boolean]
44
+ def use_3_1_features?
45
+ version = openapi_version
46
+ return version >= Gem::Version.new('3.1') if version
47
+ return @referrer.use_3_1_features? if openapi.nil? && @referrer
48
+
49
+ false
50
+ end
51
+
52
+ # Whether OpenAPI 3.2 runtime behavior applies: the document declares 3.2
53
+ # or later, or the allow_3_2_features config is set. A referenced file
54
+ # that declares no version follows the document that loaded it.
55
+ # @return [Boolean]
56
+ def use_3_2_features?
57
+ return true if @config.allow_3_2_features
58
+
59
+ version = openapi_version
60
+ return version >= Gem::Version.new('3.2') if version
61
+ return @referrer.use_3_2_features? if openapi.nil? && @referrer
62
+
63
+ false
64
+ end
65
+
36
66
  # @!attribute [r] paths
37
67
  # @return [Paths, nil]
38
68
  openapi_attr_object :paths, Paths, reference: false
@@ -53,8 +83,15 @@ module OpenAPIParser::Schemas
53
83
  # @return [String, nil] dialect URI for embedded JSON Schemas (OpenAPI 3.1+)
54
84
  openapi_attr_value :json_schema_dialect, schema_key: :jsonSchemaDialect
55
85
 
86
+ # @!attribute [r] self_uri
87
+ # @return [String, nil] the document's own URI, `$self` (OpenAPI 3.2+)
88
+ openapi_attr_value :self_uri, schema_key: :'$self'
89
+
56
90
  # @return [OpenAPIParser::RequestOperation, nil]
57
91
  def request_operation(http_method, request_path)
92
+ # 3.1: `paths` is optional (a document may have only `webhooks`), so there is nothing to find
93
+ return nil unless @path_item_finder
94
+
58
95
  OpenAPIParser::RequestOperation.create(http_method, request_path, @path_item_finder, @config)
59
96
  end
60
97
 
@@ -67,7 +104,7 @@ module OpenAPIParser::Schemas
67
104
  loaded = @schema_registry[resolved_uri]
68
105
  return loaded if loaded
69
106
 
70
- OpenAPIParser.load_uri(resolved_uri, config: @config, schema_registry: @schema_registry)
107
+ OpenAPIParser.load_uri(resolved_uri, config: @config, schema_registry: @schema_registry, referrer: self)
71
108
  end
72
109
 
73
110
  private
@@ -3,20 +3,46 @@
3
3
 
4
4
  module OpenAPIParser::Schemas
5
5
  class PathItem < Base
6
+ # `query` is an OpenAPI 3.2 addition
7
+ STANDARD_METHODS = %w[get put post delete options head patch trace query].freeze
8
+ PRE_3_2_METHODS = (STANDARD_METHODS - %w[query]).freeze
9
+
6
10
  openapi_attr_values :summary, :description
7
11
 
8
- openapi_attr_objects :get, :put, :post, :delete, :options, :head, :patch, :trace, Operation
12
+ openapi_attr_objects :get, :put, :post, :delete, :options, :head, :patch, :trace, :query, Operation
9
13
  openapi_attr_list_object :parameters, Parameter, reference: true
10
14
 
11
- # @return [Operation]
15
+ # @!attribute [r] additional_operations
16
+ # @return [Hash{String => Operation}, nil] operations for non-standard HTTP methods (OpenAPI 3.2+)
17
+ openapi_attr_hash_object :additional_operations, Operation, reference: false, schema_key: :additionalOperations
18
+
19
+ # @return [Operation, nil]
12
20
  def operation(method)
13
- public_send(method)
14
- rescue NoMethodError
15
- nil
21
+ # before 3.2: exact-case standard methods only, as it always was
22
+ unless root.use_3_2_features?
23
+ return PRE_3_2_METHODS.include?(method.to_s) ? public_send(method.to_s) : nil
24
+ end
25
+
26
+ method_name = method.to_s.downcase
27
+ return public_send(method_name) if STANDARD_METHODS.include?(method_name)
28
+
29
+ additional_operation(method.to_s)
16
30
  end
17
31
 
18
32
  def set_path_item_to_operation
19
- [:get, :put, :post, :delete, :options, :head, :patch, :trace].each{ |method| operation(method)&.set_parent_path_item(self)}
33
+ STANDARD_METHODS.each { |method| operation(method)&.set_parent_path_item(self) }
34
+ additional_operations&.each_value { |op| op.set_parent_path_item(self) }
20
35
  end
36
+
37
+ private
38
+
39
+ # additionalOperations keys are method names as sent on the wire
40
+ # (conventionally uppercase); prefer an exact match, else ignore case
41
+ def additional_operation(name)
42
+ operations = additional_operations
43
+ return nil unless operations
44
+
45
+ operations.fetch(name) { operations.find { |key, _| key.casecmp?(name) }&.last }
46
+ end
21
47
  end
22
48
  end
@@ -12,7 +12,7 @@ module OpenAPIParser::Schemas
12
12
 
13
13
  # @!attribute [r] content
14
14
  # @return [Hash{String => MediaType}, nil] content type to MediaType object
15
- openapi_attr_hash_object :content, MediaType, reference: false
15
+ openapi_attr_hash_object :content, MediaType, reference: OpenAPIParser::MediaTypeSelectable::CONTENT_REFERENCE
16
16
 
17
17
  # @param [String] content_type
18
18
  # @param [Hash] params
@@ -6,9 +6,13 @@ module OpenAPIParser::Schemas
6
6
 
7
7
  openapi_attr_values :description
8
8
 
9
+ # @!attribute [r] summary
10
+ # @return [String, nil] short summary of the response (OpenAPI 3.2+)
11
+ openapi_attr_value :summary
12
+
9
13
  # @!attribute [r] content
10
14
  # @return [Hash{String => MediaType}, nil] content_type to MediaType hash
11
- openapi_attr_hash_object :content, MediaType, reference: false
15
+ openapi_attr_hash_object :content, MediaType, reference: OpenAPIParser::MediaTypeSelectable::CONTENT_REFERENCE
12
16
 
13
17
  # @!attribute [r] headers
14
18
  # @return [Hash{String => Header}, nil] header string to Header
@@ -1,4 +1,3 @@
1
- # TODO: support 'not' because I need check reference...
2
1
  # TODO: support 'xml', 'externalDocs'
3
2
  # TODO: support extended property
4
3
 
@@ -96,6 +95,10 @@ module OpenAPIParser::Schemas
96
95
  # @return [Array<Schema, Reference>, nil]
97
96
  openapi_attr_list_object :any_of, Schema, reference: true, schema_key: :anyOf
98
97
 
98
+ # @!attribute [r] not
99
+ # @return [Schema, nil]
100
+ openapi_attr_object :not, Schema, reference: true
101
+
99
102
  # @!attribute [r] items
100
103
  # @return [Schema, nil]
101
104
  openapi_attr_object :items, Schema, reference: true
@@ -112,6 +115,10 @@ module OpenAPIParser::Schemas
112
115
  # @return [Hash{String => Schema}, nil]
113
116
  openapi_attr_hash_object :properties, Schema, reference: true
114
117
 
118
+ # @!attribute [r] defs
119
+ # @return [Hash{String => Schema}, nil] reusable schemas for `$ref` (OpenAPI 3.1+)
120
+ openapi_attr_hash_object :defs, Schema, reference: true, schema_key: :$defs
121
+
115
122
  # @!attribute [r] discriminator
116
123
  # @return [Discriminator, nil]
117
124
  openapi_attr_object :discriminator, Discriminator
@@ -125,5 +132,13 @@ module OpenAPIParser::Schemas
125
132
  def additional_properties
126
133
  @additional_properties.nil? ? true : @additional_properties
127
134
  end
135
+
136
+ # 3.1: a boolean schema (`true` / `false`) has no keywords to load.
137
+ def load_data
138
+ return super if raw_schema.is_a?(::Hash)
139
+
140
+ @_openapi_all_child_objects = {}
141
+ nil
142
+ end
128
143
  end
129
144
  end
@@ -46,12 +46,33 @@ module OpenAPIParser
46
46
  def each_schema(root, &block)
47
47
  return enum_for(:each_schema, root) unless block
48
48
 
49
- visited = {}
50
- walk(root, visited) do |node|
51
- yield node if node.is_a?(OpenAPIParser::Schemas::Schema)
49
+ each_node(root, OpenAPIParser::Schemas::Schema, &block)
50
+ end
51
+
52
+ # yields every parsed object reachable from root that is one of klasses
53
+ def each_node(root, *klasses)
54
+ walk(root, {}) do |node|
55
+ yield node if klasses.any? { |klass| node.is_a?(klass) }
56
+ end
57
+ end
58
+
59
+ # security schemes are not modeled by the parse layer, so rules
60
+ # about them read the raw `components.securitySchemes` map
61
+ def each_security_scheme(root)
62
+ components = root.raw_schema.is_a?(Hash) ? root.raw_schema['components'] : nil
63
+ schemes = components.is_a?(Hash) ? components['securitySchemes'] : nil
64
+ return unless schemes.is_a?(Hash)
65
+
66
+ schemes.each do |name, scheme|
67
+ yield(name, scheme) if scheme.is_a?(Hash)
52
68
  end
53
69
  end
54
70
 
71
+ # escapes a map key for use in a violation path, matching object_reference
72
+ def escape_reference(key)
73
+ key.to_s.gsub('~', '~0').gsub('/', '~1')
74
+ end
75
+
55
76
  def walk(node, visited, &block)
56
77
  return unless node.respond_to?(:_openapi_all_child_objects)
57
78
  return if visited[node.object_id]
@@ -0,0 +1,26 @@
1
+ module OpenAPIParser
2
+ class SpecValidator
3
+ module Rules
4
+ # `additionalOperations` is a 3.2 Path Item addition for non-standard
5
+ # HTTP methods. The parse layer accepts it permissively; this rule
6
+ # reports the version mismatch.
7
+ class AdditionalOperationsBefore32 < Rule
8
+ def check(root)
9
+ return [] unless version_before?('3.2')
10
+
11
+ violations = []
12
+ each_node(root, OpenAPIParser::Schemas::PathItem) do |node|
13
+ raw = node.raw_schema
14
+ next unless raw.is_a?(Hash) && raw.key?('additionalOperations')
15
+
16
+ violations << violation(
17
+ path: "#{node.object_reference}/additionalOperations",
18
+ message: '`additionalOperations` is a 3.2 Path Item addition; earlier documents have no such field',
19
+ )
20
+ end
21
+ violations
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ module OpenAPIParser
2
+ class SpecValidator
3
+ module Rules
4
+ # 3.2 deprecates `allowEmptyValue` on the Parameter Object (3.1 only
5
+ # called it NOT RECOMMENDED). The field is still allowed but
6
+ # discouraged, so we report it as a violation on 3.2 documents.
7
+ class AllowEmptyValueDeprecation < Rule
8
+ def check(root)
9
+ return [] unless version_at_least?('3.2')
10
+
11
+ violations = []
12
+ each_node(root, OpenAPIParser::Schemas::Parameter) do |node|
13
+ raw = node.raw_schema
14
+ next unless raw.is_a?(Hash) && raw.key?('allowEmptyValue')
15
+
16
+ violations << violation(
17
+ path: "#{node.object_reference}/allowEmptyValue",
18
+ message: '`allowEmptyValue` on a Parameter Object is deprecated in 3.2',
19
+ )
20
+ end
21
+ violations
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ module OpenAPIParser
2
+ class SpecValidator
3
+ module Rules
4
+ # A boolean schema (`true` / `false` in place of a schema object) comes
5
+ # from JSON Schema 2020-12, which 3.1 adopts. 3.0 allows a boolean only
6
+ # for `additionalProperties`, which is not loaded as a Schema, so any
7
+ # Schema whose raw value is a boolean is a spec mismatch in 3.0.
8
+ class BooleanSchemaIn30 < Rule
9
+ def check(root)
10
+ return [] unless version_before?('3.1')
11
+
12
+ violations = []
13
+ each_schema(root) do |schema|
14
+ next unless schema.raw_schema == true || schema.raw_schema == false
15
+
16
+ violations << violation(
17
+ path: schema.object_reference,
18
+ message: 'a boolean schema is a 3.1 addition (from JSON Schema 2020-12); 3.0 allows a boolean only for `additionalProperties`',
19
+ )
20
+ end
21
+ violations
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ module OpenAPIParser
2
+ class SpecValidator
3
+ module Rules
4
+ # `style: cookie` is a 3.2 Parameter style for cookie serialization.
5
+ # Runtime validation ignores `style`; this rule reports the version
6
+ # mismatch.
7
+ class CookieStyleBefore32 < Rule
8
+ def check(root)
9
+ return [] unless version_before?('3.2')
10
+
11
+ violations = []
12
+ each_node(root, OpenAPIParser::Schemas::Parameter) do |node|
13
+ raw = node.raw_schema
14
+ next unless raw.is_a?(Hash) && raw['style'] == 'cookie'
15
+
16
+ violations << violation(
17
+ path: "#{node.object_reference}/style",
18
+ message: '`style: cookie` is a 3.2 Parameter style; earlier documents have no such style',
19
+ )
20
+ end
21
+ violations
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,29 @@
1
+ module OpenAPIParser
2
+ class SpecValidator
3
+ module Rules
4
+ # `defaultMapping` on the Discriminator Object is a 3.2 addition. The
5
+ # parse layer accepts it permissively; this rule reports the version
6
+ # mismatch.
7
+ class DefaultMappingBefore32 < Rule
8
+ def check(root)
9
+ return [] unless version_before?('3.2')
10
+
11
+ violations = []
12
+ each_schema(root) do |schema|
13
+ raw = schema.raw_schema
14
+ next unless raw.is_a?(Hash)
15
+
16
+ discriminator = raw['discriminator']
17
+ next unless discriminator.is_a?(Hash) && discriminator.key?('defaultMapping')
18
+
19
+ violations << violation(
20
+ path: "#{schema.object_reference}/discriminator/defaultMapping",
21
+ message: '`defaultMapping` on a Discriminator Object is a 3.2 addition; earlier documents have no such field',
22
+ )
23
+ end
24
+ violations
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,25 @@
1
+ module OpenAPIParser
2
+ class SpecValidator
3
+ module Rules
4
+ # `$defs` is a JSON Schema 2020-12 keyword that 3.1 adopted. 3.0
5
+ # Schema Objects do not define it. The parse layer resolves `$ref`s
6
+ # into it regardless of version; this rule reports the version mismatch.
7
+ class DefsIn30 < Rule
8
+ def check(root)
9
+ return [] unless version_before?('3.1')
10
+
11
+ violations = []
12
+ each_schema(root) do |schema|
13
+ next unless schema.raw_schema.is_a?(Hash) && schema.raw_schema.key?('$defs')
14
+
15
+ violations << violation(
16
+ path: schema.object_reference,
17
+ message: '`$defs` is a 3.1 addition (from JSON Schema 2020-12); 3.0 Schema Objects do not define it',
18
+ )
19
+ end
20
+ violations
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,48 @@
1
+ module OpenAPIParser
2
+ class SpecValidator
3
+ module Rules
4
+ # 3.2 adds `dataValue` and `serializedValue` to the Example Object.
5
+ # Example Objects are not modeled by the parse layer, so this rule
6
+ # inspects the raw `examples` maps on Parameter, Media Type, and
7
+ # Header objects, plus `components.examples`. Media Types under a
8
+ # Parameter or Header `content` and anything under callbacks are not
9
+ # modeled, so examples there are not reached.
10
+ class ExampleValueFieldsBefore32 < Rule
11
+ NEW_FIELDS = %w[dataValue serializedValue].freeze
12
+
13
+ def check(root)
14
+ return [] unless version_before?('3.2')
15
+
16
+ violations = []
17
+ node_classes = [
18
+ OpenAPIParser::Schemas::Parameter,
19
+ OpenAPIParser::Schemas::MediaType,
20
+ OpenAPIParser::Schemas::Header,
21
+ OpenAPIParser::Schemas::Components,
22
+ ]
23
+ each_node(root, *node_classes) do |node|
24
+ raw = node.raw_schema
25
+ next unless raw.is_a?(Hash)
26
+
27
+ examples = raw['examples']
28
+ next unless examples.is_a?(Hash)
29
+
30
+ examples.each do |name, example|
31
+ next unless example.is_a?(Hash)
32
+
33
+ NEW_FIELDS.each do |field|
34
+ next unless example.key?(field)
35
+
36
+ violations << violation(
37
+ path: "#{node.object_reference}/examples/#{escape_reference(name)}/#{field}",
38
+ message: "`#{field}` on an Example Object is a 3.2 addition; earlier documents have no such field",
39
+ )
40
+ end
41
+ end
42
+ end
43
+ violations
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,24 @@
1
+ module OpenAPIParser
2
+ class SpecValidator
3
+ module Rules
4
+ # `components.mediaTypes` is a 3.2 addition. The parse layer accepts
5
+ # it permissively; this rule reports the version mismatch.
6
+ class MediaTypesBefore32 < Rule
7
+ def check(root)
8
+ return [] unless version_before?('3.2')
9
+
10
+ components = root.components
11
+ return [] unless components
12
+
13
+ raw = components.raw_schema
14
+ return [] unless raw.is_a?(Hash) && raw.key?('mediaTypes')
15
+
16
+ [violation(
17
+ path: "#{components.object_reference}/mediaTypes",
18
+ message: '`components.mediaTypes` is a 3.2 addition; earlier documents should not declare it',
19
+ )]
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,26 @@
1
+ module OpenAPIParser
2
+ class SpecValidator
3
+ module Rules
4
+ # `query` is a 3.2 Path Item addition for the HTTP QUERY method. The
5
+ # parse layer accepts it permissively; this rule reports the version
6
+ # mismatch.
7
+ class QueryMethodBefore32 < Rule
8
+ def check(root)
9
+ return [] unless version_before?('3.2')
10
+
11
+ violations = []
12
+ each_node(root, OpenAPIParser::Schemas::PathItem) do |node|
13
+ raw = node.raw_schema
14
+ next unless raw.is_a?(Hash) && raw.key?('query')
15
+
16
+ violations << violation(
17
+ path: "#{node.object_reference}/query",
18
+ message: 'the `query` operation is a 3.2 Path Item addition; earlier documents have no such field',
19
+ )
20
+ end
21
+ violations
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ module OpenAPIParser
2
+ class SpecValidator
3
+ module Rules
4
+ # `in: querystring` is a 3.2 Parameter location that treats the whole
5
+ # query string as one value described by `content`. Runtime validation
6
+ # does not support it yet; this rule reports the version mismatch.
7
+ class QuerystringBefore32 < Rule
8
+ def check(root)
9
+ return [] unless version_before?('3.2')
10
+
11
+ violations = []
12
+ each_node(root, OpenAPIParser::Schemas::Parameter) do |node|
13
+ raw = node.raw_schema
14
+ next unless raw.is_a?(Hash) && raw['in'] == 'querystring'
15
+
16
+ violations << violation(
17
+ path: "#{node.object_reference}/in",
18
+ message: '`in: querystring` is a 3.2 Parameter location; earlier documents have no such location',
19
+ )
20
+ end
21
+ violations
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,25 @@
1
+ module OpenAPIParser
2
+ class SpecValidator
3
+ module Rules
4
+ # 3.2 adds `summary` to the Response Object. The parse layer accepts it
5
+ # permissively; this rule reports the version mismatch.
6
+ class ResponseSummaryBefore32 < Rule
7
+ def check(root)
8
+ return [] unless version_before?('3.2')
9
+
10
+ violations = []
11
+ each_node(root, OpenAPIParser::Schemas::Response) do |node|
12
+ raw = node.raw_schema
13
+ next unless raw.is_a?(Hash) && raw.key?('summary')
14
+
15
+ violations << violation(
16
+ path: "#{node.object_reference}/summary",
17
+ message: '`summary` on a Response Object is a 3.2 addition; earlier documents have no such field',
18
+ )
19
+ end
20
+ violations
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,41 @@
1
+ module OpenAPIParser
2
+ class SpecValidator
3
+ module Rules
4
+ # 3.2 adds `deprecated` and `oauth2MetadataUrl` to the Security Scheme
5
+ # Object and the `deviceAuthorization` OAuth flow. Security schemes are
6
+ # not modeled by the parse layer, so this rule inspects the raw
7
+ # `components.securitySchemes` map; schemes given as a `$ref` are not
8
+ # followed.
9
+ class SecuritySchemeFieldsBefore32 < Rule
10
+ NEW_FIELDS = %w[deprecated oauth2MetadataUrl].freeze
11
+
12
+ def check(root)
13
+ return [] unless version_before?('3.2')
14
+
15
+ violations = []
16
+ each_security_scheme(root) do |name, scheme|
17
+ base = "#/components/securitySchemes/#{escape_reference(name)}"
18
+
19
+ NEW_FIELDS.each do |field|
20
+ next unless scheme.key?(field)
21
+
22
+ violations << violation(
23
+ path: "#{base}/#{field}",
24
+ message: "`#{field}` on a Security Scheme Object is a 3.2 addition; earlier documents have no such field",
25
+ )
26
+ end
27
+
28
+ flows = scheme['flows']
29
+ next unless flows.is_a?(Hash) && flows.key?('deviceAuthorization')
30
+
31
+ violations << violation(
32
+ path: "#{base}/flows/deviceAuthorization",
33
+ message: 'the `deviceAuthorization` OAuth flow is a 3.2 addition; earlier documents have no such flow',
34
+ )
35
+ end
36
+ violations
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,20 @@
1
+ module OpenAPIParser
2
+ class SpecValidator
3
+ module Rules
4
+ # `$self` is a 3.2 root-level addition declaring the document's own
5
+ # URI for reference resolution. The parse layer accepts it
6
+ # permissively; this rule reports the version mismatch.
7
+ class SelfBefore32 < Rule
8
+ def check(root)
9
+ return [] unless version_before?('3.2')
10
+ return [] unless root.raw_schema.is_a?(Hash) && root.raw_schema.key?('$self')
11
+
12
+ [violation(
13
+ path: '#/$self',
14
+ message: '`$self` is a 3.2 root-level addition; earlier documents have no such field',
15
+ )]
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,35 @@
1
+ module OpenAPIParser
2
+ class SpecValidator
3
+ module Rules
4
+ # 3.2 adds `name` to the Server Object. Servers are not modeled by
5
+ # the parse layer, so this rule inspects the raw `servers` arrays on
6
+ # the root, Path Item, and Operation objects. Servers under callbacks
7
+ # and Link Objects are not reached.
8
+ class ServerNameBefore32 < Rule
9
+ def check(root)
10
+ return [] unless version_before?('3.2')
11
+
12
+ violations = []
13
+ node_classes = [OpenAPIParser::Schemas::OpenAPI, OpenAPIParser::Schemas::PathItem, OpenAPIParser::Schemas::Operation]
14
+ each_node(root, *node_classes) do |node|
15
+ raw = node.raw_schema
16
+ next unless raw.is_a?(Hash)
17
+
18
+ servers = raw['servers']
19
+ next unless servers.is_a?(Array)
20
+
21
+ servers.each_with_index do |server, index|
22
+ next unless server.is_a?(Hash) && server.key?('name')
23
+
24
+ violations << violation(
25
+ path: "#{node.object_reference}/servers/#{index}/name",
26
+ message: '`name` on a Server Object is a 3.2 addition; earlier documents have no such field',
27
+ )
28
+ end
29
+ end
30
+ violations
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end