Skip to content

Commit 27f128b

Browse files
committed
Breaking: Create OpenAPI version specific Schema class
With OpenAPI 3.1 the schema object is quite different (conforming to 2012-12 spec of JSON Schema). I think the easiest way to deal with this is to create separate classes for working with it. This starts this process by creating Schema::V3_0 classes which is a rename of the existing Schema classes.
1 parent 12dcc74 commit 27f128b

11 files changed

Lines changed: 402 additions & 377 deletions

File tree

lib/openapi3_parser/node/schema.rb

Lines changed: 1 addition & 241 deletions
Original file line numberDiff line numberDiff line change
@@ -1,248 +1,8 @@
11
# frozen_string_literal: true
22

3-
require "openapi3_parser/node/object"
4-
53
module Openapi3Parser
64
module Node
7-
# @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#schemaObject
8-
# rubocop:disable Metrics/ClassLength
9-
class Schema < Node::Object
10-
# This is used to provide a name for the schema based on it's position in
11-
# an OpenAPI document.
12-
#
13-
# For example it's common to have an OpenAPI document structured like so:
14-
# components:
15-
# schemas:
16-
# Product:
17-
# properties:
18-
# product_id:
19-
# type: string
20-
# description:
21-
# type: string
22-
#
23-
# and there is then implied meaning in the field name of Product, ie
24-
# that schema now represents a product. This data is not easily or
25-
# consistently made available as it is part of the path to the data
26-
# rather than the data itself. Instead the field that would be more
27-
# appropriate would be "title" within a schema.
28-
#
29-
# As this is a common pattern in OpenAPI docs this provides a method
30-
# to look up this contextual name of the schema so it can be referenced
31-
# when working with the document, it only considers a field to be
32-
# name if it is within a group called schemas (as is the case
33-
# in #/components/schemas)
34-
#
35-
# @return [String, nil]
36-
def name
37-
segments = node_context.source_location.pointer.segments
38-
segments[-1] if segments[-2] == "schemas"
39-
end
40-
41-
# @return [String, nil]
42-
def title
43-
self["title"]
44-
end
45-
46-
# @return [Numeric, nil]
47-
def multiple_of
48-
self["multipleOf"]
49-
end
50-
51-
# @return [Integer, nil]
52-
def maximum
53-
self["maximum"]
54-
end
55-
56-
# @return [Boolean]
57-
def exclusive_maximum?
58-
self["exclusiveMaximum"]
59-
end
60-
61-
# @return [Integer, nil]
62-
def minimum
63-
self["minimum"]
64-
end
65-
66-
# @return [Boolean]
67-
def exclusive_minimum?
68-
self["exclusiveMinimum"]
69-
end
70-
71-
# @return [Integer, nil]
72-
def max_length
73-
self["maxLength"]
74-
end
75-
76-
# @return [Integer]
77-
def min_length
78-
self["minLength"]
79-
end
80-
81-
# @return [String, nil]
82-
def pattern
83-
self["pattern"]
84-
end
85-
86-
# @return [Integer, nil]
87-
def max_items
88-
self["maxItems"]
89-
end
90-
91-
# @return [Integer]
92-
def min_items
93-
self["minItems"]
94-
end
95-
96-
# @return [Boolean]
97-
def unique_items?
98-
self["uniqueItems"]
99-
end
100-
101-
# @return [Integer, nil]
102-
def max_properties
103-
self["maxProperties"]
104-
end
105-
106-
# @return [Integer]
107-
def min_properties
108-
self["minProperties"]
109-
end
110-
111-
# @return [Node::Array<String>, nil]
112-
def required
113-
self["required"]
114-
end
115-
116-
# Returns whether a property is a required field or not. Can accept the
117-
# property name or a schema
118-
#
119-
# @param [String, Schema] property
120-
# @return [Boolean]
121-
def requires?(property)
122-
if property.is_a?(Schema)
123-
# compare node_context of objects to ensure references aren't treated
124-
# as equal - only direct properties of this object will pass.
125-
properties.to_h
126-
.select { |k, _| required.to_a.include?(k) }
127-
.any? { |_, schema| schema.node_context == property.node_context }
128-
else
129-
required.to_a.include?(property)
130-
end
131-
end
132-
133-
# @return [Node::Array<Object>, nil]
134-
def enum
135-
self["enum"]
136-
end
137-
138-
# @return [String, nil]
139-
def type
140-
self["type"]
141-
end
142-
143-
# @return [Node::Array<Schema>, nil]
144-
def all_of
145-
self["allOf"]
146-
end
147-
148-
# @return [Node::Array<Schema>, nil]
149-
def one_of
150-
self["oneOf"]
151-
end
152-
153-
# @return [Node::Array<Schema>, nil]
154-
def any_of
155-
self["anyOf"]
156-
end
157-
158-
# @return [Schema, nil]
159-
def not
160-
self["not"]
161-
end
162-
163-
# @return [Schema, nil]
164-
def items
165-
self["items"]
166-
end
167-
168-
# @return [Map<String, Schema>]
169-
def properties
170-
self["properties"]
171-
end
172-
173-
# @return [Boolean]
174-
def additional_properties?
175-
self["additionalProperties"] != false
176-
end
177-
178-
# @return [Schema, nil]
179-
def additional_properties_schema
180-
properties = self["additionalProperties"]
181-
return if [true, false].include?(properties)
182-
183-
properties
184-
end
185-
186-
# @return [String, nil]
187-
def description
188-
self["description"]
189-
end
190-
191-
# @return [String, nil]
192-
def description_html
193-
render_markdown(description)
194-
end
195-
196-
# @return [String, nil]
197-
def format
198-
self["format"]
199-
end
200-
201-
# @return [Any]
202-
def default
203-
self["default"]
204-
end
205-
206-
# @return [Boolean]
207-
def nullable?
208-
self["nullable"]
209-
end
210-
211-
# @return [Discriminator, nil]
212-
def discriminator
213-
self["discriminator"]
214-
end
215-
216-
# @return [Boolean]
217-
def read_only?
218-
self["readOnly"]
219-
end
220-
221-
# @return [Boolean]
222-
def write_only?
223-
self["writeOnly"]
224-
end
225-
226-
# @return [Xml, nil]
227-
def xml
228-
self["xml"]
229-
end
230-
231-
# @return [ExternalDocumentation, nil]
232-
def external_docs
233-
self["externalDocs"]
234-
end
235-
236-
# @return [Any]
237-
def example
238-
self["example"]
239-
end
240-
241-
# @return [Boolean]
242-
def deprecated?
243-
self["deprecated"]
244-
end
5+
module Schema
2456
end
246-
# rubocop:enable Metrics/ClassLength
2477
end
2488
end

0 commit comments

Comments
 (0)