Skip to content

Commit

Permalink
x-ms-parameter-grouping for Ruby & Azure.Ruby Generator
Browse files Browse the repository at this point in the history
  • Loading branch information
vishrutshah committed Jun 22, 2016
1 parent 8c6ad3e commit 04a9525
Show file tree
Hide file tree
Showing 8 changed files with 257 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@
end

it 'should get multiple pages with offset' do
result = @client.paging.get_multiple_pages_with_offset_async(100).value!
options = PagingModule::Models::PagingGetMultiplePagesWithOffsetOptions.new
options.offset = 100
result = @client.paging.get_multiple_pages_with_offset_async(options).value!
expect(result.response.status).to eq(200)
expect(result.body.next_link).not_to be_nil

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# encoding: utf-8

$: << 'RspecTests/Generated/parameter_grouping'

require 'rspec'
require 'azure_parameter_grouping'

include ParameterGroupingModule
include ParameterGroupingModule::Models

describe 'ParameterGrouping' do
before(:all) do
@base_url = ENV['StubServerURI']
dummyToken = 'dummy123@query343423'
@credentials = MsRest::TokenCredentials.new(dummyToken)
@client = AutoRestParameterGroupingTestService.new(@credentials, @base_url)

@body = 1234
@header = 'header'
@query = 21
@path = 'path'
end

it 'should accept valid required parameters' do
required_parameters = ParameterGroupingPostRequiredParameters.new
required_parameters.body = @body
required_parameters.custom_header = @header
required_parameters.query = @query
required_parameters.path = @path

result = @client.parameter_grouping.post_required_async(required_parameters).value!

expect(result.response.status).to eq(200)
end

it 'should accept required parameters but null optional parameters' do
required_parameters = ParameterGroupingPostRequiredParameters.new
required_parameters.body = @body
required_parameters.path = @path

result = @client.parameter_grouping.post_required_async(required_parameters).value!

expect(result.response.status).to eq(200)
end

it 'should reject required parameters with missing required property' do
required_parameters = ParameterGroupingPostRequiredParameters.new
required_parameters.path = @path

expect { @client.parameter_grouping.post_required_async(required_parameters).value! }.to raise_error(MsRest::ValidationError)
end

it 'should reject null required parameters' do
required_parameters = ParameterGroupingPostRequiredParameters.new
required_parameters.path = nil

expect { @client.parameter_grouping.post_required_async(required_parameters).value! }.to raise_error(MsRest::ValidationError)
end

it 'should accept valid optional parameters' do
optional_parameters = ParameterGroupingPostOptionalParameters.new
optional_parameters.custom_header = @header
optional_parameters.query = @query

result = @client.parameter_grouping.post_optional_async(optional_parameters).value!
expect(result.response.status).to eq(200)
end

it 'should accept null optional parameters' do
result = @client.parameter_grouping.post_optional_async(nil).value!
expect(result.response.status).to eq(200)
end

it 'should allow multiple parameter groups' do
first_parameter_group = FirstParameterGroup.new
second_parameter_group = ParameterGroupingPostMultiParamGroupsSecondParamGroup.new

first_parameter_group.header_one = @header
first_parameter_group.query_one = @query

second_parameter_group.header_two = 'header2'
second_parameter_group.query_two = 42

result = @client.parameter_grouping.post_multi_param_groups_async(first_parameter_group, second_parameter_group).value!
expect(result.response.status).to eq(200)
end

it 'should allow multiple parameter groups with some defaults omitted' do
first_parameter_group = FirstParameterGroup.new
second_parameter_group = ParameterGroupingPostMultiParamGroupsSecondParamGroup.new

first_parameter_group.header_one = @header

second_parameter_group.query_two = 42

result = @client.parameter_grouping.post_multi_param_groups_async(first_parameter_group, second_parameter_group).value!
expect(result.response.status).to eq(200)
end

# This test has nothing to do with sharing of the FirstParameterGroup. It's included for test coverage
it 'should allow parameter group objects to be shared between operations' do
first_parameter_group = FirstParameterGroup.new

first_parameter_group.header_one = @header
first_parameter_group.query_one = 42

result = @client.parameter_grouping.post_shared_parameter_group_object_async(first_parameter_group).value!
expect(result.response.status).to eq(200)
end
end
5 changes: 0 additions & 5 deletions AutoRest/Generators/Ruby/Ruby/ClientModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -409,11 +409,6 @@ private static string AddMetaData(this IType type, string serializedName, IParam
throw new ArgumentNullException(nameof(type));
}

if (serializedName == null)
{
throw new ArgumentNullException(nameof(serializedName));
}

IndentedStringBuilder builder = new IndentedStringBuilder(" ");

Dictionary<Constraint, string> constraints = null;
Expand Down
1 change: 1 addition & 0 deletions AutoRest/Generators/Ruby/Ruby/RubyCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public override string ImplementationFileExtension
/// <param name="serviceClient"></param>
public override void NormalizeClientModel(ServiceClient serviceClient)
{
ParameterGroupExtensionHelper.AddParameterGroups(serviceClient);
Extensions.ProcessParameterizedHost(serviceClient, Settings);
PopulateAdditionalProperties(serviceClient);
CodeNamer.NormalizeClientModel(serviceClient);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public MethodTemplateModel(Method source, ServiceClient serviceClient)
this.LoadFrom(source);
ParameterTemplateModels = new List<ParameterTemplateModel>();
source.Parameters.ForEach(p => ParameterTemplateModels.Add(new ParameterTemplateModel(p)));

LogicalParameterTemplateModels = new List<ParameterTemplateModel>();
source.LogicalParameters.ForEach(p => LogicalParameterTemplateModels.Add(new ParameterTemplateModel(p)));

ServiceClient = serviceClient;
}

Expand Down Expand Up @@ -148,7 +152,7 @@ public virtual IEnumerable<ParameterTemplateModel> SkipEncodingPathParams
/// </summary>
public virtual IEnumerable<ParameterTemplateModel> AllPathParams
{
get { return ParameterTemplateModels.Where(p => p.Location == ParameterLocation.Path); }
get { return LogicalParameterTemplateModels.Where(p => p.Location == ParameterLocation.Path); }
}

/// <summary>
Expand All @@ -172,7 +176,10 @@ public virtual IEnumerable<ParameterTemplateModel> EncodingQueryParams
/// </summary>
public virtual IEnumerable<ParameterTemplateModel> AllQueryParams
{
get { return ParameterTemplateModels.Where(p => p.Location == ParameterLocation.Query); }
get
{
return LogicalParameterTemplateModels.Where(p => p.Location == ParameterLocation.Query);
}
}

/// <summary>
Expand Down Expand Up @@ -211,6 +218,11 @@ public virtual string SetDefaultHeaders
/// </summary>
public List<ParameterTemplateModel> ParameterTemplateModels { get; private set; }

/// <summary>
/// Gets the list of logical method paramater templates.
/// </summary>
private List<ParameterTemplateModel> LogicalParameterTemplateModels { get; set; }

/// <summary>
/// Gets the list of parameter which need to be included into HTTP header.
/// </summary>
Expand Down Expand Up @@ -326,7 +338,7 @@ public IEnumerable<ParameterTemplateModel> LocalParameters
/// </summary>
public ParameterTemplateModel RequestBody
{
get { return ParameterTemplateModels.FirstOrDefault(p => p.Location == ParameterLocation.Body); }
get { return LogicalParameterTemplateModels.FirstOrDefault(p => p.Location == ParameterLocation.Body); }
}

/// <summary>
Expand Down Expand Up @@ -435,6 +447,58 @@ public virtual string BuildUrl(string variableName)
return builder.ToString();
}

/// <summary>
/// Build parameter mapping from parameter grouping transformation.
/// </summary>
/// <returns></returns>
public virtual string BuildInputParameterMappings()
{
var builder = new IndentedStringBuilder(" ");
if (InputParameterTransformation.Count > 0)
{
builder.Indent();
foreach (var transformation in InputParameterTransformation)
{
if (transformation.OutputParameter.Type is CompositeType &&
transformation.OutputParameter.IsRequired)
{
builder.AppendLine("{0} = {1}.new",
transformation.OutputParameter.Name,
transformation.OutputParameter.Type.Name);
}
else
{
builder.AppendLine("{0} = nil", transformation.OutputParameter.Name);
}
}
foreach (var transformation in InputParameterTransformation)
{
builder.AppendLine("unless {0}", BuildNullCheckExpression(transformation))
.AppendLine().Indent();
var outputParameter = transformation.OutputParameter;
if (transformation.ParameterMappings.Any(m => !string.IsNullOrEmpty(m.OutputParameterProperty)) &&
transformation.OutputParameter.Type is CompositeType)
{
//required outputParameter is initialized at the time of declaration
if (!transformation.OutputParameter.IsRequired)
{
builder.AppendLine("{0} = {1}.new",
transformation.OutputParameter.Name,
transformation.OutputParameter.Type.Name);
}
}

foreach (var mapping in transformation.ParameterMappings)
{
builder.AppendLine("{0}{1}", transformation.OutputParameter.Name, mapping);
}

builder.Outdent().AppendLine("end");
}
}
return builder.ToString();
}

/// <summary>
/// Gets the formatted status code.
/// </summary>
Expand Down Expand Up @@ -556,5 +620,30 @@ public string GetDeserializationString(IType type, string valueReference = "resu

return builder.ToString();
}

/// <summary>
/// Builds null check expression for the given <paramref name="transformation"/>.
/// </summary>
/// <param name="transformation">ParameterTransformation for which to build null check expression.</param>
/// <returns></returns>
private static string BuildNullCheckExpression(ParameterTransformation transformation)
{
if (transformation == null)
{
throw new ArgumentNullException("transformation");
}
if (transformation.ParameterMappings.Count == 1)
{
return string.Format(CultureInfo.InvariantCulture,
"{0}.nil?",transformation.ParameterMappings[0].InputParameter.Name);
}
else
{
return string.Join(" && ",
transformation.ParameterMappings.Select(m =>
string.Format(CultureInfo.InvariantCulture,
"{0}.nil?", m.InputParameter.Name)));
}
}
}
}
7 changes: 5 additions & 2 deletions AutoRest/Generators/Ruby/Ruby/Templates/MethodTemplate.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,16 @@ def @(Model.Name)_async(@(Model.MethodParameterDeclaration))
@:@(parameter.Name) = @(parameter.DefaultValue)
}
}
@EmptyLine
@(Model.BuildInputParameterMappings())
@EmptyLine
request_headers = {}
@if (Model.Parameters.Any(p => p.Location == ParameterLocation.Header))
@if (Model.LogicalParameters.Any(p => p.Location == ParameterLocation.Header))
{
@EmptyLine
@:# Set Headers
@:@(Model.SetDefaultHeaders)
foreach (var parameter in Model.Parameters.Where(p => p.Location == ParameterLocation.Header))
foreach (var parameter in Model.LogicalParameters.Where(p => p.Location == ParameterLocation.Header))
{
if (parameter.SerializedName.ToLower() == "Content-Type".ToLower())
{
Expand Down

0 comments on commit 04a9525

Please sign in to comment.