sinatra-swagger 0.2.2 → 0.3.0

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,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MDU1MzAwYzE1OTIwMWRhYzllOGY5Mjg0NWM3MWRiNjJkNzI0ZGI0Nw==
4
+ MTY4N2VlNjk0OTliMGZhNDIwNTEyZjdlYzU5OGVjYWRkYmNjYWI0Yg==
5
5
  data.tar.gz: !binary |-
6
- ZjQ2MjM1NThmOTM0MDcyYTAxOTk5MGE5ZDcyYWEzYmNiZmZlNTEwYw==
6
+ NzYyM2FiYjczYzFlNGMwMjgxNmI2MjI3ZmFjMDdiZDI4YzM1NGE1YQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZDQwZDY4ODAxZGMxZjg5NGNlMmNlOTU4MzVkNDkzNjRmNmIzNWM3NmY5Zjdj
10
- NDZkZGYxZTFjZjQzMWJhMGI5MDk1MzRhMGM3ZGIxODY5ZjlkZWE1YzZhM2Y1
11
- ZDY3ZTRlMmY3ZmIxNGM5NDk1ZmMwZGRlY2JkOTA4OTZhMjcyOTM=
9
+ ZGM5NTY5YzU5MzNjMTA4YzQwN2JjOTIxZWVkZmYyNjlkNTViMGQ0OTAzNDU0
10
+ YTBkMTg3OTZhMzhiMTczZWY4N2Q0ODU1YTgyMjJkY2U1MjE5ZDAwNDI5Njhh
11
+ NDA4ODBmZjc5YzliM2JmMGY4YTk1Y2U4YzczZTc4ZWJmNzg1ZDc=
12
12
  data.tar.gz: !binary |-
13
- OGM5ZWFmNjQxZTMyN2JlYmIzNGMxN2E1MzVmOTM3MDEyYzhmOThkNzVjY2Vj
14
- ZWVhODU4OTJjMWZmNjMxNDQ4M2JjMDU4ODc3ZTFlNzNlMjk2ODUyZjJiM2Qz
15
- YzkxZGVjZDg2OGE2ODQ3MjAyOTkwZjVhOTEwMjY3NWFiM2U2NWY=
13
+ YzUwMWI4MWI0Nzk5ZjJiYjY1NDViYWVjMDYxY2Q1YjNkODYzNjEyZDBlNTBm
14
+ Yzg0MmZiZDE2ZTdlMTc0NDdiN2MxZGNiMTNjMmZhNGE3MjBiOTYxMDQ3ZmU5
15
+ Nzg1MjFmZGFhZThmZTUwZDA2MGFiMGVjZmU0MDE3YTg5OWRjYzI=
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.2
1
+ 0.3.0
@@ -8,34 +8,72 @@ module Sinatra
8
8
 
9
9
  app.helpers do
10
10
  def invalid_params(invalidities)
11
- content_type :json
12
- halt(
13
- 400,
14
- {
15
- error: 'invalid_params',
16
- developerMessage: 'Some of the given parameters were invalid according to the Swagger spec.',
17
- details: {
18
- invalidities: invalidities
11
+ error_response(
12
+ 'invalid_params',
13
+ 'Some of the given parameters were invalid according to the Swagger spec.',
14
+ details: { invalidities: invalidities },
15
+ status: 400
16
+ )
17
+ end
18
+
19
+ def invalid_content_type(acceptable)
20
+ error_response(
21
+ 'invalid_content',
22
+ 'The ',
23
+ details: {
24
+ content_types: {
25
+ acceptable: acceptable,
26
+ given: request.content_type
19
27
  }
20
- }.to_json
28
+ },
29
+ status: 400
21
30
  )
22
31
  end
32
+
33
+ def error_response(code, dev_message, details: {}, status: 400)
34
+ content_type :json
35
+ halt(status,{
36
+ error: code,
37
+ developerMessage: dev_message,
38
+ details: details
39
+ }.to_json)
40
+ end
23
41
  end
24
42
 
25
43
  app.before do
26
44
  next if swagger_spec.nil?
27
45
  _, captures, spec = swagger_spec.values
46
+ invalid_content_type(spec['consumes']) if spec['consumes'] && !spec['consumes'].include?(request.content_type)
47
+
48
+ unknown_params = params.keys.select { |k| k.is_a?(String) }
28
49
 
29
50
  invalidities = Hash[(spec['parameters'] || []).map { |details|
30
51
  param_name = details['name']
52
+ unknown_params.delete(param_name)
31
53
 
32
54
  parameter = case details['in']
33
55
  when "query"
34
56
  params[param_name]
35
57
  when "path"
36
58
  captures[param_name]
59
+ when "body"
60
+ request.body.rewind
61
+ params[:body] = request.body.read
62
+ next nil unless request.content_type =~ %r{^application/(?:.+\+)?json}
63
+
64
+ begin
65
+ params[:body] = JSON.parse(params[:body])
66
+ rescue JSON::ParserError
67
+ next ['POST body', :invalid_json]
68
+ end
69
+ next nil unless details['schema']
70
+
71
+ schema = details['schema'].merge('definitions' => settings.swagger['definitions'])
72
+ errors = JSON::Validator.fully_validate(schema, params[:body])
73
+ next errors.empty? ? nil : ['POST body', errors]
37
74
  else
38
- raise NotImplementedError, "Can't cope with #{details['in']} parameters right now"
75
+ # other param types aren't dealt with at the moment
76
+ next
39
77
  end
40
78
 
41
79
  if !parameter
@@ -61,7 +99,10 @@ module Sinatra
61
99
  nil
62
100
  }.compact]
63
101
 
64
- invalid_params(invalidities) if invalidities.any?
102
+ if invalidities.any?
103
+ unknown_params.each { |p| invalidities[p] = :unexpected_key }
104
+ invalid_params(invalidities)
105
+ end
65
106
  end
66
107
  end
67
108
  end
@@ -15,7 +15,7 @@ module Sinatra
15
15
  begin
16
16
  JSON::Validator.validate!(schema, body)
17
17
  rescue JSON::Schema::ValidationError => e
18
- e.message = "Response JSON did not match the Swagger schema: #{e.message}"
18
+ e.message = "Response JSON did not match the Swagger schema: #{e.message}\n#{body}"
19
19
  raise e
20
20
  end
21
21
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-swagger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - JP Hastings-Spital
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-15 00:00:00.000000000 Z
11
+ date: 2015-02-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json-schema