open_api_import 0.10.2 → 0.10.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/open_api_import.rb +61 -25
  3. metadata +8 -14
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 003ae82f9aeea04faa6bcc01766bd607695d3eed5dd09fb3059851a7156e0e76
4
- data.tar.gz: 005db7c9d7a54fd1264c9cd58ea69b68dd085bf44798da3abf4b88cbe6a1ba04
3
+ metadata.gz: 7648a1c4167d4316385baaddeb6ab83f683b148c264c8cee30f706567e2d0d68
4
+ data.tar.gz: fa8f058d9d01a39201f9d4aea2422a82289ef6a755a6a9a16679bec0e4935fa0
5
5
  SHA512:
6
- metadata.gz: b11737afc2530b6ab2a4db94c63138a8791edcd95b2c8b335791162d1ccddd8d9986a40751b6810c7a843aa7df1ad2798fa031ad51f3fd9e69824106dfc64953
7
- data.tar.gz: 61966f66d9d85cf15d01e292adf95723cbe415cbbea2f586feab6bb688204d03ffd30065c9feea631bb3c7e0a5946472400474bbe4cb550854eb93343cc8472d
6
+ metadata.gz: 96bc61aa9048d2d94a099a18d95f388d6fad31276e193b76d001040dd22c0d66b12511c5d41a337e676dd423517aeea30fb8686cf7f8b7d57535ea3aebb4255a
7
+ data.tar.gz: aa57d0978a400e5e31fb8dcdc994f414d5a994b1d521f89163b44d0d980de0ed5b29671d15464c51c2b0d42281afd47f3179314a48a81946c8627e308e030f5d
@@ -150,6 +150,10 @@ class OpenApiImport
150
150
  elsif create_method_name == :operation_id
151
151
  if (name_for_module == :tags or name_for_module == :tags_file) and cont.key?(:tags) and cont[:tags].is_a?(Array) and cont[:tags].size>0
152
152
  metnametmp = cont[:operationId].gsub(/^#{cont[:tags].join}[\s_]*/, '')
153
+ cont[:tags].join.split(' ').each do |tag|
154
+ metnametmp.gsub!(/^#{tag}[\s_]*/i, '')
155
+ end
156
+ metnametmp = met if metnametmp == ''
153
157
  else
154
158
  metnametmp = cont[:operationId]
155
159
  end
@@ -157,6 +161,10 @@ class OpenApiImport
157
161
  else
158
162
  if (name_for_module == :tags or name_for_module == :tags_file) and cont.key?(:tags) and cont[:tags].is_a?(Array) and cont[:tags].size>0
159
163
  method_name = cont[:operationId].gsub(/^#{cont[:tags].join}[\s_]*/, '')
164
+ cont[:tags].join.split(' ').each do |tag|
165
+ method_name.gsub!(/^#{tag}[\s_]*/i, '')
166
+ end
167
+ method_name = met if method_name == ''
160
168
  else
161
169
  method_name = cont[:operationId]
162
170
  end
@@ -337,7 +345,11 @@ class OpenApiImport
337
345
 
338
346
  body[:properties].each { |dpk, dpv|
339
347
  if dpv.keys.include?(:example)
340
- valv = dpv[:example].to_s
348
+ if dpv[:example].is_a?(Array) and dpv.type != 'array'
349
+ valv = dpv[:example][0]
350
+ else
351
+ valv = dpv[:example].to_s
352
+ end
341
353
  else
342
354
  if dpv.type == "object"
343
355
  if dpv.key?(:properties)
@@ -357,32 +369,48 @@ class OpenApiImport
357
369
  end
358
370
  end
359
371
  if dpv.keys.include?(:description)
360
- description_parameters << "# #{dpk}: (#{dpv[:type]}) #{dpv[:description]}"
372
+ description_parameters << "# #{dpk}: (#{dpv[:type]}) #{dpv[:description].split("\n").join("\n#\t\t\t")}"
361
373
  end
362
374
 
363
375
  data_pattern += get_patterns(dpk,dpv)
364
376
  data_pattern.uniq!
377
+ dpkeys = []
378
+ data_pattern.reject! do |dp|
379
+ dpkey = dp.scan(/^'\w+'/)
380
+
381
+ if dpkeys.include?(dpkey)
382
+ true
383
+ else
384
+ dpkeys << dpkey
385
+ false
386
+ end
387
+ end
365
388
 
366
389
  if dpv.keys.include?(:readOnly) and dpv[:readOnly] == true
367
390
  data_read_only << dpk
368
391
  end
369
392
  if dpv.keys.include?(:default)
370
- if dpv.type != "string"
393
+ if dpv[:default].nil?
394
+ data_default << "#{dpk}: nil"
395
+ elsif dpv.type != "string"
371
396
  data_default << "#{dpk}: #{dpv[:default]}"
372
397
  else
373
398
  data_default << "#{dpk}: '#{dpv[:default]}'"
374
399
  end
375
400
  end
376
401
 
377
- if dpv.key?(:type) and dpv[:type].downcase == "string"
378
- valv = '"' + valv + '"'
402
+ #todo: consider check default and insert it
403
+ #todo: remove array from here and add the option to get_examples for the case thisisthekey: ['xxxx']
404
+ if dpv.key?(:type) and dpv[:type]!='array'
405
+ params_data << get_examples({dpk => dpv}, :only_value, true).join
406
+ params_data[-1].chop!.chop! if params_data[-1].to_s[-2..-1]==', '
407
+ params_data.pop if params_data[-1].match?(/^\s*$/im)
379
408
  else
380
- #todo: consider check default and insert it
381
409
  if valv.to_s == ""
382
410
  valv = '"' + valv + '"'
383
411
  end
412
+ params_data << "#{dpk}: #{valv}"
384
413
  end
385
- params_data << "#{dpk}: #{valv}"
386
414
  }
387
415
  if params_data.size > 0
388
416
  if data_examples_all_of == true and data_examples.size > 0
@@ -674,10 +702,14 @@ class OpenApiImport
674
702
  val[:type]='array'
675
703
  end
676
704
  if val.key?(:example)
677
- example << if val[:example].is_a?(String) or val[:example].is_a?(Time)
678
- " #{prop.to_sym}: \"#{val[:example]}\", "
705
+ if val[:example].is_a?(Array) and val.key?(:type) and val[:type]=='string'
706
+ example << " #{prop.to_sym}: \"#{val[:example][0]}\", " # only the first example
679
707
  else
680
- " #{prop.to_sym}: #{val[:example]}, "
708
+ example << if val[:example].is_a?(String) or val[:example].is_a?(Time)
709
+ " #{prop.to_sym}: \"#{val[:example]}\", "
710
+ else
711
+ " #{prop.to_sym}: #{val[:example]}, "
712
+ end
681
713
  end
682
714
  elsif val.key?(:type)
683
715
  format = val[:format]
@@ -695,30 +727,32 @@ class OpenApiImport
695
727
  end
696
728
 
697
729
  if val.key?(:items) and val[:items].key?(:enum)
730
+ #before we were getting in all these cases a random value from the enum, now we are getting the first position by default
731
+ #the reason is to avoid confusion later in case we want to compare two swaggers and verify the changes
698
732
  if type==:only_value
699
733
  if val[:items][:enum][0].is_a?(String)
700
- example << " [\"" + val[:items][:enum].sample + "\"] "
734
+ example << " [\"" + val[:items][:enum][0] + "\"] "
701
735
  else
702
- example << " [" + val[:items][:enum].sample + "] "
736
+ example << " [" + val[:items][:enum][0] + "] "
703
737
  end
704
738
  else
705
739
  if val[:items][:enum][0].is_a?(String)
706
- example << " #{prop.to_sym}: [\"" + val[:items][:enum].sample + "\"], "
740
+ example << " #{prop.to_sym}: [\"" + val[:items][:enum][0] + "\"], "
707
741
  else
708
- example << " #{prop.to_sym}: [" + val[:items][:enum].sample + "], "
742
+ example << " #{prop.to_sym}: [" + val[:items][:enum][0] + "], "
709
743
  end
710
744
  end
711
745
  else
712
746
  #todo: differ between response examples and data examples
713
747
  if type == :only_value
714
- example << get_response_examples({schema: val}).join("\n")
748
+ example << get_response_examples({schema: val}, remove_readonly).join("\n")
715
749
  else
716
- example << " #{prop.to_sym}: " + get_response_examples({schema: val}).join("\n") + ", "
750
+ example << " #{prop.to_sym}: " + get_response_examples({schema: val}, remove_readonly).join("\n") + ", "
717
751
  end
718
752
  end
719
753
  when "object"
720
754
  #todo: differ between response examples and data examples
721
- res_ex = get_response_examples({schema: val})
755
+ res_ex = get_response_examples({schema: val}, remove_readonly)
722
756
  if res_ex.size == 0
723
757
  res_ex = "{ }"
724
758
  else
@@ -736,7 +770,7 @@ class OpenApiImport
736
770
  end
737
771
 
738
772
  # Retrieve the response examples from the hash
739
- private def get_response_examples(v)
773
+ private def get_response_examples(v, remove_readonly = false)
740
774
  # TODO: take in consideration the case allOf, oneOf... schema.items.allOf[0].properties schema.items.allOf[1].properties
741
775
  # example on https://github.com/OAI/OpenAPI-Specification/blob/master/examples/v2.0/yaml/petstore-expanded.yaml
742
776
  v=v.dup
@@ -746,7 +780,6 @@ class OpenApiImport
746
780
  v[:content][:'application/json'].key?(:schema)
747
781
  v=v[:content][:'application/json'].dup
748
782
  end
749
-
750
783
  if v.key?(:examples) && v[:examples].is_a?(Hash) && v[:examples].key?(:'application/json')
751
784
  if v[:examples][:'application/json'].is_a?(String)
752
785
  response_example << v[:examples][:'application/json']
@@ -776,6 +809,7 @@ class OpenApiImport
776
809
  else
777
810
  tresp = ""
778
811
  end
812
+
779
813
  if tresp.is_a?(String)
780
814
  response_example << tresp
781
815
  elsif tresp.is_a?(Hash)
@@ -797,9 +831,9 @@ class OpenApiImport
797
831
  end
798
832
  elsif v.key?(:schema) && v[:schema].is_a?(Hash) &&
799
833
  (v[:schema].key?(:properties) ||
800
- (v[:schema].key?(:items) && v[:schema][:items].key?(:properties)) ||
801
- (v[:schema].key?(:items) && v[:schema][:items].key?(:allOf)) ||
802
- v[:schema].key?(:allOf))
834
+ (v[:schema].key?(:items) && v[:schema][:items].key?(:properties)) ||
835
+ (v[:schema].key?(:items) && v[:schema][:items].key?(:allOf)) ||
836
+ v[:schema].key?(:allOf))
803
837
  properties = {}
804
838
  if v[:schema].key?(:properties)
805
839
  properties = v[:schema][:properties]
@@ -817,7 +851,7 @@ class OpenApiImport
817
851
  response_example << "["
818
852
  end
819
853
 
820
- response_example += get_examples(properties) unless properties.empty?
854
+ response_example += get_examples(properties, :key_value, remove_readonly) unless properties.empty?
821
855
 
822
856
  unless response_example.empty?
823
857
  if v[:schema].key?(:properties) || v[:schema].key?(:allOf)
@@ -837,7 +871,6 @@ class OpenApiImport
837
871
  rs.gsub!(/@(\w+):/,'\'@\1\':')
838
872
  end
839
873
  end
840
-
841
874
  return response_example
842
875
  end
843
876
 
@@ -887,7 +920,6 @@ class OpenApiImport
887
920
  # Get patterns
888
921
  private def get_patterns(dpk, dpv)
889
922
  data_pattern = []
890
-
891
923
  if dpv.keys.include?(:pattern)
892
924
  #todo: control better the cases with back slashes
893
925
  if dpv[:pattern].include?('\\\\/')
@@ -932,6 +964,10 @@ class OpenApiImport
932
964
  data_pattern += get_patterns("#{dpk}.#{dpkk}",dpvv)
933
965
  end
934
966
  end
967
+ elsif dpv[:type] == 'array' and dpv.key?(:items) and dpv[:items].is_a?(Hash) and
968
+ !dpv[:items].key?(:enum) and !dpv[:items].key?(:properties) and dpv[:items].key?(:type)
969
+ #{:title=>"labels", :description=>"Labels specified for the file system", :type=>"array", :items=>{:type=>"string", :enum=>["string"]}}
970
+ data_pattern << "'#{dpk}': [ #{get_patterns('', dpv[:items]).join[4..-1]} ]"
935
971
  elsif dpv[:type] == 'object' and dpv.key?(:properties)
936
972
  dpv[:properties].each do |dpkk,dpvv|
937
973
  if dpk == ''
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: open_api_import
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.2
4
+ version: 0.10.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mario Ruiz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-05 00:00:00.000000000 Z
11
+ date: 2020-10-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oas_parser
@@ -16,48 +16,42 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.24'
19
+ version: '0.25'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0.24'
26
+ version: '0.25'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rufo
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0.11'
33
+ version: '0.12'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0.11'
40
+ version: '0.12'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: nice_hash
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '1.15'
48
- - - ">="
49
- - !ruby/object:Gem::Version
50
- version: 1.15.4
47
+ version: '1.17'
51
48
  type: :runtime
52
49
  prerelease: false
53
50
  version_requirements: !ruby/object:Gem::Requirement
54
51
  requirements:
55
52
  - - "~>"
56
53
  - !ruby/object:Gem::Version
57
- version: '1.15'
58
- - - ">="
59
- - !ruby/object:Gem::Version
60
- version: 1.15.4
54
+ version: '1.17'
61
55
  - !ruby/object:Gem::Dependency
62
56
  name: rspec
63
57
  requirement: !ruby/object:Gem::Requirement