Skip to content

Allow paginator to be optional for resource. #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/generators/jsonapi/swagger/swagger_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ def mutable?
resource_klass.mutable?
end

def paginator
resource_klass.paginator
end

def attribute_default
Jsonapi::Swagger.attribute_default
end
Expand Down Expand Up @@ -168,4 +172,4 @@ def safe_encode(content)
content&.force_encoding('ASCII-8BIT')
end
end
end
end
104 changes: 56 additions & 48 deletions lib/generators/jsonapi/swagger/templates/swagger.json.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
<%-
def list_resource_parameters
[].tap do |parameters|
parameters << { name: 'page[number]', in: :query, type: :string, description: tt(:page_num), required: false }
parameters << { name: 'page[size]', in: :query, type: :string, description: tt(:page_size), required: false }
if paginator == :paged
parameters << { name: 'page[number]', in: :query, type: :string, description: tt(:page_num), required: false }
parameters << { name: 'page[size]', in: :query, type: :string, description: tt(:page_size), required: false }
end
if sortable_fields.present?
parameters << { name: 'sort', in: :query, type: :string, description: ori_sortable_fields_desc, required: false }
end
Expand Down Expand Up @@ -143,57 +145,63 @@
end

def list_resource_responses
{
'200' => {
description: tt(:get_list),
schema: {
response_properties = {
data: {
type: :array,
items: {
type: :object,
properties: {
data: {
type: :array,
items: {
type: :object,
properties: {
id: { type: :string, description: 'ID'},
links: {
type: :object,
properties: {
self: { type: :string, description: tt(:detail_link) },
},
description: tt(:detail_link)
},
attributes: {
type: :object,
properties: properties(attrs: attributes.each_key),
description: tt(:attributes)
},
relationships: {
type: :object,
properties: relationships_properties,
description: tt(:associate_data)
}
id: { type: :string, description: 'ID' },
links: {
type: :object,
properties: {
self: { type: :string, description: tt(:detail_link) },
},
description: tt(:detail_link)
},
},
description: tt(:data)
},
meta: {
type: :object,
properties: {
record_count: { type: :integer, description: tt(:record_count)},
page_count: { type: :integer, description: tt(:page_count)},
attributes: {
type: :object,
properties: properties(attrs: attributes.each_key),
description: tt(:attributes)
},
description: tt(:meta)
relationships: {
type: :object,
properties: relationships_properties,
description: tt(:associate_data)
}
},
links: {
type: :object,
properties: {
first: { type: :string, description: tt(:first_page_link) },
next: { type: :string, description: tt(:next_page_link) },
last: { type: :string, description: tt(:last_page_link) },
},
description: tt(:page_links) },
},
required: [:data]
description: tt(:data)
}
}

unless paginator == :none
response_properties[:meta] = {
type: :object,
properties: {
record_count: { type: :integer, description: tt(:record_count)},
page_count: { type: :integer, description: tt(:page_count)},
},
description: tt(:meta)
}
response_properties[:links] = {
type: :object,
properties: {
first: { type: :string, description: tt(:first_page_link) },
next: { type: :string, description: tt(:next_page_link) },
last: { type: :string, description: tt(:last_page_link) },
},
description: tt(:page_links)
}
end

{
'200' => {
description: tt(:get_list),
schema: {
type: :object,
properties: response_properties,
required: [:data]
}
}
}
Expand Down Expand Up @@ -316,4 +324,4 @@ else
end
-%>
"paths": <%= JSON.pretty_generate(doc['paths'] ) %>
}
}
7 changes: 6 additions & 1 deletion lib/generators/jsonapi/swagger/templates/swagger.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ RSpec.describe '<%= resouces_name %>', type: :request do
get '<%= route_resouces %> <%= t(:list) %>' do
tags '<%= route_resouces %>'
produces 'application/vnd.api+json'
<% if paginator == :paged %>
parameter name: :'page[number]', in: :query, type: :string, description: '<%= t(:page_num) %>', required: false
<% end %>
<% if sortable_fields.present? -%>
parameter name: :'sort', in: :query, type: :string, description: '<%= sortable_fields_desc %>', required: false
<% end -%>
Expand Down Expand Up @@ -80,6 +82,7 @@ RSpec.describe '<%= resouces_name %>', type: :request do
},
description: '<%= t(:data) %>'
},
<% unless paginator == :none %>
meta: {
type: :object,
properties: {
Expand All @@ -95,7 +98,9 @@ RSpec.describe '<%= resouces_name %>', type: :request do
next: { type: :string, description: '<%= t(:next_page_link) %>'},
last: { type: :string, description: '<%= t(:last_page_link) %>'},
},
description: '<%= t(:page_links) %>' },
description: '<%= t(:page_links) %>'
},
<% end %>
},
required: [:data]
run_test!
Expand Down
6 changes: 5 additions & 1 deletion lib/jsonapi/swagger/resources/fast_jsonapi_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ def filters
def mutable?
false
end

def paginator
:paged
end
end
end
end
end
6 changes: 4 additions & 2 deletions lib/jsonapi/swagger/resources/jsonapi_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ class JsonapiResource
extend Forwardable

def_delegators :@jr, :_attributes, :_relationships, :sortable_fields,
:creatable_fields, :updatable_fields, :filters, :mutable?
:creatable_fields, :updatable_fields, :filters, :mutable?,
:_paginator

def initialize(jr)
@jr = jr
end

alias attributes _attributes
alias relationships _relationships
alias paginator _paginator
end
end
end
end
6 changes: 5 additions & 1 deletion lib/jsonapi/swagger/resources/serializable_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ def filters
def mutable?
false
end

def paginator
:paged
end
end
end
end
end