open_api_import 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +0 -2
  3. data/lib/open_api_import.rb +56 -13
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9b391f64d44cb78f95b9e4765f98023d3939752f5802e390fd8b972502061579
4
- data.tar.gz: '08f6d8b7427f2dd8251c220314288b7e36cfdad30d89cf5d83e462640a5679cd'
3
+ metadata.gz: 17c79278e2222735a82cd0665f8f5d616e1dfd347d5b90bd30bf30cafbcefbe3
4
+ data.tar.gz: 8987e63d22bc8ba904c28176847092a9852977a0f096642eb86421f9738a2371
5
5
  SHA512:
6
- metadata.gz: 8e42c5dbde362282c5385b48d21852166dacb9283f5fd0e19a52a45f001a94a92d85df840c462c87252521b3a70f90bc2a1911b3cf55a787127df35bc6ca177f
7
- data.tar.gz: 589ce2df15ad9589fe1900db1a2ff455b7c9e73cd98f07eda7005f79c221f65f2f149bc99ce45246979fe84a5cf993ff2a0809cea481799a99637698c9b57acd
6
+ metadata.gz: e70c1b50b46943027b866fcb05039de1f6bb3349188d37968e014ff4eed41112e99830d3e4caae33933ed7951395efc54351d7e01c9fc11794e49f6c38c14836
7
+ data.tar.gz: 1840e5b30244b7de1ed8fd60c5d62b87de9cd25709ff0506b43f17db994ee3301d27cc555bb1d89d7d395d0ea2a1fefbaf1f1e112aa7a2854704b529130ae65b
data/README.md CHANGED
@@ -8,8 +8,6 @@ Import a Swagger or Open API file and create a Ruby Request Hash file including
8
8
 
9
9
  The Request Hash will include also the pattern (regular expressions) of the fields, parameters, default values...
10
10
 
11
- On this first preliminary version we only fully support Open API v2. Open API v3 is not fully supported yet.
12
-
13
11
  The output of this gem will be following the specification of Request Hashes: https://github.com/MarioRuiz/Request-Hash
14
12
 
15
13
  The Request Hashes generated will be able to be used with any Ruby Http Client and it is adapted even better with nice_http gem: https://github.com/MarioRuiz/nice_http
@@ -60,10 +60,6 @@ class OpenApiImport
60
60
  end
61
61
  if raw[:swagger].to_f < 2.0
62
62
  raise "Unsupported Swagger version. Only versions >= 2.0 are valid."
63
- elsif raw[:swagger].to_f >= 3.0
64
- message = "Take in consideration Open API #{raw[:swagger]} is not fully supported for the moment while we are in preliminary version of open_api_import."
65
- warn message
66
- @logger.warn message
67
63
  end
68
64
 
69
65
  base_host = ""
@@ -228,10 +224,26 @@ class OpenApiImport
228
224
 
229
225
  end
230
226
  end
227
+ # todo: for open api 3.0 add the new Link feature: https://swagger.io/docs/specification/links/
228
+ # todo: for open api 3.0 is not getting the required params in all cases
229
+
230
+ # for the case open api 3 with cont.requestBody.content.'applicatin/json'.schema
231
+ # example: petstore-expanded.yaml operationId=addPet
232
+ if cont.key?(:requestBody) and cont[:requestBody].key?(:content) and
233
+ cont[:requestBody][:content].key?(:'application/json') and cont[:requestBody][:content][:'application/json'].key?(:schema)
234
+ cont[:parameters] = [] unless cont.key?(:parameters)
235
+ cont[:parameters] << {in: 'body', schema: cont[:requestBody][:content][:'application/json'][:schema] }
236
+ end
231
237
 
232
- # todo: for open api 3.0 is not getting the required params
233
238
  if cont.key?(:parameters) && cont[:parameters].is_a?(Array)
234
239
  cont[:parameters].each do |p|
240
+ if p.keys.include?(:schema) and p[:schema].include?(:type)
241
+ type = p[:schema][:type]
242
+ elsif p.keys.include?(:type)
243
+ type = p[:type]
244
+ else
245
+ type = ""
246
+ end
235
247
  if p[:in] == "path"
236
248
  if create_method_name == :operationId
237
249
  param_name = p[:name]
@@ -242,19 +254,13 @@ class OpenApiImport
242
254
  end
243
255
  params_path << param_name
244
256
  #params_required << param_name if p[:required].to_s=="true"
245
-
246
- if p.keys.include?(:description)
247
- description_parameters << "# #{p[:name]}: (#{p[:type]}) #{"(required)" if p[:required].to_s=="true"} #{p[:description]}"
248
- end
257
+ description_parameters << "# #{p[:name]}: (#{type}) #{"(required)" if p[:required].to_s=="true"} #{p[:description]}"
249
258
  elsif p[:in] == "query"
250
259
  params_query << p[:name]
251
260
  params_required << p[:name] if p[:required].to_s=="true"
252
- if p.keys.include?(:description)
253
- description_parameters << "# #{p[:name]}: (#{p[:type]}) #{"(required)" if p[:required].to_s=="true"} #{p[:description]}"
254
- end
261
+ description_parameters << "# #{p[:name]}: (#{type}) #{"(required)" if p[:required].to_s=="true"} #{p[:description]}"
255
262
  elsif p[:in] == "body"
256
263
  if p.keys.include?(:schema)
257
- #jal
258
264
  if p[:schema].key?(:oneOf)
259
265
  bodies = p[:schema][:oneOf]
260
266
  elsif p[:schema].key?(:anyOf)
@@ -556,7 +562,14 @@ class OpenApiImport
556
562
  private def get_response_examples(v)
557
563
  # TODO: take in consideration the case allOf, oneOf... schema.items.allOf[0].properties schema.items.allOf[1].properties
558
564
  # example on https://github.com/OAI/OpenAPI-Specification/blob/master/examples/v2.0/yaml/petstore-expanded.yaml
565
+ v=v.dup
559
566
  response_example = Array.new()
567
+ # for open api 3.0 with responses schema inside content
568
+ if v.key?(:content) && v[:content].is_a?(Hash) && v[:content].key?(:'application/json') &&
569
+ v[:content][:'application/json'].key?(:schema)
570
+ v=v[:content][:'application/json'].dup
571
+ end
572
+
560
573
  if v.key?(:examples) && v[:examples].is_a?(Hash) && v[:examples].key?(:'application/json')
561
574
  if v[:examples][:'application/json'].is_a?(String)
562
575
  response_example << v[:examples][:'application/json']
@@ -575,6 +588,36 @@ class OpenApiImport
575
588
  end
576
589
  response_example << "]"
577
590
  end
591
+ # for open api 3.0. examples on reponses, for example: api-with-examples.yaml
592
+ elsif v.key?(:content) && v[:content].is_a?(Hash) && v[:content].key?(:'application/json') &&
593
+ v[:content][:'application/json'].key?(:examples)
594
+ v[:content][:'application/json'][:examples].each do |tk, tv|
595
+ #todo: for the moment we only take in consideration the first example of response.
596
+ # we need to decide how to manage to do it correctly
597
+ if tv.key?(:value)
598
+ tresp = tv[:value]
599
+ else
600
+ tresp = ""
601
+ end
602
+ if tresp.is_a?(String)
603
+ response_example << tresp
604
+ elsif tresp.is_a?(Hash)
605
+ exs = tresp.to_s
606
+ exs.gsub!(/:(\w+)=>/, "\n\\1: ")
607
+ response_example << exs
608
+ elsif tresp.is_a?(Array)
609
+ response_example << "["
610
+ tresp.each do |ex|
611
+ exs = ex.to_s
612
+ if ex.is_a?(Hash)
613
+ exs.gsub!(/:(\w+)=>/, "\n\\1: ")
614
+ end
615
+ response_example << (exs + ", ")
616
+ end
617
+ response_example << "]"
618
+ end
619
+ break #only the first one it is considered
620
+ end
578
621
  elsif v.key?(:schema) && v[:schema].is_a?(Hash) &&
579
622
  (v[:schema].key?(:properties) ||
580
623
  (v[:schema].key?(:items) && v[:schema][:items].key?(:properties)) ||
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.4.1
4
+ version: 0.5.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-11 00:00:00.000000000 Z
11
+ date: 2019-02-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oas_parser