openapi_parser 0.2.6 → 0.2.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -0
- data/lib/openapi_parser/errors.rb +23 -0
- data/lib/openapi_parser/schema_validator.rb +1 -1
- data/lib/openapi_parser/schema_validators/any_of_validator.rb +4 -0
- data/lib/openapi_parser/schema_validators/base.rb +18 -0
- data/lib/openapi_parser/schema_validators/one_of_validator.rb +4 -0
- data/lib/openapi_parser/schemas.rb +1 -0
- data/lib/openapi_parser/schemas/classes.rb +1 -0
- data/lib/openapi_parser/schemas/discriminator.rb +11 -0
- data/lib/openapi_parser/schemas/openapi.rb +0 -14
- data/lib/openapi_parser/schemas/schema.rb +5 -1
- data/lib/openapi_parser/version.rb +1 -1
- metadata +2 -2
- data/lib/openapi_parser/concerns/definition_validatable.rb +0 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2dd37ca24b93c63b4ccb98a693c9200f1850fca3349b5e70931aa40771bb31dc
|
4
|
+
data.tar.gz: c7d4e29d589c610554a8873a601d8eba674560267ccdb28a951ec120debbb0a0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a8c5ce03e3a6399403b3018f3fb5b6d3b0ff304989ac83a1c4aedb1a83709285f2a80abf90b25ac99ea5623474f7070239e62a918535f8798de2c73d1527c0fe
|
7
|
+
data.tar.gz: 85dda1a6fff6458ca87f462ede780a933448e05188e4d514c415abee3e36a365ba956e57684748c65103a83321a1760f5433ef9f909fcb5fc2178def664eccc9
|
data/CHANGELOG.md
CHANGED
@@ -43,6 +43,29 @@ module OpenAPIParser
|
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
|
+
class NotExistDiscriminatorMappingTarget < OpenAPIError
|
47
|
+
def initialize(key, reference)
|
48
|
+
super(reference)
|
49
|
+
@key = key
|
50
|
+
end
|
51
|
+
|
52
|
+
def message
|
53
|
+
"discriminator mapping key #{@key} does not exist in #{@reference}"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
class NotExistDiscriminatorPropertyName < OpenAPIError
|
58
|
+
def initialize(key, value, reference)
|
59
|
+
super(reference)
|
60
|
+
@key = key
|
61
|
+
@value = value
|
62
|
+
end
|
63
|
+
|
64
|
+
def message
|
65
|
+
"discriminator propertyName #{@key} does not exist in value #{@value} in #{@reference}"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
46
69
|
class NotOneOf < OpenAPIError
|
47
70
|
def initialize(value, reference)
|
48
71
|
super(reference)
|
@@ -88,7 +88,7 @@ class OpenAPIParser::SchemaValidator
|
|
88
88
|
private
|
89
89
|
|
90
90
|
# @return [OpenAPIParser::SchemaValidator::Base, nil]
|
91
|
-
def validator(value, schema)
|
91
|
+
def validator(value, schema)
|
92
92
|
return any_of_validator if schema.any_of
|
93
93
|
return all_of_validator if schema.all_of
|
94
94
|
return one_of_validator if schema.one_of
|
@@ -3,6 +3,10 @@ class OpenAPIParser::SchemaValidator
|
|
3
3
|
# @param [Object] value
|
4
4
|
# @param [OpenAPIParser::Schemas::Schema] schema
|
5
5
|
def coerce_and_validate(value, schema)
|
6
|
+
if schema.discriminator
|
7
|
+
return validate_discriminator_schema(schema.discriminator, value)
|
8
|
+
end
|
9
|
+
|
6
10
|
# in all schema return error (=true) not any of data
|
7
11
|
schema.any_of.each do |s|
|
8
12
|
coerced, err = validatable.validate_schema(value, s)
|
@@ -17,5 +17,23 @@ class OpenAPIParser::SchemaValidator
|
|
17
17
|
def coerce_and_validate(_value, _schema)
|
18
18
|
raise 'need implement'
|
19
19
|
end
|
20
|
+
|
21
|
+
def validate_discriminator_schema(discriminator, value)
|
22
|
+
unless value.key?(discriminator.property_name)
|
23
|
+
return [nil, OpenAPIParser::NotExistDiscriminatorPropertyName.new(discriminator.property_name, value, discriminator.object_reference)]
|
24
|
+
end
|
25
|
+
mapping_key = value[discriminator.property_name]
|
26
|
+
|
27
|
+
# TODO: it's allowed to have discriminator without mapping, then we need to lookup discriminator.property_name
|
28
|
+
# but the format is not the full path, just model name in the components
|
29
|
+
mapping_target = discriminator.mapping[mapping_key]
|
30
|
+
unless mapping_target
|
31
|
+
return [nil, OpenAPIParser::NotExistDiscriminatorMappingTarget.new(mapping_key, discriminator.object_reference)]
|
32
|
+
end
|
33
|
+
|
34
|
+
# Find object does O(n) search at worst, then caches the result, so this is ok for repeated search
|
35
|
+
resolved_schema = discriminator.root.find_object(mapping_target)
|
36
|
+
validatable.validate_schema(value, resolved_schema)
|
37
|
+
end
|
20
38
|
end
|
21
39
|
end
|
@@ -3,6 +3,10 @@ class OpenAPIParser::SchemaValidator
|
|
3
3
|
# @param [Object] value
|
4
4
|
# @param [OpenAPIParser::Schemas::Schema] schema
|
5
5
|
def coerce_and_validate(value, schema)
|
6
|
+
if schema.discriminator
|
7
|
+
return validate_discriminator_schema(schema.discriminator, value)
|
8
|
+
end
|
9
|
+
|
6
10
|
# if multiple schemas are satisfied, it's not valid
|
7
11
|
result = schema.one_of.one? do |s|
|
8
12
|
_coerced, err = validatable.validate_schema(value, s)
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module OpenAPIParser::Schemas
|
2
|
+
class Discriminator < Base
|
3
|
+
# @!attribute [r] property_name
|
4
|
+
# @return [String, nil]
|
5
|
+
openapi_attr_value :property_name, schema_key: :propertyName
|
6
|
+
|
7
|
+
# @!attribute [r] mapping
|
8
|
+
# @return [Hash{String => String]
|
9
|
+
openapi_attr_value :mapping
|
10
|
+
end
|
11
|
+
end
|
@@ -5,8 +5,6 @@
|
|
5
5
|
|
6
6
|
module OpenAPIParser::Schemas
|
7
7
|
class OpenAPI < Base
|
8
|
-
include OpenAPIParser::DefinitionValidatable
|
9
|
-
|
10
8
|
def initialize(raw_schema, config)
|
11
9
|
super('#', nil, self, raw_schema)
|
12
10
|
@find_object_cache = {}
|
@@ -30,17 +28,5 @@ module OpenAPIParser::Schemas
|
|
30
28
|
def request_operation(http_method, request_path)
|
31
29
|
OpenAPIParser::RequestOperation.create(http_method, request_path, @path_item_finder, @config)
|
32
30
|
end
|
33
|
-
|
34
|
-
# return the definition is valid or not
|
35
|
-
# @return Boolean
|
36
|
-
def valid_definition?
|
37
|
-
openapi_definition_errors.empty?
|
38
|
-
end
|
39
|
-
|
40
|
-
# return definition errors
|
41
|
-
# @return Array
|
42
|
-
def openapi_definition_errors
|
43
|
-
@openapi_definition_errors ||= validate_definitions([])
|
44
|
-
end
|
45
31
|
end
|
46
32
|
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
# TODO: support 'not' because I need check reference...
|
2
|
-
# TODO: support '
|
2
|
+
# TODO: support 'xml', 'externalDocs'
|
3
3
|
# TODO: support extended property
|
4
4
|
|
5
5
|
module OpenAPIParser::Schemas
|
@@ -101,6 +101,10 @@ module OpenAPIParser::Schemas
|
|
101
101
|
# @return [Hash{String => Schema}, nil]
|
102
102
|
openapi_attr_hash_object :properties, Schema, reference: true
|
103
103
|
|
104
|
+
# @!attribute [r] discriminator
|
105
|
+
# @return [Discriminator, nil]
|
106
|
+
openapi_attr_object :discriminator, Discriminator
|
107
|
+
|
104
108
|
# @!attribute [r] additional_properties
|
105
109
|
# @return [Boolean, Schema, Reference, nil]
|
106
110
|
openapi_attr_object :additional_properties, Schema, reference: true, allow_data_type: true, schema_key: :additionalProperties
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: openapi_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ota42y
|
@@ -144,7 +144,6 @@ files:
|
|
144
144
|
- bin/setup
|
145
145
|
- lib/openapi_parser.rb
|
146
146
|
- lib/openapi_parser/concern.rb
|
147
|
-
- lib/openapi_parser/concerns/definition_validatable.rb
|
148
147
|
- lib/openapi_parser/concerns/expandable.rb
|
149
148
|
- lib/openapi_parser/concerns/findable.rb
|
150
149
|
- lib/openapi_parser/concerns/media_type_selectable.rb
|
@@ -188,6 +187,7 @@ files:
|
|
188
187
|
- lib/openapi_parser/schemas/base.rb
|
189
188
|
- lib/openapi_parser/schemas/classes.rb
|
190
189
|
- lib/openapi_parser/schemas/components.rb
|
190
|
+
- lib/openapi_parser/schemas/discriminator.rb
|
191
191
|
- lib/openapi_parser/schemas/header.rb
|
192
192
|
- lib/openapi_parser/schemas/media_type.rb
|
193
193
|
- lib/openapi_parser/schemas/openapi.rb
|