lib/prmd/templates/schemata/helper.erb in prmd-0.5.0 vs lib/prmd/templates/schemata/helper.erb in prmd-0.6.0
- old
+ new
@@ -4,10 +4,16 @@
properties = properties.sort_by {|k,v| k} # ensure consistent ordering
properties.each do |key, value|
# found a reference to another element:
_, value = schema.dereference(value)
+
+ # include top level reference to nested things, when top level is nullable
+ if value.has_key?('type') && value['type'].include?('null') && (value.has_key?('items') || value.has_key?('properties'))
+ attributes << build_attribute(schema, key, value)
+ end
+
if value.has_key?('anyOf')
descriptions = []
examples = []
# sort anyOf! always show unique identifier first
@@ -32,54 +38,61 @@
example = [*examples].map { |e| "<code>#{e.to_json}</code>" }.join(" or ")
attributes << [key, "string", description, example]
# found a nested object
- elsif value['type'] == ['object'] && value['properties']
+ elsif value['properties']
nested = extract_attributes(schema, value['properties'])
nested.each do |attribute|
attribute[0] = "#{key}:#{attribute[0]}"
end
attributes.concat(nested)
# found an array with nested objects
- elsif value['type'] == ['array'] && value['items'] && value['items']['properties']
+ elsif value['items'] && value['items']['properties']
nested = extract_attributes(schema, value['items']['properties'])
nested.each do |attribute|
attribute[0] = "#{key}/#{attribute[0]}"
end
attributes.concat(nested)
# just a regular attribute
else
- description = value['description']
- if value['default']
- description += "<br/><b>default:</b> <code>#{value['default'].to_json}</code>"
- end
+ attributes << build_attribute(schema, key, value)
+ end
+ end
+ return attributes
+ end
- if value['enum']
- description += '<br/><b>one of:</b>' + [*value['enum']].map { |e| "<code>#{e.to_json}</code>" }.join(" or ")
- end
+ def build_attribute(schema, key, value)
+ description = value['description']
+ if value['default']
+ description += "<br/><b>default:</b> <code>#{value['default'].to_json}</code>"
+ end
- if value.has_key?('example')
- example = if value['example'].is_a?(Hash) && value['example'].has_key?('oneOf')
- value['example']['oneOf'].map { |e| "<code>#{e.to_json}</code>" }.join(" or ")
- else
- "<code>#{value['example'].to_json}</code>"
- end
- elsif value['type'] == ['array'] && value.has_key?('items')
- example = "<code>#{schema.schema_value_example(value)}</code>"
- end
+ if value['enum']
+ description += '<br/><b>one of:</b>' + [*value['enum']].map { |e| "<code>#{e.to_json}</code>" }.join(" or ")
+ end
- type = if value['type'].include?('null')
- 'nullable '
- else
- ''
- end
- type += (value['format'] || (value['type'] - ['null']).first)
- attributes << [key, type, description, example]
+ if value.has_key?('example')
+ example = if value['example'].is_a?(Hash) && value['example'].has_key?('oneOf')
+ value['example']['oneOf'].map { |e| "<code>#{e.to_json}</code>" }.join(" or ")
+ else
+ "<code>#{value['example'].to_json}</code>"
end
+ elsif value['type'] == ['array'] && value.has_key?('items')
+ example = "<code>#{schema.schema_value_example(value)}</code>"
+ elsif value['type'].include?('null')
+ example = "<code>null</code>"
end
- return attributes
+
+ type = if value['type'].include?('null')
+ 'nullable '
+ else
+ ''
+ end
+ type += (value['format'] || (value['type'] - ['null']).first)
+ [key, type, description, example]
end
+
def build_link_path(schema, link)
link['href'].gsub(%r|(\{\([^\)]+\)\})|) do |ref|
ref = ref.gsub('%2F', '/').gsub('%23', '#').gsub(%r|[\{\(\)\}]|, '')
ref_resource = ref.split('#/definitions/').last.split('/').first.gsub('-','_')