render 0.0.9 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/render/schema.rb +19 -4
- data/lib/render/version.rb +1 -1
- data/spec/functional/render/schema_spec.rb +14 -0
- data/spec/unit/render/schema_spec.rb +25 -0
- metadata +2 -2
data/lib/render/schema.rb
CHANGED
@@ -66,7 +66,12 @@ module Render
|
|
66
66
|
private
|
67
67
|
|
68
68
|
def require_attributes!
|
69
|
-
definition.
|
69
|
+
return unless definition.has_key?(:required)
|
70
|
+
|
71
|
+
required_attributes = definition.fetch(:required)
|
72
|
+
return if [true, false].include?(required_attributes)
|
73
|
+
|
74
|
+
required_attributes.each do |required_attribute|
|
70
75
|
attribute = attributes.detect { |attribute| attribute.name == required_attribute.to_sym }
|
71
76
|
attribute.required = true
|
72
77
|
end
|
@@ -97,7 +102,8 @@ module Render
|
|
97
102
|
|
98
103
|
if instance_value.has_key?(:$ref)
|
99
104
|
ref = instance_value.fetch(:$ref)
|
100
|
-
ref_definition =
|
105
|
+
ref_definition = find_foreign_definition(ref)
|
106
|
+
ref_definition ||= find_local_schema(ref, current_scope)
|
101
107
|
instance_value.replace(ref_definition)
|
102
108
|
end
|
103
109
|
|
@@ -105,6 +111,15 @@ module Render
|
|
105
111
|
end
|
106
112
|
end
|
107
113
|
|
114
|
+
def find_foreign_definition(ref)
|
115
|
+
exact_match = Definition.find(ref, false)
|
116
|
+
return exact_match if !exact_match.nil?
|
117
|
+
|
118
|
+
foreign_root_path, foreign_root_scope = ref.split(ROOT_POINTER)
|
119
|
+
fuzzy_match = Definition.instances.detect { |id, definition| id.match(%r{^#{foreign_root_path}}) }
|
120
|
+
find_at_path(foreign_root_scope.split(POINTER_SEPARATOR), fuzzy_match[1]) if fuzzy_match
|
121
|
+
end
|
122
|
+
|
108
123
|
def find_local_schema(ref, scopes)
|
109
124
|
paths = ref.split(POINTER_SEPARATOR)
|
110
125
|
if (paths.first == ROOT_POINTER)
|
@@ -120,8 +135,8 @@ module Render
|
|
120
135
|
find_at_path(scopes + path) || find_at_closest_scope(path, scopes[0...-1])
|
121
136
|
end
|
122
137
|
|
123
|
-
def find_at_path(paths)
|
124
|
-
paths.reduce(
|
138
|
+
def find_at_path(paths, working_definition = definition)
|
139
|
+
paths.reduce(working_definition) do |reduction, path|
|
125
140
|
reduction[path.to_sym] || return
|
126
141
|
end
|
127
142
|
end
|
data/lib/render/version.rb
CHANGED
@@ -80,6 +80,20 @@ module Render
|
|
80
80
|
schema.attributes[0].required.should_not be
|
81
81
|
schema.attributes[1].required.should be
|
82
82
|
end
|
83
|
+
|
84
|
+
it "is silently ignores draft-3 boolean requires" do
|
85
|
+
draft_3_definition = {
|
86
|
+
type: Object,
|
87
|
+
required: true,
|
88
|
+
properties: {
|
89
|
+
title: { type: String }
|
90
|
+
}
|
91
|
+
}
|
92
|
+
|
93
|
+
expect {
|
94
|
+
Schema.new(draft_3_definition).render!
|
95
|
+
}.to_not raise_error
|
96
|
+
end
|
83
97
|
end
|
84
98
|
end
|
85
99
|
end
|
@@ -136,6 +136,31 @@ module Render
|
|
136
136
|
}
|
137
137
|
end
|
138
138
|
|
139
|
+
it "interpolates definitions from foreign schema" do
|
140
|
+
foreign_definition = {
|
141
|
+
id: "http://foreign.com/foo#",
|
142
|
+
type: Object,
|
143
|
+
properties: {
|
144
|
+
title: { type: String }
|
145
|
+
}
|
146
|
+
}
|
147
|
+
Definition.load!(foreign_definition)
|
148
|
+
|
149
|
+
definition = {
|
150
|
+
type: Object,
|
151
|
+
properties: {
|
152
|
+
:$ref => "http://foreign.com/foo#properties"
|
153
|
+
}
|
154
|
+
}
|
155
|
+
schema = Schema.new(definition)
|
156
|
+
schema.definition.should == {
|
157
|
+
type: Object,
|
158
|
+
properties: {
|
159
|
+
title: { type: String }
|
160
|
+
}
|
161
|
+
}
|
162
|
+
end
|
163
|
+
|
139
164
|
it "creates subschemas for relative references from root" do
|
140
165
|
definition = {
|
141
166
|
definitions: {
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: render
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-
|
12
|
+
date: 2014-04-21 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: uuid
|