Class: OpenapiFirst::Builder
- Inherits:
-
Object
- Object
- OpenapiFirst::Builder
- Defined in:
- lib/openapi_first/builder.rb
Overview
Constant Summary collapse
- REQUEST_METHODS =
rubocop:disable Metrics/ClassLength
%w[get head post put patch delete trace options query].freeze
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Class Method Summary collapse
-
.build_router(contents, filepath:, config:) ⇒ Object
Builds a router from a resolved OpenAPI document.
Instance Method Summary collapse
- #build_parameter_schema(parameters) ⇒ Object
- #build_requests(path:, request_method:, operation_object:, parameters:) ⇒ Object
- #build_response_headers(headers_object) ⇒ Object
- #build_responses(responses:, request:) ⇒ Object
- #build_schemer_config(filepath:, meta_schema:) ⇒ Object
- #detect_meta_schema(document, filepath) ⇒ Object
- #group_parameters(parameter_definitions) ⇒ Object
-
#initialize(contents, filepath:, config:) ⇒ Builder
constructor
A new instance of Builder.
- #parse_parameters(parameters) ⇒ Object
- #request_without_body(path:, request_method:, parameters:, operation_object:) ⇒ Object
- #resolve_parameters(parameters) ⇒ Object
-
#router ⇒ Object
rubocop:disable Metrics/MethodLength.
Constructor Details
#initialize(contents, filepath:, config:) ⇒ Builder
Returns a new instance of Builder.
26 27 28 29 30 31 |
# File 'lib/openapi_first/builder.rb', line 26 def initialize(contents, filepath:, config:) = (contents, filepath) @schemer_configuration = build_schemer_config(filepath:, meta_schema:) @config = config @contents = RefResolver.for(contents, filepath:) end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
33 34 35 |
# File 'lib/openapi_first/builder.rb', line 33 def config @config end |
Class Method Details
.build_router(contents, filepath:, config:) ⇒ Object
Builds a router from a resolved OpenAPI document.
22 23 24 |
# File 'lib/openapi_first/builder.rb', line 22 def self.build_router(contents, filepath:, config:) new(contents, filepath:, config:).router end |
Instance Method Details
#build_parameter_schema(parameters) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/openapi_first/builder.rb', line 116 def build_parameter_schema(parameters) return unless parameters required = [] schemas = parameters.each_with_object({}) do |parameter, result| schema = parameter['schema'].schema(configuration: schemer_configuration) name = parameter['name']&.value required << name if parameter['required']&.value result[name] = schema if schema end Schema::Hash.new(schemas, required:, configuration: schemer_configuration, after_property_validation: config.hooks[:after_request_parameter_property_validation]) end |
#build_requests(path:, request_method:, operation_object:, parameters:) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/openapi_first/builder.rb', line 131 def build_requests(path:, request_method:, operation_object:, parameters:) content_objects = operation_object.dig('requestBody', 'content') if content_objects.nil? return [ request_without_body(path:, request_method:, parameters:, operation_object:) ] end required_body = operation_object['requestBody']&.resolved&.fetch('required', false) == true content_objects.map do |content_type, content_object| content_schema = content_object['schema'].schema( configuration: schemer_configuration, after_property_validation: config.hooks[:after_request_body_property_validation] ) Request.new(path:, request_method:, parameters:, operation_object: operation_object.resolved, content_type:, content_schema:, required_body:, key: [path, request_method, content_type].join(':')) end end |
#build_response_headers(headers_object) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/openapi_first/builder.rb', line 182 def build_response_headers(headers_object) return if headers_object.nil? result = [] headers_object.each do |name, header| next if header['schema'].nil? next if IGNORED_HEADER_PARAMETERS.include?(name) header = Header.new( name:, schema: header['schema'].schema(configuration: schemer_configuration), required?: header['required']&.value == true, node: header ) result << header end result end |
#build_responses(responses:, request:) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/openapi_first/builder.rb', line 162 def build_responses(responses:, request:) return [] unless responses responses.flat_map do |status, response_object| headers = build_response_headers(response_object['headers']) response_object['content']&.map do |content_type, content_object| content_schema = content_object['schema'].schema(configuration: schemer_configuration) Response.new(status:, headers:, content_type:, content_schema:, key: [request.key, status, content_type].join(':')) end || Response.new(status:, headers:, content_type: nil, content_schema: nil, key: [request.key, status, nil].join(':')) end end |
#build_schemer_config(filepath:, meta_schema:) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/openapi_first/builder.rb', line 36 def build_schemer_config(filepath:, meta_schema:) result = JSONSchemer.configuration.clone dir = (filepath && File.absolute_path(File.dirname(filepath))) || Dir.pwd result.base_uri = URI::File.build({ path: "#{dir}/" }) result.ref_resolver = JSONSchemer::CachedResolver.new do |uri| FileLoader.load(uri.path) end result. = result.insert_property_defaults = true result end |
#detect_meta_schema(document, filepath) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/openapi_first/builder.rb', line 48 def (document, filepath) # Copied from JSONSchemer 🙇🏻♂️ version = document['openapi'] case version when /\A3\.1\.\d+\z/ document.fetch('jsonSchemaDialect') { JSONSchemer::OpenAPI31::BASE_URI.to_s } when /\A3\.0\.\d+\z/ JSONSchemer::OpenAPI30::BASE_URI.to_s else raise Error, "Unsupported OpenAPI version #{version.inspect} #{filepath}" end end |
#group_parameters(parameter_definitions) ⇒ Object
201 202 203 204 205 206 207 208 |
# File 'lib/openapi_first/builder.rb', line 201 def group_parameters(parameter_definitions) result = {} parameter_definitions&.each do |parameter| (result[parameter['in']&.value&.to_sym] ||= []) << parameter end result[:header]&.reject! { IGNORED_HEADER_PARAMETERS.include?(_1['name']&.value) } result end |
#parse_parameters(parameters) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/openapi_first/builder.rb', line 94 def parse_parameters(parameters) grouped_parameters = group_parameters(parameters) ParsedParameters.new( query: resolve_parameters(grouped_parameters[:query]), path: resolve_parameters(grouped_parameters[:path]), cookie: resolve_parameters(grouped_parameters[:cookie]), header: resolve_parameters(grouped_parameters[:header]), query_schema: build_parameter_schema(grouped_parameters[:query]), path_schema: build_parameter_schema(grouped_parameters[:path]), cookie_schema: build_parameter_schema(grouped_parameters[:cookie]), header_schema: build_parameter_schema(grouped_parameters[:header]) ) end |
#request_without_body(path:, request_method:, parameters:, operation_object:) ⇒ Object
153 154 155 156 157 158 159 160 |
# File 'lib/openapi_first/builder.rb', line 153 def request_without_body(path:, request_method:, parameters:, operation_object:) Request.new(path:, request_method:, parameters:, operation_object: operation_object.resolved, content_type: nil, content_schema: nil, required_body: false, key: [path, request_method, nil].join(':')) end |
#resolve_parameters(parameters) ⇒ Object
108 109 110 111 112 113 114 |
# File 'lib/openapi_first/builder.rb', line 108 def resolve_parameters(parameters) parameters&.map do |parameter| result = parameter.resolved result['schema'] = parameter['schema'].resolved result end.to_a end |
#router ⇒ Object
rubocop:disable Metrics/MethodLength
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/openapi_first/builder.rb', line 61 def router # rubocop:disable Metrics/MethodLength router = OpenapiFirst::Router.new @contents.fetch('paths').each do |path, path_item_object| path_parameters = path_item_object['parameters'] || [] path_item_object.resolved.keys.intersection(REQUEST_METHODS).map do |request_method| operation_object = path_item_object[request_method] operation_parameters = operation_object['parameters'] || [] parameters = parse_parameters(operation_parameters.chain(path_parameters)) build_requests(path:, request_method:, operation_object:, parameters:).each do |request| router.add_request( request, request_method:, path:, content_type: request.content_type, allow_empty_content: request.allow_empty_content? ) build_responses(request:, responses: operation_object['responses']).each do |response| router.add_response( response, request_method:, path:, status: response.status, response_content_type: response.content_type ) end end end end router end |