Skip to content

Commit

Permalink
Merge pull request #1301 from vishrutshah/ruby-bug-fixes
Browse files Browse the repository at this point in the history
* Client Runtime should not throw on unknown provisioning state from server
* Enum model names must be PascalCase
* Ruby model should fall back to the current class model when no discriminator found from server
* Invalid enum values should not throw exceptions. only warnings
* Default values for enum for EnumType should be escape quoted
* Client template should not include models
* azure long running methods should have sync and async versions
  • Loading branch information
vishrutshah committed Jul 21, 2016
2 parents e19ec85 + b187178 commit b54f66a
Show file tree
Hide file tree
Showing 12 changed files with 188 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,24 @@ def check_name_availability_async(account_name, custom_headers = nil)
# the Update Storage Account API. If an account is already created and
# subsequent PUT request is issued with exact same set of properties, then
# HTTP 200 would be returned.
#
# @param resource_group_name [String] The name of the resource group within
# the user's subscription.
# @param account_name [String] The name of the storage account within the
# specified resource group. Storage account names must be between 3 and 24
# characters in length and use numbers and lower-case letters only.
# @param parameters [StorageAccountCreateParameters] The parameters to provide
# for the created account.
# @param custom_headers [Hash{String => String}] A hash of custom headers that
# will be added to the HTTP request.
#
# @return [StorageAccount] operation results.
#
def create(resource_group_name, account_name, parameters, custom_headers = nil)
response = create_async(resource_group_name, account_name, parameters, custom_headers).value!
response.body unless response.nil?
end

#
# @param resource_group_name [String] The name of the resource group within
# the user's subscription.
Expand All @@ -147,7 +165,7 @@ def check_name_availability_async(account_name, custom_headers = nil)
# @return [Concurrent::Promise] promise which provides async access to http
# response.
#
def create(resource_group_name, account_name, parameters, custom_headers = nil)
def create_async(resource_group_name, account_name, parameters, custom_headers = nil)
# Send request
promise = begin_create_async(resource_group_name, account_name, parameters, custom_headers)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ module Petstore
# A service client - single point of access to the REST API.
#
class StorageManagementClient < MsRestAzure::AzureServiceClient
include Petstore::Models
include MsRest::Serialization
include MsRestAzure

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ def self.deserialize_object(object)
return if object.nil?
output_object = AsyncOperationStatus.new

fail AzureOperationError, "Invalid status was recieved during polling: #{object['status']}" unless ALL_STATUSES.include?(object['status'])
output_object.status = object['status']

output_object.error = CloudErrorData.deserialize_object(object['error'])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,18 @@ module MsRestAzure
expect(status.retry_after).to eq(10)
end

it 'should throw error during deserialization if invalid status was provided' do
it 'should not throw error during deserialization if unknown status was provided' do
response_json = {
'status' => 'NotValidStatus',
'status' => 'Provisioning',
'error' => nil,
'retryAfter' => 5
}

expect { status = AsyncOperationStatus.deserialize_object response_json }.to raise_error(AzureOperationError)
status = AsyncOperationStatus.deserialize_object response_json

expect(status.status).to eq('Provisioning')
expect(status.error).to be_nil
expect(status.retry_after).to eq(5)
end
end

Expand Down
16 changes: 11 additions & 5 deletions src/client/Ruby/ms-rest/lib/ms_rest/serialization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ def deserialize_primary_type(mapper, response_body)
when 'String', 'Boolean', 'Object', 'Stream'
result = response_body
when 'Enum'
unless response_body.nil? || response_body.empty?
unless response_body.nil?
unless enum_is_valid(mapper, response_body)
warn "Enum #{model} does not contain #{response_body.downcase}, but was received from the server."
warn "Enum does not contain #{response_body}, but was received from the server."
end
end
result = response_body
Expand Down Expand Up @@ -146,6 +146,8 @@ def deserialize_composite_type(mapper, response_body, object_name)
parent_class = get_model(mapper[:type][:class_name])
discriminator = parent_class.class_eval("@@discriminatorMap")
model_name = response_body["#{mapper[:type][:polymorphic_discriminator]}"]
# In case we do not find model from response body then use the class defined in mapper
model_name = mapper[:type][:class_name] if model_name.nil? || model_name.empty?
model_class = get_model(discriminator[model_name])
else
model_class = get_model(mapper[:type][:class_name])
Expand Down Expand Up @@ -248,7 +250,7 @@ def serialize_primary_type(mapper, object)
when 'Number', 'Double', 'String', 'Date', 'Boolean', 'Object', 'Stream'
payload = object != nil ? object : nil
when 'Enum'
unless object.nil? || object.empty?
unless object.nil?
unless enum_is_valid(mapper, object)
fail ValidationError, "Enum #{mapper[:type][:module]} does not contain #{object.to_s}, but trying to send it to the server."
end
Expand Down Expand Up @@ -392,8 +394,12 @@ def get_model(model_name)
# @param enum_value [String] Enum value to validate
#
def enum_is_valid(mapper, enum_value)
model = get_model(mapper[:type][:module])
model.constants.any? { |e| model.const_get(e).to_s.downcase == enum_value.downcase }
if enum_value.is_a?(String) && !enum_value.empty?
model = get_model(mapper[:type][:module])
model.constants.any? { |e| model.const_get(e).to_s.downcase == enum_value.downcase }
else
false
end
end

# Splits serialized_name with '.' to compute levels of object hierarchy
Expand Down
Loading

0 comments on commit b54f66a

Please sign in to comment.