openapi_contracts 0.3.0 → 0.4.2

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: b457539132f36e2ce4da92d0b34721465ed731b5ce33323bedebbebcfe5ad8fa
4
- data.tar.gz: 378f005f60327e8835908621f3991ffd8954434c5343ad91918026b0d2ce0444
3
+ metadata.gz: c495a3bb1abc5e8a6da67fc5ed5dc7a9ba98a42ebd84d5f80bc7665cb5badded
4
+ data.tar.gz: bcfc21e9c2b09c02f32a6e9eec7644a7790b94ac7c92a25c4aa54a55a46d307b
5
5
  SHA512:
6
- metadata.gz: f556c79bb2c60a9489b9a48f31ac1acbff8fbbd0bf0279e2b8e5208c270fece99631d2cc8ee4e3d0579a2a82d8f3a4eeb566d609f79149f1d3622908460a2d8b
7
- data.tar.gz: 2961f9afd81b0954fd8e37d0c7fe290e35d87c176d28772d9c272e5d3629440b1e4da5c7223beb1f73e564fd8bc277f3bb05b1061af4138ffb02c5dc677d005a
6
+ metadata.gz: d82aab412b36a2669556e69b0cdbec4f793076b4757050ad1b6b943856446f66e01d2b17f0d296e521fc04d39ebbfd83e481d1341de1c18290044d4baed0fd0c
7
+ data.tar.gz: 6fd2334e58fc53fd75b41c1ba9df2dd2a4da2925920c9ffcf2562df3f01b03e66e5801f88fbb691d5b73225ba9817bc1e42d89846f2b15ecaffa5a0e126e6b17
data/README.md CHANGED
@@ -16,7 +16,7 @@ First parse your api documentation:
16
16
 
17
17
  ```ruby
18
18
  # This must point to the folder where the "openapi.yaml" file is
19
- $doc = OpenapiContracts::Doc.parse(Rails.root.join('spec', 'fixtures', 'openapi', 'api-docs', 'openapi'))
19
+ $doc = OpenapiContracts::Doc.parse(Rails.root.join('spec/fixtures/openapi/api-docs/openapi'))
20
20
  ```
21
21
 
22
22
  Ideally you do this once in a RSpec `before(:suite)` hook.
@@ -12,14 +12,15 @@ module OpenapiContracts
12
12
  abs_path = @dir.join(path)
13
13
  data = parse_file(abs_path, translate: false)
14
14
  data.deep_merge! merge_components
15
- join_partials(abs_path.dirname, data)
15
+ data = join_partials(abs_path.dirname, data)
16
+ nullable_to_type!(data)
16
17
  end
17
18
 
18
19
  private
19
20
 
20
21
  def parse_file(path, translate: true)
21
22
  schema = YAML.safe_load(File.read(path))
22
- translate ? translate_paths(schema, Pathname(path).parent) : schema
23
+ translate ? translate_paths!(schema, Pathname(path).parent) : schema
23
24
  end
24
25
 
25
26
  def join_partials(cwd, data)
@@ -34,10 +35,23 @@ module OpenapiContracts
34
35
  end
35
36
  end
36
37
 
38
+ def nullable_to_type!(object)
39
+ case object
40
+ when Hash
41
+ if object['type'] && object['nullable']
42
+ object['type'] = [object['type'], 'null']
43
+ object.delete 'nullable'
44
+ else
45
+ object.each_value { |o| nullable_to_type! o }
46
+ end
47
+ when Array
48
+ object.each { |o| nullable_to_type! o }
49
+ end
50
+ end
51
+
37
52
  def merge_components
38
53
  data = {}
39
54
  Dir[File.expand_path('components/**/*.yaml', @dir)].each do |file|
40
- # pn = Pathname(file).relative_path_from(@dir)
41
55
  pointer = json_pointer(Pathname(file)).split('/')
42
56
  i = 0
43
57
  pointer.reduce(data) do |h, p|
@@ -53,14 +67,17 @@ module OpenapiContracts
53
67
  data
54
68
  end
55
69
 
56
- def translate_paths(data, cwd)
57
- data.each_with_object({}) do |(key, val), m|
58
- if val.is_a?(Hash)
59
- m[key] = translate_paths(val, cwd)
60
- elsif key == '$ref' && val !~ %r{^#/}
61
- m[key] = json_pointer(cwd.join(val), '#/')
62
- else
63
- m[key] = val
70
+ def translate_paths!(data, cwd)
71
+ case data
72
+ when Array
73
+ data.each { |v| translate_paths!(v, cwd) }
74
+ when Hash
75
+ data.each_pair do |k, v|
76
+ if k == '$ref' && v !~ %r{^#/}
77
+ v.replace json_pointer(cwd.join(v), '#/')
78
+ else
79
+ translate_paths!(v, cwd)
80
+ end
64
81
  end
65
82
  end
66
83
  end
@@ -1,7 +1,7 @@
1
1
  module OpenapiContracts
2
2
  class Doc::Response
3
3
  def initialize(schema)
4
- @schema = schema
4
+ @schema = schema.follow_refs
5
5
  end
6
6
 
7
7
  def headers
@@ -7,6 +7,14 @@ module OpenapiContracts
7
7
  @path = path.freeze
8
8
  end
9
9
 
10
+ def follow_refs
11
+ if (ref = dig('$ref'))
12
+ at_path(ref.split('/')[1..])
13
+ else
14
+ self
15
+ end
16
+ end
17
+
10
18
  def fragment
11
19
  path.map { |p| p.gsub('/', '~1') }.join('/').then { |s| "#/#{s}" }
12
20
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openapi_contracts
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - mkon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-05-25 00:00:00.000000000 Z
11
+ date: 2022-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -78,28 +78,28 @@ dependencies:
78
78
  requirements:
79
79
  - - '='
80
80
  - !ruby/object:Gem::Version
81
- version: 1.29.0
81
+ version: 1.30.1
82
82
  type: :development
83
83
  prerelease: false
84
84
  version_requirements: !ruby/object:Gem::Requirement
85
85
  requirements:
86
86
  - - '='
87
87
  - !ruby/object:Gem::Version
88
- version: 1.29.0
88
+ version: 1.30.1
89
89
  - !ruby/object:Gem::Dependency
90
90
  name: rubocop-rspec
91
91
  requirement: !ruby/object:Gem::Requirement
92
92
  requirements:
93
93
  - - '='
94
94
  - !ruby/object:Gem::Version
95
- version: 2.10.0
95
+ version: 2.11.1
96
96
  type: :development
97
97
  prerelease: false
98
98
  version_requirements: !ruby/object:Gem::Requirement
99
99
  requirements:
100
100
  - - '='
101
101
  - !ruby/object:Gem::Version
102
- version: 2.10.0
102
+ version: 2.11.1
103
103
  - !ruby/object:Gem::Dependency
104
104
  name: simplecov
105
105
  requirement: !ruby/object:Gem::Requirement
@@ -146,7 +146,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
146
146
  version: '2.7'
147
147
  - - "<"
148
148
  - !ruby/object:Gem::Version
149
- version: '3.2'
149
+ version: '3.3'
150
150
  required_rubygems_version: !ruby/object:Gem::Requirement
151
151
  requirements:
152
152
  - - ">="