swagger-core 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,134 @@
1
+ swagger: 2.0
2
+ info:
3
+ version: 1.0.0
4
+ title: Swagger Petstore
5
+ description: |
6
+ A sample API that uses a petstore as an example to demonstrate
7
+ features in the swagger-2.0 specification
8
+ termsOfService: http://helloreverb.com/terms/ # Note: require TOS to be URL again?
9
+ contact:
10
+ name: Wordnik API Team
11
+ url: http://madskristensen.net
12
+ email: foo@example.com
13
+ license:
14
+ name: MIT
15
+ url: http://choosealicense.com/licenses/mit/
16
+ host: petstore.swagger.wordnik.com
17
+ basePath: /api
18
+ schemes:
19
+ - http
20
+ - https
21
+ - ws
22
+ - wss
23
+ consumes:
24
+ - application/json
25
+ - application/xml
26
+ produces:
27
+ - application/vnd.max+json
28
+ - application/xml
29
+ paths:
30
+ /pets:
31
+ parameters:
32
+ - name: foo
33
+ in: query
34
+ description: foos to bar by
35
+ required: false
36
+ type: array
37
+ format: csv
38
+ items:
39
+ type: string
40
+ collectionFormat: something # what other formats exist?
41
+ get:
42
+ tags:
43
+ - Things
44
+ - That
45
+ - Do
46
+ - Stuff
47
+ summary: Gets pets
48
+ description: |-
49
+ Returns all pets from the system that the user has access to
50
+ Nam sed condimentum est. Maecenas tempor sagittis sapien, nec rhoncus sem sagittis sit amet. Aenean at gravida augue, ac iaculis sem. Curabitur odio lorem, ornare eget elementum nec, cursus id lectus. Duis mi turpis, pulvinar ac eros ac, tincidunt varius justo. In hac habitasse platea dictumst. Integer at adipiscing ante, a sagittis ligula. Aenean pharetra tempor ante molestie imperdiet. Vivamus id aliquam diam. Cras quis velit non tortor eleifend sagittis. Praesent at enim pharetra urna volutpat venenatis eget eget mauris. In eleifend fermentum facilisis. Praesent enim enim, gravida ac sodales sed, placerat id erat. Suspendisse lacus dolor, consectetur non augue vel, vehicula interdum libero. Morbi euismod sagittis libero sed lacinia.
51
+
52
+ Sed tempus felis lobortis leo pulvinar rutrum. Nam mattis velit nisl, eu condimentum ligula luctus nec. Phasellus semper velit eget aliquet faucibus. In a mattis elit. Phasellus vel urna viverra, condimentum lorem id, rhoncus nibh. Ut pellentesque posuere elementum. Sed a varius odio. Morbi rhoncus ligula libero, vel eleifend nunc tristique vitae. Fusce et sem dui. Aenean nec scelerisque tortor. Fusce malesuada accumsan magna vel tempus. Quisque mollis felis eu dolor tristique, sit amet auctor felis gravida. Sed libero lorem, molestie sed nisl in, accumsan tempor nisi. Fusce sollicitudin massa ut lacinia mattis. Sed vel eleifend lorem. Pellentesque vitae felis pretium, pulvinar elit eu, euismod sapien.
53
+ operationId: Get Pets
54
+ produces:
55
+ - application/json
56
+ parameters:
57
+ - name: tags
58
+ in: query
59
+ description: tags to filter by
60
+ required: false
61
+ type: array
62
+ format: csv
63
+ items:
64
+ type: string
65
+ collectionFormat: something
66
+ - name: limit
67
+ in: query
68
+ description: maximum number of results to return
69
+ required: false
70
+ type: integer
71
+ format: int32
72
+ collectionFormat: something
73
+ responses:
74
+ 200:
75
+ description: pet response
76
+ schema:
77
+ type: array
78
+ items:
79
+ Pet:
80
+ $ref: '#/models/Pet'
81
+ headers:
82
+ - is-dog:
83
+ type: boolean
84
+ - is-cat:
85
+ type: boolean
86
+ examples:
87
+ application/json:
88
+ id: 9
89
+ category:
90
+ name: domestic
91
+ name: monster
92
+ tags:
93
+ - name: for sale
94
+ status: alive
95
+ default:
96
+ description: pet response (default)
97
+ schema:
98
+ type: array
99
+ items:
100
+ Pet:
101
+ $ref: '#/models/Pet'
102
+ headers:
103
+ - is-dog:
104
+ type: boolean
105
+ - is-cat:
106
+ type: boolean
107
+ examples:
108
+ application/json:
109
+ id: 9
110
+ category:
111
+ name: domestic
112
+ name: monster
113
+ tags:
114
+ - name: for sale
115
+ status: alive
116
+ schemes:
117
+ - http
118
+ - https
119
+ definitions:
120
+ Pet:
121
+ required:
122
+ - id
123
+ - name
124
+ properties:
125
+ id:
126
+ type: integer
127
+ format: int64
128
+ name:
129
+ type: string
130
+ tag:
131
+ type: string
132
+ security: #
133
+ - No clear
134
+ - Definition here
@@ -0,0 +1,100 @@
1
+ swagger: 2.0
2
+ info:
3
+ version: 1.0.0
4
+ title: Swagger Petstore
5
+ license:
6
+ name: MIT
7
+ host: petstore.swagger.wordnik.com
8
+ basePath: /v1
9
+ schemes:
10
+ - http
11
+ consumes:
12
+ - application/json
13
+ produces:
14
+ - application/json
15
+ paths:
16
+ /pets:
17
+ get:
18
+ summary: List all pets
19
+ operationId: listPets
20
+ tags:
21
+ - pets
22
+ parameters:
23
+ - name: limit
24
+ in: query
25
+ description: How many items to return at one time (max 100)
26
+ required: false
27
+ type: integer
28
+ format: int32
29
+ responses:
30
+ 200:
31
+ description: An paged array of pets
32
+ headers:
33
+ - x-next:
34
+ type: string
35
+ description: A link to the next page of responses
36
+ schema:
37
+ $ref: Pets
38
+ default:
39
+ description: unexpected error
40
+ schema:
41
+ $ref: Error
42
+ post:
43
+ summary: Create a pet
44
+ operationId: createPets
45
+ tags:
46
+ - pets
47
+ responses:
48
+ 201:
49
+ description: Null response
50
+ default:
51
+ description: unexpected error
52
+ schema:
53
+ $ref: Error
54
+ /pets/{petId}:
55
+ get:
56
+ summary: Info for a specific pet
57
+ operationId: showPetById
58
+ tags:
59
+ - pets
60
+ parameters:
61
+ - name: petId
62
+ in: path
63
+ description: The id of the pet to retrieve
64
+ type: string
65
+ responses:
66
+ 200:
67
+ description: Expected response to a valid request
68
+ schema:
69
+ $ref: Pets
70
+ default:
71
+ description: unexpected error
72
+ schema:
73
+ $ref: Error
74
+ definitions:
75
+ Pet:
76
+ required:
77
+ - id
78
+ - name
79
+ properties:
80
+ id:
81
+ type: integer
82
+ format: int64
83
+ name:
84
+ type: string
85
+ tag:
86
+ type: string
87
+ Pets:
88
+ type: array
89
+ items:
90
+ $ref: Pet
91
+ Error:
92
+ required:
93
+ - code
94
+ - message
95
+ properties:
96
+ code:
97
+ type: integer
98
+ format: int32
99
+ message:
100
+ type: string
@@ -0,0 +1,2 @@
1
+ # require 'webmock' # This will disable HTTP connections
2
+ require 'swagger'
@@ -0,0 +1,88 @@
1
+ require 'spec_helper'
2
+
3
+ module Swagger
4
+ module V2
5
+ describe APIDeclaration do
6
+ let(:swagger_file) { 'spec/fixtures/petstore-full.yaml' }
7
+ let(:swagger) { Swagger.load swagger_file }
8
+ let(:expected_host) { 'petstore.swagger.wordnik.com' }
9
+ let(:expected_basePath) { '/api' }
10
+
11
+ context 'extras' do
12
+ # These are utility methods, not part of the Swagger specification
13
+ describe '#validate' do
14
+ it 'returns true if the Swagger definition complies with the Swagger schema' do
15
+ expect(swagger.validate).to eq(true)
16
+ end
17
+
18
+ it 'raises an exception if the Swagger definition does not comply with the Swagger schema' do
19
+ swagger.swagger = 2.1
20
+ expect { swagger.validate }.to raise_error(Swagger::InvalidDefinition)
21
+ end
22
+ end
23
+
24
+ describe '#fully_validate' do
25
+ it 'returns true if the Swagger definition complies with the Swagger schema' do
26
+ expect(swagger.fully_validate).to eq(true)
27
+ end
28
+
29
+ it 'raises an exception if the Swagger definition does not comply with the Swagger schema' do
30
+ swagger.swagger = 2.1
31
+ expect { swagger.fully_validate }.to raise_error(Swagger::InvalidDefinition)
32
+ end
33
+ end
34
+
35
+ describe '#uri_template', extension: true do
36
+ it 'is combines the host and basePath into a single template' do
37
+ # expect(swagger.uri_template).to eq(Addressable::Template.new("#{expected_host}#{expected_basePath}"))
38
+ expect(swagger.uri_template).to eq("#{expected_host}#{expected_basePath}")
39
+ end
40
+ end
41
+ end
42
+
43
+ context 'Sample petstore API' do
44
+ describe '#swagger' do
45
+ subject { swagger.swagger }
46
+ it { is_expected.to eq(2.0) }
47
+ end
48
+
49
+ describe '#info' do
50
+ subject { swagger.info }
51
+ it { is_expected.to be_a_kind_of Swagger::V2::Info }
52
+ # See info_spec for more
53
+ end
54
+
55
+ describe '#host' do
56
+ subject { swagger.host }
57
+ pending { is_expected.to be_a_kind_of Addressable::Template }
58
+ it { is_expected.to eq(expected_host) }
59
+ end
60
+
61
+ describe '#basePath' do
62
+ subject { swagger.basePath }
63
+ pending { is_expected.to be_a_kind_of Addressable::Template }
64
+ it { is_expected.to eq(expected_basePath) }
65
+ end
66
+
67
+ describe '#schemes' do
68
+ subject { swagger.schemes }
69
+ it { is_expected.to eq(%w(http https ws wss)) }
70
+ end
71
+
72
+ describe '#consumes' do
73
+ subject { swagger.consumes }
74
+ it { is_expected.to eq(%w(application/json application/xml)) }
75
+ end
76
+
77
+ describe '#produces' do
78
+ subject { swagger.produces }
79
+ it { is_expected.to eq(%w(application/vnd.max+json application/xml)) }
80
+ end
81
+
82
+ skip '#paths'
83
+ skip '#definitions'
84
+ skip '#security'
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,91 @@
1
+ require 'spec_helper'
2
+
3
+ module Swagger
4
+ module V2
5
+ describe APIOperation do
6
+ context 'Sample petstore API' do
7
+ let(:swagger_file) { 'spec/fixtures/petstore-full.yaml' }
8
+ let(:swagger) { Swagger.load swagger_file }
9
+ subject { swagger.paths['/pets'].get }
10
+
11
+ context 'calculated fields' do # not defined directly in the swagger spec
12
+ describe '#parent' do
13
+ subject { swagger.paths['/pets'].get.parent }
14
+ it { is_expected.to eq(swagger.paths['/pets']) }
15
+ end
16
+
17
+ describe '#uri_template' do
18
+ it 'is calculated from the host, basePath, and path' do
19
+ expect(subject.uri_template).to eq('petstore.swagger.wordnik.com/api/pets')
20
+ end
21
+ end
22
+ end
23
+
24
+ describe '#tags' do
25
+ subject { swagger.paths['/pets'].get.tags }
26
+ it { is_expected.to eq(%w(Things That Do Stuff)) }
27
+ end
28
+
29
+ describe '#summary' do
30
+ subject { swagger.paths['/pets'].get.summary }
31
+ it { is_expected.to eq('Gets pets') }
32
+ end
33
+
34
+ # rubocop:disable Metrics/LineLength
35
+ describe '#description' do
36
+ subject { swagger.paths['/pets'].get.description }
37
+ it do
38
+ expected = <<-'EOS'.gsub(/^[ \t]+/, '').strip
39
+ Returns all pets from the system that the user has access to
40
+ Nam sed condimentum est. Maecenas tempor sagittis sapien, nec rhoncus sem sagittis sit amet. Aenean at gravida augue, ac iaculis sem. Curabitur odio lorem, ornare eget elementum nec, cursus id lectus. Duis mi turpis, pulvinar ac eros ac, tincidunt varius justo. In hac habitasse platea dictumst. Integer at adipiscing ante, a sagittis ligula. Aenean pharetra tempor ante molestie imperdiet. Vivamus id aliquam diam. Cras quis velit non tortor eleifend sagittis. Praesent at enim pharetra urna volutpat venenatis eget eget mauris. In eleifend fermentum facilisis. Praesent enim enim, gravida ac sodales sed, placerat id erat. Suspendisse lacus dolor, consectetur non augue vel, vehicula interdum libero. Morbi euismod sagittis libero sed lacinia.
41
+
42
+ Sed tempus felis lobortis leo pulvinar rutrum. Nam mattis velit nisl, eu condimentum ligula luctus nec. Phasellus semper velit eget aliquet faucibus. In a mattis elit. Phasellus vel urna viverra, condimentum lorem id, rhoncus nibh. Ut pellentesque posuere elementum. Sed a varius odio. Morbi rhoncus ligula libero, vel eleifend nunc tristique vitae. Fusce et sem dui. Aenean nec scelerisque tortor. Fusce malesuada accumsan magna vel tempus. Quisque mollis felis eu dolor tristique, sit amet auctor felis gravida. Sed libero lorem, molestie sed nisl in, accumsan tempor nisi. Fusce sollicitudin massa ut lacinia mattis. Sed vel eleifend lorem. Pellentesque vitae felis pretium, pulvinar elit eu, euismod sapien.
43
+ EOS
44
+ is_expected.to eq(expected)
45
+ end
46
+ end
47
+ # rubocop:enable Metrics/LineLength
48
+
49
+ describe '#operationId' do
50
+ subject { swagger.paths['/pets'].get.operationId }
51
+ it { is_expected.to eq('Get Pets') }
52
+ end
53
+
54
+ # TODO: Implement alias #operation_id
55
+
56
+ describe '#produces' do
57
+ subject { swagger.paths['/pets'].get.produces }
58
+ it { is_expected.to eq(%w(application/json)) }
59
+ end
60
+
61
+ skip '#consumes' # swagger spec update needed
62
+ describe '#parameters' do
63
+ subject { swagger.paths['/pets'].get.parameters }
64
+ it { is_expected.to be_an(Array) }
65
+ it 'contains Parameter objects' do
66
+ expect(subject.size).to eq(2)
67
+ subject.each do |parameter|
68
+ expect(parameter).to be_a_kind_of(Parameter)
69
+ end
70
+ end
71
+ end
72
+ skip '#resources' # complex type
73
+
74
+ describe '#schemes' do
75
+ subject { swagger.paths['/pets'].get.schemes }
76
+ it { is_expected.to eq(%w(http https)) }
77
+ end
78
+
79
+ describe '#default_response' do
80
+ subject { swagger.paths['/pets'].get.default_response }
81
+ it 'is selects the response labeled default' do
82
+ # Turns out a lot of examples are using "default" more like unknown error (i.e. else)
83
+ # rather than the default successful response.
84
+ # expect(subject.description).to eq('pet response (default)')
85
+ expect(subject.description).to eq('pet response')
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,76 @@
1
+ require 'spec_helper'
2
+
3
+ module Swagger
4
+ module V2
5
+ describe Info do
6
+ context 'Sample petstore API' do
7
+ let(:swagger_file) { 'spec/fixtures/petstore-full.yaml' }
8
+ let(:swagger) { Swagger.load swagger_file }
9
+ subject { swagger.info }
10
+
11
+ describe '#version' do
12
+ subject { swagger.info.version }
13
+ it { is_expected.to eq('1.0.0') }
14
+ end
15
+
16
+ describe '#title' do
17
+ subject { swagger.info.title }
18
+ it { is_expected.to eq('Swagger Petstore') }
19
+ end
20
+
21
+ describe '#description' do
22
+ subject { swagger.info.description }
23
+ let(:expected) do
24
+ [
25
+ 'A sample API that uses a petstore as an example to demonstrate',
26
+ "features in the swagger-2.0 specification\n"
27
+ ].join("\n")
28
+ end
29
+ it { is_expected.to eq(expected) }
30
+ end
31
+
32
+ describe '#termsOfService' do
33
+ subject { swagger.info.termsOfService }
34
+ it { is_expected.to eq('http://helloreverb.com/terms/') }
35
+ pending "Spec isn't clear if this should be URL, plaintext, or Markdown"
36
+ end
37
+
38
+ describe '#contact' do
39
+ subject { swagger.info.contact }
40
+
41
+ describe '#name' do
42
+ subject { swagger.info.contact.name }
43
+ it { is_expected.to eq('Wordnik API Team') }
44
+ end
45
+
46
+ describe '#url' do
47
+ subject { swagger.info.contact.url }
48
+ pending { is_expected.to be_a_kind_of(Addressable::URI) }
49
+ it { is_expected.to eq('http://madskristensen.net') }
50
+ end
51
+
52
+ describe '#email' do
53
+ subject { swagger.info.contact.email }
54
+ pending 'Is there a more appropriate class for email?'
55
+ it { is_expected.to eq('foo@example.com') }
56
+ end
57
+ end
58
+
59
+ describe '#license' do
60
+ subject { swagger.info.license }
61
+
62
+ describe '#name' do
63
+ subject { swagger.info.license.name }
64
+ it { is_expected.to eq('MIT') }
65
+ end
66
+
67
+ describe '#url' do
68
+ subject { swagger.info.license.url }
69
+ pending { is_expected.to be_a_kind_of(Addressable::URI) }
70
+ it { is_expected.to eq('http://choosealicense.com/licenses/mit/') }
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end