zero-rails_openapi 1.5.4 → 1.5.5

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: 8a1ad9dd33921ca9718211e6c4b3a4952e7c9462
4
- data.tar.gz: 5983dbcfc189d115bcd79b6b0495b77f450af71e
3
+ metadata.gz: e530c334773b9ef483102a2cd2c829704ac41ab4
4
+ data.tar.gz: 2cabf4dc3335eeea103fbca8863c01ba46b9988a
5
5
  SHA512:
6
- metadata.gz: 4d0b87971da06837b09304c04d8319bced499990d3634f8146cee33e87ee914c34995a648bc2b019606a8502c38759c593194e334a5745ea72be14c3b4f1c498
7
- data.tar.gz: 39055363665e740d3a88d2672622fbc5f5d80949dc1973b7d3346b4a52522a6e1ab86136d1584c517f0229af27a83b6784013a7dd9b63037a69f027870891183
6
+ metadata.gz: 1bee517abfc870ee6cac040f9a61cc03e724e9e240fdc3aa2b4a7ce217f99ed57724d00d00b00741a1694e75cba9e4368f554cdddddad6c6d407a607a3daba96
7
+ data.tar.gz: 73da6d4109dc89c1e079e83dac675099f203c83b00dad28e6da023a1cb1f97344feb0bb283d25e6f0a5ceff25c033cc2d703cac2f3d95240f986d329e420b9a5
data/CHANGELOG.md CHANGED
@@ -2,6 +2,16 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [1.5.5] - 2018/2/21 - [view diff](https://github.com/zhandao/zero-rails_openapi/compare/v1.5.4...v1.5.5)
6
+
7
+ ## Added
8
+
9
+ 1. Callback Object has been supported. (issue [#12](#https://github.com/zhandao/zero-rails_openapi/issues/12) @austbot)
10
+
11
+ ## Changed
12
+
13
+ 1. `mk`'s parameter `eq` is changed to `get`. (dssl.rb)
14
+
5
15
  ## [1.5.4] - 2018/2/15 - [view diff](https://github.com/zhandao/zero-rails_openapi/compare/v1.5.3...v1.5.4)
6
16
 
7
17
  Thanks to @austbot, fix - colorize fails at runtime.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- zero-rails_openapi (1.5.4)
4
+ zero-rails_openapi (1.5.5)
5
5
  activesupport (>= 3)
6
6
  colorize (= 0.8.1)
7
7
  rails (>= 3)
data/README.md CHANGED
@@ -4,8 +4,6 @@
4
4
  [![Build Status](https://travis-ci.org/zhandao/zero-rails_openapi.svg?branch=master)](https://travis-ci.org/zhandao/zero-rails_openapi)
5
5
  [![Maintainability](https://api.codeclimate.com/v1/badges/471fd60f6eb7b019ceed/maintainability)](https://codeclimate.com/github/zhandao/zero-rails_openapi/maintainability)
6
6
  [![Test Coverage](https://api.codeclimate.com/v1/badges/471fd60f6eb7b019ceed/test_coverage)](https://codeclimate.com/github/zhandao/zero-rails_openapi/test_coverage)
7
- [![Gitter Chat](https://badges.gitter.im/zero-rails_openapi/Lobby.svg)](https://gitter.im/zero-rails_openapi/Lobby)
8
- [![Help Contribute to Open Source](https://www.codetriage.com/zhandao/zero-rails_openapi/badges/users.svg)](https://www.codetriage.com/zhandao/zero-rails_openapi)
9
7
 
10
8
  Concise DSL for generating OpenAPI Specification 3 (**OAS3**, formerly Swagger3) JSON documentation for Rails application.
11
9
 
@@ -25,7 +23,20 @@
25
23
  - [Configure](#configure)
26
24
  - [Usage - DSL](#usage---dsl)
27
25
  - [Basic DSL](#basic-dsl)
26
+ - [route_base](#1-route_base-optional-if-youre-writing-dsl-in-controller)
27
+ - [doc_tag](#2-doc_tag-optional)
28
+ - [components](#3-components-optional)
29
+ - [api_dry](#4-api_dry-optional)
30
+ - [api](#5-api-required)
28
31
  - [DSL methods inside `api` and `api_dry`'s block](#dsl-methods-inside-api-and-api_drys-block)
32
+ - [this_api_is_invalid!](#1-this_api_is_invalid-its-aliases)
33
+ - [desc](#2-desc-description-for-the-current-api-and-its-inputs-parameters-and-request-body)
34
+ - [param family methods](#3-param-family-methods-oas---parameter-object)
35
+ - [request_body family methods](#4-request_body-family-methods-oas---request-body-object)
36
+ - [response family methods](#5-response-family-methods-oas---response-object)
37
+ - [callback](#6-callback-oas---callback-object)
38
+ - [Authentication and Authorization](#7-authentication-and-authorization)
39
+ - [server](#8-overriding-global-servers-by-server)
29
40
  - [DSL methods inside `components`'s block](#dsl-methods-inside-componentss-block-code-source)
30
41
  - [Run! - Generate JSON documentation file](#run---generate-json-documentation-file)
31
42
  - [Use Swagger UI(very beautiful web page) to show your Documentation](#use-swagger-uivery-beautiful-web-page-to-show-your-documentation)
@@ -45,7 +56,7 @@
45
56
 
46
57
  You can getting started from [swagger.io](https://swagger.io/docs/specification/basic-structure/)
47
58
 
48
- **I suggest you should understand OAS3's basic structure at least.**
59
+ **I suggest you should understand the basic structure of OAS3 at least.**
49
60
  such as component (can help you reuse DSL code, when your apis are used with the
50
61
  same data structure).
51
62
 
@@ -150,7 +161,8 @@
150
161
  ```
151
162
 
152
163
  For more example, see [goods_doc.rb](documentation/examples/goods_doc.rb), and
153
- [examples_controller.rb](documentation/examples/examples_controller.rb)
164
+ [examples_controller.rb](documentation/examples/examples_controller.rb),
165
+ or [HERE](https://github.com/zhandao/zero-rails/tree/master/app/_docs/v1).
154
166
 
155
167
  ### Basic DSL ([source code](lib/open_api/dsl.rb))
156
168
 
@@ -492,8 +504,47 @@
492
504
  ```
493
505
 
494
506
  **practice:** Automatically generate responses based on the agreed error class. [AutoGenDoc](documentation/examples/auto_gen_doc.rb#L63)
507
+
508
+ ### (6) Callback (OAS - [Callback Object](https://github.com/OAI/OpenAPI-Specification/blob/OpenAPI.next/versions/3.0.0.md#callback-object))
509
+
510
+ [About Callbacks](https://swagger.io/docs/specification/callbacks/)
511
+ > In OpenAPI 3 specs, you can define callbacks – asynchronous, out-of-band requests that your service will send to some other service in response to certain events. This helps you improve the workflow your API offers to clients.
512
+ A typical example of a callback is a subscription functionality ... you can define the format of the “subscription” operation as well as the format of callback messages and expected responses to these messages.
513
+ This description will simplify communication between different servers and will help you standardize use of webhooks in your API.
514
+ [Complete YAML Example](https://github.com/OAI/OpenAPI-Specification/blob/master/examples/v3.0/callback-example.yaml)
515
+
516
+ The structure of Callback Object:
517
+ ```
518
+ callbacks:
519
+ Event1:
520
+ path1:
521
+ ...
522
+ path2:
523
+ ...
524
+ Event2:
525
+ ...
526
+ ```
527
+
528
+ To define callbacks, you can use `callback` method:
529
+ ```ruby
530
+ # method signature
531
+ callback(event_name, http_method, callback_url, &block)
532
+ # usage
533
+ callback :myEvent, :post, 'localhost:3000/api/goods' do
534
+ query :name, String
535
+ data :token, String
536
+ response 200, 'success', :json, data: { name: String, description: String }
537
+ end
538
+ ```
539
+
540
+ Use runtime expressions in callback_url:
541
+ ```ruby
542
+ callback :myEvent, :post, '{body callback_addr}/api/goods/{query id}'
543
+ # the final URL will be: {$request.body#/callback_addr}/api/goods/{$request.query.id}
544
+ # Note: Other expressions outside "$request" are not supported yet
545
+ ```
495
546
 
496
- #### (6) Authentication and Authorization
547
+ #### (7) Authentication and Authorization
497
548
 
498
549
  First of all, please make sure that you have read one of the following documents:
499
550
  [OpenApi Auth](https://swagger.io/docs/specification/authentication/)
@@ -551,7 +602,7 @@
551
602
  auth :OAuth, scopes: %w[ read_example admin ]
552
603
  ```
553
604
 
554
- #### (7) Overriding Global Servers by `server`
605
+ #### (8) Overriding Global Servers by `server`
555
606
 
556
607
  ```ruby
557
608
  # method signature
@@ -763,6 +814,8 @@
763
814
 
764
815
  ## Development
765
816
 
817
+ TODO ..
818
+
766
819
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
767
820
 
768
821
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
data/README_zh.md CHANGED
@@ -28,17 +28,30 @@
28
28
  - [配置](#configure)
29
29
  - [DSL 介绍及用例](#usage---dsl)
30
30
  - [基本的 DSL](#基本的-dsl)
31
+ - [route_base](#1-route_base-optional-if-youre-writing-dsl-in-controller)
32
+ - [doc_tag](#2-doc_tag-optional)
33
+ - [components](#3-components-optional)
34
+ - [api_dry](#4-api_dry-optional)
35
+ - [api](#5-api-required)
31
36
  - [用于 `api` 和 `api_dry` 块内的 DSL(描述 API 的参数、响应等)](#dsl-methods-inside-api-and-api_drys-block)
37
+ - [this_api_is_invalid!](#1-this_api_is_invalid-its-aliases)
38
+ - [desc](#2-desc-description-for-the-current-api-and-its-inputs-parameters-and-request-body)
39
+ - [param family methods](#3-param-family-methods-oas---parameter-object)
40
+ - [request_body family methods](#4-request_body-family-methods-oas---request-body-object)
41
+ - [response family methods](#5-response-family-methods-oas---response-object)
42
+ - [callback](#6-callback-oas---callback-object)
43
+ - [Authentication and Authorization](#7-authentication-and-authorization)
44
+ - [server](#8-overriding-global-servers-by-server)
32
45
  - [用于 `components` 块内的 DSL(描述可复用的组件)](#dsl-methods-inside-componentss-block-code-source)
33
46
  - [执行文档生成](#run---generate-json-documentation-file)
34
47
  - [使用 Swagger-UI 可视化所生成的文档](#use-swagger-uivery-beautiful-web-page-to-show-your-documentation)
35
48
  - [技巧](#tricks)
36
- - [将 DSL 写于他处,与控制器分离](#trick1---write-the-dsl-somewhere-else)
37
- - [全局 DRY](#trick2---global-drying)
38
- - [基于 enum 等信息自动生成参数描述](#trick3---auto-generate-description)
39
- - [跳过或使用 DRY 时(`api_dry`)所定义的参数](#trick4---skip-or-use-parameters-define-in-api_dry)
40
- - [基于 DB Schema 自动生成 response 的格式](#trick5---auto-generate-indexshow-actionss-response-types-based-on-db-schema)
41
- - [定义组合的 Schema (one_of / all_of / any_of / not)](#trick6---combined-schema-one_of--all_of--any_of--not)
49
+ - [将 DSL 写于他处,与控制器分离](#trick1---write-the-dsl-somewhere-else)
50
+ - [全局 DRY](#trick2---global-drying)
51
+ - [基于 enum 等信息自动生成参数描述](#trick3---auto-generate-description)
52
+ - [跳过或使用 DRY 时(`api_dry`)所定义的参数](#trick4---skip-or-use-parameters-define-in-api_dry)
53
+ - [基于 DB Schema 自动生成 response 的格式](#trick5---auto-generate-indexshow-actionss-response-types-based-on-db-schema)
54
+ - [定义组合的 Schema (one_of / all_of / any_of / not)](#trick6---combined-schema-one_of--all_of--any_of--not)
42
55
  - [问题集](#troubleshooting)
43
56
  - [有关 `OpenApi.docs` 和 `OpenApi.routes_index`](#about-openapidocs-and-openapiroutes_index)
44
57
 
@@ -145,8 +158,9 @@
145
158
  end
146
159
  ```
147
160
 
148
- 这有两份更详细的实例: [goods_doc.rb](documentation/examples/goods_doc.rb), 以及
149
- [examples_controller.rb](documentation/examples/examples_controller.rb)
161
+ 更多更详细的实例: [goods_doc.rb](documentation/examples/goods_doc.rb)
162
+ [examples_controller.rb](documentation/examples/examples_controller.rb),以及
163
+ [这里](https://github.com/zhandao/zero-rails/tree/master/app/_docs/v1)。
150
164
 
151
165
  ### 基本的 DSL ([source code](lib/open_api/dsl.rb))
152
166
 
@@ -0,0 +1,58 @@
1
+ require 'oas_objs/helpers'
2
+
3
+ module OpenApi
4
+ module DSL
5
+ # https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#callbackObject
6
+ class CallbackObj < Hash
7
+ include Helpers
8
+
9
+ attr_accessor :event_name, :http_method, :callback_url, :block
10
+
11
+ def initialize(event_name, http_method, callback_url, &block)
12
+ self.event_name = event_name
13
+ self.http_method = http_method
14
+ self.callback_url = callback_url
15
+ self.block = block
16
+ end
17
+
18
+ def process
19
+ {
20
+ self.event_name => {
21
+ processed_url => {
22
+ self.http_method.downcase.to_sym => processed_block
23
+ }
24
+ }
25
+ }
26
+ end
27
+
28
+ def processed_url
29
+ self.callback_url.gsub(/{[^{}]*}/) do |exp|
30
+ key_location, key_name = exp[1..-2].split
31
+ connector = key_location == 'body' ? '#/' : '.'
32
+ key_location = '$request.' + key_location
33
+ ['{', key_location, connector, key_name, '}'].join
34
+ end
35
+ end
36
+
37
+ def processed_block
38
+ api = ApiInfo.new.merge! parameters: [ ], requestBody: '', responses: { }
39
+ api.instance_exec(&(self.block || ->{ }))
40
+ api.process_objs
41
+ api.delete_if { |_, v| v.blank? }
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+
48
+ __END__
49
+
50
+ myEvent:
51
+ '{$request.body#/callbackUrl}':
52
+ post: # Method
53
+ requestBody: # Contents of the callback message
54
+
55
+ responses: # Expected responses
56
+
57
+
58
+ https://github.com/OAI/OpenAPI-Specification/blob/master/examples/v3.0/callback-example.yaml
@@ -41,7 +41,7 @@ module OpenApi
41
41
  RefObj.new(:schema, t).process
42
42
  elsif t.in? %w[ float double int32 int64 ]
43
43
  { type: t.match?('int') ? 'integer' : 'number', format: t }
44
- elsif t.in? %w[ binary base64 ]
44
+ elsif t.in? %w[ binary base64 uri ]
45
45
  { type: 'string', format: t }
46
46
  elsif t == 'file'
47
47
  { type: 'string', format: Config.file_format }
data/lib/open_api/dsl.rb CHANGED
@@ -40,7 +40,8 @@ module OpenApi
40
40
 
41
41
  api = ApiInfo.new(action_path, skip: Array(skip), use: Array(use))
42
42
  .merge! description: '', summary: summary, operationId: action, tags: [@doc_tag],
43
- parameters: [ ], requestBody: '', responses: { }, security: [ ], servers: [ ]
43
+ parameters: [ ], requestBody: '', responses: { }, callbacks: { },
44
+ links: { }, security: [ ], servers: [ ]
44
45
  [action, :all].each { |blk_key| @zro_dry_blocks&.[](blk_key)&.each { |blk| api.instance_eval(&blk) } }
45
46
  api.param_use = api.param_skip = [ ] # `skip` and `use` only affect `api_dry`'s blocks
46
47
  api.instance_exec(&block) if block_given?
@@ -8,7 +8,7 @@ module OpenApi
8
8
 
9
9
  attr_accessor :action_path, :param_skip, :param_use, :param_descs, :param_order
10
10
 
11
- def initialize(action_path, skip: [ ], use: [ ])
11
+ def initialize(action_path = '', skip: [ ], use: [ ])
12
12
  self.action_path = action_path
13
13
  self.param_skip = skip
14
14
  self.param_use = use
@@ -115,6 +115,10 @@ module OpenApi
115
115
  alias auth security_require
116
116
  alias need_auth security_require
117
117
 
118
+ def callback event_name, http_method, callback_url, &block
119
+ self[:callbacks].deep_merge! CallbackObj.new(event_name, http_method, callback_url, &block).process
120
+ end
121
+
118
122
  def server url, desc: ''
119
123
  self[:servers] << { url: url, description: desc }
120
124
  end
@@ -5,6 +5,7 @@ require 'oas_objs/response_obj'
5
5
  require 'oas_objs/request_body_obj'
6
6
  require 'oas_objs/ref_obj'
7
7
  require 'oas_objs/example_obj'
8
+ require 'oas_objs/callback_obj'
8
9
  require 'open_api/dsl/helpers'
9
10
 
10
11
  module OpenApi
@@ -1,3 +1,3 @@
1
1
  module OpenApi
2
- VERSION = '1.5.4'
2
+ VERSION = '1.5.5'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zero-rails_openapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.4
4
+ version: 1.5.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - zhandao
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-02-15 00:00:00.000000000 Z
11
+ date: 2018-02-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -137,6 +137,7 @@ files:
137
137
  - documentation/examples/open_api.rb
138
138
  - documentation/examples/output_example.json
139
139
  - documentation/parameter.md
140
+ - lib/oas_objs/callback_obj.rb
140
141
  - lib/oas_objs/combined_schema.rb
141
142
  - lib/oas_objs/example_obj.rb
142
143
  - lib/oas_objs/helpers.rb