swagger_yard 0.4.4 → 1.0.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: 75d0e74a6ac517d09a0743edffa6ea2aa7ddf963
4
- data.tar.gz: cbda242b96ac6e12646362d573f93b6becc050d9
3
+ metadata.gz: 9e95fe3850f449db0d3234e6b314bf76c3570f7d
4
+ data.tar.gz: 9f2e1c1a9d2a3731431e6d606a4023d7ae692326
5
5
  SHA512:
6
- metadata.gz: 52ed18981ab935c658185407e57a65b70c8ecd711c844d1dcfd84dee2c21064dc98717d498f8101d169ed6bd9cf4fb9a80e4f6f442491d76919523d84f5cea90
7
- data.tar.gz: ab50c39b04875a413514fda5a8ece2d021421768f97a42009f52fdc0f30f940bd910272f598efae73690cfa4054db4e3386d43622f02968c66dbc9ab8b6396ae
6
+ metadata.gz: 3609ee174c38e70038f0358b735c9c1ffdf73ca76c634d9e2f93f1d42f497d6f752b3cf0b1c9f18d58ce68ff426d8f254b09fb76da748dc8f2a5e87d3ea85b38
7
+ data.tar.gz: de11893ad4b1293ad2955617ebd967c2030d7347beb15ea6ac486de6769f0a12fa2e334822dae4c2790b2c83b95abe8f251f12cce897209bab2106d9d1217bd5
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # SwaggerYard [![Build Status](https://travis-ci.org/livingsocial/swagger_yard.svg?branch=master)](https://travis-ci.org/tpitale/swagger_yard) #
1
+ # SwaggerYard [![Build Status](https://travis-ci.org/livingsocial/swagger_yard.svg?branch=master)](https://travis-ci.org/livingsocial/swagger_yard) #
2
2
 
3
- SwaggerYard is a gem to convert extended YARD syntax comments into the swagger spec compliant json format.
3
+ SwaggerYard is a gem to convert custom YARD tags in comments into Swagger 2.0 or OpenAPI 3.0.0 specs.
4
4
 
5
5
  ## Installation ##
6
6
 
@@ -15,89 +15,52 @@ Install the gem with Bunder:
15
15
 
16
16
  ## Getting Started ##
17
17
 
18
- ### Place configuration in a rails initializer ###
18
+ Place configuration in a Rails initializer or suitable configuration file:
19
19
 
20
20
  # config/initializers/swagger_yard.rb
21
21
  SwaggerYard.configure do |config|
22
22
  config.api_version = "1.0"
23
- config.reload = Rails.env.development?
23
+
24
+ config.title = 'Your API'
25
+ config.description = 'Your API does this'
24
26
 
25
27
  # where your actual api is hosted from
26
28
  config.api_base_path = "http://localhost:3000/api"
27
- end
28
-
29
- ## SwaggerYard Usage ##
30
-
31
- ### Types ###
32
29
 
33
- Types of things (parameters or responses of an operation, properties of a model)
34
- are indicated inside square-brackets (e.g., `[string]`) as part of a YARD tag.
30
+ # Where to find controllers (can be an array of paths/globs)
31
+ config.controller_path = ::Rails.root + 'app/controllers/**/*'
35
32
 
36
- - Model references should be Capitalized or CamelCased by convention.
37
- - Basic types (integer, boolean, string, object, number, date, time, date-time,
38
- uuid, etc.) should be lowercased.
39
- - An array of models or basic types is specified with `[array<...>]`.
40
- - An enum of allowed string values is specified with `[enum<one,two,three>]`.
41
- - An object definition can include the property definitions of its fields, and / or of an additional property for any remaining allowed fields. E.g., `[object<name: string, age: integer, string >]`
42
- - Structured data like objects, arrays, pairs, etc., definitions can also be nested; E.g., `[object<pairs:array<object<right:integer,left:integer>>>]`
43
- - JSON-Schema `format` attributes can be specified for basic types using
44
- `<...>`. For example, `[integer<int64>]` produces JSON
45
- `{ "type": "integer", "format": "int64" }`.
46
- - Regex pattern constraints can be specified for strings using
47
- `[regex<PATTERN>]`. For example, `[regex<^.{3}$>]` produces JSON
48
- `{ "type": "string", "pattern": "^.{3}$" }`.
49
-
50
- ### External Schema ###
33
+ # Where to find models (can be an array)
34
+ config.model_path = ::Rails.root + 'app/decorators/**/*'
35
+ end
51
36
 
52
- Types can be specified that refer to external JSON schema documents for their definition. External schema documents are expected to also define their models under a `definitions` top-level key like so:
53
- ```
54
- {
55
- "definitions": {
56
- "MyStandardModel": {
57
- }
58
- }
59
- }
60
- ```
37
+ Then start to annotate controllers and models as described below.
61
38
 
62
- To register an external schema so that it can be referenced in places where you specify a type, configure SwaggerYard as follows:
63
- ```ruby
64
- SwaggerYard.configure do |config|
65
- config.external_schema mymodels: 'https://example.com/mymodels/v1.0'
66
- end
67
- ```
39
+ ### Generate Specification ###
68
40
 
69
- Then refer to models in the schema using the syntax `[mymodels#MyStandardModel]` where types are specified. This causes SwaggerYard to emit the following schema for the type:
41
+ To generate a Swagger or OpenAPI specification, use one of the `SwaggerYard::Swagger` or `SwaggerYard::OpenAPI` classes as follows in a script or Rake task (or use [swagger_yard-rails](/livingsocial/swagger_yard-rails)):
70
42
 
71
43
  ```
72
- { "$ref": "https://example.com/mymodels/v1.0#/definitions/MyStandardModel" }
44
+ spec = SwaggerYard::OpenAPI.new
45
+ # Generate YAML
46
+ File.open("openapi.yml", "w") { |f| f << YAML.dump(spec.to_h) }
47
+ # Generate JSON
48
+ File.open("openapi.json, "w") { |f| f << JSON.pretty_generate(spec.to_h) }
73
49
  ```
74
50
 
75
- ### Options ###
51
+ ## Documenting APIs
76
52
 
77
- Parameter or property _options_ are expressed inside parenthesis immediately
78
- following the parameter or property name.
53
+ Documenting controllers and models is best illustrated by example.
79
54
 
80
- Examples:
55
+ ### Controller ###
81
56
 
82
- # @parameter name(required) [string] Name of the package
83
- # @parameter age(nullable) [integer] Age of package
84
- # @parameter package(body) [Package] Package object
57
+ Each Swagger-ized controller must have a `@resource` tag naming the API to be documented. Without this tag, no endpoints will be generated.
85
58
 
86
- Possible parameters include:
87
-
88
- - `required`: indicates a required parameter or property.
89
- - `nullable`: indicates that JSON `null` is an allowed value for the property.
90
- - `multiple`: indicates a parameter may appear multiple times (usually in a
91
- query string, e.g., `param=a&param=b&param=c`)
92
- - `body`/`query`/`path`/`formData`: Indicates where the parameter is located.
93
-
94
- ### Example of using SwaggerYard in a Controller ###
59
+ Then, document each controller action method that is an endpoint of the API. Each endpoint needs to have a `@path` tag at a minimum. `@parameter` tags and `@response_type`/`@response` tags specify the inputs and outputs to the endpoint. A request body is specified with the use of a single `@parameter` tag with a `(body)` option. (`@response_type` is shorthand for the type of the default response, while `@response` allows you to specify the HTTP status.)
95
60
 
96
61
  ```ruby
97
62
  # @resource Account ownership
98
63
  #
99
- # @resource_path /accounts/ownerships
100
- #
101
64
  # This document describes the API for creating, reading, and deleting account ownerships.
102
65
  #
103
66
  class Accounts::OwnershipsController < ActionController::Base
@@ -118,7 +81,6 @@ class Accounts::OwnershipsController < ActionController::Base
118
81
  # @parameter end_at_less [date] Filters response to include only items with end_at <= specified timestamp (e.g. end_at_less=2012-02-15T02:06:56Z).
119
82
  #
120
83
  def index
121
- ...
122
84
  end
123
85
 
124
86
  ##
@@ -126,16 +88,26 @@ class Accounts::OwnershipsController < ActionController::Base
126
88
  #
127
89
  # @path [GET] /accounts/ownerships/{id}
128
90
  # @response_type [Ownership]
129
- # @error_message [EmptyOwnership] 404 Ownership not found
130
- # @error_message 400 Invalid ID supplied
91
+ # @response [EmptyOwnership] 404 Ownership not found
92
+ # @response 400 Invalid ID supplied
131
93
  #
132
94
  def show
133
- ...
95
+ end
96
+
97
+ ##
98
+ # Creates an ownership for an account
99
+ #
100
+ # @path [POST] /accounts/ownerships
101
+ # @parameter ownership(body) [Ownership] The ownership to be created
102
+ # @response_type [Ownership]
103
+ def create
134
104
  end
135
105
  end
136
106
  ```
137
107
 
138
- ### Example of using SwaggerYard in a Model ###
108
+ ### Model ###
109
+
110
+ Each model to be exposed in the specification must have a `@model` tag. Model properties are specified with `@property` tags.
139
111
 
140
112
  ```ruby
141
113
  #
@@ -192,10 +164,132 @@ module Models
192
164
  end
193
165
  ```
194
166
 
167
+ ### Types ###
168
+
169
+ Types of things (parameters or responses of an operation, properties of a model) are indicated inside square-brackets (e.g., `[string]`) as part of a YARD tag.
170
+
171
+ - Model references should be Capitalized or CamelCased by convention.
172
+ - Basic types (integer, boolean, string, object, number, date, time, date-time, uuid, etc.) should be lowercased.
173
+ - An array of models or basic types is specified with `[array<...>]`.
174
+ - An enum of allowed string values is specified with `[enum<one,two,three>]`.
175
+ - An object definition can include the property definitions of its fields, and / or of an additional property for any remaining allowed fields. E.g., `[object<name: string, age: integer, string >]`
176
+ - Structured data like objects, arrays, pairs, etc., definitions can also be nested; E.g., `[object<pairs:array<object<right:integer,left:integer>>>]`
177
+ - JSON-Schema `format` attributes can be specified for basic types using `<...>`. For example, `[integer<int64>]` produces JSON `{ "type": "integer", "format": "int64" }`.
178
+ - Regex pattern constraints can be specified for strings using `[regex<PATTERN>]`. For example, `[regex<^.{3}$>]` produces JSON `{ "type": "string", "pattern": "^.{3}$" }`.
179
+
180
+ ### Options ###
181
+
182
+ Parameter or property _options_ are expressed inside parenthesis immediately
183
+ following the parameter or property name.
184
+
185
+ Examples:
186
+
187
+ # @parameter name(required) [string] Name of the package
188
+ # @parameter age(nullable) [integer] Age of package
189
+ # @parameter package(body) [Package] Package object
190
+
191
+ Possible parameters include:
192
+
193
+ - `required`: indicates a required parameter or property.
194
+ - `nullable`: indicates that JSON `null` is an allowed value for the property.
195
+ - `multiple`: indicates a parameter may appear multiple times (usually in a
196
+ query string, e.g., `param=a&param=b&param=c`)
197
+ - `body`/`query`/`path`/`formData`: Indicates where the parameter is located.
198
+
199
+ ### Examples
200
+
201
+ The Swagger and OpenAPI specs both allow for [specifying example data](https://swagger.io/docs/specification/adding-examples/) at multiple levels. SwaggerYard allows you to use an `@example` tag to specify example JSON data at the response, model, and individual property levels.
202
+
203
+ The basic format of an `@example` is:
204
+
205
+ ```ruby
206
+ # @example [name]
207
+ # body content
208
+ # should be indented
209
+ # and can span
210
+ # multiple lines
211
+ ```
212
+
213
+ #### Response examples
214
+
215
+ Response examples should appear in a method documentation block inside a controller, alongside the parameters and response tags. Use a named `@example` to associate the example with a specific response, or use an unnammed example to associate the data to the default response (when using a `@response_type` tag).
216
+
217
+ ```ruby
218
+ # return a Pet
219
+ # @path [GET] /pets/{id}
220
+ # @parameter id [integer] The ID for the Pet
221
+ # @response_type [Pet]
222
+ # @response [ErrorPet] 404 Pet not found
223
+ # @example
224
+ # {"id": 1, "names": ["Fido"], "age": 12}
225
+ # @example 404
226
+ # {"error": 404, "message": "Pet not found"}
227
+ def show
228
+ end
229
+ ```
230
+
231
+ #### Model examples
232
+
233
+ Use a model example to specify an example for the entire model at once. The example tag should omit any name to associate the data with the model itself and not a single property.
234
+
235
+ ```ruby
236
+ # @model
237
+ # @property id(required) [integer]
238
+ # @property name [string]
239
+ # @example
240
+ # {"id": 42, "name": "Fred Flintstone"}
241
+ class Person
242
+ end
243
+ ```
244
+
245
+ ### Property examples
246
+
247
+ Use property examples to specify data for individual properties. To associate the example data, the `@example` tag must use the same name as the property and appear _after_ the property.
248
+
249
+ ```ruby
250
+ # @model
251
+ # @property id(required) [integer]
252
+ # @example id
253
+ # 42
254
+ # @property name [string]
255
+ # @example name
256
+ # "Fred Flintstone"
257
+ class Person
258
+ end
259
+ ```
260
+
261
+
262
+ ### External Schema ###
263
+
264
+ Types can be specified that refer to external JSON schema documents for their definition. External schema documents are expected to also define their models under a `definitions` top-level key like so:
265
+ ```
266
+ {
267
+ "definitions": {
268
+ "MyStandardModel": {
269
+ }
270
+ }
271
+ }
272
+ ```
273
+
274
+ To register an external schema so that it can be referenced in places where you specify a type, configure SwaggerYard as follows:
275
+ ```ruby
276
+ SwaggerYard.configure do |config|
277
+ config.external_schema mymodels: 'https://example.com/mymodels/v1.0'
278
+ end
279
+ ```
280
+
281
+ Then refer to models in the schema using the syntax `[mymodels#MyStandardModel]` where types are specified. This causes SwaggerYard to emit the following schema for the type:
282
+
283
+ ```
284
+ { "$ref": "https://example.com/mymodels/v1.0#/definitions/MyStandardModel" }
285
+ ```
286
+
287
+
195
288
  ## Authorization ##
289
+
196
290
  ### API Key auth ###
197
291
 
198
- SwaggerYard supports API Key auth descriptions. Start by adding `@authorization` to your `ApplicationController`.
292
+ SwaggerYard supports several authorization styles. Start by adding `@authorization` to your `ApplicationController`.
199
293
 
200
294
  ```ruby
201
295
  #
@@ -205,7 +299,7 @@ class ApplicationController < ActionController::Base
205
299
  end
206
300
  ```
207
301
 
208
- Then you can use these authorizations from your controller or actions in a controller. The name comes from either header or query plus the name of the key downcased/underscored.
302
+ Then you can use these authorizations from your controller or actions in a controller.
209
303
 
210
304
  ```ruby
211
305
  #
@@ -215,16 +309,38 @@ class PetController < ApplicationController
215
309
  end
216
310
  ```
217
311
 
218
- ### Custom security definitions (OAuth2) ###
312
+ Supported formats for the `@authorization` tag are as follows:
313
+
314
+ ```ruby
315
+ # @authorization [apiKey] (query|header|cookie) key-name The rest is a description
316
+ # @authorization [bearer] mybearerauth bearer-format The rest is a description
317
+ # @authorization [basic] mybasicauth The rest is a description
318
+ # @authorization [digest] digestauth The rest is a description
319
+ # @authorization [<any-rfc7235-auth>] myrfcauth The rest is a description
219
320
 
220
- Additionally SwaggerYard also supports custom [security definitions](http://swagger.io/specification/#securityDefinitionsObject). You can define these in your configuration like:
321
+ - For `apiKey` the name of the authorization is formed as `"#{location}_#{key_name}".downcase.gsub('-','_')`.
322
+ Example: `@authorization [apiKey] header X-API-Key` is named `header_x_api_key`. (This naming scheme is kept for backwards compatibility.)
323
+
324
+ - All others are named by the tag name following the `[type]`.
325
+ Example: `@authorization [bearer] myBearerAuth Format Description` is named `myBearerAuth`.
326
+
327
+ ### Custom security schemes ###
328
+
329
+ SwaggerYard also supports custom [security schemes](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#security-scheme-object). You can define these in your configuration like:
221
330
 
222
331
  ```ruby
223
332
  SwaggerYard.configure do |config|
224
- config.security_definitions['petstore_oauth'] = {
333
+ config.security_schemes['petstore_oauth'] = {
225
334
  type: "oauth2",
226
- authorizationUrl: "http://swagger.io/api/oauth/dialog",
227
- flow: :implicit
335
+ flows: {
336
+ implicit: {
337
+ authorizationUrl: "http://swagger.io/api/oauth/dialog",
338
+ scopes: {
339
+ "write:pets": "modify pets in your account",
340
+ "read:pets": "read your pets"
341
+ }
342
+ }
343
+ }
228
344
  }
229
345
  end
230
346
  ```
@@ -237,38 +353,11 @@ class PetController < ApplicationController
237
353
  end
238
354
  ```
239
355
 
240
- ## UI ##
241
-
242
- We suggest using something like [swagger-ui_rails](https://github.com/3scale/swagger-ui_rails/tree/dev-2.1.3) for your UI needs inside of Rails.
243
356
 
244
- To generate JSON from your code on request, checkout the [swagger_yard-rails](https://github.com/livingsocial/swagger_yard-rails) project. This provides an engine to parse and render the json required for use by swagger-ui_rails.
245
-
246
- ## More Information ##
247
-
248
- * [swagger-ui_rails](https://github.com/3scale/swagger-ui_rails/tree/dev-2.1.3)
249
- * [swagger_yard-rails](https://github.com/livingsocial/swagger_yard-rails)
250
- * [Swagger-spec version 2.0](https://github.com/wordnik/swagger-spec/blob/master/versions/2.0.md)
251
- * [Yard](https://github.com/lsegal/yard)
357
+ ### Better Rails integration with swagger_yard-rails
252
358
 
253
- ## Current Parsing "Tree" Structure ##
359
+ To generate specifications from your Rails app on request, check out the [swagger_yard-rails](https://github.com/livingsocial/swagger_yard-rails) project. This provides an engine that has a mountable endpoint that will parse the source code and render the specification as a json document.
254
360
 
255
- ```
256
- ResourceListing
257
- |
258
- -> ApiDeclaration (controller)
259
- | |
260
- | -> ListingInfo (controller class)
261
- | -> Authorization (header/param for auth, also added to ResourceListing?)
262
- | -> Api(s) (controller action, by path)
263
- | |
264
- | -> Operation(s) (controller action with HTTP method)
265
- | |
266
- | -> Parameter(s) (action param)
267
- |
268
- -> Model (model)
269
- |
270
- -> Properties (model attributes)
271
- ```
272
361
 
273
362
  ### Path Discovery Function ##
274
363
 
@@ -281,7 +370,7 @@ any can be determined.
281
370
  SwaggerYard.configure do |config|
282
371
  config.path_discovery_function = ->(yard_obj) do
283
372
  # code here to inspect the yard doc object
284
- # and return a [method, path] for the api
373
+ # and return a [method, path] for the api
285
374
  end
286
375
  end
287
376
  ```
@@ -289,4 +378,14 @@ end
289
378
  In [swagger_yard-rails][], this hook is used to set a function that inspects the
290
379
  Rails routing tables to reverse look up and compute paths.
291
380
 
381
+
382
+ ## More Information ##
383
+
384
+ * [swagger_yard-rails][]
385
+ * [Swagger-spec version 2.0](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md)
386
+ * [OpenAPI version 3.0.0](https://swagger.io/docs/specification/about/)
387
+ * [Yard](https://github.com/lsegal/yard)
388
+
389
+
292
390
  [swagger_yard-rails]: https://github.com/livingsocial/swagger_yard-rails
391
+
@@ -3,15 +3,17 @@ require "json"
3
3
  require "swagger_yard/configuration"
4
4
  require "swagger_yard/type"
5
5
  require "swagger_yard/type_parser"
6
+ require "swagger_yard/example"
6
7
  require "swagger_yard/parameter"
7
8
  require "swagger_yard/property"
8
9
  require "swagger_yard/operation"
9
10
  require "swagger_yard/authorization"
10
- require "swagger_yard/resource_listing"
11
- require "swagger_yard/api_declaration"
11
+ require "swagger_yard/specification"
12
+ require "swagger_yard/api_group"
12
13
  require "swagger_yard/model"
13
- require "swagger_yard/api"
14
+ require "swagger_yard/path_item"
14
15
  require "swagger_yard/swagger"
16
+ require "swagger_yard/openapi"
15
17
 
16
18
  module SwaggerYard
17
19
  class Error < StandardError; end
@@ -98,11 +100,11 @@ module SwaggerYard
98
100
  # Register some custom yard tags used by swagger-ui
99
101
  def register_custom_yard_tags!
100
102
  ::YARD::Tags::Library.define_tag("Api resource", :resource)
101
- ::YARD::Tags::Library.define_tag("Resource path", :resource_path) # TODO: remove deprecated tag
102
103
  ::YARD::Tags::Library.define_tag("Api path", :path, :with_types)
103
104
  ::YARD::Tags::Library.define_tag("Parameter", :parameter, :with_types_name_and_default)
104
105
  ::YARD::Tags::Library.define_tag("Response type", :response_type, :with_types)
105
106
  ::YARD::Tags::Library.define_tag("Error response message", :error_message, :with_types_and_name)
107
+ ::YARD::Tags::Library.define_tag("Response", :response, :with_types_and_name)
106
108
  ::YARD::Tags::Library.define_tag("Api Summary", :summary)
107
109
  ::YARD::Tags::Library.define_tag("Model resource", :model)
108
110
  ::YARD::Tags::Library.define_tag("Model superclass", :inherits)
@@ -110,6 +112,8 @@ module SwaggerYard
110
112
  ::YARD::Tags::Library.define_tag("Model discriminator", :discriminator, :with_types_name_and_default)
111
113
  ::YARD::Tags::Library.define_tag("Authorization", :authorization, :with_types_and_name)
112
114
  ::YARD::Tags::Library.define_tag("Authorization Use", :authorize_with)
115
+ # @example is a core YARD tag, let's use it
116
+ # ::YARD::Tags::Library.define_tag("Example", :example, :with_title_and_text)
113
117
  end
114
118
  end
115
119
  end