|
| 1 | +module LibOpenApiImport |
| 2 | + # Retrieve the response examples from the hash |
| 3 | + private def get_response_examples(v, remove_readonly = false) |
| 4 | + # TODO: take in consideration the case allOf, oneOf... schema.items.allOf[0].properties schema.items.allOf[1].properties |
| 5 | + # example on https://github.com/OAI/OpenAPI-Specification/blob/master/examples/v2.0/yaml/petstore-expanded.yaml |
| 6 | + v = v.dup |
| 7 | + response_example = Array.new() |
| 8 | + # for open api 3.0 with responses schema inside content |
| 9 | + if v.key?(:content) && v[:content].is_a?(Hash) && v[:content].key?(:'application/json') && |
| 10 | + v[:content][:'application/json'].key?(:schema) |
| 11 | + v = v[:content][:'application/json'].dup |
| 12 | + end |
| 13 | + if v.key?(:examples) && v[:examples].is_a?(Hash) && v[:examples].key?(:'application/json') |
| 14 | + if v[:examples][:'application/json'].is_a?(String) |
| 15 | + response_example << v[:examples][:'application/json'] |
| 16 | + elsif v[:examples][:'application/json'].is_a?(Hash) |
| 17 | + exs = v[:examples][:'application/json'].to_s |
| 18 | + exs.gsub!(/:(\w+)=>/, "\n\\1: ") |
| 19 | + response_example << exs |
| 20 | + elsif v[:examples][:'application/json'].is_a?(Array) |
| 21 | + response_example << "[" |
| 22 | + v[:examples][:'application/json'].each do |ex| |
| 23 | + exs = ex.to_s |
| 24 | + if ex.is_a?(Hash) |
| 25 | + exs.gsub!(/:(\w+)=>/, "\n\\1: ") |
| 26 | + end |
| 27 | + response_example << (exs + ", ") |
| 28 | + end |
| 29 | + response_example << "]" |
| 30 | + end |
| 31 | + # for open api 3.0. examples on reponses, for example: api-with-examples.yaml |
| 32 | + elsif v.key?(:content) && v[:content].is_a?(Hash) && v[:content].key?(:'application/json') && |
| 33 | + v[:content][:'application/json'].key?(:examples) |
| 34 | + v[:content][:'application/json'][:examples].each do |tk, tv| |
| 35 | + #todo: for the moment we only take in consideration the first example of response. |
| 36 | + # we need to decide how to manage to do it correctly |
| 37 | + if tv.key?(:value) |
| 38 | + tresp = tv[:value] |
| 39 | + else |
| 40 | + tresp = "" |
| 41 | + end |
| 42 | + if tresp.is_a?(String) |
| 43 | + response_example << tresp |
| 44 | + elsif tresp.is_a?(Hash) |
| 45 | + exs = tresp.to_s |
| 46 | + exs.gsub!(/:(\w+)=>/, "\n\\1: ") |
| 47 | + response_example << exs |
| 48 | + elsif tresp.is_a?(Array) |
| 49 | + response_example << "[" |
| 50 | + tresp.each do |ex| |
| 51 | + exs = ex.to_s |
| 52 | + if ex.is_a?(Hash) |
| 53 | + exs.gsub!(/:(\w+)=>/, "\n\\1: ") |
| 54 | + end |
| 55 | + response_example << (exs + ", ") |
| 56 | + end |
| 57 | + response_example << "]" |
| 58 | + end |
| 59 | + break #only the first one it is considered |
| 60 | + end |
| 61 | + elsif v.key?(:schema) && v[:schema].is_a?(Hash) && |
| 62 | + (v[:schema].key?(:properties) || |
| 63 | + (v[:schema].key?(:items) && v[:schema][:items].key?(:properties)) || |
| 64 | + (v[:schema].key?(:items) && v[:schema][:items].key?(:allOf)) || |
| 65 | + v[:schema].key?(:allOf)) |
| 66 | + properties = {} |
| 67 | + if v[:schema].key?(:properties) |
| 68 | + properties = v[:schema][:properties] |
| 69 | + elsif v[:schema].key?(:allOf) |
| 70 | + v[:schema][:allOf].each do |pr| |
| 71 | + properties.merge!(pr[:properties]) if pr.key?(:properties) |
| 72 | + end |
| 73 | + elsif v[:schema][:items].key?(:properties) |
| 74 | + properties = v[:schema][:items][:properties] |
| 75 | + response_example << "[" |
| 76 | + elsif v[:schema][:items].key?(:allOf) |
| 77 | + v[:schema][:items][:allOf].each do |pr| |
| 78 | + properties.merge!(pr[:properties]) if pr.key?(:properties) |
| 79 | + end |
| 80 | + response_example << "[" |
| 81 | + end |
| 82 | + |
| 83 | + response_example += get_examples(properties, :key_value, remove_readonly) unless properties.empty? |
| 84 | + |
| 85 | + unless response_example.empty? |
| 86 | + if v[:schema].key?(:properties) || v[:schema].key?(:allOf) |
| 87 | + # |
| 88 | + else # array, items |
| 89 | + response_example << "]" |
| 90 | + end |
| 91 | + end |
| 92 | + elsif v.key?(:schema) and v[:schema].key?(:items) and v[:schema][:items].key?(:type) |
| 93 | + # for the case only type supplied but nothing else for the array |
| 94 | + response_example << "[\"#{v[:schema][:items][:type]}\"]" |
| 95 | + end |
| 96 | + response_example.each do |rs| |
| 97 | + #(@type Google) for the case in example the key is something like: @type: |
| 98 | + if rs.match?(/^\s*@\w+:/) |
| 99 | + rs.gsub!(/@(\w+):/, '\'@\1\':') |
| 100 | + end |
| 101 | + end |
| 102 | + return response_example |
| 103 | + end |
| 104 | +end |
0 commit comments