Ejemplo n.º 1
0
        private static void ScaffoldBasicFileApiGenerated(ApiProjectOptions apiProjectOptions)
        {
            // Create compilationUnit
            var compilationUnit = SyntaxFactory.CompilationUnit();

            // Create a namespace
            var @namespace = SyntaxProjectFactory.CreateNamespace(apiProjectOptions);

            // Create class
            var classDeclaration = SyntaxClassDeclarationFactory.Create("ApiRegistration")
                                   .AddGeneratedCodeAttribute(apiProjectOptions.ToolName, apiProjectOptions.ToolVersion.ToString());

            // Add class to namespace
            @namespace = @namespace.AddMembers(classDeclaration);

            // Add using statement to compilationUnit
            compilationUnit = compilationUnit.AddUsingStatements(new[] { "System.CodeDom.Compiler" });

            // Add namespace to compilationUnit
            compilationUnit = compilationUnit.AddMembers(@namespace);

            var codeAsString = compilationUnit
                               .NormalizeWhitespace()
                               .ToFullString();

            var file = new FileInfo(Path.Combine(apiProjectOptions.PathForSrcGenerate.FullName, "ApiRegistration.cs"));

            TextFileHelper.Save(file, codeAsString);
        }
Ejemplo n.º 2
0
        private static List <LogKeyValueItem> GenerateEndpoints(ApiProjectOptions apiProjectOptions, List <ApiOperationSchemaMap> operationSchemaMappings)
        {
            if (apiProjectOptions == null)
            {
                throw new ArgumentNullException(nameof(apiProjectOptions));
            }

            if (operationSchemaMappings == null)
            {
                throw new ArgumentNullException(nameof(operationSchemaMappings));
            }

            var sgEndpoints = new List <SyntaxGeneratorEndpointControllers>();

            foreach (var segmentName in apiProjectOptions.BasePathSegmentNames)
            {
                var generator = new SyntaxGeneratorEndpointControllers(apiProjectOptions, operationSchemaMappings, segmentName);
                generator.GenerateCode();
                sgEndpoints.Add(generator);
            }

            var logItems = new List <LogKeyValueItem>();

            foreach (var sg in sgEndpoints)
            {
                logItems.Add(sg.ToFile());
            }

            return(logItems);
        }
Ejemplo n.º 3
0
 public SyntaxGeneratorContractParameters(
     ApiProjectOptions apiProjectOptions,
     string focusOnSegmentName)
 {
     this.ApiProjectOptions  = apiProjectOptions ?? throw new ArgumentNullException(nameof(apiProjectOptions));
     this.FocusOnSegmentName = focusOnSegmentName ?? throw new ArgumentNullException(nameof(focusOnSegmentName));
 }
Ejemplo n.º 4
0
        public static List <LogKeyValueItem> GenerateServerApi(
            string projectPrefixName,
            DirectoryInfo outputPath,
            DirectoryInfo?outputTestPath,
            Tuple <OpenApiDocument, OpenApiDiagnostic, FileInfo> apiDocument,
            ApiOptions apiOptions)
        {
            if (projectPrefixName == null)
            {
                throw new ArgumentNullException(nameof(projectPrefixName));
            }

            if (outputPath == null)
            {
                throw new ArgumentNullException(nameof(outputPath));
            }

            if (apiDocument == null)
            {
                throw new ArgumentNullException(nameof(apiDocument));
            }

            if (apiOptions == null)
            {
                throw new ArgumentNullException(nameof(apiOptions));
            }

            var projectOptions     = new ApiProjectOptions(outputPath, outputTestPath, apiDocument.Item1, apiDocument.Item3, projectPrefixName, apiOptions);
            var serverApiGenerator = new ServerApiGenerator(projectOptions);

            return(serverApiGenerator.Generate());
        }
Ejemplo n.º 5
0
        private static void CopyApiSpecification(ApiProjectOptions apiProjectOptions)
        {
            if (apiProjectOptions == null)
            {
                throw new ArgumentNullException(nameof(apiProjectOptions));
            }

            var resourceFolder = new DirectoryInfo(Path.Combine(apiProjectOptions.PathForSrcGenerate.FullName, "Resources"));

            if (!resourceFolder.Exists)
            {
                Directory.CreateDirectory(resourceFolder.FullName);
            }

            var resourceFile = new FileInfo(Path.Combine(resourceFolder.FullName, "ApiSpecification.yaml"));

            if (File.Exists(resourceFile.FullName))
            {
                File.Delete(resourceFile.FullName);
            }

            if (apiProjectOptions.DocumentFile.Extension.Equals(".json", StringComparison.OrdinalIgnoreCase))
            {
                using var writeFile = new StreamWriter(resourceFile.FullName);
                apiProjectOptions.Document.SerializeAsV3(new OpenApiYamlWriter(writeFile));
            }
            else
            {
                File.Copy(apiProjectOptions.DocumentFile.FullName, resourceFile.FullName);
            }
        }
Ejemplo n.º 6
0
 public ServerApiGenerator(
     ILogger logger,
     ApiProjectOptions projectOptions)
 {
     this.logger         = logger ?? throw new ArgumentNullException(nameof(logger));
     this.projectOptions = projectOptions ?? throw new ArgumentNullException(nameof(projectOptions));
 }
 public SyntaxGeneratorContractInterfaces(
     ILogger logger,
     ApiProjectOptions apiProjectOptions,
     string focusOnSegmentName)
 {
     this.logger             = logger ?? throw new ArgumentNullException(nameof(logger));
     this.ApiProjectOptions  = apiProjectOptions ?? throw new ArgumentNullException(nameof(apiProjectOptions));
     this.FocusOnSegmentName = focusOnSegmentName ?? throw new ArgumentNullException(nameof(focusOnSegmentName));
 }
Ejemplo n.º 8
0
 public SyntaxGeneratorContractModels(
     ApiProjectOptions apiProjectOptions,
     List <ApiOperationSchemaMap> operationSchemaMappings,
     string focusOnSegmentName)
 {
     this.ApiProjectOptions       = apiProjectOptions ?? throw new ArgumentNullException(nameof(apiProjectOptions));
     this.OperationSchemaMappings = operationSchemaMappings ?? throw new ArgumentNullException(nameof(apiProjectOptions));
     this.FocusOnSegmentName      = focusOnSegmentName ?? throw new ArgumentNullException(nameof(focusOnSegmentName));
 }
Ejemplo n.º 9
0
        private static void DeleteLegacyScaffoldBasicFileResultFactory(ApiProjectOptions apiProjectOptions)
        {
            var file = new FileInfo(Path.Combine(apiProjectOptions.PathForSrcGenerate.FullName, "ResultFactory.cs"));

            if (file.Exists)
            {
                File.Delete(file.FullName);
            }
        }
Ejemplo n.º 10
0
        private static List <LogKeyValueItem> ScaffoldSrc(ApiProjectOptions apiProjectOptions)
        {
            if (apiProjectOptions == null)
            {
                throw new ArgumentNullException(nameof(apiProjectOptions));
            }

            if (!Directory.Exists(apiProjectOptions.PathForSrcGenerate.FullName))
            {
                Directory.CreateDirectory(apiProjectOptions.PathForSrcGenerate.FullName);
            }

            var logItems = new List <LogKeyValueItem>();

            if (apiProjectOptions.PathForSrcGenerate.Exists && apiProjectOptions.ProjectSrcCsProj.Exists)
            {
                var element = XElement.Load(apiProjectOptions.ProjectSrcCsProj.FullName);
                var originalNullableValue = SolutionAndProjectHelper.GetBoolFromNullableString(SolutionAndProjectHelper.GetNullableValueFromProject(element));

                bool hasUpdates = false;
                if (apiProjectOptions.ApiOptions.Generator.UseNullableReferenceTypes != originalNullableValue)
                {
                    var newNullableValue = SolutionAndProjectHelper.GetNullableStringFromBool(apiProjectOptions.ApiOptions.Generator.UseNullableReferenceTypes);
                    SolutionAndProjectHelper.SetNullableValueForProject(element, newNullableValue);
                    element.Save(apiProjectOptions.ProjectSrcCsProj.FullName);
                    logItems.Add(new LogKeyValueItem(LogCategoryType.Debug, "FileUpdate", "#", $"Update API csproj - Nullable value={newNullableValue}"));
                    hasUpdates = true;
                }

                if (!hasUpdates)
                {
                    logItems.Add(new LogKeyValueItem(LogCategoryType.Debug, "FileSkip", "#", "No updates for API csproj"));
                }
            }
            else
            {
                logItems.Add(SolutionAndProjectHelper.ScaffoldProjFile(
                                 apiProjectOptions.ProjectSrcCsProj,
                                 false,
                                 false,
                                 apiProjectOptions.ProjectName,
                                 apiProjectOptions.ApiOptions.Generator.UseNullableReferenceTypes,
                                 new List <string> {
                    "Microsoft.AspNetCore.App"
                },
                                 NugetPackageReferenceHelper.CreateForApiProject(),
                                 null,
                                 true));
            }

            ScaffoldBasicFileApiGenerated(apiProjectOptions);
            DeleteLegacyScaffoldBasicFileResultFactory(apiProjectOptions);
            DeleteLegacyScaffoldBasicFilePagination(apiProjectOptions);

            return(logItems);
        }
 public SyntaxGeneratorContractResult(
     ApiProjectOptions apiProjectOptions,
     OperationType apiOperationType,
     OpenApiOperation apiOperation,
     string focusOnSegmentName)
 {
     this.ApiProjectOptions  = apiProjectOptions ?? throw new ArgumentNullException(nameof(apiProjectOptions));
     this.ApiOperationType   = apiOperationType;
     this.ApiOperation       = apiOperation ?? throw new ArgumentNullException(nameof(apiOperation));
     this.FocusOnSegmentName = focusOnSegmentName ?? throw new ArgumentNullException(nameof(focusOnSegmentName));
 }
 public SyntaxGeneratorClientEndpointInterfaces(
     ILogger logger,
     ApiProjectOptions apiProjectOptions,
     List <ApiOperationSchemaMap> operationSchemaMappings,
     string focusOnSegmentName)
 {
     this.logger                  = logger ?? throw new ArgumentNullException(nameof(logger));
     this.ApiProjectOptions       = apiProjectOptions ?? throw new ArgumentNullException(nameof(apiProjectOptions));
     this.OperationSchemaMappings = operationSchemaMappings ?? throw new ArgumentNullException(nameof(operationSchemaMappings));
     this.FocusOnSegmentName      = focusOnSegmentName ?? throw new ArgumentNullException(nameof(focusOnSegmentName));
 }
Ejemplo n.º 13
0
    protected override ISyntaxCodeGenerator CreateGenerator(
        ApiProjectOptions apiProject)
    {
        ArgumentNullException.ThrowIfNull(apiProject);

        // Verify spec file supported for unit test
        Assert.Single(apiProject.Document.Components.Schemas);
        var schema = apiProject.Document.Components.Schemas.First();

        // Construct SUT
        return(new SyntaxGeneratorContractModel(NullLogger.Instance, apiProject, schema.Key, schema.Value, FocusOnSegment));
    }
Ejemplo n.º 14
0
    public static string[] CreateUsingListForEndpoint(
        ApiProjectOptions apiProjectOptions,
        bool includeRestResults,
        bool hasParameter,
        bool hasList,
        bool hasSharedModel)
    {
        ArgumentNullException.ThrowIfNull(apiProjectOptions);

        var list = new List <string>
        {
            "System.CodeDom.Compiler",
            "System.Net",
            "System.Net.Http",
            "System.Threading",
            "System.Threading.Tasks",
            "Atc.Rest.Client.Builder",
            "Microsoft.AspNetCore.Mvc",
        };

        if (hasParameter)
        {
            list.Add("System");
        }

        if (hasList)
        {
            list.Add("System.Collections.Generic");
        }

        if (includeRestResults)
        {
            list.Add("Atc.Rest.Results");
        }

        if (hasSharedModel)
        {
            list.Add(string.IsNullOrEmpty(apiProjectOptions.ClientFolderName)
                ? $"{apiProjectOptions.ProjectName}.{NameConstants.Contracts}"
                : $"{apiProjectOptions.ProjectName}.{apiProjectOptions.ClientFolderName}.{NameConstants.Contracts}");
        }

        var s = string.IsNullOrEmpty(apiProjectOptions.ClientFolderName)
            ? $"{apiProjectOptions.ProjectName}.{NameConstants.Contracts}"
            : $"{apiProjectOptions.ProjectName}.{apiProjectOptions.ClientFolderName}.{NameConstants.Contracts}";

        if (!list.Contains(s, StringComparer.Ordinal))
        {
            list.Add(s);
        }

        return(list.ToArray());
    }
Ejemplo n.º 15
0
        private static LogKeyValueItem ValidateVersioning(ApiProjectOptions apiProjectOptions)
        {
            if (apiProjectOptions == null)
            {
                throw new ArgumentNullException(nameof(apiProjectOptions));
            }

            if (!Directory.Exists(apiProjectOptions.PathForSrcGenerate.FullName))
            {
                return(LogItemHelper.Create(LogCategoryType.Information, ValidationRuleNameConstants.ProjectApiGenerated01, "Old project don't exist."));
            }

            var apiGeneratedFile = Path.Combine(apiProjectOptions.PathForSrcGenerate.FullName, "ApiRegistration.cs");

            if (!File.Exists(apiGeneratedFile))
            {
                return(LogItemHelper.Create(LogCategoryType.Information, ValidationRuleNameConstants.ProjectApiGenerated02, "Old ApiRegistration.cs in project don't exist."));
            }

            var lines = File.ReadLines(apiGeneratedFile).ToList();

            const string toolName   = "ApiGenerator";
            var          newVersion = GenerateHelper.GetAtcToolVersion();

            foreach (var line in lines)
            {
                var indexOfToolName = line.IndexOf(toolName !, StringComparison.Ordinal);
                if (indexOfToolName == -1)
                {
                    continue;
                }

                var oldVersion = line.Substring(indexOfToolName + toolName !.Length);
                if (oldVersion.EndsWith('.'))
                {
                    oldVersion = oldVersion.Substring(0, oldVersion.Length - 1);
                }

                if (!Version.TryParse(oldVersion, out var oldVersionResult))
                {
                    return(LogItemHelper.Create(LogCategoryType.Error, ValidationRuleNameConstants.ProjectApiGenerated03, "Existing project version is invalid."));
                }

                if (newVersion >= oldVersionResult)
                {
                    return(LogItemHelper.Create(LogCategoryType.Information, ValidationRuleNameConstants.ProjectApiGenerated04, "The generate project version is the same or newer."));
                }

                return(LogItemHelper.Create(LogCategoryType.Error, ValidationRuleNameConstants.ProjectApiGenerated05, "Existing project version is never than this tool version."));
            }

            return(LogItemHelper.Create(LogCategoryType.Error, ValidationRuleNameConstants.ProjectApiGenerated06, "Existing project did not contain a version."));
        }
 public SyntaxGeneratorContractParameter(
     ApiProjectOptions apiProjectOptions,
     IList <OpenApiParameter> globalPathParameters,
     OperationType apiOperationType,
     OpenApiOperation apiOperation,
     string focusOnSegmentName)
 {
     this.ApiProjectOptions    = apiProjectOptions ?? throw new ArgumentNullException(nameof(apiProjectOptions));
     this.GlobalPathParameters = globalPathParameters ?? throw new ArgumentNullException(nameof(globalPathParameters));
     this.ApiOperationType     = apiOperationType;
     this.ApiOperation         = apiOperation ?? throw new ArgumentNullException(nameof(apiOperation));
     this.FocusOnSegmentName   = focusOnSegmentName ?? throw new ArgumentNullException(nameof(focusOnSegmentName));
 }
Ejemplo n.º 17
0
    protected override ISyntaxCodeGenerator CreateGenerator(
        ApiProjectOptions apiProject)
    {
        ArgumentNullException.ThrowIfNull(apiProject);

        // Verify spec file supported for unit test
        Assert.Single(apiProject.BasePathSegmentNames);
        var segmentName             = apiProject.BasePathSegmentNames.First();
        var operationSchemaMappings = OpenApiOperationSchemaMapHelper.CollectMappings(apiProject.Document);

        // Construct SUT
        return(new SyntaxGeneratorEndpointControllers(NullLogger.Instance, apiProject, operationSchemaMappings, segmentName));
    }
    protected static VerifySettings CreateVerifySettings(
        GeneratorTestInput yamlFile,
        ApiProjectOptions apiOptions)
    {
        ArgumentNullException.ThrowIfNull(yamlFile);

        var settings = new VerifySettings();

        settings.UseDirectory(yamlFile.TestDirectory);
        settings.UseFileName(yamlFile.TestName);
        settings.UseExtension("cs");
        settings.AddScrubber(input => input.Replace(apiOptions.ToolVersion.ToString(), "x.x.x.x"));
        return(settings);
    }
 public ClientCSharpApiGenerator(ClientCSharpApiProjectOptions projectOptions)
 {
     this.projectOptions    = projectOptions ?? throw new ArgumentNullException(nameof(projectOptions));
     this.apiProjectOptions = new ApiProjectOptions(
         projectOptions.PathForSrcGenerate,
         projectTestGeneratePath: null,
         projectOptions.Document,
         projectOptions.DocumentFile,
         projectOptions.ProjectName,
         projectSuffixName: null,
         projectOptions.ApiOptions,
         projectOptions.ForClient,
         projectOptions.ClientFolderName);
 }
    protected override ISyntaxGeneratorContractModels CreateGenerator(
        ApiProjectOptions apiProject)
    {
        ArgumentNullException.ThrowIfNull(apiProject);

        // Verify spec file supported for unit test
        Assert.True(apiProject.Document.Components.Schemas.Count > 0);

        // Construct SUT
        var apiOperationSchemaMaps = apiProject.Document.Components.Schemas
                                     .Select(schema => new ApiOperationSchemaMap(schema.Key, SchemaMapLocatedAreaType.Response, FocusOnSegment, OperationType.Get, parentSchemaKey: null))
                                     .ToList();

        return(new SyntaxGeneratorContractModels(NullLogger.Instance, apiProject, apiOperationSchemaMaps, FocusOnSegment));
    }
Ejemplo n.º 21
0
 public SyntaxGeneratorContractModel(
     ApiProjectOptions apiProjectOptions,
     string apiSchemaKey,
     OpenApiSchema apiSchema,
     string focusOnSegmentName)
 {
     this.ApiProjectOptions  = apiProjectOptions ?? throw new ArgumentNullException(nameof(apiProjectOptions));
     this.ApiSchemaKey       = apiSchemaKey ?? throw new ArgumentNullException(nameof(apiSchemaKey));
     this.ApiSchema          = apiSchema ?? throw new ArgumentNullException(nameof(apiSchema));
     this.FocusOnSegmentName = focusOnSegmentName ?? throw new ArgumentNullException(nameof(focusOnSegmentName));
     if (this.FocusOnSegmentName == "#")
     {
         this.IsSharedContract = true;
     }
 }
Ejemplo n.º 22
0
        private static void DeleteLegacyScaffoldBasicFilePagination(ApiProjectOptions apiProjectOptions)
        {
            var(key, _) = apiProjectOptions.Document.Components.Schemas.FirstOrDefault(x => x.Key.Equals(Microsoft.OpenApi.Models.NameConstants.Pagination, StringComparison.OrdinalIgnoreCase));
            if (key == null)
            {
                return;
            }

            var file = new FileInfo(Path.Combine(apiProjectOptions.PathForSrcGenerate.FullName, $"{Microsoft.OpenApi.Models.NameConstants.Pagination}.cs"));

            if (file.Exists)
            {
                File.Delete(file.FullName);
            }
        }
        public static void CollectMissingContractModelFromOperationSchemaMappings(
            ApiProjectOptions projectOptions,
            List <ApiOperationSchemaMap> operationSchemaMappings,
            List <SyntaxGeneratorContractModel> sgContractModels)
        {
            if (projectOptions == null)
            {
                throw new ArgumentNullException(nameof(projectOptions));
            }

            if (operationSchemaMappings == null)
            {
                throw new ArgumentNullException(nameof(operationSchemaMappings));
            }

            if (sgContractModels == null)
            {
                throw new ArgumentNullException(nameof(sgContractModels));
            }

            var missingOperationSchemaMappings = new List <ApiOperationSchemaMap>();

            foreach (var map in operationSchemaMappings)
            {
                if (sgContractModels.FirstOrDefault(x => x.ApiSchemaKey.Equals(map.SchemaKey, StringComparison.OrdinalIgnoreCase)) == null)
                {
                    missingOperationSchemaMappings.Add(map);
                }
            }

            foreach (var map in missingOperationSchemaMappings)
            {
                if (missingOperationSchemaMappings.Count(x => x.SchemaKey.Equals(map.SchemaKey, StringComparison.OrdinalIgnoreCase)) > 1)
                {
                    throw new NotImplementedException($"SchemaKey: {map.SchemaKey} is not generated and exist multiple times - location-calculation is missing.");
                }

                var generatorModel = new SyntaxGeneratorContractModel(
                    projectOptions,
                    map.SchemaKey,
                    projectOptions.Document.Components.Schemas.First(x => x.Key.Equals(map.SchemaKey, StringComparison.OrdinalIgnoreCase)).Value,
                    map.SegmentName);

                generatorModel.GenerateCode();
                sgContractModels.Add(generatorModel);
            }
        }
Ejemplo n.º 24
0
        private static List <LogKeyValueItem> PerformCleanup(ApiProjectOptions apiProjectOptions, List <LogKeyValueItem> orgLogItems)
        {
            if (apiProjectOptions == null)
            {
                throw new ArgumentNullException(nameof(apiProjectOptions));
            }

            if (orgLogItems == null)
            {
                throw new ArgumentNullException(nameof(orgLogItems));
            }

            var logItems = new List <LogKeyValueItem>();

            if (Directory.Exists(apiProjectOptions.PathForContracts.FullName))
            {
                var files = Directory.GetFiles(apiProjectOptions.PathForContracts.FullName, "*.*", SearchOption.AllDirectories);
                foreach (string file in files)
                {
                    if (orgLogItems.FirstOrDefault(x => x.Description == file) != null)
                    {
                        continue;
                    }

                    File.Delete(file);
                    logItems.Add(new LogKeyValueItem(LogCategoryType.Debug, "FileDelete", "#", file));
                }
            }

            if (Directory.Exists(apiProjectOptions.PathForEndpoints.FullName))
            {
                var files = Directory.GetFiles(apiProjectOptions.PathForEndpoints.FullName, "*.*", SearchOption.AllDirectories);
                foreach (string file in files)
                {
                    if (orgLogItems.FirstOrDefault(x => x.Description == file) != null)
                    {
                        continue;
                    }

                    File.Delete(file);
                    logItems.Add(new LogKeyValueItem(LogCategoryType.Debug, "FileDelete", "#", file));
                }
            }

            return(logItems);
        }
Ejemplo n.º 25
0
 public SyntaxGeneratorClientEndpoint(
     ApiProjectOptions apiProjectOptions,
     List <ApiOperationSchemaMap> operationSchemaMappings,
     OperationType apiOperationType,
     OpenApiOperation apiOperation,
     string focusOnSegmentName,
     string urlPath,
     bool hasParametersOrRequestBody)
 {
     this.ApiProjectOptions       = apiProjectOptions ?? throw new ArgumentNullException(nameof(apiProjectOptions));
     this.OperationSchemaMappings = operationSchemaMappings ?? throw new ArgumentNullException(nameof(apiProjectOptions));
     this.ApiOperationType        = apiOperationType;
     this.ApiOperation            = apiOperation ?? throw new ArgumentNullException(nameof(apiOperation));
     this.FocusOnSegmentName      = focusOnSegmentName ?? throw new ArgumentNullException(nameof(focusOnSegmentName));
     this.ApiUrlPath = urlPath ?? throw new ArgumentNullException(nameof(urlPath));
     this.HasParametersOrRequestBody = hasParametersOrRequestBody;
 }
 public SyntaxGeneratorContractInterface(
     ILogger logger,
     ApiProjectOptions apiProjectOptions,
     IList <OpenApiParameter> globalPathParameters,
     OperationType apiOperationType,
     OpenApiOperation apiOperation,
     string focusOnSegmentName,
     bool hasParametersOrRequestBody)
 {
     this.logger                     = logger ?? throw new ArgumentNullException(nameof(logger));
     this.ApiProjectOptions          = apiProjectOptions ?? throw new ArgumentNullException(nameof(apiProjectOptions));
     this.GlobalPathParameters       = globalPathParameters ?? throw new ArgumentNullException(nameof(globalPathParameters));
     this.ApiOperationType           = apiOperationType;
     this.ApiOperation               = apiOperation ?? throw new ArgumentNullException(nameof(apiOperation));
     this.FocusOnSegmentName         = focusOnSegmentName ?? throw new ArgumentNullException(nameof(focusOnSegmentName));
     this.HasParametersOrRequestBody = hasParametersOrRequestBody;
 }
    public static string[] CreateUsingListForEndpoint(
        ApiProjectOptions apiProjectOptions,
        List <OpenApiOperation> apiOperations,
        bool hasSharedModel,
        bool includeRestResults,
        string focusOnSegmentName)
    {
        ArgumentNullException.ThrowIfNull(apiProjectOptions);
        ArgumentNullException.ThrowIfNull(apiOperations);

        var list = new List <string>
        {
            "System",
            "System.CodeDom.Compiler",
            "System.Threading",
            "System.Threading.Tasks",
        };

        if (includeRestResults)
        {
            list.Add("Atc.Rest.Results");
        }

        list.Add("Microsoft.AspNetCore.Http");
        list.Add("Microsoft.AspNetCore.Mvc");

        if (apiOperations.HasDataTypeFromSystemCollectionGenericNamespace())
        {
            list.Add("System.Collections.Generic");
        }

        if (apiProjectOptions.ApiOptions.Generator.UseAuthorization)
        {
            list.Add("Microsoft.AspNetCore.Authorization");
        }

        if (hasSharedModel)
        {
            list.Add($"{apiProjectOptions.ProjectName}.{NameConstants.Contracts}");
        }

        list.Add($"{apiProjectOptions.ProjectName}.{NameConstants.Contracts}.{focusOnSegmentName.EnsureFirstCharacterToUpper()}");

        return(list.ToArray());
    }
Ejemplo n.º 28
0
    public SyntaxGeneratorContractParameter(
        ILogger logger,
        ApiProjectOptions apiProjectOptions,
        IList <OpenApiParameter> globalPathParameters,
        OperationType apiOperationType,
        OpenApiOperation apiOperation,
        string focusOnSegmentName)
    {
        this.logger               = logger ?? throw new ArgumentNullException(nameof(logger));
        this.ApiProjectOptions    = apiProjectOptions ?? throw new ArgumentNullException(nameof(apiProjectOptions));
        this.GlobalPathParameters = globalPathParameters ?? throw new ArgumentNullException(nameof(globalPathParameters));
        this.ApiOperationType     = apiOperationType;
        this.ApiOperation         = apiOperation ?? throw new ArgumentNullException(nameof(apiOperation));
        this.FocusOnSegmentName   = focusOnSegmentName ?? throw new ArgumentNullException(nameof(focusOnSegmentName));

        this.IsForClient  = false;
        this.UseOwnFolder = true;
    }
Ejemplo n.º 29
0
        private static List <LogKeyValueItem> ScaffoldTest(ApiProjectOptions apiProjectOptions)
        {
            if (apiProjectOptions == null)
            {
                throw new ArgumentNullException(nameof(apiProjectOptions));
            }

            var logItems = new List <LogKeyValueItem>();

            if (apiProjectOptions.PathForTestGenerate == null || apiProjectOptions.ProjectTestCsProj == null)
            {
                return(logItems);
            }

            if (apiProjectOptions.PathForTestGenerate.Exists && apiProjectOptions.ProjectTestCsProj.Exists)
            {
                logItems.Add(new LogKeyValueItem(LogCategoryType.Debug, "FileSkip", "#", "No updates for API test csproj"));
            }
            else
            {
                if (!Directory.Exists(apiProjectOptions.PathForTestGenerate.FullName))
                {
                    Directory.CreateDirectory(apiProjectOptions.PathForTestGenerate.FullName);
                }

                var projectReferences = new List <FileInfo>
                {
                    apiProjectOptions.ProjectSrcCsProj
                };

                logItems.Add(SolutionAndProjectHelper.ScaffoldProjFile(
                                 apiProjectOptions.ProjectTestCsProj,
                                 false,
                                 true,
                                 $"{apiProjectOptions.ProjectName}.Tests",
                                 apiProjectOptions.ApiOptions.Generator.UseNullableReferenceTypes,
                                 null,
                                 NugetPackageReferenceHelper.CreateForTestProject(false),
                                 projectReferences,
                                 true));
            }

            return(logItems);
        }
Ejemplo n.º 30
0
    public SyntaxGeneratorContractModel(
        ILogger logger,
        ApiProjectOptions apiProjectOptions,
        string apiSchemaKey,
        OpenApiSchema apiSchema,
        string focusOnSegmentName)
    {
        this.logger             = logger ?? throw new ArgumentNullException(nameof(logger));
        this.ApiProjectOptions  = apiProjectOptions ?? throw new ArgumentNullException(nameof(apiProjectOptions));
        this.ApiSchemaKey       = apiSchemaKey ?? throw new ArgumentNullException(nameof(apiSchemaKey));
        this.ApiSchema          = apiSchema ?? throw new ArgumentNullException(nameof(apiSchema));
        this.FocusOnSegmentName = focusOnSegmentName ?? throw new ArgumentNullException(nameof(focusOnSegmentName));
        if (this.FocusOnSegmentName == "#")
        {
            this.IsSharedContract = true;
        }

        this.IsForClient  = false;
        this.UseOwnFolder = true;
    }