Skip to content
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

Adde user defined attributes order support #83

Merged
merged 1 commit into from
Aug 28, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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": "",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

а можно чтобы по дефолту properties был последний а не первый? просто в большой схеме yaml потом трудно найти где внизу соответствие required & desc нужному объекту

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В данном случае я все правильно сделал, дописал дефолт вниз, как и договаривались 😂

Так что жаловаться не на что. Все же мой вариант с заданным порядком в этом плане нравился больше

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Для новых схем все должно быть по порядку. Только для готовых сохраняем их порядок

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

а. понял, норм. главное чтобы чистая схема генерилась правильно. я правда когда-то глаза ломал по отступам

"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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Place the . on the previous line, together with the method call receiver.


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] ||= {}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

хм. последний...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ага, он и так - последний

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