Skip to content

Commit

Permalink
Merge branch 'master' of github.com:icyleaf/swagger
Browse files Browse the repository at this point in the history
  • Loading branch information
icyleaf committed Feb 28, 2020
2 parents e2dc5fc + 913d88d commit 29bd062
Show file tree
Hide file tree
Showing 39 changed files with 138 additions and 200 deletions.
6 changes: 3 additions & 3 deletions examples/authorization.cr
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ builder = Swagger::Builder.new(
)

builder.add(Swagger::Controller.new("Auth", "Authorization", [
Swagger::Action.new("get", "/access_token", "Get Access Token"),
Swagger::Action.new("get", "/access_token", [Swagger::Response.new("200", "Success response")], "Get Access Token"),
], external_docs: Swagger::ExternalDocs.new("http://auth.example.com/private_token", "See Details")))

builder.add(Swagger::Controller.new("Users", "User Resources", [
Swagger::Action.new("get", "/users", "List users", parameters: [
Swagger::Action.new("get", "/users", [Swagger::Response.new("200", "Success response")], "List users", parameters: [
Swagger::Parameter.new("page", "query", "integer", "Current page"),
Swagger::Parameter.new("limit", "query", "integer", "How many items to return at one time (max 100)"),
], authorization: true),
Swagger::Action.new("get", "/users/{id}", "Get user by id", parameters: [Swagger::Parameter.new("id", "path")], responses: [
Swagger::Action.new("get", "/users/{id}", description: "Get user by id", parameters: [Swagger::Parameter.new("id", "path")], responses: [
Swagger::Response.new("200", "Success response"),
Swagger::Response.new("404", "Not found user"),
], authorization: true),
Expand Down
6 changes: 3 additions & 3 deletions examples/frameworks/kemal.cr
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ builder = Swagger::Builder.new(
)

builder.add(Swagger::Controller.new("Users", "User Resources", [
Swagger::Action.new("get", "/users", "List users", parameters: [
Swagger::Action.new("get", "/users", description: "List users", parameters: [
Swagger::Parameter.new("page", "query", "integer", "Current page", default_value: 1),
Swagger::Parameter.new("limit", "query", "integer", "How many items to return at one time (max 100)", default_value: 10),
], responses: [
Swagger::Response.new("200", "Success response"),
]),
Swagger::Action.new("get", "/users/{id}", "Get user by id", parameters: [Swagger::Parameter.new("id", "path")], responses: [
Swagger::Action.new("get", "/users/{id}", description: "Get user by id", parameters: [Swagger::Parameter.new("id", "path")], responses: [
Swagger::Response.new("200", "Success response"),
Swagger::Response.new("404", "Not found user"),
]),
Expand All @@ -75,7 +75,7 @@ builder.add(Swagger::Controller.new("Users", "User Resources", [
Swagger::Response.new("404", "Not found user"),
]
),
Swagger::Action.new("delete", "/users/{id}", "Get user by id", parameters: [Swagger::Parameter.new("id", "path")], responses: [
Swagger::Action.new("delete", "/users/{id}", description: "Get user by id", parameters: [Swagger::Parameter.new("id", "path")], responses: [
Swagger::Response.new("200", "Success response"),
Swagger::Response.new("404", "Not found user"),
]),
Expand Down
8 changes: 4 additions & 4 deletions examples/simple.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ builder.add(Swagger::Object.new("User", "object", [
]))

builder.add(Swagger::Controller.new("Users", "User Resources", [
Swagger::Action.new("get", "/users", "List users", parameters: [
Swagger::Action.new("get", "/users", [Swagger::Response.new("200", "Success response")], "List users", parameters: [
Swagger::Parameter.new("page", "query", "integer", "Current page", default_value: 1, format: "int32"),
Swagger::Parameter.new("limit", "query", "integer", "How many items to return at one time (max 100)", default_value: 50, format: "int32"),
]),
Swagger::Action.new("get", "/users/{id}", "Get user by id", parameters: [Swagger::Parameter.new("id", "path")], responses: [
Swagger::Action.new("get", "/users/{id}", description: "Get user by id", parameters: [Swagger::Parameter.new("id", "path")], responses: [
Swagger::Response.new("200", "Success response"),
Swagger::Response.new("404", "Not found user"),
], request: Swagger::Request.new("User")),
Swagger::Action.new("post", "/users", "Create user",
Swagger::Action.new("post", "/users", description: "Create user",
request: Swagger::Request.new([
Swagger::Property.new("username", required: true, description: "The name of user"),
Swagger::Property.new("email", "string", required: true, description: "Email"),
Expand All @@ -40,7 +40,7 @@ builder.add(Swagger::Controller.new("Users", "User Resources", [
Swagger::Response.new("404", "Not found user"),
]
),
Swagger::Action.new("get", "/user/{id}", "Get user by id", parameters: [Swagger::Parameter.new("id", "path")], responses: [
Swagger::Action.new("get", "/user/{id}", description: "Get user by id", parameters: [Swagger::Parameter.new("id", "path")], responses: [
Swagger::Response.new("200", "Success response"),
Swagger::Response.new("404", "Not found user"),
], deprecated: true),
Expand Down
4 changes: 4 additions & 0 deletions spec/swagger/action_helper.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require "spec"
require "../../src/swagger/response"

OK_RESPONSE = [Swagger::Response.new "200", "OK"]
13 changes: 7 additions & 6 deletions spec/swagger/action_spec.cr
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
require "../spec_helper"
require "./action_helper"
require "../../src/swagger/action"

describe Swagger::Action do
describe "#new" do
it "should works" do
raw = Swagger::Action.new("get", "/users")
raw = Swagger::Action.new("get", "/users", OK_RESPONSE)
raw.method.should eq "get"
raw.route.should eq "/users"
raw.responses.should eq OK_RESPONSE
raw.summary.should be_nil
raw.description.should be_nil
raw.parameters.should be_nil
raw.request.should be_nil
raw.responses.should be_nil
raw.authorization.should be_false
raw.deprecated.should be_false
end

it "should stored downcase method" do
raw = Swagger::Action.new("GET", "/users")
raw = Swagger::Action.new("GET", "/users", OK_RESPONSE)
raw.method.should eq "get"
end

{% for ivar in Swagger::Objects::PathItem::METHODS %}
it "should define {{ ivar.id }} method" do
raw = Swagger::Action.new("{{ ivar.id }}", "/users")
raw = Swagger::Action.new("{{ ivar.id }}", "/users", OK_RESPONSE)
raw.method.should eq "{{ ivar.id }}"
raw.route.should eq "/users"
end
{% end %}

it "throws an exception with undefined method" do
expect_raises Swagger::UndefinedMethod do
Swagger::Action.new("fake", "/users")
Swagger::Action.new("fake", "/users", OK_RESPONSE)
end
end
end
Expand Down
7 changes: 4 additions & 3 deletions spec/swagger/controller_spec.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "../spec_helper"
require "./action_helper"
require "../../src/swagger/controller"

describe Swagger::Controller do
describe "#new" do
Expand All @@ -12,11 +13,11 @@ describe Swagger::Controller do

it "should works with actions" do
raw = Swagger::Controller.new("Users", "User APIs", [
Swagger::Action.new("get", "/users", "List Users"),
Swagger::Action.new("get", "/users", OK_RESPONSE, "List Users"),
])
raw.name.should eq "Users"
raw.description.should eq "User APIs"
raw.actions.should eq([Swagger::Action.new("get", "/users", "List Users")])
raw.actions.should eq([Swagger::Action.new("get", "/users", OK_RESPONSE, "List Users")])
raw.external_docs.should be_nil
end
end
Expand Down
15 changes: 8 additions & 7 deletions spec/swagger/objects/operation_spec.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "../../spec_helper"
require "./response_helper"
require "../../../src/swagger/objects/operation"

describe Swagger::Objects::Operation do
describe ".from" do
Expand All @@ -7,13 +8,13 @@ describe Swagger::Objects::Operation do

describe "#new" do
it "should works" do
raw = Swagger::Objects::Operation.new
raw = Swagger::Objects::Operation.new SWAGGER_OK_RESPONSE
raw.responses.should eq SWAGGER_OK_RESPONSE
raw.summary.should be_nil
raw.description.should be_nil
raw.tags.should be_nil
raw.parameters.should be_nil
raw.request_body.should be_nil
raw.responses.should be_nil
raw.deprecated.should be_false
raw.security.should be_nil

Expand All @@ -26,13 +27,13 @@ describe Swagger::Objects::Operation do

describe "#to_json" do
it "should return default hash string" do
raw = Swagger::Objects::Operation.new
raw.to_json.should eq %Q{{"deprecated":false}}
raw = Swagger::Objects::Operation.new SWAGGER_OK_RESPONSE
raw.to_json.should eq %Q{{"responses":{"200":{"description":"OK"}},"deprecated":false}}
end

it "should returns OpenAPI spec Link json string" do
raw = Swagger::Objects::Operation.new(request_body: Swagger::Objects::RequestBody.new)
raw.to_json.should eq %Q{{"requestBody":{"required":false},"deprecated":false}}
raw = Swagger::Objects::Operation.new(SWAGGER_OK_RESPONSE, request_body: Swagger::Objects::RequestBody.new)
raw.to_json.should eq %Q{{"requestBody":{"required":false},"responses":{"200":{"description":"OK"}},"deprecated":false}}
end
end
end
7 changes: 4 additions & 3 deletions spec/swagger/objects/path_item_spec.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "../../spec_helper"
require "./response_helper"
require "../../../src/swagger/objects/path_item"

describe Swagger::Objects::PathItem do
describe "#new" do
Expand All @@ -21,10 +22,10 @@ describe Swagger::Objects::PathItem do
describe "#add" do
it "should works" do
raw = Swagger::Objects::PathItem.new
raw.add("get", Swagger::Objects::Operation.new)
raw.add("get", Swagger::Objects::Operation.new SWAGGER_OK_RESPONSE)
raw.summary.should be_nil
raw.description.should be_nil
raw.get.should eq Swagger::Objects::Operation.new
raw.get.should eq Swagger::Objects::Operation.new SWAGGER_OK_RESPONSE
raw.put.should be_nil
raw.post.should be_nil
raw.delete.should be_nil
Expand Down
3 changes: 3 additions & 0 deletions spec/swagger/objects/response_helper.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require "spec"

SWAGGER_OK_RESPONSE = {"200" => Swagger::Objects::Response.new "OK"}
5 changes: 0 additions & 5 deletions src/swagger.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ require "json"
require "./swagger/*"

module Swagger
VERSION = "0.1.1"

OPENAPI_MAJAR_VERSION = "3"
OPENAPI_VERSION = "3.0.1"

alias Document = Objects::Document
alias Info = Objects::Info
alias Contact = Objects::Info::Contact
Expand Down
13 changes: 11 additions & 2 deletions src/swagger/action.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
require "./objects/path_item"
require "./request"
require "./response"
require "./error"

module Swagger
# Define a action
#
Expand All @@ -18,8 +23,12 @@ module Swagger
property authorization
property deprecated

def initialize(@method : String, @route : String, @summary : String? = nil, @parameters : Array(Parameter)? = nil,
@description : String? = nil, @request : Request? = nil, @responses : Array(Response)? = nil,
def initialize(@method : String,
@route : String,
@responses : Array(Response),
@summary : String? = nil,
@parameters : Array(Parameter)? = nil,
@description : String? = nil, @request : Request? = nil,
@authorization = false, @deprecated = false)
unless Objects::PathItem::METHODS.includes?(@method.downcase)
raise UndefinedMethod.new("Undefined method `#{@method}`, avaiabled in #{Objects::PathItem::METHODS}.")
Expand Down
3 changes: 3 additions & 0 deletions src/swagger/controller.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
require "./action"
require "./objects/external_docs"

module Swagger
# Define a controller
#
Expand Down
Binary file removed src/swagger/http/assets/swagger-ui/favicon-16x16.png
Binary file not shown.
Binary file removed src/swagger/http/assets/swagger-ui/favicon-32x32.png
Binary file not shown.
93 changes: 0 additions & 93 deletions src/swagger/http/assets/swagger-ui/swagger-ui-bundle.js

This file was deleted.

Loading

0 comments on commit 29bd062

Please sign in to comment.