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);
            }
        }
    public List <SyntaxGeneratorContractModel> GenerateSyntaxTrees()
    {
        var list = new List <SyntaxGeneratorContractModel>();

        var apiOperationSchemaMaps = OperationSchemaMappings
                                     .Where(x => x.LocatedArea is SchemaMapLocatedAreaType.Response or SchemaMapLocatedAreaType.RequestBody &&
                                            x.SegmentName.Equals(FocusOnSegmentName, StringComparison.OrdinalIgnoreCase))
                                     .ToList();

        var schemaKeys = apiOperationSchemaMaps
                         .Select(x => x.SchemaKey)
                         .Distinct(StringComparer.Ordinal)
                         .OrderBy(x => x)
                         .ToList();

        foreach (var schemaKey in schemaKeys)
        {
            var apiSchema = ApiProjectOptions.Document.Components.Schemas.First(x => x.Key.Equals(schemaKey, StringComparison.OrdinalIgnoreCase));
            if (apiSchema.Value.IsSchemaEnumOrPropertyEnum())
            {
                var syntaxGeneratorContractModel = new SyntaxGeneratorContractModel(logger, ApiProjectOptions, apiSchema.Key, apiSchema.Value, FocusOnSegmentName);
                syntaxGeneratorContractModel.GenerateCode();
                list.Add(syntaxGeneratorContractModel);
            }
            else
            {
                var isShared = OperationSchemaMappings.IsShared(schemaKey);
                if (isShared)
                {
                    var syntaxGeneratorContractModel = new SyntaxGeneratorContractModel(logger, ApiProjectOptions, apiSchema.Key, apiSchema.Value, "#");
                    syntaxGeneratorContractModel.GenerateCode();
                    list.Add(syntaxGeneratorContractModel);
                }
                else
                {
                    var syntaxGeneratorContractModel = new SyntaxGeneratorContractModel(logger, ApiProjectOptions, apiSchema.Key, apiSchema.Value, FocusOnSegmentName);
                    syntaxGeneratorContractModel.GenerateCode();
                    list.Add(syntaxGeneratorContractModel);
                }
            }
        }

        return(list);
    }
Ejemplo n.º 3
0
        private static List <LogKeyValueItem> GenerateContracts(ApiProjectOptions apiProjectOptions, List <ApiOperationSchemaMap> operationSchemaMappings)
        {
            if (apiProjectOptions == null)
            {
                throw new ArgumentNullException(nameof(apiProjectOptions));
            }

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

            var sgContractModels     = new List <SyntaxGeneratorContractModel>();
            var sgContractParameters = new List <SyntaxGeneratorContractParameter>();
            var sgContractResults    = new List <SyntaxGeneratorContractResult>();
            var sgContractInterfaces = new List <SyntaxGeneratorContractInterface>();

            foreach (var basePathSegmentName in apiProjectOptions.BasePathSegmentNames)
            {
                var generatorModels = new SyntaxGeneratorContractModels(apiProjectOptions, operationSchemaMappings, basePathSegmentName);
                var generatedModels = generatorModels.GenerateSyntaxTrees();
                sgContractModels.AddRange(generatedModels);

                var generatorParameters = new SyntaxGeneratorContractParameters(apiProjectOptions, basePathSegmentName);
                var generatedParameters = generatorParameters.GenerateSyntaxTrees();
                sgContractParameters.AddRange(generatedParameters);

                var generatorResults = new SyntaxGeneratorContractResults(apiProjectOptions, basePathSegmentName);
                var generatedResults = generatorResults.GenerateSyntaxTrees();
                sgContractResults.AddRange(generatedResults);

                var generatorInterfaces = new SyntaxGeneratorContractInterfaces(apiProjectOptions, basePathSegmentName);
                var generatedInterfaces = generatorInterfaces.GenerateSyntaxTrees();
                sgContractInterfaces.AddRange(generatedInterfaces);
            }

            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(
                    apiProjectOptions,
                    map.SchemaKey,
                    apiProjectOptions.Document.Components.Schemas.First(x => x.Key.Equals(map.SchemaKey, StringComparison.OrdinalIgnoreCase)).Value,
                    map.SegmentName);

                generatorModel.GenerateCode();
                sgContractModels.Add(generatorModel);
            }

            var logItems = new List <LogKeyValueItem>();

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

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

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

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

            return(logItems);
        }