Beispiel #1
0
        /// <summary>
        /// Create the Swagger path for the Edm entity.
        /// </summary>
        /// <param name="entitySet">The entity set.</param>
        /// <returns></returns>
        public static PathItem CreateSwaggerPathForEntity(IEdmEntitySet entitySet)
        {
            Contract.Requires(entitySet != null);
            Contract.Ensures(Contract.Result <PathItem>() != null);

            var keyParameters = new List <Parameter>();

            foreach (var key in entitySet.GetEntityType().GetKey())
            {
                Contract.Assume(key != null);
                string format;
                var    keyDefinition = key.GetPropertyType().GetDefinition() as IEdmPrimitiveType;
                Contract.Assume(keyDefinition != null);
                var type = GetPrimitiveTypeAndFormat(keyDefinition, out format);
                keyParameters.Parameter(key.Name, "path", "key: " + key.Name, type, true, format);
            }

            return(new PathItem
            {
                get = new Operation()
                      .Summary("Get entity from " + entitySet.Name + " by key.")
                      .OperationId(entitySet.Name + "_GetById")
                      .Description("Returns the entity with the key from " + entitySet.Name)
                      .Tags(entitySet.Name)
                      .Parameters(keyParameters.DeepClone()
                                  .Parameter("$expand", "query", "Expands related entities inline.", "string", false))
                      .Parameters(keyParameters.DeepClone()
                                  .Parameter("$select", "query", "Selects which properties to include in the response.", "string", false))
                      .Responses(new Dictionary <string, Response>().Response("200", "EntitySet " + entitySet.Name, entitySet.GetEntityType()).DefaultErrorResponse()),

                patch = new Operation()
                        .Summary("Update entity in EntitySet " + entitySet.Name)
                        .OperationId(entitySet.Name + "_PatchById")
                        .Description("Update entity in EntitySet " + entitySet.Name)
                        .Tags(entitySet.Name)
                        .Parameters(keyParameters.DeepClone()
                                    .Parameter(entitySet.GetEntityType().Name, "body", "The entity to patch", entitySet.GetEntityType()))
                        .Responses(new Dictionary <string, Response>()
                                   .Response("204", "Empty response").DefaultErrorResponse()),

                put = new Operation()
                      .Summary("Replace entity in EntitySet " + entitySet.Name)
                      .OperationId(entitySet.Name + "_PutById")
                      .Description("Replace entity in EntitySet " + entitySet.Name)
                      .Tags(entitySet.Name)
                      .Parameters(keyParameters.DeepClone()
                                  .Parameter(entitySet.GetEntityType().Name, "body", "The entity to put", entitySet.GetEntityType()))
                      .Responses(new Dictionary <string, Response>().Response("204", "Empty response").DefaultErrorResponse()),

                delete = new Operation().Summary("Delete entity in EntitySet " + entitySet.Name)
                         .OperationId(entitySet.Name + "_DeleteById")
                         .Description("Delete entity in EntitySet " + entitySet.Name)
                         .Tags(entitySet.Name)
                         .Parameters(keyParameters.DeepClone()
                                     .Parameter("If-Match", "header", "If-Match header", "string", false))
                         .Responses(new Dictionary <string, Response>().Response("204", "Empty response").DefaultErrorResponse())
            });
        }
        private static string AppendSingleColumnKeyTemplate(IEdmEntitySet entitySet, string singleEntityPath, ODataRoute oDataRoute)
        {
            Contract.Requires(entitySet.GetEntityType().GetKey().Count() == 1);
            Contract.Ensures(Contract.Result <string>() != null);

            var key = entitySet.GetEntityType().GetKey().Single();

            singleEntityPath += GetParameterPathAssignment(key.Name, key.Type, false, oDataRoute);
            return(singleEntityPath);
        }
Beispiel #3
0
        private static string AppendSingleColumnKeyTemplate(IEdmEntitySet entitySet, string singleEntityPath)
        {
            Contract.Requires(entitySet.GetEntityType().GetKey().Count() == 1);
            Contract.Ensures(Contract.Result <string>() != null);

            var key = entitySet.GetEntityType().GetKey().Single();

            singleEntityPath += "{" + key.Name + "}, ";
            return(singleEntityPath);
        }
        /// <summary>
        /// Create the Swagger path for the Edm entity set.
        /// </summary>
        /// <param name="entitySet">The entity set.</param>
        /// <param name="oDataRoute"></param>
        public static PathItem CreateSwaggerPathForEntitySet(IEdmEntitySet entitySet, ODataRoute oDataRoute)
        {
            Contract.Requires(entitySet != null);
            Contract.Ensures(Contract.Result <PathItem>() != null);

            return(new PathItem
            {
                get = new Operation()
                      .Summary("Get EntitySet " + entitySet.Name)
                      .OperationId(entitySet.Name + "_Get")
                      .Description("Returns the EntitySet " + entitySet.Name)
                      .Tags(entitySet.Name)
                      .Parameters(AddQueryOptionParametersForEntitySet(new List <Parameter>()))
                      .Parameters(AddRoutePrefixParameters(oDataRoute))
                      .Responses(new Dictionary <string, Response>().Response("200", "EntitySet " + entitySet.Name, entitySet.Type).DefaultErrorResponse()),
                post = new Operation()
                       .Summary("Post a new entity to EntitySet " + entitySet.Name)
                       .OperationId(entitySet.Name + "_Post")
                       .Description("Post a new entity to EntitySet " + entitySet.Name)
                       .Tags(entitySet.Name)
                       .Parameters(new List <Parameter>()
                                   .Parameter(entitySet.GetEntityType().Name, "body", "The entity to post", entitySet.GetEntityType(), true))
                       .Parameters(AddRoutePrefixParameters(oDataRoute))
                       .Responses(new Dictionary <string, Response>().Response("200", "EntitySet " + entitySet.Name, entitySet.GetEntityType()).DefaultErrorResponse())
            });
        }
        /// <summary>
        /// Create the Swagger path for the Edm operation bound to the Edm entity.
        /// </summary>
        /// <param name="operation">The Edm operation.</param>
        /// <param name="entitySet">The entity set.</param>
        /// <param name="oDataRoute">The OData route.</param>
        /// <returns></returns>
        public static PathItem CreateSwaggerPathForOperationOfEntity(IEdmOperation operation, IEdmEntitySet entitySet, ODataRoute oDataRoute)
        {
            Contract.Requires(operation != null);
            Contract.Requires(entitySet != null);

            var isFunction        = operation is IEdmFunction;
            var swaggerParameters = new List <Parameter>();

            foreach (var key in entitySet.GetEntityType().GetKey())
            {
                Contract.Assume(key != null);
                string format;
                var    edmPrimitiveType = key.GetPropertyType().GetDefinition() as IEdmPrimitiveType;
                Contract.Assume(edmPrimitiveType != null);
                var type = GetPrimitiveTypeAndFormat(edmPrimitiveType, out format);
                swaggerParameters.Parameter(key.Name, "path", "key: " + key.Name, type, true, format);
            }

            var edmOperationParameters = operation.Parameters;

            Contract.Assume(edmOperationParameters != null);

            if (isFunction)
            {
                AddSwaggerParametersForFunction(swaggerParameters, operation);
            }
            else
            {
                AddSwaggerParametersForAction(swaggerParameters, operation);
            }

            var swaggerResponses = new Dictionary <string, Response>();

            if (operation.ReturnType != null)
            {
                swaggerResponses.Response("200", "Response from " + operation.Name, operation.ReturnType.GetDefinition());
            }

            var swaggerOperation = new Operation()
                                   .Summary("Call operation  " + operation.Name)
                                   .Description("Call operation  " + operation.Name)
                                   .Parameters(AddRoutePrefixParameters(oDataRoute))
                                   .Tags(entitySet.Name, isFunction ? "Function" : "Action");

            if (swaggerParameters.Count > 0)
            {
                swaggerOperation.Parameters(swaggerParameters);
            }
            swaggerOperation.Responses(swaggerResponses.DefaultErrorResponse());

            return(isFunction ? new PathItem
            {
                get = swaggerOperation
            } : new PathItem
            {
                post = swaggerOperation
            });
        }
        private static string AppendMultiColumnKeyTemplate(IEdmEntitySet entitySet, string singleEntityPath, ODataRoute oDataRoute)
        {
            Contract.Ensures(Contract.Result <string>() != null);

            foreach (var key in entitySet.GetEntityType().GetKey())
            {
                Contract.Assume(key != null);
                singleEntityPath += GetParameterPathAssignment(key.Name, key.Type, true, oDataRoute);
            }
            Contract.Assume(singleEntityPath != null);
            return(singleEntityPath);
        }
Beispiel #7
0
        private static string AppendMultiColumnKeyTemplate(IEdmEntitySet entitySet, string singleEntityPath)
        {
            Contract.Ensures(Contract.Result <string>() != null);

            foreach (var key in entitySet.GetEntityType().GetKey())
            {
                Contract.Assume(key != null);
                singleEntityPath += key.Name + "={" + key.Name + "}, ";
            }
            Contract.Assume(singleEntityPath != null);
            return(singleEntityPath);
        }
        /// <summary>
        /// Get the Uri Swagger path for the Edm entity set.
        /// </summary>
        /// <param name="entitySet">The entity set.</param>
        /// <param name="oDataRoute">The OData route.</param>
        /// <returns>
        /// The <see cref="System.String" /> path represents the related Edm entity set.
        /// </returns>
        public static string GetPathForEntity(IEdmEntitySet entitySet, ODataRoute oDataRoute)
        {
            Contract.Requires(entitySet != null);

            var singleEntityPath = GetPathForEntitySet(entitySet) + "(";

            singleEntityPath = entitySet.GetEntityType().GetKey().Count() == 1
                ? AppendSingleColumnKeyTemplate(entitySet, singleEntityPath, oDataRoute)
                : AppendMultiColumnKeyTemplate(entitySet, singleEntityPath, oDataRoute);
            Contract.Assume(singleEntityPath.Length - EntityPathSubStrOffset >= 0);
            singleEntityPath  = singleEntityPath.Substring(0, singleEntityPath.Length - EntityPathSubStrOffset);
            singleEntityPath += ")";

            return(singleEntityPath);
        }
Beispiel #9
0
        /// <summary>
        /// Get the Uri Swagger path for the Edm entity set.
        /// </summary>
        /// <param name="routePrefix">The route prefix.</param>
        /// <param name="entitySet">The entity set.</param>
        /// <returns>
        /// The <see cref="System.String" /> path represents the related Edm entity set.
        /// </returns>
        public static string GetPathForEntity(string routePrefix, IEdmEntitySet entitySet)
        {
            Contract.Requires(routePrefix != null);
            Contract.Requires(entitySet != null);

            var singleEntityPath = GetPathForEntitySet(routePrefix, entitySet) + "(";

            singleEntityPath = entitySet.GetEntityType().GetKey().Count() == 1
                ? AppendSingleColumnKeyTemplate(entitySet, singleEntityPath)
                : AppendMultiColumnKeyTemplate(entitySet, singleEntityPath);
            Contract.Assume(singleEntityPath.Length - 2 >= 0);
            singleEntityPath  = singleEntityPath.Substring(0, singleEntityPath.Length - 2);
            singleEntityPath += ")";

            return(singleEntityPath);
        }
        /// <summary>
        /// Create the Swagger path for the Edm entity.
        /// </summary>
        /// <param name="entitySet">The entity set.</param>
        /// <param name="oDataRoute">The OData route.</param>
        /// <returns></returns>
        public static PathItem CreateSwaggerPathForEntity(IEdmEntitySet entitySet, ODataRoute oDataRoute)
        {
            Contract.Requires(entitySet != null);
            Contract.Ensures(Contract.Result <PathItem>() != null);

            var keyParameters = new List <Parameter>();

            foreach (var key in entitySet.GetEntityType().GetKey())
            {
                Contract.Assume(key != null);

                // Create key parameters for primitive and enum types
                IEdmType keyDefinitionAsType = null;
                var      keyDefinition       = key.GetPropertyType().GetDefinition();

                if (EdmTypeKind.Primitive == keyDefinition.TypeKind)
                {
                    keyDefinitionAsType = keyDefinition as IEdmPrimitiveType;
                }
                else if (EdmTypeKind.Enum == keyDefinition.TypeKind)
                {
                    keyDefinitionAsType = keyDefinition as IEdmEnumType;
                }
                Contract.Assume(keyDefinitionAsType != null);

                keyParameters.Parameter(
                    key.Name,
                    "path",
                    "key: " + key.Name,
                    keyDefinitionAsType,
                    true);
            }
            return(new PathItem
            {
                get = new Operation()
                      .Summary("Get entity from " + entitySet.Name + " by key.")
                      .OperationId(entitySet.Name + "_GetById")
                      .Description("Returns the entity with the key from " + entitySet.Name)
                      .Tags(entitySet.Name)
                      .Parameters(AddQueryOptionParametersForEntity(keyParameters.DeepClone()))
                      .Parameters(AddRoutePrefixParameters(oDataRoute))
                      .Responses(new Dictionary <string, Response>().Response("200", "EntitySet " + entitySet.Name, entitySet.GetEntityType()).DefaultErrorResponse()),

                patch = new Operation()
                        .Summary("Update entity in EntitySet " + entitySet.Name)
                        .OperationId(entitySet.Name + "_PatchById")
                        .Description("Update entity in EntitySet " + entitySet.Name)
                        .Tags(entitySet.Name)
                        .Parameters(keyParameters.DeepClone()
                                    .Parameter(entitySet.GetEntityType().Name, "body", "The entity to patch", entitySet.GetEntityType(), true))
                        .Parameters(AddRoutePrefixParameters(oDataRoute))
                        .Responses(new Dictionary <string, Response>()
                                   .Response("204", "Empty response").DefaultErrorResponse()),

                put = new Operation()
                      .Summary("Replace entity in EntitySet " + entitySet.Name)
                      .OperationId(entitySet.Name + "_PutById")
                      .Description("Replace entity in EntitySet " + entitySet.Name)
                      .Tags(entitySet.Name)
                      .Parameters(keyParameters.DeepClone()
                                  .Parameter(entitySet.GetEntityType().Name, "body", "The entity to put", entitySet.GetEntityType(), true))
                      .Parameters(AddRoutePrefixParameters(oDataRoute))
                      .Responses(new Dictionary <string, Response>().Response("204", "Empty response").DefaultErrorResponse()),

                delete = new Operation()
                         .Summary("Delete entity in EntitySet " + entitySet.Name)
                         .OperationId(entitySet.Name + "_DeleteById")
                         .Description("Delete entity in EntitySet " + entitySet.Name)
                         .Tags(entitySet.Name)
                         .Parameters(keyParameters.DeepClone()
                                     .Parameter("If-Match", "header", "If-Match header", "string", false))
                         .Parameters(AddRoutePrefixParameters(oDataRoute))
                         .Responses(new Dictionary <string, Response>().Response("204", "Empty response").DefaultErrorResponse())
            });
        }