private OpenApiMetadataPathsResource CreateGetByExampleEndpoint(
     OpenApiMetadataResource swagggerResource,
     XElement routeDefinition,
     bool withIdParameters = false)
 {
     return(new OpenApiMetadataPathsResource(swagggerResource.Resource)
     {
         Name = swagggerResource.Resource.Name,
         Path = ToSwaggerIdentifier($"/{GetResourcePathValue(swagggerResource, routeDefinition)}"),
         CompositeResourceContext =
             new CompositeResourceContext
         {
             OrganizationCode = _openApiMetadataDocumentContext.CompositeContext.OrganizationCode,
             BaseResource = swagggerResource.Name,
             Specification = routeDefinition
         },
         OperationId =
             $"get{swagggerResource.Resource.PluralName}{GetOperationSpecNickname(swagggerResource.Resource, routeDefinition)}",
         RequestProperties =
             GetSwaggerEndpointGetByExampleParameters(swagggerResource)
             .Concat(
                 withIdParameters
                                ? GetSwaggerEndpointGetByIdParameters(
                     swagggerResource,
                     routeDefinition)
                                : Enumerable.Empty <ResourceProperty>()),
         Readable = true,
         Writable = false
     });
 }
 protected override void Act()
 {
     _compositeResource =
         new OpenApiCompositeStrategy(_compositesMetadataProvider).GetFilteredResources(
             _openApiMetadataDocumentContext)
         .FirstOrDefault();
 }
        private IEnumerable <ResourceProperty> GetSwaggerEndpointGetByIdParameters(
            OpenApiMetadataResource swagggerResource,
            XElement routeDefinition)
        {
            string path = "/" + _openApiMetadataDocumentContext.CompositeContext.CategoryName
                          + routeDefinition.AttributeValue("relativeRouteTemplate")
                          .Replace(
                "{compositeName}",
                swagggerResource.Resource.PluralName.ToCamelCase());

            var matches = Regex.Matches(path, @"\{(?<Parameter>[\w\.]+)\}");

            return
                ((from m in matches.Cast <Match>()
                  select new ResourceProperty(
                      swagggerResource.Resource,
                      ToSwaggerIdentifier(
                          m.Groups["Parameter"]
                          .Value),
                      new PropertyType(DbType.String),
                      new PropertyCharacteristics(
                          false,
                          false,
                          false,
                          false,
                          false,
                          new FullName(
                              EdFiConventions.PhysicalSchemaName,
                              swagggerResource.Resource.Name)),
                      swagggerResource.Description))
                 .ToList());
        }
Exemplo n.º 4
0
 public string GetCollectionReferenceName(OpenApiMetadataResource openApiMetadataResource, Collection collection)
 {
     return(CreateCompositeChildModelTypeName(
                openApiMetadataResource.Resource.Name,
                collection.ItemType.Name,
                collection.Parent));
 }
        private IEnumerable <OpenApiMetadataPathsResource> GetContainedResources(
            OpenApiMetadataResource openApiMetadataResource)
        {
            return(_openApiMetadataDocumentContext.CompositeContext.RouteElements
                   .Where(
                       route =>
                       !IsDefaultRoute(route) &&
                       RouteAppliesToResource(route, openApiMetadataResource))
                   .SelectMany(
                       d =>
            {
                var operationSpecSuffix = GetOperationSpecNickname(openApiMetadataResource.Resource, d);

                var openApiMetadataResources = new List <OpenApiMetadataPathsResource>();

                if (operationSpecSuffix.EqualsIgnoreCase("All"))
                {
                    openApiMetadataResources.Add(CreateGetByExampleEndpoint(openApiMetadataResource, d));
                    openApiMetadataResources.Add(CreateGetByIdEndpoint(openApiMetadataResource, d));
                }

                if (!operationSpecSuffix.StartsWith("By"))
                {
                    return openApiMetadataResources;
                }

                openApiMetadataResources.Add(
                    operationSpecSuffix.Equals("ById")
                                ? CreateGetByIdEndpoint(openApiMetadataResource, d)
                                : CreateGetByExampleEndpoint(openApiMetadataResource, d, true));

                return openApiMetadataResources;
            }));
        }
 private Schema CreateEmbeddedObjectSchema(EmbeddedObject embeddedObject, OpenApiMetadataResource openApiMetadataResource)
 {
     return(new Schema
     {
         @ref = OpenApiMetadataDocumentHelper.GetDefinitionReference(
             _openApiMetadataDefinitionsFactoryNamingStrategy.GetEmbeddedObjectReferenceName(openApiMetadataResource, embeddedObject))
     });
 }
Exemplo n.º 7
0
        public string GetContainedItemTypeName(
            OpenApiMetadataResource openApiMetadataResource,
            ResourceChildItem resourceChildItem)
        {
            var schemaPrefix = resourceChildItem.SchemaProperCaseName;

            return(OpenApiMetadataDocumentHelper.CamelCaseSegments($"{schemaPrefix}_{resourceChildItem.Name}"));
        }
Exemplo n.º 8
0
 public string GetContainedItemTypeName(
     OpenApiMetadataResource openApiMetadataResource,
     ResourceChildItem resourceChildItem)
 {
     return(CreateCompositeChildModelTypeName(
                openApiMetadataResource.Resource.Name,
                resourceChildItem.Name,
                resourceChildItem.Parent));
 }
Exemplo n.º 9
0
 public string GetEmbeddedObjectReferenceName(
     OpenApiMetadataResource openApiMetadataResource,
     EmbeddedObject embeddedObject)
 {
     return(CreateCompositeChildModelTypeName(
                openApiMetadataResource.Resource.Name,
                embeddedObject.ObjectType.Name,
                embeddedObject.Parent));
 }
 private Schema CreateCollectionSchema(Collection collection, OpenApiMetadataResource openApiMetadataResource)
 {
     return(new Schema
     {
         type = "array",
         description = CollectionDescription(
             collection.ItemType.PluralName.ToCamelCase(), collection.ItemType.Description.ScrubForOpenApi()),
         items = RefSchema(
             _openApiMetadataDefinitionsFactoryNamingStrategy.GetCollectionReferenceName(openApiMetadataResource, collection))
     });
 }
        public string GetResourceName(OpenApiMetadataResource openApiMetadataResource, ContentTypeUsage contentTypeUsage)
        {
            var resource = openApiMetadataResource.Resource;

            var schemaPrefix =
                resource.Entity.DomainModel.SchemaNameMapProvider.GetSchemaMapByPhysicalName(
                    resource.Entity.Schema)
                .ProperCaseName;

            return(OpenApiMetadataDocumentHelper.CamelCaseSegments($"{schemaPrefix}_{resource.Name}"));
        }
        public string GetContainedItemTypeName(OpenApiMetadataResource openApiMetadataResource, ResourceChildItem resourceChildItem)
        {
            var name = resourceChildItem.IsDerivedFrom(openApiMetadataResource.Resource)
                ? CreateChildModelTypeName(
                openApiMetadataResource.Resource,
                resourceChildItem.Name,
                resourceChildItem.Parent)
                : resourceChildItem.Name;

            return
                (OpenApiMetadataDocumentHelper.CamelCaseSegments(
                     $"{resourceChildItem.SchemaProperCaseName}_{name}_{openApiMetadataResource.OperationNamingContext}"));
        }
        public string GetCollectionReferenceName(OpenApiMetadataResource openApiMetadataResource, Collection collection)
        {
            var name = collection.IsDerivedFrom(openApiMetadataResource.Resource)
                ? CreateChildModelTypeName(
                openApiMetadataResource.Resource,
                collection.ItemType.Name,
                collection.Parent)
                : collection.ItemType.Name;

            return
                (OpenApiMetadataDocumentHelper.CamelCaseSegments(
                     $"{collection.ItemType.SchemaProperCaseName}_{name}_{openApiMetadataResource.OperationNamingContext}"));
        }
        private string GetResourcePathValue(OpenApiMetadataResource swagggerResource, XElement routeDefinition)
        {
            string regexPattern = @"\{([^)]*)\}";

            var resourcePathValue = _openApiMetadataDocumentContext.CompositeContext.CategoryName
                                    + routeDefinition.AttributeValue("relativeRouteTemplate")
                                    .Replace("{compositeName}", swagggerResource.Resource.PluralName.ToCamelCase());

            var parameterValue = Regex.Match(resourcePathValue, regexPattern)
                                 .Groups[1]
                                 .Value;

            return(Regex.Replace(resourcePathValue, regexPattern, $"{{{parameterValue.ToCamelCase()}}}"));
        }
Exemplo n.º 15
0
        public string GetEmbeddedObjectReferenceName(
            OpenApiMetadataResource openApiMetadataResource,
            EmbeddedObject embeddedObject)
        {
            var schemaPrefix = embeddedObject
                               .ObjectType
                               .Entity
                               .DomainModel
                               .SchemaNameMapProvider
                               .GetSchemaMapByPhysicalName(embeddedObject.ObjectType.Entity.Schema)
                               .ProperCaseName;

            return(OpenApiMetadataDocumentHelper.CamelCaseSegments($"{schemaPrefix}_{embeddedObject.ObjectType.Name}"));
        }
Exemplo n.º 16
0
        public string GetCollectionReferenceName(
            OpenApiMetadataResource openApiMetadataResource,
            Collection collection)
        {
            var schemaPrefix = collection
                               .ItemType
                               .Entity
                               .DomainModel
                               .SchemaNameMapProvider
                               .GetSchemaMapByPhysicalName(collection.ItemType.Entity.Schema)
                               .ProperCaseName;

            return(OpenApiMetadataDocumentHelper.CamelCaseSegments($"{schemaPrefix}_{collection.ItemType.Name}"));
        }
        private bool RouteAppliesToResource(
            XElement routeDefinition,
            OpenApiMetadataResource openApiMetadataResource)
        {
            string routeTemplateToTest = routeDefinition.AttributeValue("relativeRouteTemplate");

            // ASSUMPTION: Routes will only contain a single GUID parameter. If there are multiple parameters
            // in the route, then the logic will have to be rewritten to drive off the route template's contents
            // to identify all the parameters that need to be defined in the specification, and fail to match the
            // route if any of the route's parameters aren't defined in the resource specification.

            // Get the specification parameters
            // Match this route if any of the parameters are present
            return(openApiMetadataResource.CompositeResourceContext.Specification != null &&
                   openApiMetadataResource.CompositeResourceContext.Specification
                   .Elements("Parameter")
                   .Any(
                       p => routeTemplateToTest
                       .Contains("{" + p.AttributeValue("name") + "}")));
        }
Exemplo n.º 18
0
        public string GetResourceName(OpenApiMetadataResource openApiMetadataResource, ContentTypeUsage contentTypeUsage)
        {
            var schemaPrefix =
                openApiMetadataResource.Resource.Entity.DomainModel.SchemaNameMapProvider
                .GetSchemaMapByPhysicalName(openApiMetadataResource.Resource.Entity.Schema)
                .ProperCaseName;

            var name = openApiMetadataResource.ContextualResource == null
                ? string.Format(
                "{0}_{1}_{2}",
                schemaPrefix,
                CompositeTermInflector.MakeSingular(openApiMetadataResource.Resource.Name),
                contentTypeUsage)
                : string.Format(
                "{0}_{1}_{2}_{3}",
                schemaPrefix,
                CompositeTermInflector.MakeSingular(openApiMetadataResource.Resource.Name),
                openApiMetadataResource.ContextualResource.Name,
                contentTypeUsage);

            return(OpenApiMetadataDocumentHelper.CamelCaseSegments(name));
        }
Exemplo n.º 19
0
        private OpenApiMetadataResource GetBaseResourceInProfile(
            IList <Resource> resources,
            OpenApiMetadataResource openApiMetadataResource,
            ContentTypeUsage contentTypeUsage)
        {
            if (openApiMetadataResource.Resource.IsBase())
            {
                return(null);
            }

            var baseResource =
                resources.FirstOrDefault(r => openApiMetadataResource.Resource.IsDerivedFrom(r));

            return(baseResource == null
                ? null
                : new OpenApiMetadataResource(baseResource)
            {
                Name = GetBaseResourceName(baseResource, openApiMetadataResource.Resource, contentTypeUsage),
                Readable = contentTypeUsage == ContentTypeUsage.Readable,
                Writable = contentTypeUsage == ContentTypeUsage.Writable,
                IsProfileResource = true
            });
        }
Exemplo n.º 20
0
        private OpenApiMetadataResource GetGenerationContextForSwaggerResource(
            IList <Resource> resources,
            OpenApiMetadataResource openApiMetadataResource,
            ContentTypeUsage contentTypeUsage)
        {
            if (!openApiMetadataResource.Resource.IsBase())
            {
                return(null);
            }

            var derivedResource =
                resources.FirstOrDefault(r => r.IsDerivedFrom(openApiMetadataResource.Resource));

            return(derivedResource == null
                ? null
                : new OpenApiMetadataResource(openApiMetadataResource.Resource)
            {
                ContextualResource = derivedResource,
                Name = openApiMetadataResource.Name,
                Readable = contentTypeUsage == ContentTypeUsage.Readable,
                Writable = contentTypeUsage == ContentTypeUsage.Writable,
                IsProfileResource = true
            });
        }
 public string GetResourceName(OpenApiMetadataResource openApiMetadataResource, ContentTypeUsage contentTypeUsage)
 {
     return(openApiMetadataResource.Resource.Name.ToCamelCase());
 }
 public string GetEmbeddedObjectReferenceName(OpenApiMetadataResource openApiMetadataResource, EmbeddedObject embeddedObject)
 {
     return
         (OpenApiMetadataDocumentHelper.CamelCaseSegments(
              $@"{embeddedObject.ObjectType.SchemaProperCaseName}_{embeddedObject.ObjectType.Name}_{openApiMetadataResource.OperationNamingContext}"));
 }
        private IEnumerable <ResourceProperty> GetSwaggerEndpointGetByExampleParameters(OpenApiMetadataResource swagggerResource)
        {
            var resourceSpecification = swagggerResource.CompositeResourceContext.Specification;

            var queryParameters = resourceSpecification.Elements("Parameter")
                                  .Where(
                e => e.AttributeValue("queryable")
                .EqualsIgnoreCase("true"))
                                  .ToList();

            var openApiMetadataQueryParameters =
                (from p in queryParameters
                 where swagggerResource.Resource != null
                 select
                 new ResourceProperty(
                     swagggerResource.Resource,
                     ToSwaggerIdentifier(p.AttributeValue("name")),
                     new PropertyType(
                         GetSwaggerTypeForQueryParameter(swagggerResource.Resource, p)
                         ?? DbType.String),
                     new PropertyCharacteristics(
                         false,
                         false,
                         false,
                         false,
                         false,
                         swagggerResource.Resource.FullName),
                     swagggerResource.Description ?? p.AttributeValue("description")))
                .ToList();

            return(openApiMetadataQueryParameters.Concat(GetMetaDataPropertiesForCurrentResource(swagggerResource)));
        }
        private IEnumerable <ResourceProperty> GetMetaDataPropertiesForCurrentResource(OpenApiMetadataResource openApiMetadataResource)
        {
            var currentResourceBaseAndSpec = openApiMetadataResource.CompositeResourceContext;

            //  This value will be calculated differently as a part of the Phase 4 work.
            var physicalName = EdFiConventions.PhysicalSchemaName;

            var resource =
                _openApiMetadataDocumentContext.ResourceModel.GetResourceByFullName(
                    new FullName(physicalName, currentResourceBaseAndSpec.BaseResource));

            return(resource
                   .AllProperties
                   .Where(p => p.EntityProperty?.IsIdentifying ?? false)
                   .Select(p => CreateResourcePropertyForQueryStringParameter(openApiMetadataResource.Resource, p)));
        }
Exemplo n.º 25
0
 public string GetOperationContentType(OpenApiMetadataResource openApiMetadataResource, ContentTypeUsage contentTypeUsage)
 => ProfilesContentTypeHelper.CreateContentType(
     openApiMetadataResource.Resource.Name,
     _documentContext.ProfileContext.ProfileName,
     contentTypeUsage);
        private Schema CreateResourceSchema(OpenApiMetadataResource openApiMetadataResource)
        {
            var resource = openApiMetadataResource.Resource;

            var properties = resource.UnifiedKeyAllProperties()
                             .Select(
                x => new PropertySchemaInfo
            {
                PropertyName = x.JsonPropertyName,
                IsRequired   = !x.PropertyType.IsNullable && !x.IsServerAssigned,
                Sort         = SortOrder(x.PropertyName, x.IsIdentifying),
                Schema       = OpenApiMetadataDocumentHelper.CreatePropertySchema(x)
            }).Concat(
                resource.Collections.Select(
                    x => new PropertySchemaInfo
            {
                PropertyName = CreateCollectionKey(x),
                IsRequired   = x.Association.IsRequiredCollection,
                Sort         = SortOrder(x.PropertyName, x.Association.IsRequiredCollection),
                Schema       = CreateCollectionSchema(x, openApiMetadataResource)
            })).Concat(
                resource.EmbeddedObjects.Select(
                    x => new PropertySchemaInfo
            {
                PropertyName = x.JsonPropertyName,
                Sort         = SortOrder(x.PropertyName, false),
                Schema       = CreateEmbeddedObjectSchema(x, openApiMetadataResource)
            })).Concat(
                resource.References.Select(
                    x => new PropertySchemaInfo
            {
                PropertyName = x.PropertyName,
                IsRequired   = x.IsRequired,
                IsReference  = true,
                Sort         = 3,
                Schema       = new Schema
                {
                    @ref = OpenApiMetadataDocumentHelper.GetDefinitionReference(
                        _openApiMetadataDefinitionsFactoryNamingStrategy.GetReferenceName(openApiMetadataResource.Resource, x))
                }

                // NOTE: currently there is an open issue with openApiMetadata-ui to address
                // objects showing up in the ui that have a reference and
                // other properties within the schema. The standard at this time does
                // not support this use case. (The error we get is:
                // Sibling values are not allowed along side $refs.
                // As of openApiMetadata-ui 3.11.0 this has not been resolved.
                // https://github.com/OAI/OpenAPI-Specification/issues/556
                // TODO: JSM - Once the standard is updated to accept sibling values along
                // side with $refs, uncomment the line below
                // isIdentity = x.Association.IsIdentifying ? true : (bool?) null
            })).Distinct(new PropertySchemaInfoComparer()).ToList();

            var propertyDict = properties.OrderBy(x => x.Sort).ThenBy(x => x.PropertyName)
                               .ToDictionary(x => x.PropertyName.ToCamelCase(), x => x.Schema);

            propertyDict.Add("_etag", EtagSchema);
            var bridgeSchema = GetEdFiExtensionBridgeReferenceSchema(resource, openApiMetadataResource);

            if (bridgeSchema != null)
            {
                propertyDict.Add(ExtensionCollectionKey, bridgeSchema);
            }

            var requiredProperties = properties
                                     .Where(x => x.IsRequired && !x.PropertyName.EqualsIgnoreCase("id"))
                                     .Select(x => x.PropertyName.ToCamelCase()).ToList();

            return(new Schema
            {
                type = "object",
                required = requiredProperties.Any()
                    ? requiredProperties
                    : null,
                properties = propertyDict
            });
        }
Exemplo n.º 27
0
 public string GetOperationContentType(OpenApiMetadataResource openApiMetadataResource, ContentTypeUsage contentTypeUsage)
 {
     return(OpenApiMetadataDocumentHelper.ContentType);
 }
        private Schema CreateResourceChildSchema(ResourceChildItem resourceChildItem, OpenApiMetadataResource openApiMetadataResource)
        {
            var properties = resourceChildItem.Properties
                             .Select(
                p => new
            {
                IsRequired = p.IsIdentifying || !p.PropertyType.IsNullable,
                Key        = UniqueIdSpecification.GetUniqueIdPropertyName(p.JsonPropertyName).ToCamelCase(),
                Schema     = OpenApiMetadataDocumentHelper.CreatePropertySchema(p)
            }).Concat(
                resourceChildItem.References.Select(
                    r => new
            {
                r.IsRequired,
                Key    = r.PropertyName.ToCamelCase(),
                Schema = new Schema
                {
                    @ref = OpenApiMetadataDocumentHelper.GetDefinitionReference(
                        _openApiMetadataDefinitionsFactoryNamingStrategy.GetReferenceName(openApiMetadataResource.Resource, r))
                }
            })).Concat(
                resourceChildItem.Collections.Select(
                    c => new
            {
                IsRequired = c.Association.IsRequiredCollection,
                Key        = c.JsonPropertyName,
                Schema     = CreateCollectionSchema(c, openApiMetadataResource)
            })).Concat(
                resourceChildItem.EmbeddedObjects.Select(
                    e => new
            {
                IsRequired = false,
                Key        = e.JsonPropertyName,
                Schema     = CreateEmbeddedObjectSchema(e, openApiMetadataResource)
            })).ToList();

            var bridgeSchema = GetEdFiExtensionBridgeReferenceSchema(resourceChildItem, openApiMetadataResource);

            if (bridgeSchema != null)
            {
                properties.Add(
                    new
                {
                    IsRequired = false,
                    Key        = ExtensionCollectionKey,
                    Schema     = bridgeSchema
                });
            }

            var requiredProperties = properties.Where(x => x.IsRequired).Select(x => x.Key).ToList();

            return(new Schema
            {
                type = "object",
                required = requiredProperties.Any()
                    ? requiredProperties
                    : null,
                properties = properties.ToDictionary(k => k.Key, v => v.Schema)
            });
        }