-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Define entity in serializer for grape_swagger (#9)
* Model entity in serializer definition * Use constantize for ref * Fix loading and entity camel_lower * Update README with example for entity * Change check for enumarable * Revert using hash for attributes_to_serialize * Rename method * Add changelog entry
- Loading branch information
Showing
9 changed files
with
238 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'parser' | ||
|
||
module BrightSerializer | ||
module Entity | ||
class Base | ||
DEFAULT_DEFINITION = { type: :undefined }.freeze | ||
|
||
# https://swagger.io/specification/v2/?sbsearch=array%20response#schema-object | ||
|
||
def initialize(definition) | ||
@definition = definition | ||
end | ||
|
||
def to_h | ||
@definition.transform_keys! { |k| Inflector.camel_lower k.to_s } | ||
parse_ref! | ||
@definition | ||
end | ||
|
||
def parse_ref! | ||
object = nested_hash(@definition, 'ref') | ||
return unless object | ||
|
||
ref_entity_name = Inflector.constantize(object.delete('ref')).entity_name | ||
relation = "#/definitions/#{ref_entity_name}" | ||
object['$ref'] = relation | ||
end | ||
|
||
def nested_hash(obj, key) | ||
if obj.respond_to?(:key?) && obj.key?(key) | ||
obj | ||
elsif obj.respond_to?(:each) | ||
r = nil | ||
obj.find { |*a| r = nested_hash(a.last, key) } | ||
r | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
module BrightSerializer | ||
module Entity | ||
class Parser | ||
attr_reader :model | ||
attr_reader :endpoint | ||
|
||
def initialize(model, endpoint) | ||
@model = model | ||
@endpoint = endpoint | ||
end | ||
|
||
def call | ||
@model.entity | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe BrightSerializer::Entity::Base do | ||
let(:instance) { described_class.new(type: :string) } | ||
let(:entity_class) do | ||
Class.new do | ||
include BrightSerializer::Serializer | ||
|
||
def self.entity_name | ||
'user' | ||
end | ||
end | ||
end | ||
|
||
before do | ||
allow(Inflector).to receive(:constantize) { entity_class } | ||
end | ||
|
||
describe '#to_h' do | ||
subject { instance.to_h } | ||
|
||
it 'return the definition' do | ||
expect(subject).to eq('type' => :string) | ||
end | ||
end | ||
|
||
describe '#parser_ref!' do | ||
subject { instance.to_h } | ||
|
||
let(:instance) { described_class.new(ref: 'SomeModule::User') } | ||
|
||
it 'modified @definition' do | ||
expect(subject).to eq('$ref' => '#/definitions/user') | ||
end | ||
|
||
context 'when deep ref' do | ||
let(:instance) do | ||
described_class.new( | ||
type: :array, | ||
items: { 'ref' => 'SomeModule::User' } | ||
) | ||
end | ||
|
||
it 'modified @definition' do | ||
expect(subject).to( | ||
eq( | ||
'type' => :array, | ||
'items' => { | ||
'$ref' => '#/definitions/user' | ||
} | ||
) | ||
) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe BrightSerializer::Serializer do | ||
describe '.entity' do | ||
subject { serializer_class.entity } | ||
|
||
let(:serializer_class) do | ||
Class.new do | ||
include BrightSerializer::Serializer | ||
attribute :name, entity: { type: :string } | ||
end | ||
end | ||
|
||
it 'return name with his entity' do | ||
expect(subject).to eq(name: { 'type' => :string }) | ||
end | ||
|
||
context 'when one is undefined' do | ||
let(:serializer_class) do | ||
Class.new do | ||
include BrightSerializer::Serializer | ||
attribute :name, entity: { type: :string } | ||
attribute :id | ||
end | ||
end | ||
|
||
it 'return name with his entity' do | ||
expect(subject).to eq(id: { type: :undefined }, name: { 'type' => :string }) | ||
end | ||
end | ||
end | ||
|
||
describe '.entity_name' do | ||
subject { serializer_class.entity_name } | ||
|
||
let(:serializer_class) do | ||
Class.new do | ||
include BrightSerializer::Serializer | ||
end | ||
end | ||
|
||
it 'return the class name downcase' do | ||
allow(serializer_class).to receive(:name).and_return('BrightSerializer::Serializer::User') | ||
expect(subject).to eq('user') | ||
end | ||
end | ||
end |