oas_parser 0.18.2 → 0.19.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 86b2e4324d8cc77f1acd3dd60c95f613144330308d65a68a5a51233bf63ff8b1
4
- data.tar.gz: 013b58bd9b70b51146a80f1d382681e9e2ccd9ded347a599ff4257e1a7e74668
3
+ metadata.gz: 825c569ee33db4ed6f8b1e666b9bbba45a5440d34d3490074e63f5815414efd8
4
+ data.tar.gz: 289f26572fa6abbf927305aaba1452d1c15ca6639726bf29768ec5ad12b490ef
5
5
  SHA512:
6
- metadata.gz: 6d1885cd3dae1b5d17f2c0d050e6fcc96b365b6bd5cdbe696abf9d18186b6d48475a70c28baa01efbbff77e8b5fb1f19231a3dda3b3d1425390162c1aafacc6d
7
- data.tar.gz: e3415696c0428e05e6f73de30f5464479d1ffefc6389b373184f8d0b927bbca1d3536dc34ccc21cb7751712f684b15042e1317159ed740cbf87749aac6384f6f
6
+ metadata.gz: ff8b3c7bcedfd51b38de3a61b0781ea8f64af25238918091b83304c549331aec97ec9714c7daa5c481e37867adb1306529460406e66b21e33fc987772d2b81d8
7
+ data.tar.gz: 35fe373a593e01a8b26fa8a1e04f0dbc388a6d87475c6523254a54a5de5883adb7ca65c5cc28f566c6c641f5e7a98c21aa9bf0e5db356b561043d875c0efba69
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- oas_parser (0.18.2)
4
+ oas_parser (0.19.0)
5
5
  activesupport (>= 4.0.0)
6
6
  addressable (~> 2.3)
7
7
  builder (~> 3.2.3)
@@ -0,0 +1,3 @@
1
+ ## Unreleased
2
+
3
+ - Parser now handles circular/self reference by returning the $ref as is instead of expanding it indefinitely. (Qihuan Piao)
@@ -11,38 +11,38 @@ module OasParser
11
11
  end
12
12
 
13
13
  def resolve
14
- deeply_expand_refs(@content)
14
+ deeply_expand_refs(@content, nil)
15
15
  end
16
16
 
17
17
  private
18
18
 
19
- def deeply_expand_refs(fragment)
20
- fragment = expand_refs(fragment)
19
+ def deeply_expand_refs(fragment, path)
20
+ fragment, current_path = expand_refs(fragment, path)
21
21
 
22
22
  if fragment.is_a?(Hash)
23
23
  fragment.reduce({}) do |hash, (k, v)|
24
- hash.merge(k => deeply_expand_refs(v))
24
+ hash.merge(k => deeply_expand_refs(v, "#{current_path}/#{k}"))
25
25
  end
26
26
  elsif fragment.is_a?(Array)
27
- fragment.map { |e| deeply_expand_refs(e) }
27
+ fragment.map { |e| deeply_expand_refs(e, current_path) }
28
28
  else
29
29
  fragment
30
30
  end
31
31
  end
32
32
 
33
- def expand_refs(fragment)
34
- if fragment.is_a?(Hash) && fragment.has_key?("$ref")
35
- ref = fragment["$ref"]
33
+ def expand_refs(fragment, current_path)
34
+ unless fragment.is_a?(Hash) && fragment.key?("$ref")
35
+ return [fragment, current_path]
36
+ end
36
37
 
37
- if ref =~ /^#/
38
- expand_pointer(ref)
39
- elsif ref =~ /^(http(s)?|\/\/)/
40
- expand_url(ref)
41
- else
42
- expand_file(ref)
43
- end
38
+ ref = fragment["$ref"]
39
+
40
+ if ref =~ /^#/
41
+ expand_pointer(ref, current_path)
42
+ elsif ref =~ /^(http(s)?|\/\/)/
43
+ expand_url(ref)
44
44
  else
45
- fragment
45
+ expand_file(ref)
46
46
  end
47
47
  end
48
48
 
@@ -59,11 +59,15 @@ module OasParser
59
59
  resolved_remote_reference
60
60
  end
61
61
 
62
- def expand_pointer(ref, content=nil)
62
+ def expand_pointer(ref, current_path)
63
63
  pointer = OasParser::Pointer.new(ref)
64
- fragment = pointer.resolve(content || @content)
65
64
 
66
- expand_refs(fragment)
65
+ if pointer.circular_reference?(current_path)
66
+ { "$ref" => ref }
67
+ else
68
+ fragment = pointer.resolve(@content)
69
+ expand_refs(fragment, current_path + pointer.escaped_pointer)
70
+ end
67
71
  end
68
72
 
69
73
  def expand_url(ref)
@@ -12,7 +12,28 @@ module OasParser
12
12
  end
13
13
  end
14
14
 
15
- private
15
+ # Detect circular reference by checking whether the ref exists in current path.
16
+ #
17
+ # Example:
18
+ # components:
19
+ # schemas:
20
+ # Pet:
21
+ # type: object
22
+ # properties:
23
+ # name:
24
+ # type: string
25
+ # children:
26
+ # type: array
27
+ # items: # <--- parsing here
28
+ # - $ref: '#/components/schemas/Pet'
29
+ #
30
+ # path: "/components/schemas/Pet/properties/children/items"
31
+ # raw_pointer: "#/components/schemas/Pet"
32
+ #
33
+ # It'd return true when we're parsing the pet children items where the ref points back to itself.
34
+ def circular_reference?(path)
35
+ path.include?("#{escaped_pointer}/")
36
+ end
16
37
 
17
38
  def escaped_pointer
18
39
  if @raw_pointer.start_with?("#")
@@ -22,6 +43,8 @@ module OasParser
22
43
  end
23
44
  end
24
45
 
46
+ private
47
+
25
48
  def parse_token(token)
26
49
  if token =~ /\A\d+\z/
27
50
  token.to_i
@@ -1,3 +1,3 @@
1
1
  module OasParser
2
- VERSION = '0.18.2'.freeze
2
+ VERSION = '0.19.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oas_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.18.2
4
+ version: 0.19.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Butler
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-05-09 00:00:00.000000000 Z
11
+ date: 2019-06-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable
@@ -212,6 +212,7 @@ files:
212
212
  - bin/console
213
213
  - bin/setup
214
214
  - lib/oas_parser.rb
215
+ - lib/oas_parser/CHANGELOG.md
215
216
  - lib/oas_parser/abstract_attribute.rb
216
217
  - lib/oas_parser/callback.rb
217
218
  - lib/oas_parser/definition.rb