public ServiceClientTemplateModel(ServiceClient serviceClient)
 {
     this.LoadFrom(serviceClient);
     MethodTemplateModels = new List<MethodTemplateModel>();
     Methods.Where(m => m.Group == null)
         .ForEach(m => MethodTemplateModels.Add(new MethodTemplateModel(m, serviceClient)));
 }
Beispiel #2
0
        public static void ProcessParameterizedHost(ServiceClient serviceClient, Settings settings)
        {
            if (serviceClient == null)
            {
                throw new ArgumentNullException("serviceClient");
            }

            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            if (serviceClient.Extensions.ContainsKey(ParameterizedHostExtension) && !hostChecked)
            { 
                SwaggerModeler modeler = new SwaggerModeler(settings);
                modeler.Build();
                var hostExtension = serviceClient.Extensions[ParameterizedHostExtension] as JObject;

                if (hostExtension != null)
                {
                    var hostTemplate = (string)hostExtension["hostTemplate"];
                    var parametersJson = hostExtension["parameters"].ToString();
                    if (!string.IsNullOrEmpty(parametersJson))
                    {
                        var jsonSettings = new JsonSerializerSettings
                        {
                            TypeNameHandling = TypeNameHandling.None,
                            MetadataPropertyHandling = MetadataPropertyHandling.Ignore
                        };

                        var swaggerParams = JsonConvert.DeserializeObject<List<SwaggerParameter>>(parametersJson, jsonSettings);
                        
                        foreach (var swaggerParameter in swaggerParams)
                        {
                            // Build parameter
                            var parameterBuilder = new ParameterBuilder(swaggerParameter, modeler);
                            var parameter = parameterBuilder.Build();

                            // check to see if the parameter exists in properties, and needs to have its name normalized
                            if (serviceClient.Properties.Any(p => p.SerializedName.Equals(parameter.SerializedName)))
                            {
                                parameter.ClientProperty = serviceClient.Properties.Single(p => p.SerializedName.Equals(parameter.SerializedName));
                            }
                            parameter.Extensions["hostParameter"] = true;

                            foreach (var method in serviceClient.Methods)
                            {
                                method.Parameters.Add(parameter);
                            }
                        }

                        serviceClient.BaseUrl = string.Format(CultureInfo.InvariantCulture, "{0}://{1}{2}",
                        modeler.ServiceDefinition.Schemes[0].ToString().ToLowerInvariant(),
                        hostTemplate, modeler.ServiceDefinition.BasePath);
                    }
                }
            }

            hostChecked = true;
        }
 public void ParseWithEmptyServiceClient()
 {
     ServiceClient serviceClient = new ServiceClient();
     IDictionary<string, ResourceSchema> schemas = ResourceSchemaParser.Parse(serviceClient);
     Assert.NotNull(schemas);
     Assert.Equal(0, schemas.Count);
 }
 public override async Task Generate(ServiceClient serviceClient)
 {
     var viewModel = new SampleViewModel();
     var model = new SampleModel();
     model.Model = viewModel;
     await Write(model, Path.Combine("Models", "Pet.cs"));
 }
 public override async Task Generate(ServiceClient serviceClient)
 {
     var viewModel = new SampleViewModel();
     var model = new SampleModel();
     model.Model = viewModel;
     await Write(model, "Models\\Pet.cs");
 }
 /// <summary>
 /// Normalizes client model by updating names and types to be language specific.
 /// </summary>
 /// <param name="serviceClient"></param>
 public override void NormalizeClientModel(ServiceClient serviceClient)
 {
     PopulateAdditionalProperties(serviceClient);
     _namer.NormalizeClientModel(serviceClient);
     _namer.ResolveNameCollisions(serviceClient, Settings.Namespace,
         Settings.Namespace + ".Models");
 }
 /// <summary>
 /// Changes paginated method signatures to return Page type.
 /// </summary>
 /// <param name="serviceClient"></param>
 public virtual void NormalizePaginatedMethods(ServiceClient serviceClient)
 {
     if (serviceClient == null)
     {
         throw new ArgumentNullException("serviceClient");
     }
 }
 /// <summary>
 /// Initializes a new instance of the AzureServiceClientTemplateModel class.
 /// </summary>
 /// <param name="serviceClient">The service client instance.</param>
 public AzureServiceClientTemplateModel(ServiceClient serviceClient)
     : base(serviceClient)
 {
     MethodTemplateModels.Clear();
     Methods.Where(m => m.Group == null)
         .ForEach(m => MethodTemplateModels.Add(new AzureMethodTemplateModel(m, serviceClient)));
 }
 /// <summary>
 /// Initializes a new instance of the class MethodTemplateModel.
 /// </summary>
 /// <param name="source">The source object.</param>
 /// <param name="serviceClient">The service client.</param>
 public MethodTemplateModel(Method source, ServiceClient serviceClient)
 {
     this.LoadFrom(source);
     ParameterTemplateModels = new List<ParameterTemplateModel>();
     source.Parameters.ForEach(p => ParameterTemplateModels.Add(new ParameterTemplateModel(p)));
     ServiceClient = serviceClient;
 }
Beispiel #10
0
        /// <summary>
        /// Normalizes client model using Azure-specific extensions.
        /// </summary>
        /// <param name="serviceClient">Service client</param>
        /// <param name="settings">AutoRest settings</param>
        /// <param name="codeNamer">AutoRest settings</param>
        /// <returns></returns>
        public static void NormalizeAzureClientModel(ServiceClient serviceClient, Settings settings, CodeNamer codeNamer)
        {
            if (serviceClient == null)
            {
                throw new ArgumentNullException("serviceClient");
            }
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }
            if (codeNamer == null)
            {
                throw new ArgumentNullException("codeNamer");
            }

            settings.AddCredentials = true;
            UpdateHeadMethods(serviceClient);
            ParseODataExtension(serviceClient);
            FlattenResourceProperties(serviceClient);
            FlattenRequestPayload(serviceClient, settings);
            AddLongRunningOperations(serviceClient);
            AddAzureProperties(serviceClient);
            SetDefaultResponses(serviceClient);
            AddParameterGroups(serviceClient);
            AddPageableMethod(serviceClient, codeNamer);
        }
Beispiel #11
0
 /// <summary>
 /// Normalizes client model using generic extensions.
 /// </summary>
 /// <param name="serviceClient">Service client</param>
 /// <param name="settings">AutoRest settings</param>
 /// <returns></returns>
 public static void NormalizeClientModel(ServiceClient serviceClient, Settings settings)
 {
     FlattenModels(serviceClient);
     FlattenMethodParameters(serviceClient, settings);
     ParameterGroupExtensionHelper.AddParameterGroups(serviceClient);
     ProcessParameterizedHost(serviceClient, settings);
 }
 /// <summary>
 /// Normalizes client model by updating names and types to be language specific.
 /// </summary>
 /// <param name="serviceClient"></param>
 public override void NormalizeClientModel(ServiceClient serviceClient)
 {
     base.NormalizeClientModel(serviceClient);
     _namer.NormalizeClientModel(serviceClient);
     _namer.ResolveNameCollisions(serviceClient, Settings.Namespace,
         Settings.Namespace + ".Models");
 }
        public override void NormalizeClientModel(ServiceClient client)
        {
            if (client == null)
            {
                throw new ArgumentNullException("client");
            }

            base.NormalizeClientModel(client);
            foreach (var method in client.Methods)
            {
                var scope = new ScopeProvider();
                foreach (var parameter in method.Parameters)
                {
                    if (parameter.ClientProperty != null)
                    {
                        parameter.Name = string.Format(CultureInfo.InvariantCulture,
                            "{0}.{1}", 
                            method.Group == null ? "this" : "this.Client", 
                            parameter.ClientProperty.Name);
                    }
                    else
                    {
                        parameter.Name = scope.GetVariableName(parameter.Name);   
                    }
                }
            }
        }
Beispiel #14
0
 /// <summary>
 /// Normalizes client model by updating names and types to be language specific.
 /// </summary>
 /// <param name="serviceClientModel"></param>
 public override void NormalizeClientModel(ServiceClient serviceClientModel)
 {
     PopulateAdditionalProperties(serviceClientModel);
     namingFramework.NormalizeClientModel(serviceClientModel);
     namingFramework.ResolveNameCollisions(serviceClientModel, Settings.Namespace,
         Settings.Namespace + "::Models");
 }
        private static async Task TestGenerate(string apiVersion, string[] methodUrls, string expectedJsonString)
        {
            MemoryFileSystem fileSystem = new MemoryFileSystem();

            Settings settings = new Settings();
            settings.FileSystem = fileSystem;

            ServiceClient serviceClient = new ServiceClient();
            serviceClient.ApiVersion = apiVersion;
            foreach(string methodUrl in methodUrls)
            {
                serviceClient.Methods.Add(new Method()
                {
                    Url = methodUrl,
                    HttpMethod = HttpMethod.Put,
                });
            }
            await CreateGenerator(settings).Generate(serviceClient);

            Assert.Equal(2, fileSystem.VirtualStore.Count);

            string folderPath = fileSystem.VirtualStore.Keys.First();
            Assert.Equal("Folder", fileSystem.VirtualStore[folderPath].ToString());

            JObject expectedJSON = JObject.Parse(expectedJsonString);

            string fileContents = fileSystem.VirtualStore[fileSystem.VirtualStore.Keys.Skip(1).First()].ToString();
            JObject actualJson = JObject.Parse(fileContents);

            Assert.Equal(expectedJSON, actualJson);
        }
        public void NormalizeClientModelDoesNothing()
        {
            ServiceClient serviceClient = new ServiceClient();
            CreateGenerator().NormalizeClientModel(serviceClient);

            // Nothing happens
        }
 public AzureMethodGroupTemplateModel(ServiceClient serviceClient, string methodGroupName)
     : base(serviceClient, methodGroupName)
 {
     MethodTemplateModels.Clear();
     Methods.Where(m => m.Group == MethodGroupName)
         .ForEach(m => MethodTemplateModels.Add(new AzureMethodTemplateModel(m, serviceClient)));
 }
Beispiel #18
0
        /// <summary>
        /// Normalizes client model using Azure-specific extensions.
        /// </summary>
        /// <param name="serviceClient">Service client</param>
        /// <param name="settings">AutoRest settings</param>
        /// <param name="codeNamer">AutoRest settings</param>
        /// <returns></returns>
        public static void NormalizeAzureClientModel(ServiceClient serviceClient, Settings settings, CodeNamer codeNamer)
        {
            if (serviceClient == null)
            {
                throw new ArgumentNullException("serviceClient");
            }
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }
            if (codeNamer == null)
            {
                throw new ArgumentNullException("codeNamer");
            }

            settings.AddCredentials = true;

            // This extension from general extensions must be run prior to Azure specific extensions.
            ProcessParameterizedHost(serviceClient, settings);

            UpdateHeadMethods(serviceClient);
            ParseODataExtension(serviceClient);
            FlattenModels(serviceClient);
            FlattenMethodParameters(serviceClient, settings);
            AddParameterGroups(serviceClient);
            AddLongRunningOperations(serviceClient);
            AddAzureProperties(serviceClient);
            SetDefaultResponses(serviceClient);
            AddPageableMethod(serviceClient, codeNamer);
        }
Beispiel #19
0
 public MethodTemplateModel(Method source, ServiceClient serviceClient)
 {
     this.LoadFrom(source);
     ParameterTemplateModels = new List<ParameterTemplateModel>();
     source.Parameters.ForEach(p => ParameterTemplateModels.Add(new ParameterTemplateModel(p)));
     ServiceClient = serviceClient;
     if (source.Group != null)
     {
         OperationName = source.Group.ToPascalCase();
     }
     else
     {
         OperationName = serviceClient.Name;
     }
     AddCustomHeader = true;
     string formatter;
     foreach (var parameter in LocalParameters)
     {
         if (string.IsNullOrWhiteSpace(parameter.DefaultValue))
         {
             parameter.DefaultValue = PythonConstants.None;
         }
     }
     foreach (Match m in Regex.Matches(Url, @"\{[\w]+:[\w]+\}"))
     {
         formatter = m.Value.Split(':').First() + '}';
         Url = Url.Replace(m.Value, formatter);
     }
 }
        /// <summary>
        /// Recursively normalizes names in the client model
        /// </summary>
        /// <param name="client"></param>
        public virtual void NormalizeClientModel(ServiceClient client)
        {
            if (client == null)
            {
                throw new ArgumentNullException("client");
            }

            client.Name = GetTypeName(client.Name);
            client.Namespace = GetNamespaceName(client.Namespace);
            foreach (var property in client.Properties)
            {
                property.Name = GetPropertyName(property.Name);
                property.Type = NormalizeType(property.Type);
            }

            var normalizedModels = new List<CompositeType>();
            foreach (var modelType in client.ModelTypes)
            {
                normalizedModels.Add(NormalizeType(modelType) as CompositeType);
            }
            client.ModelTypes.Clear();
            normalizedModels.ForEach( (item) => client.ModelTypes.Add(item));

            var normalizedEnums = new List<EnumType>();
            foreach (var enumType in client.EnumTypes)
            {
                var normalizedType = NormalizeType(enumType) as EnumType;
                if (normalizedType != null)
                {
                    normalizedEnums.Add(NormalizeType(enumType) as EnumType);
                }
            }
            client.EnumTypes.Clear();
            normalizedEnums.ForEach((item) => client.EnumTypes.Add(item));
            
            foreach (var method in client.Methods)
            {
                method.Name = GetMethodName(method.Name);
                method.Group = GetTypeName(method.Group);
                method.ReturnType = NormalizeType(method.ReturnType);
                method.DefaultResponse = NormalizeType(method.DefaultResponse);
                var normalizedResponses = new Dictionary<HttpStatusCode, IType>();
                foreach (var statusCode in method.Responses.Keys)
                {
                    normalizedResponses[statusCode] = NormalizeType(method.Responses[statusCode]);
                }

                method.Responses.Clear();
                foreach (var statusCode in normalizedResponses.Keys)
                {
                    method.Responses[statusCode] = normalizedResponses[statusCode];
                }
                foreach (var parameter in method.Parameters)
                {
                    parameter.Name = GetParameterName(parameter.Name);
                    parameter.Type = NormalizeType(parameter.Type);
                }
            }
        }
 public ServiceClientTemplateModel(ServiceClient serviceClient, bool internalConstructors)
 {
     this.LoadFrom(serviceClient);
     MethodTemplateModels = new List<MethodTemplateModel>();
     Methods.Where(m => m.Group == null)
         .ForEach(m => MethodTemplateModels.Add(new MethodTemplateModel(m, serviceClient)));
     ConstructorVisibility = internalConstructors ? "internal" : "public";
 }
 public ExtensionsTemplateModel(ServiceClient serviceClient, string operationName)
 {
     this.LoadFrom(serviceClient);
     MethodTemplateModels = new List<MethodTemplateModel>();
     ExtensionName = operationName ?? this.Name;
     this.Methods.Where(m => m.Group == operationName)
         .ForEach(m => MethodTemplateModels.Add(new MethodTemplateModel(m, serviceClient)));
 }
 /// <summary>
 /// Normalizes client model by updating names and types to be language specific.
 /// </summary>
 /// <param name="serviceClient"></param>
 public override void NormalizeClientModel(ServiceClient serviceClient)
 {
     AzureExtensions.NormalizeAzureClientModel(serviceClient, Settings, _namer);
     _namer.NormalizeClientModel(serviceClient);
     _namer.ResolveNameCollisions(serviceClient, Settings.Namespace,
         Settings.Namespace + ".Models");
     _namer.NormalizePaginatedMethods(serviceClient, pageClasses);
 }
 public AzureServiceClientTemplateModel(ServiceClient serviceClient, bool internalConstructors)
     : base(serviceClient, internalConstructors)
 {
     // TODO: Initialized in the base constructor. Why Clear it?
     MethodTemplateModels.Clear();
     Methods.Where(m => m.Group == null)
         .ForEach(m => MethodTemplateModels.Add(new AzureMethodTemplateModel(m, serviceClient)));
 }
        /// <summary>
        /// Generates C# code for service client.
        /// </summary>
        /// <param name="serviceClient"></param>
        /// <returns></returns>
        public override async Task Generate(ServiceClient serviceClient)
        {
            var serviceClientTemplateModel = new ServiceClientTemplateModel(serviceClient);
            // Service client
            var serviceClientTemplate = new ServiceClientTemplate
            {
                Model = serviceClientTemplateModel,
            };
            await Write(serviceClientTemplate, serviceClient.Name.ToPascalCase() + "Impl.java");

            var serviceClientInterfaceTemplate = new ServiceClientInterfaceTemplate
            {
                Model = serviceClientTemplateModel,
            };
            await Write(serviceClientInterfaceTemplate, serviceClient.Name.ToPascalCase() + ".java");

            //Models
            if (serviceClient.ModelTypes.Any())
            {
                foreach (var modelType in serviceClientTemplateModel.ModelTemplateModels)
                {
                    var modelTemplate = new ModelTemplate
                    {
                        Model = modelType
                    };
                    await Write(modelTemplate, Path.Combine("models", modelType.Name.ToPascalCase() + ".java"));
                }
            }

            //MethodGroups
            if (serviceClientTemplateModel.MethodGroupModels.Any())
            {
                foreach (var methodGroupModel in serviceClientTemplateModel.MethodGroupModels)
                {
                    var methodGroupTemplate = new MethodGroupTemplate
                    {
                        Model = methodGroupModel
                    };
                    await Write(methodGroupTemplate, methodGroupModel.MethodGroupType.ToPascalCase() + "Impl.java");
                    var methodGroupInterfaceTemplate = new MethodGroupInterfaceTemplate
                    {
                        Model = methodGroupModel
                    };
                    await Write(methodGroupInterfaceTemplate, methodGroupModel.MethodGroupType.ToPascalCase() + ".java");
                }
            }

            //Enums
            foreach (var enumType in serviceClient.EnumTypes)
            {
                var enumTemplate = new EnumTemplate
                {
                    Model = new EnumTemplateModel(enumType),
                };
                await Write(enumTemplate, Path.Combine("models", enumTemplate.Model.Name.ToPascalCase() + ".java"));
            }
        }
 public AzureServiceClientTemplateModel(ServiceClient serviceClient)
     : base(serviceClient)
 {
     MethodTemplateModels.Clear();
     Methods.Where(m => m.Group == null)
         .ForEach(m => MethodTemplateModels.Add(new AzureMethodTemplateModel(m, serviceClient)));
     ModelTemplateModels.RemoveAll(m => m.Extensions.ContainsKey(AzureCodeGenerator.ExternalExtension));
     ModelTemplateModels.RemoveAll(m => m.Extensions.ContainsKey(AzureCodeGenerator.AzureResourceExtension));
 }
 /// <summary>
 /// Normalizes client model by updating names and types to be language specific.
 /// </summary>
 /// <param name="serviceClient"></param>
 public override void NormalizeClientModel(ServiceClient serviceClient)
 {
     Extensions.NormalizeClientModel(serviceClient, Settings);
     PopulateAdditionalProperties(serviceClient);
     Namer.NormalizeClientModel(serviceClient);
     Namer.ResolveNameCollisions(serviceClient, Settings.Namespace,
         Settings.Namespace + ".Models");
     Namer.NormalizeOdataFilterParameter(serviceClient);
 }
 public ServiceClientTemplateModel(ServiceClient serviceClient, bool internalConstructors)
 {
     this.LoadFrom(serviceClient);
     MethodTemplateModels = new List<MethodTemplateModel>();
     Methods.Where(m => m.Group == null)
         .ForEach(m => MethodTemplateModels.Add(new MethodTemplateModel(m, serviceClient)));
     ConstructorVisibility = internalConstructors ? "internal" : "public";
     this.IsCustomBaseUri = serviceClient.Extensions.ContainsKey(Microsoft.Rest.Generator.Extensions.ParameterizedHostExtension);
 }
 /// <summary>
 /// Initializes a new instance 
 /// </summary>
 /// <param name="serviceClient">The service client instance.</param>
 /// <param name="methodGroupName">The name of the method group.</param>
 public AzureMethodGroupTemplateModel(ServiceClient serviceClient, string methodGroupName)
     : base(serviceClient, methodGroupName)
 {
     // Clear base initialized MethodTemplateModels and re-populate with
     // AzureMethodTemplateModel
     MethodTemplateModels.Clear();
     Methods.Where(m => m.Group == methodGroupName)
         .ForEach(m => MethodTemplateModels.Add(new AzureMethodTemplateModel(m, serviceClient)));
 }
 /// <summary>
 /// Initializes a new instance of ServiceClientTemplateModel class.
 /// </summary>
 /// <param name="serviceClient"></param>
 public ServiceClientTemplateModel(ServiceClient serviceClient)
 {
     this.LoadFrom(serviceClient);
     HasModelTypes = serviceClient.HasModelTypes();
     MethodTemplateModels = new List<MethodTemplateModel>();
     Methods.Where(m => m.Group == null)
         .ForEach(m => MethodTemplateModels.Add(new MethodTemplateModel(m, serviceClient)));
     this.IsCustomBaseUri = serviceClient.Extensions.ContainsKey(Microsoft.Rest.Generator.Extensions.ParameterizedHostExtension);
 }