Skip to content

Commit

Permalink
Adde user defined attributes order support
Browse files Browse the repository at this point in the history
  • Loading branch information
Strech (Sergey Fedorov) committed Aug 28, 2014
1 parent 37156bb commit f4881bd
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 48 deletions.
18 changes: 9 additions & 9 deletions features/dereferencing_through_inlining.feature
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,23 @@ Feature: $ref defererencing through inlining
Then the file "lurker/definitions/user_request_parameters.json" should contain exactly:
"""json
{
"description": "",
"type": "object",
"additionalProperties": false,
"required": [
],
"properties": {
"name": {
"description": "",
"type": "string",
"example": "Bob"
"example": "Bob",
"description": ""
},
"surname": {
"description": "",
"type": "string",
"example": "Marley"
}
}
},
"description": "",
"type": "object",
"additionalProperties": false,
"required": [
]
}
"""
15 changes: 8 additions & 7 deletions lib/lurker/json/schema/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,22 @@ def eql?(schema)

private

def initialize_default_properties
@schema[Json::DESCRIPTION] ||= ''
@schema[Json::TYPE] ||= ''
@schema[Json::EXAMPLE] ||= ''
end

def parse_schema(schema)
@schema = {}
initialize_properties

if schema.is_a?(Hash)
@schema.merge!(schema)
else
@schema = attributify(schema)
end

initialize_default_properties
end

def attributify(schema)
Expand All @@ -71,12 +78,6 @@ def attributify(schema)
attribute
end

def initialize_properties
@schema[Json::DESCRIPTION] ||= ''
@schema[Json::TYPE] ||= ''
@schema[Json::EXAMPLE] ||= ''
end

def serialize_example(data)
if data.is_a?(ActionDispatch::Http::UploadedFile)
data.headers
Expand Down
34 changes: 14 additions & 20 deletions lib/lurker/json/schema/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,30 @@ def replace!(property, schema)

private

def initialize_properties
def initialize_default_properties(empty_items = {})
@schema[Json::TYPE] ||= Json::ARRAY
@schema[Json::ITEMS] ||= polymorph_items({})
@schema[Json::ITEMS] ||= polymorph_items(empty_items)
end

def parse_schema(schema)
@schema = {}
initialize_properties

if schema_of_any_kind?(schema)
@schema[Json::ITEMS] = polymorph_items(schema)
return
end
schema.is_a?(Array) ? parse_array(schema.dup) : parse_hash(schema.dup)
end

schema = schema.dup
if schema.is_a?(Array)
@schema[Json::ITEMS] = @parser.typed.parse(schema.shift)
def parse_array(schema)
initialize_default_properties([])
return if schema.empty?

schema.each { |payload| @schema[Json::ITEMS].merge!(payload) }
else
@schema[Json::ITEMS] = @parser.typed.parse(schema.delete Json::ITEMS) if schema.key?(Json::ITEMS)
@schema.merge!(schema)
end
@schema[Json::ITEMS] = @parser.typed.parse(schema.shift)
schema.each { |payload| @schema[Json::ITEMS].merge!(payload) }
end

def schema_of_any_kind?(schema)
return true if schema.empty?
return false unless schema.respond_to?(:key?) && schema.key?(Json::ITEMS)
def parse_hash(schema)
@schema.merge!(schema)
@schema[Json::ITEMS] = @parser.typed(polymorph_if_empty: true)
.parse(schema.delete(Json::ITEMS) || schema)

schema[Json::ITEMS].empty?
initialize_default_properties
end

def polymorph_items(schema)
Expand Down
25 changes: 13 additions & 12 deletions lib/lurker/json/schema/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,28 @@ def replace!(property, property_schema)

private

def initialize_default_properties
@schema[Json::DESCRIPTION] ||= ''
@schema[Json::TYPE] ||= Json::OBJECT
@schema[Json::ADDITIONAL_PROPERTIES] = !!@schema[Json::ADDITIONAL_PROPERTIES]
@schema[Json::REQUIRED] ||= []
@schema[Json::PROPERTIES] ||= {}
end

def parse_schema(schema)
@schema = {}
initialize_properties

schema = schema.dup
merge_required = schema.key?(Json::PROPERTIES)
if schema.key?(Json::PROPERTIES)
@schema.merge!(schema)
end

initialize_default_properties

(schema.delete(Json::PROPERTIES) || schema).each do |property, property_schema|
@schema[Json::PROPERTIES][property] = @parser.typed.parse_property(
property, property_schema)
end

@schema.merge!(schema) if merge_required
end

def initialize_properties
@schema[Json::DESCRIPTION] ||= ''
@schema[Json::TYPE] ||= Json::OBJECT
@schema[Json::ADDITIONAL_PROPERTIES] = !!@schema[Json::ADDITIONAL_PROPERTIES]
@schema[Json::REQUIRED] ||= []
@schema[Json::PROPERTIES] ||= {}
end
end
end
Expand Down
26 changes: 26 additions & 0 deletions spec/lurker/json/attribute_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'spec_helper'

describe Lurker::Json::Attribute do
let(:klass) { described_class }

describe '#parse_schema' do
context 'when default attributes order are broken by user' do
let(:attribute) do
klass.new(
'example' => 'razum2um',
'description' => '',
'type' => 'string'
)
end
let(:expected) do
{
'example' => 'razum2um',
'description' => '',
'type' => 'string'
}.to_json
end

it { expect(attribute.to_json).to eq expected }
end
end
end
12 changes: 12 additions & 0 deletions spec/lurker/json/list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@

it { expect(list.to_hash).to eq expected }
end

context 'when default attributes order are broken by user' do
let(:list) { klass.new('items' => {}, 'type' => 'array') }
let(:expected) do
{
'items' => {},
'type' => 'array'
}.to_json
end

it { expect(list.to_json).to eq expected }
end
end

describe '#merge!' do
Expand Down
23 changes: 23 additions & 0 deletions spec/lurker/json/object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,29 @@

it { expect(object.to_hash).to eq expected }
end

context 'when default attributes order are broken by user' do
let(:object) do
klass.new(
'type' => 'object',
'additionalProperties' => true,
'description' => '',
'properties' => {},
'required' => []
)
end
let(:expected) do
{
'type' => 'object',
'additionalProperties' => true,
'description' => '',
'properties' => {},
'required' => []
}.to_json
end

it { expect(object.to_json).to eq expected }
end
end

describe '#merge!' do
Expand Down

0 comments on commit f4881bd

Please sign in to comment.