private IEnumerable <OpenApiContent> CreateProfileSection()
        {
            // profiles using tightly coupled extensions
            var resourceFilter = _openApiMetadataResourceFilters
                                 .FirstOrDefault(x => x.Key.Equals(Profiles));

            return(_profileResourceNamesProvider
                   .GetProfileResourceNames()
                   .Select(x => x.ProfileName)
                   .Select(
                       x => new SwaggerProfileContext
            {
                ProfileName = x,
                ProfileResourceModel = _profileResourceModelProvider
                                       .GetProfileResourceModel(x)
            })
                   .Select(
                       x => new SwaggerDocumentContext(_resourceModelProvider.GetResourceModel())
            {
                ProfileContext = x,
                IsIncludedExtension = r => true
            })
                   .Select(
                       c =>
                       new OpenApiContent(
                           OpenApiMetadataSections.Profiles,
                           c.ProfileContext.ProfileName,
                           new Lazy <string>(() => new SwaggerDocumentFactory(c).Create(resourceFilter.Value)),
                           _odsDataBasePath,
                           $"{OpenApiMetadataSections.Profiles}/{c.ProfileContext.ProfileName}")));
        }
Example #2
0
        public IEnumerable <ResourceProfileData> GetResourceProfileData()
        {
            if (ProjectHasProfileDefinition)
            {
                var profileNamesAndResourceModels =
                    _profileResourceNamesProvider
                    .GetProfileResourceNames()
                    .Select(prn => prn.ProfileName)
                    .Distinct()
                    .Select(
                        profileName =>
                        new
                {
                    ProfileName          = profileName,
                    ProfileResourceModel = _profileResourceModelProvider.GetProfileResourceModel(profileName)
                });

                var readablesAndWritables =
                    profileNamesAndResourceModels
                    .Select(
                        x =>
                {
                    // we need to include resources if they are derived from other entities that are writable so we can
                    // satisfy the interface
                    var includeInWritable = new List <Resource>();

                    if (x.ProfileResourceModel.Resources.Any(y => y.Writable == null))
                    {
                        var possibleWritable = x.ProfileResourceModel.Resources.Where(
                            y => y.Writable == null)
                                               .Select(y => y.Readable);

                        var writable = x.ProfileResourceModel.Resources.Where(y => y.Writable != null)
                                       .Select(y => y.Writable)
                                       .ToList();

                        foreach (var resource in possibleWritable)
                        {
                            includeInWritable.AddRange(
                                writable.Where(wr => wr.IsDerivedFrom(resource))
                                .Select(wr => resource));
                        }
                    }

                    // to satisfy future profiles that may have this situation also.
                    var includeInReadable = new List <Resource>();

                    if (x.ProfileResourceModel.Resources.Any(y => y.Readable == null))
                    {
                        var possibleReadable = x.ProfileResourceModel.Resources.Where(
                            y => y.Readable == null)
                                               .Select(y => y.Writable);

                        var readable = x.ProfileResourceModel.Resources.Where(y => y.Readable != null)
                                       .Select(y => y.Readable)
                                       .ToList();

                        foreach (var resource in possibleReadable)
                        {
                            includeInReadable.AddRange(
                                readable.Where(wr => wr.IsDerivedFrom(resource))
                                .Select(wr => resource));
                        }
                    }

                    return(new
                    {
                        ProfileName = x.ProfileName,
                        Readable = x.ProfileResourceModel.Resources
                                   .Where(y => y.Readable != null)
                                   .Select(y => y.Readable)
                                   .Concat(
                            includeInReadable)                 // Added to ensure that the pipeline step, which defines 2 generic parameters for readable and writable, has a type available
                                   .ToList(),
                        Writable = x.ProfileResourceModel.Resources
                                   .Where(y => y.Writable != null)
                                   .Select(y => y.Writable)
                                   .Concat(
                            includeInWritable)                 // Added to ensure that the pipeline step, which defines 2 generic parameters for readable and writable, has a type available
                                   .ToList()
                    });
                });

                var finalReadablesAndWritables =
                    readablesAndWritables.Select(
                        prm =>
                {
                    return(new
                    {
                        ProfileName = prm.ProfileName,
                        Readable = prm.Readable
                                   .SelectMany(
                            pct =>
                            CreateProfileModel(
                                pct,
                                prm.ProfileName,
                                ReadableContext,
                                true))
                                   .ToList(),
                        Writable = prm.Writable
                                   .SelectMany(
                            pct => CreateProfileModel(
                                pct,
                                prm.ProfileName,
                                WritableContext,
                                false))
                                   .ToList()
                    });
                })
                    .SelectMany(
                        y => y.Readable.OrderBy(q => q.ResourceName)
                        .Concat(y.Writable.OrderBy(q => q.ResourceName)))
                    .ToList();

                return(finalReadablesAndWritables);
            }

            // sorting for template comparison.
            return(ResourceModel
                   .GetAllResources()
                   .Select(
                       resource =>
                       new ResourceProfileData
            {
                HasProfile = ProjectHasProfileDefinition,
                SuppliedResource = resource,
                UnfilteredResource = (Resource)resource.FilterContext?.UnfilteredResourceClass ?? resource,
                ContextualRootResource = resource
            })
                   .OrderBy(x => x.ResourceName));
        }