private IEnumerable <SwaggerPathsResource> GetContainedResources(
            SwaggerResource swaggerResource)
        {
            return(_swaggerDocumentContext.CompositeContext.RouteElements
                   .Where(
                       route =>
                       !IsDefaultRoute(route) &&
                       RouteAppliesToResource(route, swaggerResource))
                   .SelectMany(
                       d =>
            {
                var operationSpecSuffix = GetOperationSpecNickname(swaggerResource.Resource, d);

                var swaggerResources = new List <SwaggerPathsResource>();

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

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

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

                return swaggerResources;
            }));
        }
 protected override void Act()
 {
     _compositeResource =
         new OpenApiCompositeStrategy(_compositesMetadataProvider).GetFilteredResources(
             _swaggerDocumentContext)
         .FirstOrDefault();
 }
 private SwaggerPathsResource CreateGetByExampleEndpoint(
     SwaggerResource swagggerResource,
     XElement routeDefinition,
     bool withIdParameters = false)
 {
     return(new SwaggerPathsResource(swagggerResource.Resource)
     {
         Name = swagggerResource.Resource.Name,
         Path = ToSwaggerIdentifier($"/{GetResourcePathValue(swagggerResource, routeDefinition)}"), CompositeResourceContext =
             new CompositeResourceContext
         {
             OrganizationCode = _swaggerDocumentContext.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
     });
 }
        private IEnumerable <ResourceProperty> GetSwaggerEndpointGetByIdParameters(
            SwaggerResource swagggerResource,
            XElement routeDefinition)
        {
            string path = "/" + _swaggerDocumentContext.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());
        }
 public string GetCollectionReferenceName(SwaggerResource swaggerResource, Collection collection)
 {
     return(CreateCompositeChildModelTypeName(
                swaggerResource.Resource.Name,
                collection.ItemType.Name,
                collection.Parent));
 }
Ejemplo n.º 6
0
 private Schema CreateEmbeddedObjectSchema(EmbeddedObject embeddedObject, SwaggerResource swaggerResource)
 {
     return(new Schema
     {
         @ref = SwaggerDocumentHelper.GetDefinitionReference(
             _swaggerDefinitionsFactoryNamingStrategy.GetEmbeddedObjectReferenceName(swaggerResource, embeddedObject))
     });
 }
        public string GetContainedItemTypeName(
            SwaggerResource swaggerResource,
            ResourceChildItem resourceChildItem)
        {
            var schemaPrefix = resourceChildItem.SchemaProperCaseName;

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

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

            return(SwaggerDocumentHelper.CamelCaseSegments($"{schemaPrefix}_{resource.Name}"));
        }
Ejemplo n.º 12
0
        public string GetCollectionReferenceName(SwaggerResource swaggerResource, Collection collection)
        {
            var name = collection.IsDerivedFrom(swaggerResource.Resource)
                ? CreateChildModelTypeName(
                swaggerResource.Resource,
                collection.ItemType.Name,
                collection.Parent)
                : collection.ItemType.Name;

            return
                (SwaggerDocumentHelper.CamelCaseSegments(
                     $"{collection.ItemType.SchemaProperCaseName}_{name}_{swaggerResource.OperationNamingContext}"));
        }
Ejemplo n.º 13
0
        public string GetContainedItemTypeName(SwaggerResource swaggerResource, ResourceChildItem resourceChildItem)
        {
            var name = resourceChildItem.IsDerivedFrom(swaggerResource.Resource)
                ? CreateChildModelTypeName(
                swaggerResource.Resource,
                resourceChildItem.Name,
                resourceChildItem.Parent)
                : resourceChildItem.Name;

            return
                (SwaggerDocumentHelper.CamelCaseSegments(
                     $"{resourceChildItem.SchemaProperCaseName}_{name}_{swaggerResource.OperationNamingContext}"));
        }
        private string GetResourcePathValue(SwaggerResource swagggerResource, XElement routeDefinition)
        {
            string regexPattern = @"\{([^)]*)\}";

            var resourcePathValue = _swaggerDocumentContext.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()}}}"));
        }
        public string GetCollectionReferenceName(
            SwaggerResource swaggerResource,
            Collection collection)
        {
            var schemaPrefix = collection
                               .ItemType
                               .Entity
                               .DomainModel
                               .SchemaNameMapProvider
                               .GetSchemaMapByPhysicalName(collection.ItemType.Entity.Schema)
                               .ProperCaseName;

            return(SwaggerDocumentHelper.CamelCaseSegments($"{schemaPrefix}_{collection.ItemType.Name}"));
        }
        public string GetEmbeddedObjectReferenceName(
            SwaggerResource swaggerResource,
            EmbeddedObject embeddedObject)
        {
            var schemaPrefix = embeddedObject
                               .ObjectType
                               .Entity
                               .DomainModel
                               .SchemaNameMapProvider
                               .GetSchemaMapByPhysicalName(embeddedObject.ObjectType.Entity.Schema)
                               .ProperCaseName;

            return(SwaggerDocumentHelper.CamelCaseSegments($"{schemaPrefix}_{embeddedObject.ObjectType.Name}"));
        }
        private bool RouteAppliesToResource(
            XElement routeDefinition,
            SwaggerResource swaggerResource)
        {
            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(swaggerResource.CompositeResourceContext.Specification != null &&
                   swaggerResource.CompositeResourceContext.Specification
                   .Elements("Parameter")
                   .Any(
                       p => routeTemplateToTest
                       .Contains("{" + p.AttributeValue("name") + "}")));
        }
Ejemplo n.º 18
0
        private SwaggerResource GetGenerationContextForSwaggerResource(
            IList <Resource> resources,
            SwaggerResource swaggerResource,
            ContentTypeUsage contentTypeUsage)
        {
            if (!swaggerResource.Resource.IsBase())
            {
                return(null);
            }

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

            return(derivedResource == null
                ? null
                : new SwaggerResource(swaggerResource.Resource)
            {
                ContextualResource = derivedResource, Name = swaggerResource.Name, Readable = contentTypeUsage == ContentTypeUsage.Readable,
                Writable = contentTypeUsage == ContentTypeUsage.Writable, IsProfileResource = true
            });
        }
Ejemplo n.º 19
0
        public IList <SwaggerResource> Get()
        {
            SwaggerResource resource = new SwaggerResource
            {
                Location       = "/v2/api-docs",
                Name           = "default",
                SwaggerVersion = "2.0"
            };

            SwaggerResource resource2 = new SwaggerResource
            {
                Location       = "/gateway/v2/api-docs",
                Name           = "gateway",
                SwaggerVersion = "2.0"
            };

            GetApplicationDeployed();

            return(new List <SwaggerResource> {
                resource, resource2
            });
        }
        public string GetResourceName(SwaggerResource swaggerResource, ContentTypeUsage contentTypeUsage)
        {
            var schemaPrefix =
                swaggerResource.Resource.Entity.DomainModel.SchemaNameMapProvider
                .GetSchemaMapByPhysicalName(swaggerResource.Resource.Entity.Schema)
                .ProperCaseName;

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

            return(SwaggerDocumentHelper.CamelCaseSegments(name));
        }
Ejemplo n.º 21
0
        private SwaggerResource GetBaseResourceInProfile(
            IList <Resource> resources,
            SwaggerResource swaggerResource,
            ContentTypeUsage contentTypeUsage)
        {
            if (swaggerResource.Resource.IsBase())
            {
                return(null);
            }

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

            return(baseResource == null
                ? null
                : new SwaggerResource(baseResource)
            {
                Name = GetBaseResourceName(baseResource, swaggerResource.Resource, contentTypeUsage),
                Readable = contentTypeUsage == ContentTypeUsage.Readable, Writable = contentTypeUsage == ContentTypeUsage.Writable,
                IsProfileResource = true
            });
        }
 public string GetOperationContentType(SwaggerResource swaggerResource, ContentTypeUsage contentTypeUsage)
 => ProfilesContentTypeHelper.CreateContentType(
     swaggerResource.Resource.Name,
     _documentContext.ProfileContext.ProfileName,
     contentTypeUsage);
Ejemplo n.º 23
0
 public string GetOperationContentType(SwaggerResource swaggerResource, ContentTypeUsage contentTypeUsage)
 {
     return(SwaggerDocumentHelper.ContentType);
 }
        private IEnumerable <ResourceProperty> GetSwaggerEndpointGetByExampleParameters(SwaggerResource swagggerResource)
        {
            var resourceSpecification = swagggerResource.CompositeResourceContext.Specification;

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

            var swaggerQueryParameters =
                (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(swaggerQueryParameters.Concat(GetMetaDataPropertiesForCurrentResource(swagggerResource)));
        }
Ejemplo n.º 25
0
        private Schema CreateResourceSchema(SwaggerResource swaggerResource)
        {
            var resource = swaggerResource.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       = SwaggerDocumentHelper.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, swaggerResource)
            })).Concat(
                resource.EmbeddedObjects.Select(
                    x => new PropertySchemaInfo
            {
                PropertyName = x.JsonPropertyName,
                Sort         = SortOrder(x.PropertyName, false),
                Schema       = CreateEmbeddedObjectSchema(x, swaggerResource)
            })).Concat(
                resource.References.Select(
                    x => new PropertySchemaInfo
            {
                PropertyName = x.PropertyName,
                IsRequired   = x.IsRequired,
                IsReference  = true,
                Sort         = 3,
                Schema       = new Schema
                {
                    @ref = SwaggerDocumentHelper.GetDefinitionReference(
                        _swaggerDefinitionsFactoryNamingStrategy.GetReferenceName(swaggerResource.Resource, x))
                }

                // NOTE: currently there is an open issue with swagger-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 swagger-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, swaggerResource);

            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
            });
        }
        private IEnumerable <ResourceProperty> GetMetaDataPropertiesForCurrentResource(SwaggerResource swagggerResource)
        {
            var currentResourceBaseAndSpec = swagggerResource.CompositeResourceContext;

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

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

            return(resource
                   .AllProperties
                   .Where(p => p.EntityProperty?.IsIdentifying ?? false)
                   .Select(p => CreateResourcePropertyForQueryStringParameter(swagggerResource.Resource, p)));
        }
Ejemplo n.º 27
0
 public string GetEmbeddedObjectReferenceName(SwaggerResource swaggerResource, EmbeddedObject embeddedObject)
 {
     return
         (SwaggerDocumentHelper.CamelCaseSegments(
              $@"{embeddedObject.ObjectType.SchemaProperCaseName}_{embeddedObject.ObjectType.Name}_{swaggerResource.OperationNamingContext}"));
 }
Ejemplo n.º 28
0
        private Schema CreateResourceChildSchema(ResourceChildItem resourceChildItem, SwaggerResource swaggerResource)
        {
            var properties = resourceChildItem.Properties
                             .Select(
                p => new
            {
                IsRequired = p.IsIdentifying || !p.PropertyType.IsNullable,
                Key        = UniqueIdSpecification.GetUniqueIdPropertyName(p.JsonPropertyName).ToCamelCase(),
                Schema     = SwaggerDocumentHelper.CreatePropertySchema(p)
            }).Concat(
                resourceChildItem.References.Select(
                    r => new
            {
                r.IsRequired,
                Key    = r.PropertyName.ToCamelCase(),
                Schema = new Schema
                {
                    @ref = SwaggerDocumentHelper.GetDefinitionReference(
                        _swaggerDefinitionsFactoryNamingStrategy.GetReferenceName(swaggerResource.Resource, r))
                }
            })).Concat(
                resourceChildItem.Collections.Select(
                    c => new
            {
                IsRequired = c.Association.IsRequiredCollection,
                Key        = c.JsonPropertyName,
                Schema     = CreateCollectionSchema(c, swaggerResource)
            })).Concat(
                resourceChildItem.EmbeddedObjects.Select(
                    e => new
            {
                IsRequired = false,
                Key        = e.JsonPropertyName,
                Schema     = CreateEmbeddedObjectSchema(e, swaggerResource)
            })).ToList();

            var bridgeSchema = GetEdFiExtensionBridgeReferenceSchema(resourceChildItem, swaggerResource);

            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)
            });
        }
 public string GetResourceName(SwaggerResource swaggerResource, ContentTypeUsage contentTypeUsage)
 {
     return(swaggerResource.Resource.Name.ToCamelCase());
 }