예제 #1
0
        public static (bool isSingleClient, string typeName) GenerateCSharp(SwaggerDocument document, ApiClientDefinition definition, bool promptOnFileOverwrite)
        {
            var className = Path.GetFileNameWithoutExtension(definition.CSharpClient);

            var settings = new SwaggerToCSharpClientGeneratorSettings()
            {
                GenerateSyncMethods        = true,
                OperationNameGenerator     = new CustomOperationNameGenerator(),
                GenerateOptionalParameters = true,
                CSharpGeneratorSettings    =
                {
                    ClassStyle            = CSharpClassStyle.Poco,
                    Namespace             = definition.Namespace,
                    ArrayType             = "System.Collections.Generic.List",
                    PropertyNameGenerator = new CustomPropertyNameGenerator(c => ConversionUtilities.ConvertToUpperCamelCase(c,true)),
                }
            };

            // detect whether there will be multiple clients or just one
            var clientNames = document.Operations
                              .Select(o => settings.OperationNameGenerator.GetClientName(document, o.Path, o.Method, o.Operation))
                              .Distinct()
                              .ToArray();

            definition.IsSingleClient = clientNames.Length == 1;

            if (definition.IsSingleClient)
            {
                // set the class name only when Swagger generates one client, otherwise all classes would have the same name
                settings.ClassName = className;
            }

            settings.CSharpGeneratorSettings.TypeNameGenerator =
                new DotvmmCSharpTypeNameGenerator(settings.CSharpGeneratorSettings, document);
            settings.CSharpGeneratorSettings.TemplateFactory = new DotvvmClientTemplateFactory(settings.CodeGeneratorSettings, new[] {
                typeof(CSharpGeneratorSettings).Assembly,
                typeof(SwaggerToCSharpGeneratorSettings).Assembly
            });

            var resolver  = SwaggerToCSharpTypeResolver.CreateWithDefinitions(settings.CSharpGeneratorSettings, document);
            var generator = new DotvvmSwaggerToCSharpClientGenerator(document, settings, resolver);
            var csharp    = generator.GenerateFile();

            if (definition.GenerateWrapperClass && !definition.IsSingleClient)
            {
                csharp = ApiClientUtils.InjectWrapperClass(csharp, className, clientNames);
            }

            FileSystemHelpers.WriteFile(definition.CSharpClient, csharp, promptOnFileOverwrite);

            return(definition.IsSingleClient, className);
        }
예제 #2
0
        public static (bool isSingleClient, string typeName) GenerateCSharp(SwaggerDocument document, ApiClientDefinition definition)
        {
            var settings = new SwaggerToCSharpClientGeneratorSettings()
            {
                GenerateSyncMethods        = true,
                OperationNameGenerator     = new CustomOperationNameGenerator(),
                GenerateOptionalParameters = true,
                CSharpGeneratorSettings    =
                {
                    ClassStyle            = CSharpClassStyle.Poco,
                    Namespace             = definition.Namespace,
                    ArrayType             = "System.Collections.Generic.List",
                    PropertyNameGenerator = new CustomPropertyNameGenerator(c => ConversionUtilities.ConvertToUpperCamelCase(c,true)),
                }
            };

            settings.CSharpGeneratorSettings.TypeNameGenerator =
                new DotvmmCSharpTypeNameGenerator(settings.CSharpGeneratorSettings, document);
            settings.CSharpGeneratorSettings.TemplateFactory = new DotvvmClientTemplateFactory(settings.CodeGeneratorSettings, new[] {
                typeof(CSharpGeneratorSettings).Assembly,
                typeof(SwaggerToCSharpGeneratorSettings).Assembly
            });

            var resolver  = SwaggerToCSharpTypeResolver.CreateWithDefinitions(settings.CSharpGeneratorSettings, document);
            var generator = new DotvvmSwaggerToCSharpClientGenerator(document, settings, resolver);
            var csharp    = generator.GenerateFile();

            var newClient = ApiClientUtils.InjectWrapperClass(csharp, Path.GetFileNameWithoutExtension(definition.CSharpClient),
                                                              document, settings.OperationNameGenerator, out var isSingleClient, out var wrapperTypeName);

            definition.IsSingleClient = isSingleClient;

            if (definition.GenerateWrapperClass)
            {
                csharp = newClient;
            }

            File.WriteAllText(definition.CSharpClient, csharp);

            return(isSingleClient, wrapperTypeName);
        }