lib/jdoc/link.rb in jdoc-0.1.1 vs lib/jdoc/link.rb in jdoc-0.1.2

- old
+ new

@@ -62,45 +62,56 @@ end end # @return [String, nil] Example request body in JSON format def request_body - JSON.pretty_generate(RequestGenerator.call(schema.properties)) + "\n" + JSON.pretty_generate(RequestGenerator.call(request_schema.properties)) + "\n" end # @return [true, false] True if this endpoint must have request body def has_request_body? ["PATCH", "POST", "PUT"].include?(method) end # @return [String] JSON response body generated from example properties def response_body - JSON.pretty_generate(response_hash) + object = has_list_data? ? [response_hash] : response_hash + JSON.pretty_generate(object) end # @return [Fixnum] Preferred respone status code for this endpoint def response_status method == "POST" ? 201 : 200 end - # @return [JsonSchema::Schema] Schema for this link, specified by targetSchema or parent schema - def schema + # @return [JsonSchema::Schema] Response schema for this link + def response_schema @raw_link.target_schema || @raw_link.parent end + # @return [JsonSchema::Schema] Request schema for this link + def request_schema + @raw_link.schema || @raw_link.parent + end + # @return [Json::Link::Resource] # @note Resource means each property of top-level properties in this context def resource - @resource ||= Resource.new(schema) + @resource ||= Resource.new(response_schema) end private + # @return [true, false] True if response is intended to be list data + def has_list_data? + @raw_link.rel == "instances" + end + # @return [Hash] # @raise [Rack::Spec::Mock::ExampleNotFound] def response_hash - ResponseGenerator.call(schema.properties) + ResponseGenerator.call(response_schema.properties) end # @return [Fixnum] Order score, used to sort links by preferred method order def method_order_score case method @@ -146,10 +157,10 @@ when !value.data["example"].nil? value.data["example"] when value.type.include?("null") nil when value.type.include?("array") - call(value.items.properties) + [call(value.items.properties)] else raise ExampleNotFound, "No example found for #{schema.pointer}/#{key}" end ) end