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].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_headers_schema(headers_object) ⇒ Object
- #build_parameter_schema(parameters) ⇒ Object
- #build_parameters_schema(parameters) ⇒ Object
- #build_requests(path:, request_method:, operation_object:, parameters:) ⇒ Object
- #build_responses(responses:, request:) ⇒ 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.
19 20 21 22 23 24 25 26 |
# File 'lib/openapi_first/builder.rb', line 19 def initialize(contents, filepath:, config:) @schemer_configuration = JSONSchemer.configuration.clone @schemer_configuration. = (contents, filepath) @schemer_configuration.insert_property_defaults = true @config = config @contents = RefResolver.for(contents, filepath:) end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
28 29 30 |
# File 'lib/openapi_first/builder.rb', line 28 def config @config end |
Class Method Details
.build_router(contents, filepath:, config:) ⇒ Object
Builds a router from a resolved OpenAPI document.
15 16 17 |
# File 'lib/openapi_first/builder.rb', line 15 def self.build_router(contents, filepath:, config:) new(contents, filepath:, config:).router end |
Instance Method Details
#build_headers_schema(headers_object) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/openapi_first/builder.rb', line 174 def build_headers_schema(headers_object) return unless headers_object&.any? properties = {} required = [] headers_object.each do |name, header| schema = header['schema'] next if name.casecmp('content-type').zero? properties[name] = schema if schema required << name if header['required'] end { 'properties' => properties, 'required' => required } end |
#build_parameter_schema(parameters) ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/openapi_first/builder.rb', line 101 def build_parameter_schema(parameters) schema = build_parameters_schema(parameters) JSONSchemer.schema(schema, configuration: schemer_configuration, after_property_validation: config.hooks[:after_request_parameter_property_validation]) end |
#build_parameters_schema(parameters) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/openapi_first/builder.rb', line 192 def build_parameters_schema(parameters) return unless parameters properties = {} required = [] parameters.each do |parameter| schema = parameter['schema'] name = parameter['name'] properties[name] = schema if schema required << name if parameter['required'] end { 'properties' => properties, 'required' => required } end |
#build_requests(path:, request_method:, operation_object:, parameters:) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/openapi_first/builder.rb', line 109 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_responses(responses:, request:) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/openapi_first/builder.rb', line 140 def build_responses(responses:, request:) return [] unless responses responses.flat_map do |status, response_object| headers = response_object['headers']&.resolved headers_schema = JSONSchemer::Schema.new( build_headers_schema(headers), configuration: schemer_configuration ) response_object['content']&.map do |content_type, content_object| content_schema = content_object['schema'].schema(configuration: schemer_configuration) Response.new(status:, headers:, headers_schema:, content_type:, content_schema:, key: [request.key, status, content_type].join(':')) end || Response.new(status:, headers:, headers_schema:, content_type: nil, content_schema: nil, key: [request.key, status, nil].join(':')) end end |
#detect_meta_schema(document, filepath) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/openapi_first/builder.rb', line 31 def (document, filepath) # Copied from JSONSchemer 🙇🏻♂️ version = document['openapi'] case version when /\A3\.1\.\d+\z/ @document_schema = JSONSchemer.openapi31_document document.fetch('jsonSchemaDialect') { JSONSchemer::OpenAPI31::BASE_URI.to_s } when /\A3\.0\.\d+\z/ @document_schema = JSONSchemer.openapi30_document JSONSchemer::OpenAPI30::BASE_URI.to_s else raise Error, "Unsupported OpenAPI version #{version.inspect} #{filepath}" end end |
#group_parameters(parameter_definitions) ⇒ Object
165 166 167 168 169 170 171 172 |
# File 'lib/openapi_first/builder.rb', line 165 def group_parameters(parameter_definitions) result = {} parameter_definitions&.each do |parameter| (result[parameter['in'].to_sym] ||= []) << parameter end result[:header]&.reject! { IGNORED_HEADER_PARAMETERS.include?(_1['name']) } result end |
#parse_parameters(parameters) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/openapi_first/builder.rb', line 79 def parse_parameters(parameters) grouped_parameters = group_parameters(parameters) ParsedParameters.new( query: grouped_parameters[:query], path: grouped_parameters[:path], cookie: grouped_parameters[:cookie], header: 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
131 132 133 134 135 136 137 138 |
# File 'lib/openapi_first/builder.rb', line 131 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
93 94 95 96 97 98 99 |
# File 'lib/openapi_first/builder.rb', line 93 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
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/openapi_first/builder.rb', line 46 def router # rubocop:disable Metrics/MethodLength router = OpenapiFirst::Router.new @contents.fetch('paths').each do |path, path_item_object| path_parameters = resolve_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 = resolve_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 |