Esempio n. 1
0
        public static async Task Main(string[] args)
        {
            // Read the Open API YAML specification
            OpenApiDocument document = await OpenApiYamlDocument.FromUrlAsync("https://raw.githubusercontent.com/netlify/open-api/master/swagger.yml");

            // Generate the code
            CSharpClientGeneratorSettings clientSettings = new CSharpClientGeneratorSettings
            {
                ClassName = nameof(NetlifyClient),
                CSharpGeneratorSettings =
                {
                    Namespace             = nameof(NetlifySharp),
                    PropertyNameGenerator = new PascalCasePropertyNameGenerator()
                },
                ParameterNameGenerator      = new CamelCaseParameterNameGenerator(),
                ExceptionClass              = nameof(NetlifyException),
                InjectHttpClient            = false,
                UseHttpClientCreationMethod = true
            };

            clientSettings.CSharpGeneratorSettings.TemplateFactory = new CustomTemplateFactory(
                clientSettings.CSharpGeneratorSettings.TemplateFactory,
                clientSettings);
            CSharpClientGenerator generator = new CSharpClientGenerator(document, clientSettings);
            string code = generator.GenerateFile();

            // Make the constructor private so we can write our own
            code = code.Replace($"public {clientSettings.ClassName}(", $"private {clientSettings.ClassName}(");

            // Write to a file in the client project
            System.IO.File.WriteAllText($"../../src/NetlifySharp/{clientSettings.ClassName}.Generated.cs", code);
        }
        public static async Task <string> CreateNSwagStudioFileAsync(
            EnterOpenApiSpecDialogResult enterOpenApiSpecDialogResult,
            INSwagStudioOptions options = null,
            string outputNamespace      = null)
        {
            var specifications  = enterOpenApiSpecDialogResult.OpenApiSpecification;
            var outputFilename  = enterOpenApiSpecDialogResult.OutputFilename;
            var url             = enterOpenApiSpecDialogResult.Url;
            var openApiDocument = url.EndsWith("yaml") || url.EndsWith("yml")
                ? await OpenApiYamlDocument.FromUrlAsync(url)
                : await OpenApiDocument.FromJsonAsync(specifications);

            var className = options?.UseDocumentTitle ?? true
                ? openApiDocument.GenerateClassName()
                : outputFilename;

            return(new
            {
                Runtime = "Default",
                SwaggerGenerator = new
                {
                    FromSwagger = GetFromSwagger(enterOpenApiSpecDialogResult, specifications)
                },
                CodeGenerators = new
                {
                    SwaggerToCSharpClient = new
                    {
                        ClassName = className,
                        InjectHttpClient = options?.InjectHttpClient ?? true,
                        GenerateClientInterfaces = options?.GenerateClientInterfaces ?? true,
                        GenerateDtoTypes = options?.GenerateDtoTypes ?? true,
                        UseBaseUrl = options?.UseBaseUrl ?? false,
                        OperationGenerationMode = "MultipleClientsFromOperationId",
                        GenerateResponseClasses = options?.GenerateResponseClasses ?? true,
                        GenerateJsonMethods = options?.GenerateJsonMethods ?? true,
                        RequiredPropertiesMustBeDefined = options?.RequiredPropertiesMustBeDefined ?? true,
                        ClassStyle = options?.ClassStyle ?? CSharpClassStyle.Poco,
                        GenerateDefaultValues = options?.GenerateDefaultValues ?? true,
                        GenerateDataAnnotations = options?.GenerateDataAnnotations ?? true,
                        Namespace = outputNamespace ?? "GeneratedCode",
                        Output = $"{className}.cs"
                    }
                }
            }
                   .ToJson());
        }
Esempio n. 3
0
        static async Task Generate(string source, string target, string name, bool repeat = false)
        {
            var document = await OpenApiYamlDocument.FromUrlAsync(source);

            var settings = new CSharpClientGeneratorSettings {
                ClassName = "{controller}Client",
                ExposeJsonSerializerSettings = true,
                CSharpGeneratorSettings      =
                {
                    Namespace = name
                }
            };

            var generator = new CSharpClientGenerator(document, settings);
            var code      = generator.GenerateFile();

            File.WriteAllText(target, code);
        }
Esempio n. 4
0
 protected async Task <OpenApiDocument> ReadSwaggerDocumentAsync(string input)
 {
     if (!IsJson(input) && !IsYaml(input))
     {
         if (input.StartsWith("http://") || input.StartsWith("https://"))
         {
             if (input.EndsWith(".yaml", StringComparison.OrdinalIgnoreCase) ||
                 input.EndsWith(".yml", StringComparison.OrdinalIgnoreCase))
             {
                 return(await OpenApiYamlDocument.FromUrlAsync(input).ConfigureAwait(false));
             }
             else
             {
                 return(await OpenApiDocument.FromUrlAsync(input).ConfigureAwait(false));
             }
         }
         else
         {
             if (input.EndsWith(".yaml", StringComparison.OrdinalIgnoreCase) ||
                 input.EndsWith(".yml", StringComparison.OrdinalIgnoreCase))
             {
                 return(await OpenApiYamlDocument.FromFileAsync(input).ConfigureAwait(false));
             }
             else
             {
                 return(await OpenApiDocument.FromFileAsync(input).ConfigureAwait(false));
             }
         }
     }
     else
     {
         if (IsYaml(input))
         {
             return(await OpenApiYamlDocument.FromYamlAsync(input).ConfigureAwait(false));
         }
         else
         {
             return(await OpenApiDocument.FromJsonAsync(input).ConfigureAwait(false));
         }
     }
 }
Esempio n. 5
0
 internal static async Task <OpenApiDocument> ReadSwaggerDocumentAsync(this string input)
 {
     if (!input.IsJson() && !input.IsYaml())
     {
         if (input.StartsWith("http://", StringComparison.OrdinalIgnoreCase) || input.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
         {
             if (input.EndsWith(".yaml", StringComparison.OrdinalIgnoreCase) ||
                 input.EndsWith(".yml", StringComparison.OrdinalIgnoreCase))
             {
                 return(await OpenApiYamlDocument.FromUrlAsync(input).ConfigureAwait(false));
             }
             else
             {
                 return(await OpenApiDocument.FromUrlAsync(input).ConfigureAwait(false));
             }
         }
         else
         {
             if (input.EndsWith(".yaml", StringComparison.OrdinalIgnoreCase) ||
                 input.EndsWith(".yml", StringComparison.OrdinalIgnoreCase))
             {
                 return(await OpenApiYamlDocument.FromFileAsync(input).ConfigureAwait(false));
             }
             else
             {
                 return(await OpenApiDocument.FromFileAsync(input).ConfigureAwait(false));
             }
         }
     }
     else
     {
         return(input.IsYaml()
             ? await OpenApiYamlDocument.FromYamlAsync(input).ConfigureAwait(false)
             : await OpenApiDocument.FromJsonAsync(input).ConfigureAwait(false));
     }
 }
Esempio n. 6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="specification"></param>
        /// <returns></returns>
        public NSwagRunner FromYamlSpecification(Uri specification)
        {
            OpenApiDocument document = OpenApiYamlDocument.FromUrlAsync(specification.ToString()).Result;

            return(new NSwagRunner(Context, document));
        }