Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Metrics/BlockLength:
# Offense count: 5
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 175
Max: 185

# Offense count: 17
# Configuration parameters: AllowedMethods, AllowedPatterns.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,21 @@ def send_async(_request_info, _factory, _errors_mapping)
raise NotImplementedError
end

# TODO: we're most likley missing something for enums and collections or at least at the implemenation level
def send_collection_async(_request_info, _factory, _errors_mapping)
raise NotImplementedError
end

def send_collection_of_primitive_async(_request_info, _type, _errors_mapping)
raise NotImplementedError
end

def send_primitive_async(_request_info, _type, _errors_mapping)
raise NotImplementedError
end

def send_no_response_content_async(_request_info, _errors_mapping)
raise NotImplementedError
end

def get_serialization_writer_factory
raise NotImplementedError
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def assign_field_values(_item)
raise NotImplementedError
end

def get_collection_of_enum_values(_type)
raise NotImplementedError
end

def get_enum_value(_type)
raise NotImplementedError
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'microsoft_kiota_abstractions'
require 'faraday'
require 'net/http'
require 'stringio'
require_relative 'kiota_client_factory'
require_relative 'middleware/response_handler_option'

Expand Down Expand Up @@ -44,10 +45,63 @@ def get_serialization_writer_factory
@serialization_writer_factory
end

PRIMITIVE_READERS = {
'String' => :get_string_value,
'Float' => :get_float_value,
'Integer' => :get_number_value,
'Date' => :get_date_value,
'DateTime' => :get_date_time_value,
'Time' => :get_time_value,
'MicrosoftKiotaAbstractions::ISODuration' => :get_duration_value,
'UUIDTools::UUID' => :get_guid_value,
'boolean' => :get_boolean_value,
'Boolean' => :get_boolean_value
}.freeze

def send_async(request_info, factory, errors_mapping)
raise StandardError, 'request_info cannot be null' unless request_info
raise StandardError, 'factory cannot be null' unless factory

execute_async(request_info, errors_mapping) do |response|
get_root_parse_node(response)&.get_object_value(factory)
end
end

def send_collection_async(request_info, factory, errors_mapping)
raise StandardError, 'factory cannot be null' unless factory

execute_async(request_info, errors_mapping) do |response|
get_root_parse_node(response)&.get_collection_of_object_values(factory)
end
end

def send_collection_of_primitive_async(request_info, type, errors_mapping)
execute_async(request_info, errors_mapping) do |response|
root_node = get_root_parse_node(response)
next root_node&.get_collection_of_enum_values(type) if type.is_a?(Hash)

root_node&.get_collection_of_primitive_values(type)
end
end

def send_primitive_async(request_info, type, errors_mapping)
execute_async(request_info, errors_mapping) do |response|
next StringIO.new(response.body.to_s) if type == StringIO
next get_root_parse_node(response)&.get_enum_value(type) if type.is_a?(Hash)
Comment on lines +87 to +89

reader = PRIMITIVE_READERS[type.to_s]
raise StandardError, "unexpected primitive response type #{type}" if reader.nil?

get_root_parse_node(response)&.public_send(reader)
end
end

def send_no_response_content_async(request_info, errors_mapping)
execute_async(request_info, errors_mapping) { nil }
end

def execute_async(request_info, errors_mapping)
raise StandardError, 'request_info cannot be null' unless request_info

Fiber.new do
set_base_url_for_request_information(request_info)
@authentication_provider.authenticate_request(request_info).resume
Expand All @@ -57,8 +111,7 @@ def send_async(request_info, factory, errors_mapping)
response_handler = get_response_handler(request_info)
response_handler&.call(response)&.resume
throw_if_failed_reponse(response, errors_mapping)
root_node = get_root_parse_node(response)
root_node.get_object_value(factory)
yield response
end
end

Expand All @@ -72,9 +125,10 @@ def get_response_handler(request_info)
def get_root_parse_node(response)
raise StandardError, 'response cannot be null' unless response

return if response.body.nil? || response.body.empty?

response_content_type = get_response_content_type(response)
raise StandardError, 'no response content type found for deserialization' unless response_content_type
return if response.body.nil? || response.body.empty?

@parse_node_factory.get_parse_node(response_content_type, response.body)
end
Expand Down
103 changes: 103 additions & 0 deletions components/http/spec/microsoft_kiota_faraday/send_methods_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# frozen_string_literal: true

require 'stringio'

RSpec.describe MicrosoftKiotaFaraday::FaradayRequestAdapter do
subject(:adapter) do
described_class.new(authentication_provider, parse_node_factory, nil, client).tap do |a|
a.set_base_url('https://example.com')
end
end

let(:authentication_provider) { double('authentication_provider') }
let(:parse_node_factory) { double('parse_node_factory') }
let(:parse_node) { double('parse_node') }
let(:client) { double('client') }
let(:headers) { { 'content-type' => 'application/json' } }
let(:factory) { ->(_pn) {} }

let(:request_info) do
info = MicrosoftKiotaAbstractions::RequestInformation.new
info.http_method = :GET
info.url_template = '{+baseurl}/things'
info.path_parameters = { 'baseurl' => 'https://example.com' }
info
end

def respond_with(body, status: 200, response_headers: headers)
response = instance_double(Faraday::Response, status:, body:, headers: response_headers)
allow(client).to receive(:build_request).and_return(Faraday::Request.create(:get))
allow(client).to receive(:run_request).and_return(response)
response
end

before do
allow(authentication_provider).to receive(:authenticate_request).and_return(Fiber.new { nil })
allow(parse_node_factory).to receive(:get_parse_node).and_return(parse_node)
end

describe '#send_no_response_content_async' do
it 'returns without parsing a body' do
respond_with('', status: 204, response_headers: {})
expect(parse_node_factory).not_to receive(:get_parse_node)
expect(adapter.send_no_response_content_async(request_info, nil).resume).to be_nil
end
end

describe '#send_collection_async' do
it 'reads the payload as a collection of objects' do
respond_with('[{"name":"a"},{"name":"b"}]')
allow(parse_node).to receive(:get_collection_of_object_values).with(factory).and_return(%w[a b])
expect(adapter.send_collection_async(request_info, factory, nil).resume).to eq(%w[a b])
end
end

describe '#send_collection_of_primitive_async' do
it 'reads the payload as a collection of primitives' do
respond_with('["a","b"]')
allow(parse_node).to receive(:get_collection_of_primitive_values).with(String).and_return(%w[a b])
expect(adapter.send_collection_of_primitive_async(request_info, String, nil).resume).to eq(%w[a b])
end
end

describe '#send_collection_of_primitive_async with an enum' do
it 'reads the payload as a collection of enum values' do
colour = { Red: :Red, Blue: :Blue }.freeze
respond_with('["red","blue"]')
allow(parse_node).to receive(:get_collection_of_enum_values).with(colour).and_return(%i[Red Blue])
expect(adapter.send_collection_of_primitive_async(request_info, colour, nil).resume).to eq(%i[Red Blue])
end
end

describe '#send_primitive_async' do
it 'reads a scalar through the matching parse node getter' do
respond_with('"hello"')
allow(parse_node).to receive(:get_string_value).and_return('hello')
expect(adapter.send_primitive_async(request_info, String, nil).resume).to eq('hello')
end

it 'reads an enum through get_enum_value' do
colour = { Red: :Red, Green: :Green }.freeze
respond_with('"Red"')
allow(parse_node).to receive(:get_enum_value).with(colour).and_return(:Red)
expect(adapter.send_primitive_async(request_info, colour, nil).resume).to eq(:Red)
end

it 'returns the raw body for a binary response without parsing it' do
respond_with('rawbytes', response_headers: { 'content-type' => 'application/octet-stream' })
expect(parse_node_factory).not_to receive(:get_parse_node)
expect(adapter.send_primitive_async(request_info, StringIO, nil).resume.read).to eq('rawbytes')
end
end
describe 'an empty response with no content type' do
it 'returns nil from send_async rather than raising' do
respond_with('', status: 204, response_headers: {})
expect(adapter.send_async(request_info, factory, nil).resume).to be_nil
end

it 'returns nil from send_collection_async rather than raising' do
respond_with('', status: 204, response_headers: {})
expect(adapter.send_collection_async(request_info, factory, nil).resume).to be_nil
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,27 @@ def assign_field_values(item)
end
end

def get_enum_values(_type)
def get_enum_values(type)
raw_values = get_string_value
return [] if raw_values.nil?
return [] if raw_values.nil? || raw_values.empty?

raw_values.split(',').map(&:strip)
raw_values.split(',').map { |raw| resolve_enum_member(type, raw.strip) }
end

def get_enum_value(type)
items = get_enum_values(type).map(&:to_sym)
items[0] if items.length.positive?
get_enum_values(type).first
end

def get_collection_of_enum_values(type)
return get_enum_values(type) unless @current_node.is_a?(Array)

@current_node.map { |value| JsonParseNode.new(value).get_enum_value(type) }
end

def resolve_enum_member(type, raw)
return raw.to_sym unless type.is_a?(Hash)

type[raw.to_sym] || type[(raw[0].to_s.upcase + raw[1..].to_s).to_sym]
end

def get_child_node(name)
Expand Down
42 changes: 42 additions & 0 deletions components/serialization/json/spec/json_enum_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

require_relative 'spec_helper'
require 'microsoft_kiota_abstractions'

RSpec.describe 'reading enums' do
let(:colour) { { Red: :Red, Green: :Green, Blue: :Blue }.freeze }

def node_for(json) = MicrosoftKiotaSerializationJson::JsonParseNode.new(JSON.parse(json))

describe '#get_enum_value' do
it 'resolves a wire value against the enum definition' do
expect(node_for('"blue"').get_enum_value(colour)).to eq(:Blue)
end

it 'resolves a wire value that already matches the member name' do
expect(node_for('"Blue"').get_enum_value(colour)).to eq(:Blue)
end

it 'returns nil for a member the enum does not declare' do
expect(node_for('"purple"').get_enum_value(colour)).to be_nil
end
end

describe '#get_collection_of_enum_values' do
it 'reads a JSON array of enum values' do
expect(node_for('["red","blue"]').get_collection_of_enum_values(colour)).to eq(%i[Red Blue])
end

it 'reads a comma separated string, the way a flags enum arrives' do
expect(node_for('"red,blue"').get_collection_of_enum_values(colour)).to eq(%i[Red Blue])
end

it 'drops members the enum does not declare' do
expect(node_for('["red","purple"]').get_collection_of_enum_values(colour).compact).to eq([:Red])
end

it 'returns an empty collection for an empty string' do
expect(node_for('""').get_collection_of_enum_values(colour)).to eq([])
end
end
end