@@ -10,11 +10,12 @@ module Openapi3Parser
1010 # @attr_reader [Source] root_source
1111 # @attr_reader [Array<String>] warnings
1212 # @attr_reader [Boolean] emit_warnings
13+ # rubocop:disable Metrics/ClassLength
1314 class Document
1415 extend Forwardable
1516 include Enumerable
1617
17- attr_reader :openapi_version , :root_source , :warnings , : emit_warnings
18+ attr_reader :openapi_version , :root_source , :emit_warnings
1819
1920 # A collection of the openapi versions that are supported
2021 SUPPORTED_OPENAPI_VERSIONS = %w[ 3.0 3.1 ] . freeze
@@ -92,7 +93,8 @@ def initialize(source_input, emit_warnings: true)
9293 @reference_registry = ReferenceRegistry . new
9394 @root_source = Source . new ( source_input , self , reference_registry )
9495 @emit_warnings = emit_warnings
95- @warnings = [ ]
96+ @build_warnings = [ ]
97+ @unsupported_schema_dialects = Set . new
9698 @openapi_version = determine_openapi_version ( root_source . data [ "openapi" ] )
9799 @build_in_progress = false
98100 @built = false
@@ -162,15 +164,35 @@ def node_at(pointer, relative_to = nil)
162164 look_up_pointer ( pointer , relative_to , root )
163165 end
164166
167+ # An array of any warnings enountered in the initialisation / validation
168+ # of the document. Reflects warnings related to this gems ability to parse
169+ # the document.
170+ #
171+ # @return [Array<String>]
172+ def warnings
173+ @warnings ||= begin
174+ factory . errors # ensure factory has completed validation
175+ @build_warnings . freeze
176+ end
177+ end
178+
165179 # @return [String]
166180 def inspect
167181 %{#{ self . class . name } (openapi_version: #{ openapi_version } , } +
168182 %{root_source: #{ root_source . inspect } )}
169183 end
170184
185+ # :nodoc:
186+ def unsupported_schema_dialect ( schema_dialect )
187+ return if @build_warnings . frozen? || unsupported_schema_dialects . include? ( schema_dialect )
188+
189+ unsupported_schema_dialects << schema_dialect
190+ add_warning ( "Unsupported schema dialect (#{ schema_dialect } ), it may not parse or validate correctly." )
191+ end
192+
171193 private
172194
173- attr_reader :reference_registry , :built , :build_in_progress
195+ attr_reader :reference_registry , :built , :build_in_progress , :unsupported_schema_dialects , :build_warnings
174196
175197 def look_up_pointer ( pointer , relative_pointer , subject )
176198 merged_pointer = Source ::Pointer . merge_pointers ( relative_pointer ,
@@ -179,8 +201,8 @@ def look_up_pointer(pointer, relative_pointer, subject)
179201 end
180202
181203 def add_warning ( text )
182- warn ( "Warning: #{ text } - disable these by opening a document with emit_warnings: false" ) if emit_warnings
183- @warnings << text
204+ warn ( "Warning: #{ text } Disable these warnings by opening a document with emit_warnings: false. " ) if emit_warnings
205+ @build_warnings << text
184206 end
185207
186208 def build
@@ -190,7 +212,6 @@ def build
190212 context = NodeFactory ::Context . root ( root_source . data , root_source )
191213 @factory = NodeFactory ::Openapi . new ( context )
192214 reference_registry . freeze
193- @warnings . freeze
194215 @build_in_progress = false
195216 @built = true
196217 end
@@ -225,4 +246,5 @@ def reference_factories
225246 reference_registry . factories . reject { |f | f . context . source . root? }
226247 end
227248 end
249+ # rubocop:enable Metrics/ClassLength
228250end
0 commit comments