open_api_import 0.5.5 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/open_api_import.rb +68 -12
  3. metadata +8 -8
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d6aa027f9f436fa544d6abb6a501ef9fd6b45fd9955e4fc1a8167be0f86d99f2
4
- data.tar.gz: 3a8ebb0c1582303b5c3e4d501628f23bb74a6c586108dafbe7ef14b8b05242cd
3
+ metadata.gz: dd409540237e0b7a67a323e92d46254cf63aefe515773cbee86a4a758eee1b47
4
+ data.tar.gz: 458b9577143635e64f136d31aafff98c62a9b2f77c057f7d6cf19c340a5c85fe
5
5
  SHA512:
6
- metadata.gz: 1453d6c95a5c3d5155293e7f30621895b6e32b37361d1cbfa21ccae76a3e1a0001eaff30cfd8903da917dd45a43f9cc689563d6813ded330079012521cd986d4
7
- data.tar.gz: fc8fc5a426f4d922eaff47c5c56d05108336248542babac5da2447f65811cc0290ae19ebec3a862f04255a28209f1a1144cd426a57fcf419342300573e61c955
6
+ metadata.gz: 4c07b1576d2bae97f39a0ba5ee9e1b6742180cefb6629d7b54462af1c2ee3b67e8e2e2a648164b79de562b6b757967aadef4564b2d0deeaae48e7627de47a4c9
7
+ data.tar.gz: 2380b32911e136fd69f7d7d90cdc6f1123ce76e3bf3f135b8d66e061eb0fde86b830583fbf8795414eb52eb608f96721e9dd80074bd741ccefb854506b54fc03
@@ -254,7 +254,7 @@ class OpenApiImport
254
254
  cont[:parameters] = [] unless cont.key?(:parameters)
255
255
  cont[:parameters] << {in: 'body', schema: cont[:requestBody][:content][:'application/json'][:schema] }
256
256
  end
257
-
257
+ data_examples_all_of = false
258
258
  if cont.key?(:parameters) && cont[:parameters].is_a?(Array)
259
259
  cont[:parameters].each do |p|
260
260
  if p.keys.include?(:schema) and p[:schema].include?(:type)
@@ -280,13 +280,14 @@ class OpenApiImport
280
280
  params_required << p[:name] if p[:required].to_s=="true"
281
281
  description_parameters << "# #{p[:name]}: (#{type}) #{"(required)" if p[:required].to_s=="true"} #{p[:description]}"
282
282
  elsif p[:in] == "body"
283
+
283
284
  if p.keys.include?(:schema)
284
285
  if p[:schema].key?(:oneOf)
285
286
  bodies = p[:schema][:oneOf]
286
287
  elsif p[:schema].key?(:anyOf)
287
288
  bodies = p[:schema][:anyOf]
288
289
  elsif p[:schema].key?(:allOf)
289
- bodies = p[:schema][:allOf]
290
+ data_examples_all_of, bodies = get_data_all_of_bodies(p)
290
291
  else
291
292
  bodies = [p[:schema]]
292
293
  end
@@ -300,13 +301,25 @@ class OpenApiImport
300
301
  end
301
302
 
302
303
  if body.keys.include?(:properties) and body[:properties].size > 0
304
+
303
305
  body[:properties].each { |dpk, dpv|
304
306
  if dpv.keys.include?(:example)
305
307
  valv = dpv[:example].to_s
306
308
  else
307
309
  if dpv.type == "object"
308
- valv = "{}"
309
- else
310
+ if dpv.key?(:properties)
311
+ valv = get_examples(dpv[:properties]).join("\n")
312
+ else
313
+ valv = "{}"
314
+ end
315
+ elsif dpv.type == 'array'
316
+ if dpv.key?(:items)
317
+ valv = get_examples({dpk => dpv}, :only_value)
318
+ valv = valv.join("\n")
319
+ else
320
+ valv = "[]"
321
+ end
322
+ else
310
323
  valv = ""
311
324
  end
312
325
  end
@@ -338,7 +351,11 @@ class OpenApiImport
338
351
  params_data << "#{dpk}: #{valv}"
339
352
  }
340
353
  if params_data.size > 0
341
- data_examples << params_data
354
+ if data_examples_all_of == true and data_examples.size > 0
355
+ data_examples[0]+=params_data
356
+ else
357
+ data_examples << params_data
358
+ end
342
359
  params_data = []
343
360
  end
344
361
  end
@@ -541,10 +558,10 @@ class OpenApiImport
541
558
 
542
559
  class << self
543
560
  # Retrieve the examples from the properties hash
544
- private def get_examples(properties)
561
+ private def get_examples(properties, type=:key_value)
545
562
  #todo: consider using this method also to get data examples
546
563
  example = []
547
- example << "{" unless properties.empty?
564
+ example << "{" unless properties.empty? or type==:only_value
548
565
  properties.each do |prop, val|
549
566
  if val.key?(:properties) and !val.key?(:example) and !val.key?(:type)
550
567
  val[:type]='object'
@@ -566,15 +583,31 @@ class OpenApiImport
566
583
  when "boolean"
567
584
  example << " #{prop.to_sym}: true, "
568
585
  when "array"
586
+ if val.key?(:items) and val[:items].size==1 and val[:items].is_a?(Hash) and val[:items].key?(:type)
587
+ val[:items][:enum]=[val[:items][:type]]
588
+ end
589
+
569
590
  if val.key?(:items) and val[:items].key?(:enum)
570
- if val[:items][:enum][0].is_a?(String)
571
- example << " #{prop.to_sym}: \"" + val[:items][:enum].sample + "\", "
591
+ if type==:only_value
592
+ if val[:items][:enum][0].is_a?(String)
593
+ example << " [\"" + val[:items][:enum].sample + "\"] "
594
+ else
595
+ example << " [" + val[:items][:enum].sample + "] "
596
+ end
572
597
  else
573
- example << " #{prop.to_sym}: " + val[:items][:enum].sample + ", "
598
+ if val[:items][:enum][0].is_a?(String)
599
+ example << " #{prop.to_sym}: [\"" + val[:items][:enum].sample + "\"], "
600
+ else
601
+ example << " #{prop.to_sym}: [" + val[:items][:enum].sample + "], "
602
+ end
574
603
  end
575
604
  else
576
605
  #todo: differ between response examples and data examples
577
- example << " #{prop.to_sym}: " + get_response_examples({schema: val}).join("\n") + ", "
606
+ if type == :only_value
607
+ example << get_response_examples({schema: val}).join("\n")
608
+ else
609
+ example << " #{prop.to_sym}: " + get_response_examples({schema: val}).join("\n") + ", "
610
+ end
578
611
  end
579
612
  when "object"
580
613
  #todo: differ between response examples and data examples
@@ -590,7 +623,7 @@ class OpenApiImport
590
623
  end
591
624
  end
592
625
  end
593
- example << "}" unless properties.empty?
626
+ example << "}" unless properties.empty? or type==:only_value
594
627
  example
595
628
  end
596
629
 
@@ -691,5 +724,28 @@ class OpenApiImport
691
724
  end
692
725
  return response_example
693
726
  end
727
+
728
+
729
+ private def get_data_all_of_bodies(p)
730
+ bodies = []
731
+ data_examples_all_of = false
732
+ if p.is_a?(Array)
733
+ q = p
734
+ elsif p.key?(:schema) and p[:schema].key?(:allOf)
735
+ q = p[:schema][:allOf]
736
+ else
737
+ q =[p]
738
+ end
739
+ q.each do |pt|
740
+ if pt.is_a?(Hash) and pt.key?(:allOf)
741
+ #bodies += pt[:allOf]
742
+ bodies += get_data_all_of_bodies(pt[:allOf])[1]
743
+ data_examples_all_of = true
744
+ else
745
+ bodies << pt
746
+ end
747
+ end
748
+ return data_examples_all_of, bodies
749
+ end
694
750
  end
695
751
  end
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.5.5
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mario Ruiz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-02-22 00:00:00.000000000 Z
11
+ date: 2019-02-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oas_parser
@@ -16,20 +16,20 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.15'
19
+ version: '0.16'
20
20
  - - ">="
21
21
  - !ruby/object:Gem::Version
22
- version: 0.15.1
22
+ version: 0.16.0
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
27
  - - "~>"
28
28
  - !ruby/object:Gem::Version
29
- version: '0.15'
29
+ version: '0.16'
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
- version: 0.15.1
32
+ version: 0.16.0
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: rufo
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -59,7 +59,7 @@ dependencies:
59
59
  version: '1.9'
60
60
  - - ">="
61
61
  - !ruby/object:Gem::Version
62
- version: 1.9.0
62
+ version: 1.9.1
63
63
  type: :runtime
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
@@ -69,7 +69,7 @@ dependencies:
69
69
  version: '1.9'
70
70
  - - ">="
71
71
  - !ruby/object:Gem::Version
72
- version: 1.9.0
72
+ version: 1.9.1
73
73
  - !ruby/object:Gem::Dependency
74
74
  name: rspec
75
75
  requirement: !ruby/object:Gem::Requirement