committee 2.0.0.pre5 → 2.0.0.pre6

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b8ffc44027f6147a34b278d70e7f20117fa9cf93
4
- data.tar.gz: 068dec170ff98a7e06e88372f396adcae0b3934a
3
+ metadata.gz: d9c150399d562ad7e8daafbf27c82044a2e3c639
4
+ data.tar.gz: f2879d489c219d68a9191c9ce7ce31972107334e
5
5
  SHA512:
6
- metadata.gz: 457ab6c2e9c8206208d9f4960ffaeba6f82d416a286116ed36cea327ff873cf5bf217493a71cb7edf46991c05a8ae646719af6d1f7471a9d8805637beab6577f
7
- data.tar.gz: e8a55c657884295a59ed2559c5e1017c428d037c75df1eb27fa62516c4630b24222722b784b94d2f76fff47abe1332f65e73c4410b9c32d5dfdc4ffd1156ec9f
6
+ metadata.gz: a019bde4a85727e0bbb676376ece423d4731645bb1a49309f57d178c7bd0eac11049acef972923b7907f2cf3e331c84b968a784fc3d24084a8c9e73db36f0817
7
+ data.tar.gz: dcb6b47b74931ea684c6e9f92004923e09f72755be7abdfb9381cf1b457286f44b02c6fd3741902901aa2843cadac9b8a984d336d25b931ec3f9b0c92e11a5cc
@@ -19,12 +19,13 @@ module Committee::Middleware
19
19
  if link
20
20
  headers = { "Content-Type" => "application/json" }
21
21
 
22
- data = cache(link) do
22
+ data, schema = cache(link) do
23
23
  Committee::ResponseGenerator.new.call(link)
24
24
  end
25
25
 
26
26
  if @call
27
27
  request.env["committee.response"] = data
28
+ request.env["committee.response_schema"] = schema
28
29
  call_status, call_headers, call_body = @app.call(request.env)
29
30
 
30
31
  # a committee.suppress signal initiates a direct pass through
@@ -36,7 +37,7 @@ module Committee::Middleware
36
37
  # made, and stub normally
37
38
  headers.merge!(call_headers)
38
39
 
39
- # allow allow the handler to change the data object (if unchanged, it
40
+ # allow the handler to change the data object (if unchanged, it
40
41
  # will be the same one that we set above)
41
42
  data = request.env["committee.response"]
42
43
  end
@@ -1,7 +1,8 @@
1
1
  module Committee
2
2
  class ResponseGenerator
3
3
  def call(link)
4
- data = generate_properties(link, target_schema(link))
4
+ schema = target_schema(link)
5
+ data = generate_properties(link, schema)
5
6
 
6
7
  # List is a special case; wrap data in an array.
7
8
  #
@@ -14,7 +15,7 @@ module Committee
14
15
  data = [data]
15
16
  end
16
17
 
17
- data
18
+ [data, schema]
18
19
  end
19
20
 
20
21
  private
@@ -37,6 +38,7 @@ module Committee
37
38
  # special example attribute was included; use its value
38
39
  if schema.data && !schema.data["example"].nil?
39
40
  schema.data["example"]
41
+
40
42
  elsif !schema.all_of.empty? || !schema.properties.empty?
41
43
  data = {}
42
44
  schema.all_of.each do |subschema|
@@ -46,12 +48,26 @@ module Committee
46
48
  data[key] = generate_properties(link, value)
47
49
  end
48
50
  data
51
+
49
52
  elsif schema.type.include?("array") && !schema.items.nil?
50
53
  [generate_properties(link, schema.items)]
54
+
55
+ elsif schema.enum
56
+ schema.enum.first
57
+
51
58
  elsif schema.type.any? { |t| SCALAR_TYPES.include?(t) }
52
59
  SCALAR_TYPES.each do |k, v|
53
60
  break(v) if schema.type.include?(k)
54
61
  end
62
+
63
+ # Generate an empty array for arrays.
64
+ elsif schema.type == ["array"]
65
+ []
66
+
67
+ # Schema is an object with no properties: just generate an empty object.
68
+ elsif schema.type == ["object"]
69
+ {}
70
+
55
71
  else
56
72
  raise(%{At "#{link.method} #{link.href}" "#{schema.pointer}": no } +
57
73
  %{"example" attribute and "null" } +
@@ -38,6 +38,15 @@ describe Committee::Middleware::Stub do
38
38
  assert_equal "", last_response.body
39
39
  end
40
40
 
41
+ it "optionally returns the application's response schema" do
42
+ @app = new_rack_app(call: true, schema: hyper_schema, suppress: true)
43
+ get "/apps/heroku-api"
44
+ assert_equal 429, last_response.status
45
+ assert_equal "#/definitions/app/links/2/targetSchema",
46
+ last_response.headers["Committee-Response-Schema"]
47
+ assert_equal "", last_response.body
48
+ end
49
+
41
50
  it "takes a prefix" do
42
51
  @app = new_rack_app(prefix: "/v1", schema: hyper_schema)
43
52
  get "/v1/apps/heroku-api"
@@ -75,7 +84,7 @@ describe Committee::Middleware::Stub do
75
84
  get "/apps/heroku-api"
76
85
  assert_equal 200, last_response.status
77
86
 
78
- data = cache[cache.first[0]]
87
+ data, _schema = cache[cache.first[0]]
79
88
  assert_equal ValidApp.keys.sort, data.keys.sort
80
89
 
81
90
  # replace what we have in the cache
@@ -106,6 +115,12 @@ describe Committee::Middleware::Stub do
106
115
  })
107
116
  end
108
117
 
118
+ if res = env["committee.response_schema"]
119
+ headers.merge!({
120
+ "Committee-Response-Schema" => res.pointer,
121
+ })
122
+ end
123
+
109
124
  env["committee.suppress"] = suppress
110
125
 
111
126
  [429, headers, []]
@@ -10,13 +10,18 @@ describe Committee::ResponseGenerator do
10
10
  @list_link = @schema.properties["app"].links[3]
11
11
  end
12
12
 
13
+ it "returns the schema used to match" do
14
+ _data, schema = call
15
+ assert_equal @get_link.target_schema, schema
16
+ end
17
+
13
18
  it "generates string properties" do
14
- data = call
19
+ data, _schema = call
15
20
  assert data["name"].is_a?(String)
16
21
  end
17
22
 
18
23
  it "generates non-string properties" do
19
- data = call
24
+ data, _schema = call
20
25
  assert_includes [FalseClass, TrueClass], data["maintenance"].class
21
26
  end
22
27
 
@@ -25,7 +30,7 @@ describe Committee::ResponseGenerator do
25
30
 
26
31
  @link.rel = nil
27
32
 
28
- data = call
33
+ data, _schema = call
29
34
  assert data.is_a?(Array)
30
35
  end
31
36
 
@@ -35,7 +40,7 @@ describe Committee::ResponseGenerator do
35
40
  # forces the link to use `parent`
36
41
  @link.target_schema = nil
37
42
 
38
- data = call
43
+ data, _schema = call
39
44
 
40
45
  # We're testing for legacy behavior here: even without a `targetSchema` as
41
46
  # long as `rel` is set to `instances` we still wrap the the result in an
@@ -65,18 +70,46 @@ describe Committee::ResponseGenerator do
65
70
  assert_equal expected, e.message
66
71
  end
67
72
 
73
+ it "generates first enum value for a schema with enum" do
74
+ link = Committee::Drivers::OpenAPI2::Link.new
75
+ link.target_schema = JsonSchema::Schema.new
76
+ link.target_schema.enum = ["foo"]
77
+ link.target_schema.type = ["string"]
78
+ data, _schema = Committee::ResponseGenerator.new.call(link)
79
+ assert_equal("foo", data)
80
+ end
81
+
68
82
  it "generates basic types" do
69
83
  link = Committee::Drivers::OpenAPI2::Link.new
70
84
  link.target_schema = JsonSchema::Schema.new
71
85
 
72
86
  link.target_schema.type = ["integer"]
73
- assert_equal 0, Committee::ResponseGenerator.new.call(link)
87
+ data, _schema = Committee::ResponseGenerator.new.call(link)
88
+ assert_equal 0, data
74
89
 
75
90
  link.target_schema.type = ["null"]
76
- assert_equal nil, Committee::ResponseGenerator.new.call(link)
91
+ data, _schema = Committee::ResponseGenerator.new.call(link)
92
+ assert_equal nil, data
77
93
 
78
94
  link.target_schema.type = ["string"]
79
- assert_equal "", Committee::ResponseGenerator.new.call(link)
95
+ data, _schema = Committee::ResponseGenerator.new.call(link)
96
+ assert_equal "", data
97
+ end
98
+
99
+ it "generates an empty array for an array type" do
100
+ link = Committee::Drivers::OpenAPI2::Link.new
101
+ link.target_schema = JsonSchema::Schema.new
102
+ link.target_schema.type = ["array"]
103
+ data, _schema = Committee::ResponseGenerator.new.call(link)
104
+ assert_equal([], data)
105
+ end
106
+
107
+ it "generates an empty object for an object with no fields" do
108
+ link = Committee::Drivers::OpenAPI2::Link.new
109
+ link.target_schema = JsonSchema::Schema.new
110
+ link.target_schema.type = ["object"]
111
+ data, _schema = Committee::ResponseGenerator.new.call(link)
112
+ assert_equal({}, data)
80
113
  end
81
114
 
82
115
  it "prefers an example to a built-in value" do
@@ -86,7 +119,8 @@ describe Committee::ResponseGenerator do
86
119
  link.target_schema.data = { "example" => 123 }
87
120
  link.target_schema.type = ["integer"]
88
121
 
89
- assert_equal 123, Committee::ResponseGenerator.new.call(link)
122
+ data, _schema = Committee::ResponseGenerator.new.call(link)
123
+ assert_equal 123, data
90
124
  end
91
125
 
92
126
  it "prefers non-null types to null types" do
@@ -94,7 +128,8 @@ describe Committee::ResponseGenerator do
94
128
  link.target_schema = JsonSchema::Schema.new
95
129
 
96
130
  link.target_schema.type = ["null", "integer"]
97
- assert_equal 0, Committee::ResponseGenerator.new.call(link)
131
+ data, _schema = Committee::ResponseGenerator.new.call(link)
132
+ assert_equal 0, data
98
133
  end
99
134
 
100
135
  def call
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: committee
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.pre5
4
+ version: 2.0.0.pre6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandur
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-01-16 00:00:00.000000000 Z
12
+ date: 2017-02-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json_schema