Skip to content

Commit 123b76b

Browse files
committed
Correct handling of minimum and maximum of schema
This fixes an error in OpenAPI 3.0 schema handling where minimum and maximum were flagged as integers when actually they are numeric. It then corrects the configuration of exclusiveMaximum and exclusiveMinimum for OpenAPI 3.1 where JSON Schema 2021 [1] defines these as numeric values compared to the boolean values they held in OpenAPI 3.0 (via JSON Schema 2017 [2]). [1]: https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-validation-00#section-6.2.3 [2]: https://datatracker.ietf.org/doc/html/draft-wright-json-schema-validation-00#section-5.3
1 parent af96354 commit 123b76b

8 files changed

Lines changed: 40 additions & 25 deletions

File tree

json-schema-for-3.1.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ enum: array - in 3.0
4444
const: any type - done
4545
multipleOf: number - in 3.0
4646
maximum: number - in 3.0
47-
exclusiveMaximum: number - in 3.0
47+
exclusiveMaximum: number - done
4848
minimum: number - in 3.0
49-
exclusiveMinimum: number - in 3.0
49+
exclusiveMinimum: number - done
5050
maxLength: integer >= 0 - in 3.0 (missing >= val)
5151
minLength: integer >= 0 - in 3.0
5252
pattern: string - in 3.0

lib/openapi3_parser/node/schema.rb

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,16 @@ def multiple_of
5050
self["multipleOf"]
5151
end
5252

53-
# @return [Integer, nil]
53+
# @return [Numeric, nil]
5454
def maximum
5555
self["maximum"]
5656
end
5757

58-
# @return [Boolean]
59-
def exclusive_maximum?
60-
self["exclusiveMaximum"]
61-
end
62-
63-
# @return [Integer, nil]
58+
# @return [Numeric, nil]
6459
def minimum
6560
self["minimum"]
6661
end
6762

68-
# @return [Boolean]
69-
def exclusive_minimum?
70-
self["exclusiveMinimum"]
71-
end
72-
7363
# @return [Integer, nil]
7464
def max_length
7565
self["maxLength"]

lib/openapi3_parser/node/schema/v3_0.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ class V3_0 < Schema # rubocop:disable Naming/ClassAndModuleCamelCase
1111
def type
1212
self["type"]
1313
end
14+
15+
# @return [Boolean]
16+
def exclusive_maximum?
17+
self["exclusiveMaximum"]
18+
end
19+
20+
# @return [Boolean]
21+
def exclusive_minimum?
22+
self["exclusiveMinimum"]
23+
end
1424
end
1525
end
1626
end

lib/openapi3_parser/node/schema/v3_1.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ def const
2525
self["const"]
2626
end
2727

28+
# @return [Numeric]
29+
def exclusive_maximum
30+
self["exclusiveMaximum"]
31+
end
32+
33+
# @return [Numeric]
34+
def exclusive_minimum
35+
self["exclusiveMinimum"]
36+
end
37+
2838
# @return [Integer, nil]
2939
def max_contains
3040
self["maxContains"]

lib/openapi3_parser/node_factory/schema/common.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ def self.included(base)
1414
base.field "title", input_type: String
1515

1616
base.field "multipleOf", input_type: Numeric
17-
base.field "maximum", input_type: Integer
18-
base.field "exclusiveMaximum", input_type: :boolean, default: false
19-
base.field "minimum", input_type: Integer
20-
base.field "exclusiveMinimum", input_type: :boolean, default: false
17+
base.field "maximum", input_type: Numeric
18+
base.field "minimum", input_type: Numeric
2119
base.field "maxLength", input_type: Integer
2220
base.field "minLength", input_type: Integer, default: 0
2321
base.field "pattern", input_type: String

lib/openapi3_parser/node_factory/schema/v3_0.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ class V3_0 < NodeFactory::Object # rubocop:disable Naming/ClassAndModuleCamelCas
1212
# OpenAPI 3.0 requires a type of String, whereas >= 3.1 is String or Array
1313
field "type", input_type: String
1414

15+
# JSON Schema 2016 has these exclusive fields as booleans whereas
16+
# in JSON Schema 2021 (OpenAPI 3.1) these are numbers
17+
field "exclusiveMaximum", input_type: :boolean, default: false
18+
field "exclusiveMinimum", input_type: :boolean, default: false
19+
1520
validate :items_for_array
1621

1722
def build_node(data, node_context)

lib/openapi3_parser/node_factory/schema/v3_1.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class V3_1 < NodeFactory::Object # rubocop:disable Naming/ClassAndModuleCamelCas
2020
field "$ref", input_type: String, factory: :ref_factory
2121
field "type", factory: :type_factory, validate: :validate_type
2222
field "const"
23+
field "exclusiveMaximum", input_type: Numeric
24+
field "exclusiveMinimum", input_type: Numeric
2325
field "maxContains", input_type: Integer
2426
field "minContains", input_type: Integer, default: 1
2527
# dependentRequired - map with basic validation rules

spec/support/examples/v3.1/changes.yaml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ components:
7979
type: string
8080
exp:
8181
type: string
82-
# Number:
83-
# type: integer
84-
# multipleOf: 5
85-
# maximum: 110
86-
# exclusiveMaximum: 111
87-
# minimum: 10
88-
# exclusiveMinimum: 9
82+
Number:
83+
type: integer
84+
multipleOf: 5
85+
maximum: 110
86+
exclusiveMaximum: 111
87+
minimum: 10
88+
exclusiveMinimum: 9
8989
String:
9090
type: string
9191
maxLength: 10

0 commit comments

Comments
 (0)