Exemple #1
0
        public void OnProvidersExecuted(ApiDescriptionProviderContext context)
        {
            var typeProvider = new DynamicTypeBuilder(typeBindingRepo);

            foreach (var route in voyagerRoutes)
            {
                var descriptor = new ApiDescription
                {
                    HttpMethod       = route.Method,
                    ActionDescriptor = new Microsoft.AspNetCore.Mvc.Abstractions.ActionDescriptor
                    {
                        RouteValues = new Dictionary <string, string>()
                    },
                    RelativePath = route.Template,
                };
                var validBindingSources = new[] { BindingSource.Form, BindingSource.Query, BindingSource.Path };
                foreach (var property in typeBindingRepo.GetProperties(route.RequestType))
                {
                    if (validBindingSources.Contains(property.BindingSource))
                    {
                        descriptor.ParameterDescriptions.Add(new ApiParameterDescription
                        {
                            Name   = property.Name.ToLower(),
                            Type   = property.Property.PropertyType,
                            Source = property.BindingSource,
                            ParameterDescriptor = new Microsoft.AspNetCore.Mvc.Abstractions.ParameterDescriptor {
                                Name = property.Description
                            }
                        });
                    }
                }

                var requestBodyType = typeProvider.CreateBodyType(route.RequestType);
                if (requestBodyType != null)
                {
                    var requestModel = modelMetadataProvider.GetMetadataForType(requestBodyType);
                    descriptor.ParameterDescriptions.Add(new ApiParameterDescription
                    {
                        Type          = requestBodyType,
                        Source        = BindingSource.Body,
                        ModelMetadata = requestModel
                    });
                }
                descriptor.ActionDescriptor.RouteValues["controller"] = GetTopRoute(route.Template);
                descriptor.SupportedRequestFormats.Add(new ApiRequestFormat {
                    MediaType = "application/json"
                });
                var responseType = GetResponseType(route.RequestType);
                if (responseType != null)
                {
                    var response = new ApiResponseType();
                    response.ApiResponseFormats.Add(new ApiResponseFormat {
                        MediaType = "application/json"
                    });
                    response.ModelMetadata = modelMetadataProvider.GetMetadataForType(responseType);
                    descriptor.SupportedResponseTypes.Add(response);
                }
                context.Results.Add(descriptor);
            }
        }
Exemple #2
0
        public Type CreateBodyType(Type type)
        {
            var typeBuilder = GetRequestBodyTypeBuilder(type);

            typeBuilder.DefineDefaultConstructor(MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName);
            var hasProperties = false;

            foreach (var property in typeBindingRepo.GetProperties(type))
            {
                if (property.BindingSource == BindingSource.Body || property.BindingSource == null)
                {
                    CreateProperty(typeBuilder, property.Name, property.Property.PropertyType);
                    hasProperties = true;
                }
            }
            return(hasProperties ? typeBuilder.CreateTypeInfo().AsType() : null);
        }