// ------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. // ------------------------------------------------------------ using System.Collections.Generic; using System.Linq; using Microsoft.OData.Edm; using Microsoft.OpenApi.Models; using Microsoft.OpenApi.OData.Common; using Microsoft.OpenApi.OData.Generator; using Microsoft.OpenApi.OData.Operation; using Microsoft.OpenApi.OData.PathItem; using Microsoft.OpenApi.OData.Vocabulary.Capabilities; namespace Microsoft.OpenApi.OData.Edm { /// /// Context information for the , configuration, etc. /// internal class ODataContext { private IEnumerable _allPaths; private IODataPathProvider _pathProvider; /// /// Initializes a new instance of class. /// /// The Edm model. public ODataContext(IEdmModel model) : this(model, new OpenApiConvertSettings()) { } /// /// Initializes a new instance of class. /// /// The Edm model. /// The convert setting. public ODataContext(IEdmModel model, OpenApiConvertSettings settings) { Model = model ?? throw Error.ArgumentNull(nameof(model)); Settings = settings ?? throw Error.ArgumentNull(nameof(settings)); EdmSpatialTypeVisitor visitor = new EdmSpatialTypeVisitor(); visitor.Visit(model); IsSpatialTypeUsed = visitor.IsSpatialTypeUsed; OperationHanderProvider = new OperationHandlerProvider(); PathItemHanderProvider = new PathItemHandlerProvider(); // If no path provider, use the default path provider. _pathProvider = settings.PathProvider ?? new ODataPathProvider(); if (settings.EnableKeyAsSegment != null) { // We have the global setting, use the global setting KeyAsSegment = settings.EnableKeyAsSegment.Value; } else { KeyAsSegment = false; if (model.EntityContainer != null) { var keyAsSegment = model.GetBoolean(model.EntityContainer, CapabilitiesConstants.KeyAsSegmentSupported); if (keyAsSegment != null) { KeyAsSegment = keyAsSegment.Value; } } } } /// /// Gets the path item handler provider. /// public IPathItemHandlerProvider PathItemHanderProvider { get; } /// /// Gets the operation handler provider. /// public IOperationHandlerProvider OperationHanderProvider { get; } /// /// Gets the Edm model. /// public IEdmModel Model { get; } /// /// Gets the Entity Container. /// public IEdmEntityContainer EntityContainer { get { return Model.EntityContainer; } } /// /// Gets the s. /// public IEnumerable AllPaths { get { if (_allPaths == null) { _allPaths = LoadAllODataPaths().ToList(); } return _allPaths; } } /// /// Gets the boolean value indicating to support key as segment. /// public bool KeyAsSegment { get; } /// /// Gets the value indicating the Edm spatial type used. /// public bool IsSpatialTypeUsed { get; private set; } /// /// Gets the convert settings. /// public OpenApiConvertSettings Settings { get; } /// /// Gets all tags. /// public IList Tags { get; private set; } /// /// Append tag. /// /// The tag item. public void AppendTag(OpenApiTag tagItem) { if (Tags == null) { Tags = new List(); } if (Tags.Any(c => c.Name == tagItem.Name)) { return; } Tags.Add(tagItem); } /// /// Gets all OData paths /// /// All acceptable OData path. private IEnumerable LoadAllODataPaths() { IEnumerable allPaths = _pathProvider.GetPaths(Model, Settings); foreach (var path in allPaths) { if ((path.Kind == ODataPathKind.Operation && !Settings.EnableOperationPath) || (path.Kind == ODataPathKind.OperationImport && !Settings.EnableOperationImportPath) || (path.Kind == ODataPathKind.NavigationProperty && !Settings.EnableNavigationPropertyPath)) { continue; } yield return path; } } } }