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

- adds support for vendor specific serialization in Ruby #2103

Merged
merged 1 commit into from
Dec 28, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added support for auto-registration of serializers in Ruby. [#478](https://github.com/microsoft/kiota/issues/478)
- Added support for middleware infrastructure in Ruby. [#1650](https://github.com/microsoft/kiota/issues/1650)
- Added support for query parameters names aliasing in Ruby. [#1664](https://github.com/microsoft/kiota/issues/1664)
- Added support for vendor specific serialization in Ruby. [#1661](https://github.com/microsoft/kiota/issues/1661)

### Changed

Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,42 @@
require_relative 'parse_node_factory'

module MicrosoftKiotaAbstractions
class ParseNodeFactoryRegistry
include ParseNodeFactory
class ParseNodeFactoryRegistry
include ParseNodeFactory

class << self
attr_accessor :default_instance
def default_instance; @default_instance ||= ParseNodeFactoryRegistry.new; end
end
class << self
attr_accessor :default_instance
def default_instance; @default_instance ||= ParseNodeFactoryRegistry.new; end
end

def default_instance
self.class.default_instance
end
def default_instance
self.class.default_instance
end

def content_type_associated_factories
@content_type_associated_factories ||= Hash.new
end
def content_type_associated_factories
@content_type_associated_factories ||= Hash.new
end

def get_parse_node(content_type, content)
if !content_type
raise Exception.new 'content type cannot be undefined or empty'
end
if !content
raise Exception.new 'content cannot be undefined or empty'
end
factory = @content_type_associated_factories[content_type]
if factory
return factory.get_parse_node(content_type, content)
else
raise Exception.new "Content type #{contentType} does not have a factory to be parsed"
end
end

end
def get_parse_node(content_type, content)
if !content_type
raise Exception.new 'content type cannot be undefined or empty'
end
if !content
raise Exception.new 'content cannot be undefined or empty'
end
vendor_specific_content_type = content_type.split(';').first
factory = @content_type_associated_factories[vendor_specific_content_type]
if factory
return factory.get_parse_node(vendor_specific_content_type, content)
end

clean_content_type = vendor_specific_content_type.gsub(/[^\/]+\+/i, '')
factory = @content_type_associated_factories[clean_content_type]
if factory
return factory.get_parse_node(clean_content_type, content)
end
raise Exception.new "Content type #{contentType} does not have a factory to be parsed"
end

end
end
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,19 @@ def get_serialization_writer(content_type)
if !content_type
raise Exception.new 'content type cannot be undefined or empty'
end
factory = @content_type_associated_factories[content_type]
vendor_specific_content_type = content_type.split(';').first
factory = @content_type_associated_factories[vendor_specific_content_type]
if factory
return factory.get_serialization_writer(content_type)
else
raise Exception.new "Content type #{contentType} does not have a factory to be serialized"
return factory.get_serialization_writer(vendor_specific_content_type)
end

clean_content_type = vendor_specific_content_type.gsub(/[^\/]+\+/i, '')
factory = @content_type_associated_factories[clean_content_type]
if factory
return factory.get_serialization_writer(clean_content_type)
end

raise Exception.new "Content type #{contentType} does not have a factory to be serialized"
end

end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module MicrosoftKiotaAbstractions
VERSION = "0.6.0"
VERSION = "0.7.0"
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'microsoft_kiota_abstractions'
class ParseNodeFactoryMock
include MicrosoftKiotaAbstractions::ParseNodeFactory

def get_valid_content_type
'application/json'
end
def get_parse_node(clean_content_type, content)
return {}
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'microsoft_kiota_abstractions'
require_relative 'parse_node_factory_mock'
RSpec.describe MicrosoftKiotaAbstractions do
values = []
values << "application/json"
values << "application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8"
values << "application/vnd.github.mercy-preview+json"
values << "application/vnd.github.mercy-preview+json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8"
serialized_value = "[1]"
MicrosoftKiotaAbstractions::ParseNodeFactoryRegistry.default_instance.content_type_associated_factories["application/json"] = ParseNodeFactoryMock.new
it "gets the parse node" do
values.each do |value|
expect(MicrosoftKiotaAbstractions::ParseNodeFactoryRegistry.default_instance.get_parse_node(value, serialized_value)).not_to be nil
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'microsoft_kiota_abstractions'
class SerializationWriterFactoryMock
include MicrosoftKiotaAbstractions::SerializationWriterFactory

def get_valid_content_type
'application/json'
end
def get_serialization_writer(clean_content)
return {}
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'microsoft_kiota_abstractions'
require_relative 'serialization_writer_factory_mock'
RSpec.describe MicrosoftKiotaAbstractions do
values = []
values << "application/json"
values << "application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8"
values << "application/vnd.github.mercy-preview+json"
values << "application/vnd.github.mercy-preview+json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8"
MicrosoftKiotaAbstractions::SerializationWriterFactoryRegistry.default_instance.content_type_associated_factories["application/json"] = SerializationWriterFactoryMock.new
it "gets the serialization writer" do
values.each do |value|
expect(MicrosoftKiotaAbstractions::SerializationWriterFactoryRegistry.default_instance.get_serialization_writer(value)).not_to be nil
end
end
end