Skip to content

Commit

Permalink
Merge pull request #35 from zhandao/v2
Browse files Browse the repository at this point in the history
  • Loading branch information
zhandao authored Feb 12, 2019
2 parents fea981b + 2e60868 commit f8e07d5
Show file tree
Hide file tree
Showing 38 changed files with 894 additions and 1,359 deletions.
838 changes: 404 additions & 434 deletions README.md

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 +359,11 @@


# method signature
# `exp_by` (select_example_by): choose the example fields.
examples(exp_by = :all, examples_hash)
# `exp_params` (select_example_by): choose the example fields.
examples(exp_params = :all, examples_hash)
# usage
# it defines 2 examples by using parameter :id and :name
# if pass :all to `exp_by`, keys will be all the parameter's names.
# if pass :all to `exp_params`, keys will be all the parameter's names.
examples [:id, :name], {
:right_input => [ 1, 'user'], # == { id: 1, name: 'user' }
:wrong_input => [ -1, '' ]
Expand Down Expand Up @@ -425,7 +425,7 @@
:password! => { type: String, pattern: /[0-9]{6,10}/, desc: 'password' },
# optional
:remarks => { type: String, desc: 'remarks' },
}, exp_by: %i[ name password ],
}, exp_params: %i[ name password ],
examples: { # ↓ ↓
:right_input => [ 'user1', '123456' ],
:wrong_input => [ 'user2', 'abc' ]
Expand All @@ -448,7 +448,7 @@

1. `media_type`: we provide some [mapping](lib/oas_objs/media_type_obj.rb) from symbols to real media-types.
2. `schema_info`: as above (see param).
3. `exp_by` and `examples`: for the above example, the following has the same effect:
3. `exp_params` and `examples`: for the above example, the following has the same effect:
```
examples: {
:right_input => { name: 'user1', password: '123456' },
Expand Down
29 changes: 0 additions & 29 deletions documentation/examples/auto_gen_desc.rb

This file was deleted.

60 changes: 0 additions & 60 deletions documentation/examples/examples_controller.rb

This file was deleted.

52 changes: 0 additions & 52 deletions documentation/examples/goods_doc.rb

This file was deleted.

69 changes: 0 additions & 69 deletions documentation/parameter.md

This file was deleted.

File renamed without changes.
26 changes: 11 additions & 15 deletions documentation/examples/open_api.rb → examples/open_api.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
require 'open_api'

OpenApi::Config.tap do |c|
OpenApi::Config.class_eval do
# Config DSL
c.instance_eval do
open_api :zero_rails, base_doc_classes: [ApiDoc]
info version: '0.0.1', title: 'Zero Rails APIs', description: 'API documentation of Zero-Rails Application.'
server 'http://localhost:3000', desc: 'Main (production) server'
server 'http://localhost:3000', desc: 'Internal staging server for testing'
bearer_auth :Token
global_auth :Token
end
open_api :zero_rails, base_doc_classes: [ApiDoc]
info version: '0.0.1', title: 'Zero Rails APIs', description: 'API documentation of Zero-Rails Application.'
server 'http://localhost:3000', desc: 'Main (production) server'
server 'http://localhost:3000', desc: 'Internal staging server for testing'
bearer_auth :Token
global_auth :Token

# [REQUIRED] The location where .json doc file will be output.
c.file_output_path = 'public/open_api'
self.file_output_path = 'public/open_api'

# [Optional] Use this txt instead of running `rails routes`.
# c.rails_routes_file = 'config/routes.txt'
# self.rails_routes_file = 'config/routes.txt'

# Everything about OAS3 is on https://github.com/OAI/OpenAPI-Specification/blob/OpenAPI.next/versions/3.0.0.md
# Getting started: https://swagger.io/docs/specification/basic-structure/
c.open_api_docs = {
self.open_api_docs = {
blog_api: {
# [REQUIRED] ZRO will scan all the descendants of the base_doc_classes, then generate their docs.
base_doc_classes: [ApiController],
Expand Down Expand Up @@ -96,9 +94,7 @@

end

Object.const_set('Boolean', 'boolean') # Support `Boolean` writing in DSL

OpenApi.write_docs generate_files: !Rails.env.production?
OpenApi.write_docs if: !Rails.env.production?


__END__
Expand Down
File renamed without changes.
13 changes: 3 additions & 10 deletions lib/oas_objs/callback_obj.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,22 @@ def initialize(event_name, http_method, callback_url, &block)

def process
{
self.event_name => {
event_name => {
processed_url => {
self.http_method.downcase.to_sym => processed_block
http_method.downcase.to_sym => Api.new.run_dsl(&(self.block || -> { }))
}
}
}
end

def processed_url
self.callback_url.gsub(/{[^{}]*}/) do |exp|
callback_url.gsub(/{[^{}]*}/) do |exp|
key_location, key_name = exp[1..-2].split
connector = key_location == 'body' ? '#/' : '.'
key_location = '$request.' + key_location
['{', key_location, connector, key_name, '}'].join
end
end

def processed_block
api = Api.new.merge! parameters: [ ], requestBody: '', responses: { }
api.instance_exec(&(self.block || -> { }))
api.process_objs
api.delete_if { |_, v| v.blank? }
end
end
end
end
Expand Down
26 changes: 14 additions & 12 deletions lib/oas_objs/combined_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,25 @@ module DSL
# https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/
# https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#schemaObject
class CombinedSchema < Hash
attr_accessor :processed
include Helpers

def initialize(combined_schema)
self.processed = { }
attr_accessor :processed, :mode, :schemas

def initialize(combined_schema)
combined_schema.delete_if { |_, v| v.nil? }
@mode = combined_schema.keys.first.to_s.sub('_not', 'not').camelize(:lower).to_sym
@schemas = combined_schema.values.first
self.mode = combined_schema.keys.first.to_s.camelize(:lower).to_sym
self.schemas = combined_schema.values.first
end

def process(options = { inside_desc: false })
processed[@mode] = @schemas.map do |schema|
type = schema.is_a?(Hash) ? schema[:type] : schema
schema = { } unless schema.is_a?(Hash)
SchemaObj.new(type, schema).process(options)
end
processed
def process
self.processed = {
mode =>
schemas.map do |schema|
type = schema.is_a?(Hash) ? schema[:type] : schema
schema = { } unless schema.is_a?(Hash)
SchemaObj.new(type, schema).process
end
}
end
end
end
Expand Down
Loading

0 comments on commit f8e07d5

Please sign in to comment.