Skip to content

Commit

Permalink
Update ParameterGrouping to support multiple groups
Browse files Browse the repository at this point in the history
 - Multiple named groups are supported.  The default (if no name is specified)
   is MethodGroup.Name + Method.Name + "parameters"
  • Loading branch information
matthchr committed Oct 9, 2015
1 parent 105ea6a commit af20e76
Show file tree
Hide file tree
Showing 16 changed files with 750 additions and 119 deletions.
47 changes: 24 additions & 23 deletions AutoRest/Generators/Azure.Common/Azure.Common/AzureCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ public abstract class AzureCodeGenerator : CodeGenerator
public const string ParameterGroupExtension = "x-ms-parameter-grouping";
public const string ApiVersion = "api-version";
public const string AcceptLanguage = "accept-language";

public const string ParameterGroupName = "parameters";


private const string ResourceType = "Resource";
private const string SubResourceType = "SubResource";
private const string ResourceProperties = "Properties";
Expand Down Expand Up @@ -222,45 +220,48 @@ public static void AddParameterGroups(ServiceClient serviceClient)
foreach (Method method in serviceClient.Methods)
{
//TODO: Does this group name need to be normalized later, or does that already happen?
string parameterGroupName = method.Group + "-" + method.Name + "-" + "Parameters";
List<Property> properties = new List<Property>();
Dictionary<string, List<Property>> properties = new Dictionary<string, List<Property>>();
foreach (Parameter parameter in method.Parameters)
{
if (parameter.Extensions.ContainsKey(ParameterGroupExtension))
{
Newtonsoft.Json.Linq.JContainer extensionObject = parameter.Extensions[ParameterGroupExtension] as Newtonsoft.Json.Linq.JContainer;
if (extensionObject != null)
{
bool isParameterGrouped = (bool)extensionObject["value"];
if (isParameterGrouped)
string parameterGroupName = method.Group + "-" + method.Name + "-" + "Parameters";
parameterGroupName = extensionObject.Value<string>("name") ?? parameterGroupName;

parameter.ParameterGroup = parameterGroupName;

if (!properties.ContainsKey(parameterGroupName))
{
parameter.ParameterGroup = parameterGroupName;

properties.Add(new Property()
{
IsReadOnly = false, //Since these properties are used as parameters they are never read only
Name = CodeNamer.PascalCase(parameter.Name),
IsRequired = parameter.IsRequired,
DefaultValue = parameter.DefaultValue,
//Constraints = parameter.Constraints, TODO do these need to be copied?
Documentation = parameter.Documentation,
Type = parameter.Type,
SerializedName = null
});
properties.Add(parameterGroupName, new List<Property>());
}

properties[parameterGroupName].Add(new Property()
{
IsReadOnly = false, //Since these properties are used as parameters they are never read only
Name = CodeNamer.PascalCase(parameter.Name),
IsRequired = parameter.IsRequired,
DefaultValue = parameter.DefaultValue,
//Constraints = parameter.Constraints, TODO do these need to be copied?
Documentation = parameter.Documentation,
Type = parameter.Type,
SerializedName = null //Parameter is never serialized directly
});
}
}
}

if (method.ParameterGroups.Any())
foreach (string parameterGroupName in method.ParameterGroups)
{
CompositeType parameterGroupType = new CompositeType()
{
Name = parameterGroupName,
Documentation = "Additional parameters for the " + method.Name + " operation."
};

foreach (Property property in properties)
foreach (Property property in properties[parameterGroupName])
{
parameterGroupType.Properties.Add(property);
}
Expand All @@ -269,7 +270,7 @@ public static void AddParameterGroups(ServiceClient serviceClient)

Parameter groupedParameter = new Parameter()
{
Name = ParameterGroupName,
Name = parameterGroupName,
IsRequired = isGroupParameterRequired,
Location = ParameterLocation.None,
SerializedName = string.Empty,
Expand Down
13 changes: 13 additions & 0 deletions AutoRest/Generators/CSharp/Azure.CSharp.Tests/AcceptanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,7 @@ public void CustomNamedRequestIdTest()
}
}

[Fact]
public void ParameterGroupingTests()
{
const int body = 1234;
Expand Down Expand Up @@ -730,6 +731,18 @@ public void ParameterGroupingTests()

//null optional paramters
client.ParameterGrouping.PostOptional(null);

//Multiple grouped parameters
FirstParameterGroup firstGroup = new FirstParameterGroup(header, query);
SecondParameterGroup secondGroup = new SecondParameterGroup("header2", 42);

client.ParameterGrouping.PostMultipleParameterGroups(firstGroup, secondGroup);

//Multiple grouped parameters -- some optional parameters omitted
firstGroup = new FirstParameterGroup(header);
secondGroup = new SecondParameterGroup(queryTwo: 42);

client.ParameterGrouping.PostMultipleParameterGroups(firstGroup, secondGroup);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public partial interface IParameterGroupingOperations
/// <summary>
/// Post a bunch of required parameters grouped
/// </summary>
/// <param name='parameters'>
/// <param name='parameterGroupingPostRequiredParameters'>
/// Additional parameters for the operation
/// </param>
/// <param name='customHeaders'>
Expand All @@ -34,11 +34,11 @@ public partial interface IParameterGroupingOperations
/// <param name='cancellationToken'>
/// The cancellation token.
/// </param>
Task<AzureOperationResponse> PostRequiredWithHttpMessagesAsync(ParameterGroupingPostRequiredParameters parameters, Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));
Task<AzureOperationResponse> PostRequiredWithHttpMessagesAsync(ParameterGroupingPostRequiredParameters parameterGroupingPostRequiredParameters, Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));
/// <summary>
/// Post a bunch of optional parameters grouped
/// </summary>
/// <param name='parameters'>
/// <param name='parameterGroupingPostOptionalParameters'>
/// Additional parameters for the operation
/// </param>
/// <param name='customHeaders'>
Expand All @@ -47,6 +47,22 @@ public partial interface IParameterGroupingOperations
/// <param name='cancellationToken'>
/// The cancellation token.
/// </param>
Task<AzureOperationResponse> PostOptionalWithHttpMessagesAsync(ParameterGroupingPostOptionalParameters parameters = default(ParameterGroupingPostOptionalParameters), Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));
Task<AzureOperationResponse> PostOptionalWithHttpMessagesAsync(ParameterGroupingPostOptionalParameters parameterGroupingPostOptionalParameters = default(ParameterGroupingPostOptionalParameters), Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));
/// <summary>
/// Post parameters from multiple different parameter groups
/// </summary>
/// <param name='firstParameterGroup'>
/// Additional parameters for the operation
/// </param>
/// <param name='secondParameterGroup'>
/// Additional parameters for the operation
/// </param>
/// <param name='customHeaders'>
/// The headers that will be added to request.
/// </param>
/// <param name='cancellationToken'>
/// The cancellation token.
/// </param>
Task<AzureOperationResponse> PostMultipleParameterGroupsWithHttpMessagesAsync(FirstParameterGroup firstParameterGroup = default(FirstParameterGroup), SecondParameterGroup secondParameterGroup = default(SecondParameterGroup), Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));
}
}
Loading

0 comments on commit af20e76

Please sign in to comment.