예제 #1
0
        private HttpServiceActionTransferObject ReadAction(HttpServiceActionTypeTransferObject actionType, Dictionary <IEdmType, TypeTransferObject> mapping, IEdmNavigationSource navigationSource)
        {
            HttpServiceActionTransferObject action = new HttpServiceActionTransferObject
            {
                Name  = actionType.ToString(),
                Type  = actionType,
                Route = "",
                RequireBodyParameter = false
            };

            switch (actionType)
            {
            case HttpServiceActionTypeTransferObject.Get:
                action.Parameters.Add(new HttpServiceActionParameterTransferObject {
                    Name = "query", Type = new TypeTransferObject {
                        Name = "Edm.String"
                    }, AppendName = false
                });
                action.ReturnType = this.ToTransferObject(navigationSource.Type, mapping);
                break;

            case HttpServiceActionTypeTransferObject.Post:
            case HttpServiceActionTypeTransferObject.Put:
                action.Parameters.Add(new HttpServiceActionParameterTransferObject {
                    Name = "model", FromBody = true, Type = this.ToTransferObject(navigationSource.Type, mapping, false)
                });
                action.ReturnType = new TypeTransferObject {
                    Name = "Edm.Void"
                };
                action.RequireBodyParameter = true;
                break;

            case HttpServiceActionTypeTransferObject.Patch:
                this.AddKeysToParameters(mapping, navigationSource, action);
                action.Parameters.Add(new HttpServiceActionParameterTransferObject {
                    Name = "model", FromBody = true, Type = this.ToTransferObject(navigationSource.Type, mapping, false)
                });
                action.ReturnType = new TypeTransferObject {
                    Name = "Edm.Void"
                };
                action.RequireBodyParameter = true;
                break;

            case HttpServiceActionTypeTransferObject.Delete:
                this.AddKeysToParameters(mapping, navigationSource, action);
                action.ReturnType = new TypeTransferObject {
                    Name = "Edm.Void"
                };
                break;
            }
            return(action);
        }
예제 #2
0
        private void Read(OpenApiReadConfiguration configuration, OpenApiDocument document)
        {
            HttpServiceTransferObject service = new HttpServiceTransferObject
            {
                Name = configuration.Name ?? document.Info.Description.Replace("Class ", "").Trim()
            };

            foreach (KeyValuePair <string, OpenApiPathItem> pathPair in document.Paths)
            {
                foreach (KeyValuePair <OperationType, OpenApiOperation> operationPair in pathPair.Value.Operations)
                {
                    HttpServiceActionTypeTransferObject type   = operationPair.Key.ToActionType();
                    HttpServiceActionTransferObject     action = new HttpServiceActionTransferObject
                    {
                        Name  = pathPair.Key.Split('/').Last(),
                        Route = pathPair.Key,
                        Type  = type,
                        RequireBodyParameter = type.IsBodyParameterRequired()
                    };
                    foreach (OpenApiParameter parameter in operationPair.Value.Parameters)
                    {
                        action.Parameters.Add(new HttpServiceActionParameterTransferObject
                        {
                            Name = parameter.Name,
                            Type = this.Read(configuration, parameter.Schema)
                        });
                    }
                    OpenApiMediaType content = operationPair.Value.RequestBody.Content.Single().Value;
                    if (action.RequireBodyParameter)
                    {
                        action.Parameters.Add(new HttpServiceActionParameterTransferObject
                        {
                            Name     = "request",
                            Type     = this.Read(configuration, content.Schema),
                            FromBody = true
                        });
                    }
                    else
                    {
                        foreach (KeyValuePair <string, OpenApiSchema> propertyPair in content.Schema.Properties)
                        {
                            action.Parameters.Add(new HttpServiceActionParameterTransferObject
                            {
                                Name = propertyPair.Key,
                                Type = this.Read(configuration, propertyPair.Value)
                            });
                        }
                    }
                    OpenApiResponse response = operationPair.Value.Responses.SingleOrDefault(x => x.Key == "200").Value;
                    if (response == null)
                    {
                        action.ReturnType = new TypeTransferObject {
                            Name = "void"
                        };
                    }
                    else
                    {
                        action.ReturnType = this.Read(configuration, response.Content.Single().Value.Schema);
                    }
                    service.Actions.Add(action);
                }
            }
        }
        public virtual void Read(AspDotNetReadConfiguration configuration, List <ITransferObject> transferObjects)
        {
            configuration.Controller.AssertIsNotNull($"ASP: {nameof(configuration.Controller)}");
            configuration.Controller.Name.AssertIsNotNull($"ASP: {nameof(configuration.Controller)}.{nameof(configuration.Controller.Name)}");
            configuration.Controller.Namespace.AssertIsNotNull($"ASP: {nameof(configuration.Controller)}.{nameof(configuration.Controller.Namespace)}");
            Logger.Trace($"Read ASP.NET controller {configuration.Controller.Namespace}{configuration.Controller.Name}...");
            Type type = GeneratorTypeLoader.Get(configuration, configuration.Controller.Assembly, configuration.Controller.Namespace, configuration.Controller.Name);

            if (type == null)
            {
                return;
            }

            HttpServiceTransferObject controller = new HttpServiceTransferObject();

            controller.Name     = type.Name;
            controller.Language = ReflectionLanguage.Instance;

            Attribute routeAttribute = type.GetCustomAttributes().FirstOrDefault();

            controller.Route = routeAttribute?.GetType().GetProperty("Template")?.GetValue(routeAttribute)?.ToString();

            MethodInfo[] methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
            foreach (MethodInfo method in methods)
            {
                this.modelReader.Read(method.ReturnType, transferObjects);
                foreach (Attribute attribute in method.GetCustomAttributes())
                {
                    Type attributeType = attribute.GetType();
                    HttpServiceActionTransferObject action = new HttpServiceActionTransferObject();
                    action.ReturnType = method.ReturnType.ToTransferObject();
                    if (action.ReturnType.Name == "ActionResult")
                    {
                        action.ReturnType = action.ReturnType.Generics.Single().Type;
                    }
                    action.Route = attributeType.GetProperty("Template")?.GetValue(attribute)?.ToString();
                    int methodNameIndex = 1;
                    while (true)
                    {
                        string actionName = $"{method.Name}{(methodNameIndex > 1 ? methodNameIndex.ToString() : "")}";
                        if (controller.Actions.All(x => !x.Name.Equals(actionName)))
                        {
                            action.Name = actionName;
                            break;
                        }
                        methodNameIndex++;
                    }
                    ParameterInfo[] parameters = method.GetParameters();
                    foreach (ParameterInfo parameter in parameters)
                    {
                        this.modelReader.Read(parameter.ParameterType, transferObjects);
                    }
                    switch (attributeType.Name)
                    {
                    case "HttpGetAttribute":
                        action.Type = HttpServiceActionTypeTransferObject.Get;
                        break;

                    case "HttpPostAttribute":
                        action.Type = HttpServiceActionTypeTransferObject.Post;
                        break;

                    case "HttpPatchAttribute":
                        action.Type = HttpServiceActionTypeTransferObject.Patch;
                        break;

                    case "HttpPutAttribute":
                        action.Type = HttpServiceActionTypeTransferObject.Put;
                        break;

                    case "HttpDeleteAttribute":
                        action.Type = HttpServiceActionTypeTransferObject.Delete;
                        break;

                    case "ConditionalAttribute":
                        // Ignore these attributes
                        continue;

                    default:
                        Logger.Warning($"Unknown controller action attribute {attributeType.Name}");
                        continue;
                    }
                    action.RequireBodyParameter = action.Type.IsBodyParameterRequired();
                    foreach (ParameterInfo parameter in parameters)
                    {
                        HttpServiceActionParameterTransferObject actionParameter = new HttpServiceActionParameterTransferObject();
                        actionParameter.Name        = parameter.Name;
                        actionParameter.Type        = parameter.ParameterType.ToTransferObject();
                        actionParameter.FromBody    = action.RequireBodyParameter && parameter.GetCustomAttributes().Any(parameterAttribute => parameterAttribute.GetType().Name == "FromBodyAttribute");
                        actionParameter.Inline      = action.Route != null && action.Route.Contains($"{{{parameter.Name}}}");
                        actionParameter.InlineIndex = actionParameter.Inline ? action.Route.IndexOf($"{{{parameter.Name}}}", StringComparison.Ordinal) : 0;
                        action.Parameters.Add(actionParameter);
                    }
                    if (action.RequireBodyParameter)
                    {
                        if (action.Parameters.Count == 0)
                        {
                            throw new InvalidOperationException($"Can not write {method.Name}. {action.Type} requires at least one parameter, but no parameter found.");
                        }
                        if (action.Parameters.Count == 1)
                        {
                            action.Parameters.Single().FromBody = true;
                        }
                        else if (action.Parameters.All(x => !x.FromBody))
                        {
                            throw new InvalidOperationException($"Can not write {method.Name}. {action.Type} requires at least one parameter marked with [FromBody] or can have only one parameter");
                        }
                    }
                    controller.Actions.Add(action);
                }
            }
            transferObjects.Add(controller);
        }
예제 #4
0
        private void AddKeysToParameters(Dictionary <IEdmType, TypeTransferObject> mapping, IEdmNavigationSource navigationSource, HttpServiceActionTransferObject action)
        {
            IEdmEntityType entity = this.ToEntity(navigationSource.Type);

            if (entity.DeclaredKey != null)
            {
                foreach (IEdmStructuralProperty property in entity.DeclaredKey)
                {
                    action.Parameters.Add(new HttpServiceActionParameterTransferObject {
                        Name = property.Name, Type = this.ToTransferObject(property.Type.Definition, mapping)
                    });
                }
            }
        }