Module: Webspicy::Web::Openapi::Utils
- Included in:
- Reporter
- Defined in:
- lib/webspicy/web/openapi/utils.rb
Instance Method Summary collapse
- #actual_body_schema(service) ⇒ Object
- #actual_input_schema(service) ⇒ Object
- #actual_output_schema(test_case, counterexample) ⇒ Object
- #actual_parameters_schema(service) ⇒ Object
- #base_path_for(specification) ⇒ Object
- #base_request_body_for(service) ⇒ Object
- #base_request_example_for(test_case) ⇒ Object
- #base_request_response_for(invocation) ⇒ Object
- #base_verb_for(test_case) ⇒ Object
- #content_type_for(service) ⇒ Object
- #downcase_verb(service) ⇒ Object
- #empty_schema?(schema) ⇒ Boolean
- #extract_request_body_info_for(service) ⇒ Object
- #into_service_request_body(service, x) ⇒ Object
- #into_service_responses(service, x) ⇒ Object
- #into_service_verb(service, x) ⇒ Object
- #into_specification_path(specification, x) ⇒ Object
- #parameters_for(service, test_case) ⇒ Object
- #standardize(url) ⇒ Object
- #tags_for(specification) ⇒ Object
Instance Method Details
#actual_body_schema(service) ⇒ Object
204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/webspicy/web/openapi/utils.rb', line 204 def actual_body_schema(service) schema = actual_input_schema(service) a_schema = schema a_schema = schema.target if schema.is_a?(Finitio::AliasType) return schema unless a_schema.is_a?(Finitio::HashBasedType) in_url = service.specification.url_placeholders.map(&:to_sym) return schema if in_url.empty? a_schema.allbut(in_url) end |
#actual_input_schema(service) ⇒ Object
187 188 189 |
# File 'lib/webspicy/web/openapi/utils.rb', line 187 def actual_input_schema(service) service.input_schema['Main'] end |
#actual_output_schema(test_case, counterexample) ⇒ Object
179 180 181 182 183 184 185 |
# File 'lib/webspicy/web/openapi/utils.rb', line 179 def actual_output_schema(test_case, counterexample) if counterexample test_case.service.error_schema['Main'] else test_case.service.output_schema['Main'] end end |
#actual_parameters_schema(service) ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/webspicy/web/openapi/utils.rb', line 191 def actual_parameters_schema(service) schema = actual_input_schema(service) a_schema = schema a_schema = schema.target if schema.is_a?(Finitio::AliasType) return schema unless a_schema.is_a?(Finitio::HashBasedType) in_url = service.specification.url_placeholders.map(&:to_sym) return schema if in_url.empty? a_schema.project(in_url) end |
#base_path_for(specification) ⇒ Object
33 34 35 36 37 |
# File 'lib/webspicy/web/openapi/utils.rb', line 33 def base_path_for(specification) into_specification_path(specification, { summary: specification.name.to_s || 'API Specification' }) end |
#base_request_body_for(service) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/webspicy/web/openapi/utils.rb', line 71 def base_request_body_for(service) schema, content_type = extract_request_body_info_for(service) return {} unless schema into_service_request_body(service, { required: true, content: { content_type => { schema: schema.to_json_schema, }.compact, }, }) end |
#base_request_example_for(test_case) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/webspicy/web/openapi/utils.rb', line 85 def base_request_example_for(test_case) schema, content_type = extract_request_body_info_for(service) return {} unless schema value = test_case.params in_url = service.specification.url_placeholders value = value.reject{|k,v| in_url.include?(k) } if value.is_a?(Hash) example = { description: test_case.description, value: value, } into_service_request_body(service, { required: true, content: { content_type => { examples: { test_case.description => example } }.compact, }, }) end |
#base_request_response_for(invocation) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/webspicy/web/openapi/utils.rb', line 110 def base_request_response_for(invocation) test_case = invocation.test_case service = invocation.service content_type = content_type_for(service) verb = downcase_verb(service) content = nil unless invocation.is_empty_response? schema = actual_output_schema(test_case, false) example = invocation.loaded_output content = { content_type => { schema: schema&.to_json_schema, examples: { test_case.description => { description: test_case.description, value: example, }, }, }.compact } end into_service_responses(service, { invocation.response_code.to_s => { description: '', content: content, }.compact }) end |
#base_verb_for(test_case) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/webspicy/web/openapi/utils.rb', line 39 def base_verb_for(test_case) service = test_case.service verb_defn = { summary: service.name, description: service.description, tags: (service.specification).map{|s| s[:name] }, parameters: parameters_for(service, test_case), } verb_defn = service.conditions.inject(verb_defn) do |memo, p| if p.respond_to?(:contribute_to_openapi_verb) p.contribute_to_openapi_verb(memo) else memo end end into_service_verb(service, verb_defn) end |
#content_type_for(service) ⇒ Object
226 227 228 229 230 231 232 |
# File 'lib/webspicy/web/openapi/utils.rb', line 226 def content_type_for(service) if service.method.downcase == 'post_form' 'application/x-www-form-urlencoded' else 'application/json' end end |
#downcase_verb(service) ⇒ Object
222 223 224 |
# File 'lib/webspicy/web/openapi/utils.rb', line 222 def downcase_verb(service) service.method.downcase.gsub(/_form$/, '') end |
#empty_schema?(schema) ⇒ Boolean
234 235 236 237 |
# File 'lib/webspicy/web/openapi/utils.rb', line 234 def empty_schema?(schema) schema = schema.target if schema.is_a?(Finitio::AliasType) schema.is_a?(Finitio::HashBasedType) && schema.heading.empty? end |
#extract_request_body_info_for(service) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/webspicy/web/openapi/utils.rb', line 59 def extract_request_body_info_for(service) verb = downcase_verb(service) return nil if ['get', 'options', 'head'].include?(verb) schema = actual_body_schema(service) return nil if empty_schema?(schema) content_type = content_type_for(service) [schema, content_type] end |
#into_service_request_body(service, x) ⇒ Object
19 20 21 22 23 |
# File 'lib/webspicy/web/openapi/utils.rb', line 19 def into_service_request_body(service, x) into_service_verb(service, { requestBody: x.compact, }) end |
#into_service_responses(service, x) ⇒ Object
25 26 27 28 29 |
# File 'lib/webspicy/web/openapi/utils.rb', line 25 def into_service_responses(service, x) into_service_verb(service, { responses: x.compact, }) end |
#into_service_verb(service, x) ⇒ Object
12 13 14 15 16 17 |
# File 'lib/webspicy/web/openapi/utils.rb', line 12 def into_service_verb(service, x) verb = downcase_verb(service) into_specification_path(service.specification, { verb => x.compact, }) end |
#into_specification_path(specification, x) ⇒ Object
6 7 8 9 10 |
# File 'lib/webspicy/web/openapi/utils.rb', line 6 def into_specification_path(specification, x) { standardize(specification.url) => x.compact, } end |
#parameters_for(service, test_case) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/webspicy/web/openapi/utils.rb', line 152 def parameters_for(service, test_case) schema = actual_parameters_schema(service) if schema.is_a?(Finitio::HashBasedType) schema.heading.map do |attr| { in: 'path', name: attr.name, description: attr.[:description], schema: attr.to_json_schema, required: true, example: test_case.params[attr.name.to_s] }.compact end else params = service.specification.url_placeholders.map{|p| { in: 'path', name: p, schema: { type: 'string' }, required: true, example: test_case.params[attr.name.to_s] } } params.empty? ? nil : params end end |
#standardize(url) ⇒ Object
217 218 219 220 |
# File 'lib/webspicy/web/openapi/utils.rb', line 217 def standardize(url) url = url.gsub(/\/$/, '') if url =~ /\/$/ url end |
#tags_for(specification) ⇒ Object
141 142 143 144 145 146 147 148 149 150 |
# File 'lib/webspicy/web/openapi/utils.rb', line 141 def (specification) tag = specification&.openapi&.[](:tag) || specification.name return [] unless tag.is_a?(String) return [] if tag.empty? [{ name: tag.gsub(/\n/, ' ').strip }] end |