private OpenApiPaths BuildPaths()

in src/Core/Services/OpenAPI/OpenApiDocumentor.cs [181:284]


        private OpenApiPaths BuildPaths(RuntimeEntities entities, string defaultDataSourceName)
        {
            OpenApiPaths pathsCollection = new();

            ISqlMetadataProvider metadataProvider = _metadataProviderFactory.GetMetadataProvider(defaultDataSourceName);
            foreach (KeyValuePair<string, DatabaseObject> entityDbMetadataMap in metadataProvider.EntityToDatabaseObject)
            {
                string entityName = entityDbMetadataMap.Key;
                if (!entities.ContainsKey(entityName))
                {
                    // This can happen for linking entities which are not present in runtime config.
                    continue;
                }

                string entityRestPath = GetEntityRestPath(entities[entityName].Rest, entityName);
                string entityBasePathComponent = $"/{entityRestPath}";

                DatabaseObject dbObject = entityDbMetadataMap.Value;
                SourceDefinition sourceDefinition = metadataProvider.GetSourceDefinition(entityName);

                // Entities which disable their REST endpoint must not be included in
                // the OpenAPI description document.
                if (entities.TryGetValue(entityName, out Entity? entity) && entity is not null)
                {
                    if (!entity.Rest.Enabled)
                    {
                        continue;
                    }
                }
                else
                {
                    continue;
                }

                // Explicitly exclude setting the tag's Description property since the Name property is self-explanatory.
                OpenApiTag openApiTag = new()
                {
                    Name = entityRestPath
                };

                // The OpenApiTag will categorize all paths created using the entity's name or overridden REST path value.
                // The tag categorization will instruct OpenAPI document visualization tooling to display all generated paths together.
                List<OpenApiTag> tags = new()
                {
                    openApiTag
                };

                Dictionary<OperationType, bool> configuredRestOperations = GetConfiguredRestOperations(entity, dbObject);

                if (dbObject.SourceType is EntitySourceType.StoredProcedure)
                {
                    Dictionary<OperationType, OpenApiOperation> operations = CreateStoredProcedureOperations(
                        entityName: entityName,
                        sourceDefinition: sourceDefinition,
                        configuredRestOperations: configuredRestOperations,
                        tags: tags);

                    OpenApiPathItem openApiPathItem = new()
                    {
                        Operations = operations
                    };

                    pathsCollection.TryAdd(entityBasePathComponent, openApiPathItem);
                }
                else
                {
                    // Create operations for SourceType.Table and SourceType.View
                    // Operations including primary key
                    Dictionary<OperationType, OpenApiOperation> pkOperations = CreateOperations(
                        entityName: entityName,
                        sourceDefinition: sourceDefinition,
                        includePrimaryKeyPathComponent: true,
                        tags: tags);

                    Tuple<string, List<OpenApiParameter>> pkComponents = CreatePrimaryKeyPathComponentAndParameters(entityName, metadataProvider);
                    string pkPathComponents = pkComponents.Item1;
                    string fullPathComponent = entityBasePathComponent + pkPathComponents;

                    OpenApiPathItem openApiPkPathItem = new()
                    {
                        Operations = pkOperations,
                        Parameters = pkComponents.Item2
                    };

                    pathsCollection.TryAdd(fullPathComponent, openApiPkPathItem);

                    // Operations excluding primary key
                    Dictionary<OperationType, OpenApiOperation> operations = CreateOperations(
                        entityName: entityName,
                        sourceDefinition: sourceDefinition,
                        includePrimaryKeyPathComponent: false,
                        tags: tags);

                    OpenApiPathItem openApiPathItem = new()
                    {
                        Operations = operations
                    };

                    pathsCollection.TryAdd(entityBasePathComponent, openApiPathItem);
                }
            }

            return pathsCollection;
        }