Skip to content

Commit

Permalink
Rswag for child resource
Browse files Browse the repository at this point in the history
  • Loading branch information
PedroAugustoRamalhoDuarte committed Jan 4, 2023
1 parent 0fce040 commit 1f555b7
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 24 deletions.
8 changes: 8 additions & 0 deletions lib/generators/rest_api_generator/spec/rswag_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ def create_service_file

private

# Changes nested routes for rswag format
# Example: /cars/{car.id}/drivers/{id}
def nested_routes
return "" if options["father"].blank?

"#{options["father"].downcase.pluralize}/{#{options["father"].singularize.downcase}_id}/#{plural_name}"
end

def spec_routes
{
index: initial_route,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,51 +1,72 @@
require "rails_helper"

RSpec.describe "<%= controller_path %>", type: :request do
let(:<%= singular_table_name %>) { create(:<%= singular_table_name %>) }
RSpec.describe "<%= spec_routes[:index] %>", type: :request do
let(:resource) { create(:<%= singular_table_name %>, <%= options['father'].downcase.singularize %>: parent_resource) }
let(:parent_resource ) { create(:<%= options['father'].downcase.singularize %>) }

path "<%= spec_routes[:index] %>" do
parameter name: "<%= options['father'].downcase.singularize %>_id", in: :path, type: :string, description: "<%= options['father'].downcase.singularize %> id"
let(:<%= options['father'].downcase.singularize %>_id) { parent_resource.id }

path "/<%= spec_routes[:index] %>" do
get("list <%= plural_name %>") do
consumes "application/json"

response(200, "successful") do
after do |example|
example.metadata[:response][:content] = {
'application/json' => {
"application/json" => {
example: JSON.parse(response.body, symbolize_names: true)
}
}
end

run_test!
end
end

post("create <%= singular_table_name %>") do
response(200, "successful") do
consumes "application/json"

# You'll want to customize the parameter types...
parameter name: :<%= singular_table_name %>, in: :body, schema: {
type: :object,
properties: {
<% attributes.each do |attribute| -%>
<%= attribute.name %>: { type: :string },
<% end -%>
}
}
response(201, "successful") do
let(:<%= singular_table_name %>) { attributes_for(:<%= singular_table_name %>) }

after do |example|
example.metadata[:response][:content] = {
'application/json' => {
"application/json" => {
example: JSON.parse(response.body, symbolize_names: true)
}
}
end

run_test!
end
end
end

path "<%= spec_routes[:show] %>" do
# You'll want to customize the parameter types...
parameter name: 'id', in: :path, type: :string, description: 'id'
parameter name: "id", in: :path, type: :string, description: "id"
parameter name: "<%= options['father'].downcase.singularize %>_id", in: :path, type: :string, description: "<%= options['father'].downcase.singularize %> id"

let(:id) { resource.id }
let(:<%= options['father'].downcase.singularize %>_id) { parent_resource.id }

get('show <%= singular_table_name %>') do
security [{ bearerAuth: [] }]
get("show <%= singular_table_name %>") do
consumes "application/json"

response(200, "successful") do
let(:id) { <%= singular_table_name %>.id }

after do |example|
example.metadata[:response][:content] = {
'application/json' => {
"application/json" => {
example: JSON.parse(response.body, symbolize_names: true)
}
}
Expand All @@ -56,33 +77,40 @@ RSpec.describe "<%= controller_path %>", type: :request do


patch("update <%= singular_table_name %>") do
consumes "application/json"

# You'll want to customize the parameter types...
parameter name: :<%= singular_table_name %>, in: :body, schema: {
type: :object,
properties: {
<% attributes.each do |attribute| -%>
<%= attribute.name %>: { type: :string },
<% end -%>
}
}

response(200, "successful") do
let(:id) { <%= singular_table_name %>.id }
let(:<%= singular_table_name %>) { attributes_for(:<%= singular_table_name %>) }

after do |example|
example.metadata[:response][:content] = {
'application/json' => {
"application/json" => {
example: JSON.parse(response.body, symbolize_names: true)
}
}
end

run_test!
end
end

delete("delete plan") do
response(200, "successful") do
let(:id) { <%= singular_table_name %>.id }
consumes "application/json"

response(204, "successful") do

after do |example|
example.metadata[:response][:content] = {
"application/json" => {
example: JSON.parse(response.body, symbolize_names: true)
}
}
end
run_test!
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,28 @@
end
end
end

context "when is nested resource" do
before do
run_generator ["Driver", "car:references", "name:string", "--father", "Cars"]
end

describe "spec file" do
subject(:spec_file) { file("spec/requests/cars/drivers_spec.rb") }

it { is_expected.to exist }

it "describe index and create path" do
expect(spec_file).to contain("/cars/{car_id}/drivers")
end

it "describe show, update, delete path" do
expect(spec_file).to contain("/cars/{car_id}/drivers/{id}")
end

it "has name schema for input" do
expect(spec_file).to contain("name: { type: :string }")
end
end
end
end
117 changes: 117 additions & 0 deletions spec/lib/rest_api_generator/child_resource_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# frozen_string_literal: true

# Generated with: "rails g rest_api_generator:spec:rswag Driver car:references name:string --father Cars"
require "swagger_helper"
require "rest_api_generator/child_resource_controller"

RSpec.describe "/cars/{car_id}/drivers", type: :request do
let(:resource) { Driver.create!(car: parent_resource) }
let(:parent_resource) { Car.create! }

path "/cars/{car_id}/drivers" do
parameter name: "car_id", in: :path, type: :string, description: " car id"

let(:car_id) { parent_resource.id }

get("list drivers") do
consumes "application/json"

response(200, "successful") do
after do |example|
example.metadata[:response][:content] = {
"application/json" => {
example: JSON.parse(response.body, symbolize_names: true),
},
}
end

run_test!
end
end

post("create driver") do
consumes "application/json"

# You'll want to customize the parameter types...
parameter name: :driver, in: :body, schema: {
type: :object,
properties: {
car: { type: :string },
name: { type: :string },
},
}
response(201, "successful") do
let(:driver) { { name: "Chefe" } }

after do |example|
example.metadata[:response][:content] = {
"application/json" => {
example: JSON.parse(response.body, symbolize_names: true),
},
}
end

run_test!
end
end
end

path "/cars/{car_id}/drivers/{id}" do
parameter name: "id", in: :path, type: :string, description: "id"
parameter name: "car_id", in: :path, type: :string, description: " car id"

let(:id) { resource.id }
let(:car_id) { parent_resource.id }

get("show driver") do
consumes "application/json"

response(200, "successful") do
after do |example|
example.metadata[:response][:content] = {
"application/json" => {
example: JSON.parse(response.body, symbolize_names: true),
},
}
end

run_test!
end
end

patch("update driver") do
consumes "application/json"

# You'll want to customize the parameter types...
parameter name: :driver, in: :body, schema: {
type: :object,
properties: {
car: { type: :string },
name: { type: :string },
},
}

response(200, "successful") do
let(:driver) { { name: "Chefe" } }

after do |example|
example.metadata[:response][:content] = {
"application/json" => {
example: JSON.parse(response.body, symbolize_names: true),
},
}
end

run_test!
end
end

delete("delete plan") do
consumes "application/json"

response(204, "successful") do
run_test!
end
end
end
end

0 comments on commit 1f555b7

Please sign in to comment.