private static Parameter CreateParameterFromGrouping(IEnumerable <ParameterTransformation> grouping, Method method, ServiceClient serviceClient) { var properties = new List <Property>(); string parameterGroupName = null; foreach (var parameter in grouping.Select(g => g.OutputParameter)) { Newtonsoft.Json.Linq.JContainer extensionObject = parameter.Extensions[ParameterGroupExtension] as Newtonsoft.Json.Linq.JContainer; string specifiedGroupName = extensionObject.Value <string>("name"); if (specifiedGroupName == null) { string postfix = extensionObject.Value <string>("postfix") ?? "Parameters"; parameterGroupName = method.Group + "-" + method.Name + "-" + postfix; } else { parameterGroupName = specifiedGroupName; } Property groupProperty = new Property() { IsReadOnly = false, //Since these properties are used as parameters they are never read only Name = parameter.Name, IsRequired = parameter.IsRequired, DefaultValue = parameter.DefaultValue, //Constraints = parameter.Constraints, Omit these since we don't want to perform parameter validation Documentation = parameter.Documentation, Type = parameter.Type, SerializedName = null //Parameter is never serialized directly }; properties.Add(groupProperty); } var parameterGroupType = new CompositeType() { Name = parameterGroupName, Documentation = "Additional parameters for the " + method.Name + " operation." }; //Add to the service client serviceClient.ModelTypes.Add(parameterGroupType); foreach (Property property in properties) { parameterGroupType.Properties.Add(property); } bool isGroupParameterRequired = parameterGroupType.Properties.Any(p => p.IsRequired); //Create the new parameter object based on the parameter group type return(new Parameter() { Name = parameterGroupName, IsRequired = isGroupParameterRequired, Location = ParameterLocation.None, SerializedName = string.Empty, Type = parameterGroupType, Documentation = "Additional parameters for the operation" }); }
/// <summary> /// Adds the parameter groups to operation parameters. /// </summary> /// <param name="serviceClient"></param> public static void AddParameterGroups(ServiceClient serviceClient) { if (serviceClient == null) { throw new ArgumentNullException("serviceClient"); } HashSet <CompositeType> generatedParameterGroups = new HashSet <CompositeType>(); foreach (Method method in serviceClient.Methods) { //This group name is normalized by each languages code generator later, so it need not happen here. Dictionary <string, Dictionary <Property, Parameter> > parameterGroups = new Dictionary <string, Dictionary <Property, Parameter> >(); 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) { string specifiedGroupName = extensionObject.Value <string>("name"); string parameterGroupName; if (specifiedGroupName == null) { string postfix = extensionObject.Value <string>("postfix") ?? "Parameters"; parameterGroupName = method.Group + "-" + method.Name + "-" + postfix; } else { parameterGroupName = specifiedGroupName; } if (!parameterGroups.ContainsKey(parameterGroupName)) { parameterGroups.Add(parameterGroupName, new Dictionary <Property, Parameter>()); } Property groupProperty = new Property() { IsReadOnly = false, //Since these properties are used as parameters they are never read only Name = parameter.Name, IsRequired = parameter.IsRequired, DefaultValue = parameter.DefaultValue, //Constraints = parameter.Constraints, Omit these since we don't want to perform parameter validation Documentation = parameter.Documentation, Type = parameter.Type, SerializedName = null //Parameter is never serialized directly }; parameterGroups[parameterGroupName].Add(groupProperty, parameter); } } } foreach (string parameterGroupName in parameterGroups.Keys) { CompositeType parameterGroupType = generatedParameterGroups.FirstOrDefault(item => item.Name == parameterGroupName); bool createdNewCompositeType = false; if (parameterGroupType == null) { parameterGroupType = new CompositeType() { Name = parameterGroupName, Documentation = "Additional parameters for the " + method.Name + " operation." }; generatedParameterGroups.Add(parameterGroupType); //Populate the parameter group type with properties. //Add to the service client serviceClient.ModelTypes.Add(parameterGroupType); createdNewCompositeType = true; } foreach (Property property in parameterGroups[parameterGroupName].Keys) { //Either the paramter group is "empty" since it is new, or it is "full" and we don't allow different schemas if (createdNewCompositeType) { parameterGroupType.Properties.Add(property); } else { Property matchingProperty = parameterGroupType.Properties.FirstOrDefault( item => item.Name == property.Name && item.IsReadOnly == property.IsReadOnly && item.DefaultValue == property.DefaultValue && item.SerializedName == property.SerializedName); if (matchingProperty == null) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "Property {0} was specified on group {1} but it is not on shared parameter group object {2}", property.Name, method.Name, parameterGroupType.Name)); } } } bool isGroupParameterRequired = parameterGroupType.Properties.Any(p => p.IsRequired); //Create the new parameter object based on the parameter group type Parameter parameterGroup = new Parameter() { Name = parameterGroupName, IsRequired = isGroupParameterRequired, Location = ClientModel.ParameterLocation.None, SerializedName = string.Empty, Type = parameterGroupType, Documentation = "Additional parameters for the operation" }; method.Parameters.Add(parameterGroup); //Link the grouped parameters to their parent, and remove them from the method parameters foreach (Property property in parameterGroups[parameterGroupName].Keys) { Parameter p = parameterGroups[parameterGroupName][property]; var parameterTransformation = new ParameterTransformation { OutputParameter = p }; parameterTransformation.ParameterMappings.Add(new ParameterMapping { InputParameter = parameterGroup, InputParameterProperty = property.Name }); method.InputParameterTransformation.Add(parameterTransformation); method.Parameters.Remove(p); } } } }