swagui 0.2.1 → 0.3.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
  SHA1:
3
- metadata.gz: 2fc715306148d70c2920dd1b350402168421d8a1
4
- data.tar.gz: 4e117c70a2450acbbb9e185cb7000ff735d62742
3
+ metadata.gz: 50e4fe6a730fae9960c9038449f113c3cfef72e6
4
+ data.tar.gz: f31291c54b4bea9ef5f75c3fc4d2d2de4ca30ad1
5
5
  SHA512:
6
- metadata.gz: d64b02242b743ce1923df4073a01ef1cc8ef8537725122b13b16603efd2421fdc1b543bb9ade3ec3983a01df52f944e328e34cadd7be12af5220a8d5f9fcdf56
7
- data.tar.gz: 781936a2430e3539af3413a8269b34bf8472c4289112af43152c68ed822dc61f903cc0c1f6d9ea7d1568ca29022c55014c166305e1582445fa3a3cf67ea187fc
6
+ metadata.gz: e2d25877a39a5ae885ca6ac9eaa06573b3db7b96afc7d3ef40cf369593332cda51d694a64d41e816414f49f708bcc60f89a2950a20f12674b5d6a122cdab8c87
7
+ data.tar.gz: ebaf37b73fe27809e2e18a35c2d60b79815f5083cef08b63b09b80a949dae586e669825acbd6981309591c281a283dfb9c07d07df89d38aff49db64c28837bf0
data/.gitignore CHANGED
@@ -7,7 +7,6 @@ Gemfile.lock
7
7
  InstalledFiles
8
8
  _yardoc
9
9
  coverage
10
- doc/
11
10
  lib/bundler/man
12
11
  pkg
13
12
  rdoc
@@ -1,18 +1,32 @@
1
1
  module Swagui
2
2
  class JsonSchema
3
3
 
4
+ attr_reader :type
4
5
  attr_reader :name
5
6
 
6
7
  def initialize(schema_hash, name)
8
+ @nested_objects = []
9
+
7
10
  @name = name
8
11
  @schema_hash = schema_hash
9
- @nested_objects = []
10
12
 
11
- @schema_hash['properties'].each do |pname, pattributes|
12
- if pattributes['type'] == 'object'
13
- nested_object_name = "#{@name}-#{pname}"
14
- @schema_hash['properties'][pname] = {'$ref' => nested_object_name }
15
- @nested_objects << JsonSchema.new(pattributes, nested_object_name)
13
+ if @schema_hash['type'] == 'array'
14
+ @type = 'array'
15
+ nested_object_name = name
16
+ @nested_objects << JsonSchema.new(@schema_hash['items'], nested_object_name)
17
+ @schema_hash['items'] = {'$ref' => nested_object_name }
18
+ else
19
+ @type = 'class'
20
+ (@schema_hash['properties'] ||= []).each do |pname, pattributes|
21
+ if pattributes['type'] == 'object' || (pattributes['type'] == 'array' && !pattributes['items'].nil?)
22
+ nested_object_name = "#{@name}-#{pname}"
23
+ if pattributes['type'] == 'object'
24
+ @schema_hash['properties'][pname] = {'$ref' => nested_object_name }
25
+ else
26
+ @schema_hash['properties'][pname] = { 'type' => 'array', 'items' => {'$ref' => nested_object_name } }
27
+ end
28
+ @nested_objects << JsonSchema.new(pattributes, nested_object_name)
29
+ end
16
30
  end
17
31
  end
18
32
  end
@@ -31,7 +45,11 @@ module Swagui
31
45
  end
32
46
 
33
47
  def all_objects
34
- ([self] + @nested_objects.map {|x| x.all_objects}).flatten
48
+ ([self].select {|x| !x.properties.nil?} + @nested_objects.map {|x| x.all_objects}).flatten
49
+ end
50
+
51
+ def array?
52
+ type == 'array'
35
53
  end
36
54
 
37
55
  end
@@ -1,3 +1,3 @@
1
1
  module Swagui
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -38,26 +38,40 @@ module Swagui
38
38
  def process_schemas(response_hash)
39
39
  (response_hash['apis'] || []).each do |api_hash|
40
40
  (api_hash['operations'] || []).each do |operations_hash|
41
+ operation_name = operations_hash['nickname']
42
+
43
+ merge_schema!(operations_hash, response_hash, operation_name)
44
+
41
45
  (operations_hash['parameters'] || []).each do |parameters_hash|
42
- if schema_file = parameters_hash.delete('schema')
43
- schema_file_path = Swagui.file_full_path(schema_file)
44
- schema = JsonSchemaParser.parse(schema_file_path, "#{operations_hash['nickname']}-Request")
45
- schema.models.each {|m| (response_hash['models'] ||= {})[m['id']] = m }
46
- parameters_hash.merge!('type' => schema.name)
47
- end
46
+ merge_schema!(parameters_hash, response_hash, "#{operation_name}-#{parameters_hash['name']}")
48
47
  end
48
+
49
49
  (operations_hash['responseMessages'] || []).each do |response_messages_hash|
50
- if schema_file = response_messages_hash.delete('schema')
51
- schema_file_path = Swagui.file_full_path(schema_file)
52
- schema = JsonSchemaParser.parse(schema_file_path, "#{operations_hash['nickname']}-Response-#{response_messages_hash['message'].gsub(' ', '-')}")
53
- schema.models.each {|m| (response_hash['models'] ||= {})[m['id']] = m }
54
- response_messages_hash.merge!('responseModel' => schema.name)
55
- end
50
+ merge_schema!(response_messages_hash, response_hash, "#{operation_name}-Response-#{response_messages_hash['code']}", true)
56
51
  end
57
52
  end
58
53
  end
59
54
  end
60
55
 
56
+ def merge_schema!(parent_hash, response_hash, name, response_model = false)
57
+ if schema_file = parent_hash.delete('schema')
58
+ schema_file_path = Swagui.file_full_path(schema_file)
59
+ schema = JsonSchemaParser.parse(schema_file_path, name)
60
+ schema.models.each {|m| (response_hash['models'] ||= {})[m['id']] = m }
61
+ parent_hash.merge!(schema_hash(schema, response_model))
62
+ end
63
+ end
64
+
65
+ def schema_hash(schema, response_model = false)
66
+ if response_model
67
+ {'responseModel' => schema.name}
68
+ elsif schema.array?
69
+ {'type' => 'array', 'items' => { '$ref' => schema.name} }
70
+ else
71
+ {'type' => schema.name}
72
+ end
73
+ end
74
+
61
75
  def process_api_docs_api_listing(response_hash, doc_path)
62
76
  if doc_path.end_with?('api-docs.yml') && response_hash['models'].nil? # requesting the root
63
77
  dir_path = File.dirname(doc_path)
@@ -0,0 +1,32 @@
1
+ ---
2
+ produces:
3
+ - application/json
4
+ apis:
5
+ -
6
+ path: /account
7
+ description: create or update a account
8
+ operations:
9
+ -
10
+ method: POST
11
+ summary: create or update an account
12
+ nickname: PostAccount
13
+ parameters:
14
+ -
15
+ name: body
16
+ required: true
17
+ schema: test/doc/schemas/account_post_request_body.schema.json
18
+ paramType: body
19
+ responseMessages:
20
+ -
21
+ code: 200
22
+ message: account creation success
23
+ schema: test/doc/schemas/account_post_creation_success.schema.json
24
+ -
25
+ code: 400
26
+ message: account creation failure
27
+ schema: test/doc/schemas/account_post_creation_failure.schema.json
28
+ -
29
+ method: GET
30
+ summary: Get all the accounts
31
+ nickname: GETAccounts
32
+ schema: test/doc/schemas/account_get_request_body.schema.json
@@ -0,0 +1,8 @@
1
+ ---
2
+ info:
3
+ title: "a sojourner's api"
4
+ description: "Ut? Amet, et eros nascetur parturient diam scelerisque, egestas, pulvinar sit cum, rhoncus eros vel urna aliquam massa! Turpis purus auctor proin aliquam nunc, nec proin vel enim est, scelerisque! Ac vel integer proin sed in."
5
+ contact: "sojourner@world.net"
6
+ apiVersion: "1.0.0"
7
+ swaggerVersion: "1.2"
8
+ authorizations: {}
@@ -0,0 +1,45 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-04/schema#",
3
+ "title": "Product set",
4
+ "type": "array",
5
+ "items": {
6
+ "title": "Product",
7
+ "type": "object",
8
+ "properties": {
9
+ "id": {
10
+ "description": "The unique identifier for a product",
11
+ "type": "number"
12
+ },
13
+ "name": {
14
+ "type": "string"
15
+ },
16
+ "price": {
17
+ "type": "number",
18
+ "minimum": 0,
19
+ "exclusiveMinimum": true
20
+ },
21
+ "tags": {
22
+ "type": "array",
23
+ "items": {
24
+ "type": "string"
25
+ },
26
+ "minItems": 1,
27
+ "uniqueItems": true
28
+ },
29
+ "dimensions": {
30
+ "type": "object",
31
+ "properties": {
32
+ "length": {"type": "number"},
33
+ "width": {"type": "number"},
34
+ "height": {"type": "number"}
35
+ },
36
+ "required": ["length", "width", "height"]
37
+ },
38
+ "warehouseLocation": {
39
+ "description": "Coordinates of the warehouse with the product",
40
+ "$ref": "http://json-schema.org/geo"
41
+ }
42
+ },
43
+ "required": ["id", "name", "price"]
44
+ }
45
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "type":"object",
3
+ "required":true,
4
+ "properties":{
5
+ "errors": {
6
+ "type":"string",
7
+ "required":true
8
+ }
9
+ }
10
+ }
@@ -0,0 +1,16 @@
1
+ {
2
+ "type":"object",
3
+ "required":true,
4
+ "properties":{
5
+ "token": {
6
+ "type":"object",
7
+ "required":true,
8
+ "properties":{
9
+ "uuid": {
10
+ "type":"string",
11
+ "required":true
12
+ }
13
+ }
14
+ }
15
+ }
16
+ }
@@ -0,0 +1,51 @@
1
+ {
2
+ "type":"object",
3
+ "required":true,
4
+ "properties":{
5
+ "address": {
6
+ "type":"string",
7
+ "required":true
8
+ },
9
+ "age": {
10
+ "type":"number",
11
+ "required":true
12
+ },
13
+ "contact": {
14
+ "type":"object",
15
+ "required":true,
16
+ "properties":{
17
+ "email": {
18
+ "type":"string",
19
+ "required":true
20
+ },
21
+ "phones": {
22
+ "type":"array",
23
+ "required":true,
24
+ "items": {
25
+ "type":"string",
26
+ "required":true
27
+ }
28
+ }
29
+ }
30
+ },
31
+ "entries": {
32
+ "type":"array",
33
+ "required":false,
34
+ "items":
35
+ {
36
+ "type":"object",
37
+ "required":false,
38
+ "properties":{
39
+ "guid": {
40
+ "type":"string",
41
+ "required":false
42
+ }
43
+ }
44
+ }
45
+ },
46
+ "name": {
47
+ "type":"string",
48
+ "required":true
49
+ }
50
+ }
51
+ }
@@ -3,19 +3,29 @@ require 'test_helper'
3
3
  class TestJsonSchema < Minitest::Test
4
4
 
5
5
  def setup
6
- sampole_schema_file = File.expand_path('doc/schemas/account_post_request_body.schema.json', File.dirname(__FILE__))
7
- @schema_json = JSON.load(File.open(sampole_schema_file).read)
6
+ sample_schema_file = File.expand_path('doc/schemas/account_post_request_body.schema.json', File.dirname(__FILE__))
7
+ @schema_json = JSON.load(File.open(sample_schema_file).read)
8
+
9
+ sample_array_schema_file = File.expand_path('doc/schemas/account_get_request_body.schema.json', File.dirname(__FILE__))
10
+ @array_schema_json = JSON.load(File.open(sample_array_schema_file).read)
8
11
  end
9
12
 
10
13
  def test_objects_method_returns_array
11
14
  schema = Swagui::JsonSchema.new(@schema_json, 'PostAccount')
12
- assert_equal schema.all_objects.size, 2
15
+ assert_equal schema.all_objects.size, 4
13
16
  end
14
17
 
15
18
  def test_models_methods_return_array_of_hash
16
19
  schema = Swagui::JsonSchema.new(@schema_json, 'PostAccount')
17
- assert_equal schema.models.size, 2
18
- assert_equal schema.models.map {|x| x['id']}, ['PostAccount', 'PostAccount-contact']
20
+ assert_equal schema.models.size, 4
21
+ assert_equal schema.models.map {|x| x['id']}, ["PostAccount", "PostAccount-contact", "PostAccount-contact-phones", "PostAccount-entries"]
22
+ assert_equal schema.models.first['properties']['entries'], {'type' => 'array', 'items' => {"$ref"=>"PostAccount-entries"}}
23
+ end
24
+
25
+ def test_array_schema
26
+ schema = Swagui::JsonSchema.new(@array_schema_json, 'GetAccount')
27
+ assert_equal schema.models.size, 3
28
+ assert_equal schema.models.map {|m| m['id']}, %w(GetAccount GetAccount-tags GetAccount-dimensions)
19
29
  end
20
30
 
21
31
  end
@@ -7,7 +7,7 @@ class TestJsonSchemaParser < Minitest::Test
7
7
  result = Swagui::JsonSchemaParser.parse(file_path, 'PostAccount')
8
8
  assert_equal result.class, Swagui::JsonSchema
9
9
  assert_equal result.name, 'PostAccount'
10
- assert_equal result.models.size, 2
10
+ assert_equal result.models.size, 4
11
11
  assert_equal result.models.first.class, Hash
12
12
  end
13
13
 
@@ -20,9 +20,16 @@ class TestSwaggerDocHandler < Minitest::Test
20
20
  get '/doc/account'
21
21
  assert last_response.ok?
22
22
  response_json = JSON.parse(last_response.body)
23
+
23
24
  assert_equal response_json['apis'].size, 1
24
25
  assert response_json['models'].size > 1
25
- assert_equal response_json['models'].keys.first, 'PostAccount-Request'
26
+ assert_equal response_json['models'].keys.first, 'PostAccount-body'
26
27
  refute response_json['basePath'].nil?
28
+ assert_equal response_json['apis'].first['operations'].first['parameters'].first['type'], 'PostAccount-body'
29
+
30
+ # body schema parsing
31
+ assert_equal response_json['apis'].first['operations'].last['type'], 'array'
32
+ assert_equal response_json['apis'].first['operations'].last['items'], {'$ref' => 'GETAccounts'}
27
33
  end
34
+
28
35
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swagui
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jack Xu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-13 00:00:00.000000000 Z
11
+ date: 2014-10-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -99,6 +99,12 @@ files:
99
99
  - swagger-ui/swagger-ui.js
100
100
  - swagger-ui/swagger-ui.min.js
101
101
  - swagui.gemspec
102
+ - test/doc/account.yml
103
+ - test/doc/api-docs.yml
104
+ - test/doc/schemas/account_get_request_body.schema.json
105
+ - test/doc/schemas/account_post_creation_failure.schema.json
106
+ - test/doc/schemas/account_post_creation_success.schema.json
107
+ - test/doc/schemas/account_post_request_body.schema.json
102
108
  - test/test_helper.rb
103
109
  - test/test_json_schema.rb
104
110
  - test/test_json_schema_parser.rb
@@ -128,6 +134,12 @@ signing_key:
128
134
  specification_version: 4
129
135
  summary: A rack-based swagger-ui middleware and commandline utility.
130
136
  test_files:
137
+ - test/doc/account.yml
138
+ - test/doc/api-docs.yml
139
+ - test/doc/schemas/account_get_request_body.schema.json
140
+ - test/doc/schemas/account_post_creation_failure.schema.json
141
+ - test/doc/schemas/account_post_creation_success.schema.json
142
+ - test/doc/schemas/account_post_request_body.schema.json
131
143
  - test/test_helper.rb
132
144
  - test/test_json_schema.rb
133
145
  - test/test_json_schema_parser.rb