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

feature/ruby serializers registration #2093

Merged
merged 10 commits into from
Dec 28, 2022
11 changes: 4 additions & 7 deletions .github/workflows/abstractions-ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
# Due to https://github.com/actions/runner/issues/849, we have to use quotes for '3.0'
ruby-version: [2.5, 2.6, 2.7, '3.0', head, jruby, jruby-head, truffleruby, truffleruby-head]
ruby-version: ['2.7', '3.0', '3.1', head, jruby, jruby-head, truffleruby, truffleruby-head]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
Expand All @@ -32,7 +31,7 @@ jobs:
run: bundle exec rake
working-directory: ${{ env.relativePath }}
- name: Upload artifacts for ruby version 3 and ubuntu
if: ${{ matrix.os == 'ubuntu-latest' && matrix.ruby-version == '3.0'}}
if: ${{ matrix.os == 'ubuntu-latest' && matrix.ruby-version == '3.1'}}
uses: actions/upload-artifact@v3
with:
name: drop
Expand All @@ -51,8 +50,7 @@ jobs:
- uses: actions/checkout@v3
- uses: ruby/setup-ruby@v1
with:
# Due to https://github.com/actions/runner/issues/849, we have to use quotes for '3.0'
ruby-version: '3.0'
ruby-version: '3.1'
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
bundler: 'latest'
cache-version: 1
Expand Down Expand Up @@ -83,8 +81,7 @@ jobs:
# - uses: actions/checkout@v3
# - uses: ruby/setup-ruby@v1
# with:
# # Due to https://github.com/actions/runner/issues/849, we have to use quotes for '3.0'
# ruby-version: '3.0'
# ruby-version: '3.1'
# bundler-cache: true # runs 'bundle install' and caches installed gems automatically
# - run: bundle exec rake
# - name: Publish to RubyGems
Expand Down
11 changes: 4 additions & 7 deletions .github/workflows/authentication-ruby-oauth.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
# Due to https://github.com/actions/runner/issues/849, we have to use quotes for '3.0'
ruby-version: [2.5, 2.6, 2.7, '3.0', head, jruby, jruby-head, truffleruby, truffleruby-head]
ruby-version: ['2.7', '3.0', '3.1', head, jruby, jruby-head, truffleruby, truffleruby-head]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
Expand All @@ -34,7 +33,7 @@ jobs:
run: bundle exec rake
working-directory: ${{ env.relativePath }}
- name: Upload artifacts for ruby version 3 and ubuntu
if: ${{ matrix.os == 'ubuntu-latest' && matrix.ruby-version == '3.0'}}
if: ${{ matrix.os == 'ubuntu-latest' && matrix.ruby-version == '3.1'}}
uses: actions/upload-artifact@v3
with:
name: drop
Expand All @@ -54,8 +53,7 @@ jobs:
- uses: actions/checkout@v3
- uses: ruby/setup-ruby@v1
with:
# Due to https://github.com/actions/runner/issues/849, we have to use quotes for '3.0'
ruby-version: '3.0'
ruby-version: '3.1'
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
bundler: 'latest'
cache-version: 1
Expand Down Expand Up @@ -87,8 +85,7 @@ jobs:
# - uses: actions/checkout@v3
# - uses: ruby/setup-ruby@v1
# with:
# # Due to https://github.com/actions/runner/issues/849, we have to use quotes for '3.0'
# ruby-version: '3.0'
# ruby-version: '3.1'
# bundler-cache: true # runs 'bundle install' and caches installed gems automatically
# - run: bundle exec rake
# - name: Publish to RubyGems
Expand Down
11 changes: 4 additions & 7 deletions .github/workflows/http-ruby-nethttp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
# Due to https://github.com/actions/runner/issues/849, we have to use quotes for '3.0'
ruby-version: [2.5, 2.6, 2.7, '3.0', head, jruby, jruby-head, truffleruby, truffleruby-head]
ruby-version: ['2.7', '3.0', '3.1', head, jruby, jruby-head, truffleruby, truffleruby-head]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
Expand All @@ -34,7 +33,7 @@ jobs:
run: bundle exec rake
working-directory: ${{ env.relativePath }}
- name: Upload artifacts for ruby version 3 and ubuntu
if: ${{ matrix.os == 'ubuntu-latest' && matrix.ruby-version == '3.0'}}
if: ${{ matrix.os == 'ubuntu-latest' && matrix.ruby-version == '3.1'}}
uses: actions/upload-artifact@v3
with:
name: drop
Expand All @@ -54,8 +53,7 @@ jobs:
- uses: actions/checkout@v3
- uses: ruby/setup-ruby@v1
with:
# Due to https://github.com/actions/runner/issues/849, we have to use quotes for '3.0'
ruby-version: '3.0'
ruby-version: '3.1'
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
bundler: 'latest'
cache-version: 1
Expand Down Expand Up @@ -86,8 +84,7 @@ jobs:
# - uses: actions/checkout@v3
# - uses: ruby/setup-ruby@v1
# with:
# # Due to https://github.com/actions/runner/issues/849, we have to use quotes for '3.0'
# ruby-version: '3.0'
# ruby-version: '3.1'
# bundler-cache: true # runs 'bundle install' and caches installed gems automatically
# - run: bundle exec rake
# - name: Publish to RubyGems
Expand Down
11 changes: 4 additions & 7 deletions .github/workflows/serialization-ruby-json.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
# Due to https://github.com/actions/runner/issues/849, we have to use quotes for '3.0'
ruby-version: [2.5, 2.6, 2.7, '3.0', head, jruby, jruby-head, truffleruby, truffleruby-head]
ruby-version: ['2.7', '3.0', '3.1', head, jruby, jruby-head, truffleruby, truffleruby-head]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
Expand All @@ -34,7 +33,7 @@ jobs:
run: bundle exec rake
working-directory: ${{ env.relativePath }}
- name: Upload artifacts for ruby version 3 and ubuntu
if: ${{ matrix.os == 'ubuntu-latest' && matrix.ruby-version == '3.0'}}
if: ${{ matrix.os == 'ubuntu-latest' && matrix.ruby-version == '3.1'}}
uses: actions/upload-artifact@v3
with:
name: drop
Expand All @@ -54,8 +53,7 @@ jobs:
- uses: actions/checkout@v3
- uses: ruby/setup-ruby@v1
with:
# Due to https://github.com/actions/runner/issues/849, we have to use quotes for '3.0'
ruby-version: '3.0'
ruby-version: '3.1'
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
bundler: 'latest'
cache-version: 1
Expand Down Expand Up @@ -87,8 +85,7 @@ jobs:
# - uses: actions/checkout@v3
# - uses: ruby/setup-ruby@v1
# with:
# # Due to https://github.com/actions/runner/issues/849, we have to use quotes for '3.0'
# ruby-version: '3.0'
# ruby-version: '3.1'
# bundler-cache: true # runs 'bundle install' and caches installed gems automatically
# - run: bundle exec rake
# - name: Publish to RubyGems
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Added support for auto-registration of serializers in Ruby. [#478](https://github.com/microsoft/kiota/issues/478)

### Changed

- Fixed a bug in Ruby where the request adapter URL would be overwritten by the client defaults. [#1647](https://github.com/microsoft/kiota/issues/1647)
Expand Down
2 changes: 2 additions & 0 deletions abstractions/ruby/microsoft_kiota_abstractions/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@

# rspec failure tracking
.rspec_status

Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require_relative "microsoft_kiota_abstractions/authentication/base_bearer_token_authentication_provider"
require_relative "microsoft_kiota_abstractions/authentication/anonymous_authentication_provider"
require_relative "microsoft_kiota_abstractions/http_method"
require_relative "microsoft_kiota_abstractions/api_client_builder"
require_relative "microsoft_kiota_abstractions/request_adapter"
require_relative "microsoft_kiota_abstractions/request_information"
require_relative "microsoft_kiota_abstractions/response_handler"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,16 @@
require_relative "serialization/serialization_writer_factory_registry"

module MicrosoftKiotaAbstractions
module ApiClientBuilder

#TODO: Implement default de/serializer registration in the api_client generation issue #478
def register_default_serializer(factory_class)
begin
factory = factory_class.new()
MicrosoftKiotaAbstractions::SerializationWriterFactoryRegistry.new().content_type_associated_factories[factory.get_valid_content_type(), factory]
rescue => exception
raise exception
end
end
class ApiClientBuilder
def self.register_default_serializer(factory_class)
factory = factory_class.new()
MicrosoftKiotaAbstractions::SerializationWriterFactoryRegistry.default_instance.content_type_associated_factories[factory.get_valid_content_type()] = factory
end

def register_default_deserializer(factory_class)
begin
factory = factory_class.new()
MicrosoftKiotaAbstractions::ParseNodeFactoryRegistry.new().content_type_associated_factories[factory.get_valid_content_type(), factory]
rescue => exception
raise exception
end
end
def self.register_default_deserializer(factory_class)
factory = factory_class.new()
MicrosoftKiotaAbstractions::ParseNodeFactoryRegistry.default_instance.content_type_associated_factories[factory.get_valid_content_type()] = factory
end
end
end

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
module MicrosoftKiotaAbstractions
# Provides a base class for implementing AuthenticationProvider for Bearer token scheme
class BaseBearerTokenAuthenticationProvider
include MicrosoftKiotaAbstractions::AccessTokenProvider
include MicrosoftKiotaAbstractions::AuthenticationProvider
include Concurrent::Async
def initialize(access_token_provider)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ module MicrosoftKiotaAbstractions
class ParseNodeFactoryRegistry
include ParseNodeFactory

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

def default_instance
self.class.default_instance
end

def content_type_associated_factories
@content_type_associated_factories ||= Hash.new
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ module MicrosoftKiotaAbstractions
class SerializationWriterFactoryRegistry
include SerializationWriterFactory

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

def default_instance
self.class.default_instance
end

def content_type_associated_factories
@content_type_associated_factories ||= Hash.new
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.3.0"
VERSION = "0.3.1"
end
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,12 @@
expect(url5).to eq(false)
expect(url6).to eq(false)
end

it 'tests the default instance for ParseNodeFactoryRegistry is set' do
expect(MicrosoftKiotaAbstractions::ParseNodeFactoryRegistry.default_instance).to_not be_nil
end

it 'tests the default instance for SerializationWriterFactoryRegistry is set' do
expect(MicrosoftKiotaAbstractions::SerializationWriterFactoryRegistry.default_instance).to_not be_nil
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@

# rspec failure tracking
.rspec_status

Gemfile.lock
2 changes: 2 additions & 0 deletions http/ruby/nethttp/microsoft_kiota_nethttplibrary/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@

# rspec failure tracking
.rspec_status

Gemfile.lock
2 changes: 1 addition & 1 deletion http/ruby/nethttp/microsoft_kiota_nethttplibrary/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ gemspec
git_source(:github) { |repo_name| 'https://rubygems.pkg.github.com/microsoft' }

source 'https://rubygems.pkg.github.com/microsoft' do
gem 'microsoft_kiota_abstractions', '0.1.11'
gem 'microsoft_kiota_abstractions', '0.3.1'
end

gem 'rake', '~> 13.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@ class NetHttpRequestAdapter

attr_accessor :authentication_provider, :content_type_header_key, :parse_node_factory, :serialization_writer_factory, :client

# TODO: When #478 is implemented then parse_node_factory and serialization_writer_factory should default to nil
def initialize(authentication_provider, parse_node_factory, serialization_writer_factory, client = Net::HTTP)
def initialize(authentication_provider, parse_node_factory=MicrosoftKiotaAbstractions::ParseNodeFactoryRegistry.default_instance, serialization_writer_factory=MicrosoftKiotaAbstractions::SerializationWriterFactoryRegistry.default_instance, client = Net::HTTP)

if !authentication_provider
raise StandardError , 'authentication provider cannot be null'
end
@authentication_provider = authentication_provider
@content_type_header_key = 'Content-Type'
# TODO: When #478 is implemented get the static factories if @parse_node_factory and @serialization_writer_factory are nil
@parse_node_factory = parse_node_factory
@parse_node_factory = parse_node_factory
if @parse_node_factory.nil?
@parse_node_factory = MicrosoftKiotaAbstractions::ParseNodeFactoryRegistry.default_instance
end
@serialization_writer_factory = serialization_writer_factory
if @serialization_writer_factory.nil?
@serialization_writer_factory = MicrosoftKiotaAbstractions::SerializationWriterFactoryRegistry.default_instance
end
@client = client
@base_url = ''
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module MicrosoftKiotaNethttplibrary
VERSION = '0.2.0'
VERSION = '0.2.1'
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class AuthenticationProvider
include MicrosoftKiotaAbstractions::AuthenticationProvider
include Concurrent::Async
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true
require_relative 'authentication_provider'

RSpec.describe MicrosoftKiotaNethttplibrary do
it "has a version number" do
Expand All @@ -8,4 +9,10 @@
it "does something useful" do
expect(true).to eq(true)
end

it "can instantiate a request adapter" do
auth_provider = AuthenticationProvider.new()
adapter = MicrosoftKiotaNethttplibrary::NetHttpRequestAdapter.new(auth_provider)
expect(adapter).to_not be nil
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@

# rspec failure tracking
.rspec_status

Gemfile.lock
4 changes: 2 additions & 2 deletions src/Kiota.Builder/Refiners/CommonLanguageRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ currentMethod.Parent is CodeClass currentClass &&
CrawlTree(generatedCode, x => AddSerializationModulesImport(x, serializationWriterFactoryInterfaceAndRegistrationFullName, parseNodeFactoryInterfaceAndRegistrationFullName, separator));
}
protected static void ReplaceDefaultSerializationModules(CodeElement generatedCode, HashSet<string> defaultValues, HashSet<string> newModuleNames) {
if(ReplaceSerializationModules(generatedCode, x => x.SerializerModules, (x, y) => x.SerializerModules = y, defaultValues, newModuleNames))
if(ReplaceSerializationModules(generatedCode, static x => x.SerializerModules, (x, y) => x.SerializerModules = y, defaultValues, newModuleNames))
return;
CrawlTree(generatedCode, x => ReplaceDefaultSerializationModules(x, defaultValues, newModuleNames));
}
protected static void ReplaceDefaultDeserializationModules(CodeElement generatedCode, HashSet<string> defaultValues, HashSet<string> newModuleNames) {
if(ReplaceSerializationModules(generatedCode, x => x.DeserializerModules, (x, y) => x.DeserializerModules = y, defaultValues, newModuleNames))
if(ReplaceSerializationModules(generatedCode, static x => x.DeserializerModules, (x, y) => x.DeserializerModules = y, defaultValues, newModuleNames))
return;
CrawlTree(generatedCode, x => ReplaceDefaultDeserializationModules(x, defaultValues, newModuleNames));
}
Expand Down
Loading