Exemplo n.º 1
0
        // Default factory for sample objects
        private static object DefaultSampleObjectFactory(AdminSampleGenerator sampleGenerator, Type type)
        {
            // Try to create a default sample object
            ObjectGenerator objectGenerator = new ObjectGenerator();

            return(objectGenerator.GenerateObject(type));
        }
        private static bool TryGetResourceParameter(ApiDescription apiDescription, HttpConfiguration config, out ApiParameterDescription parameterDescription, out Type resourceType)
        {
            parameterDescription = apiDescription.ParameterDescriptions.FirstOrDefault(
                p => p.Source == ApiParameterSource.FromBody ||
                (p.ParameterDescriptor != null && p.ParameterDescriptor.ParameterType == typeof(HttpRequestMessage)));

            if (parameterDescription == null)
            {
                resourceType = null;
                return(false);
            }

            resourceType = parameterDescription.ParameterDescriptor.ParameterType;

            if (resourceType == typeof(HttpRequestMessage))
            {
                AdminSampleGenerator sampleGenerator = config.GetAdminSampleGenerator();
                resourceType = sampleGenerator.ResolveHttpRequestMessageType(apiDescription);
            }

            if (resourceType == null)
            {
                parameterDescription = null;
                return(false);
            }

            return(true);
        }
 /// <summary>
 /// Sets the help page sample generator.
 /// </summary>
 /// <param name="config">The <see cref="HttpConfiguration"/>.</param>
 /// <param name="sampleGenerator">The help page sample generator.</param>
 public static void SetAdminSampleGenerator(this HttpConfiguration config, AdminSampleGenerator sampleGenerator)
 {
     config.Properties.AddOrUpdate(
         typeof(AdminSampleGenerator),
         k => sampleGenerator,
         (k, o) => sampleGenerator);
 }
        private static AdminApiModel GenerateApiModel(ApiDescription apiDescription, HttpConfiguration config)
        {
            AdminApiModel apiModel = new AdminApiModel()
            {
                ApiDescription = apiDescription,
            };

            ModelDescriptionGenerator modelGenerator  = config.GetModelDescriptionGenerator();
            AdminSampleGenerator      sampleGenerator = config.GetAdminSampleGenerator();

            GenerateUriParameters(apiModel, modelGenerator);
            GenerateRequestModelDescription(apiModel, modelGenerator, sampleGenerator);
            GenerateResourceDescription(apiModel, modelGenerator);
            GenerateSamples(apiModel, sampleGenerator);

            return(apiModel);
        }
        private static void GenerateSamples(AdminApiModel apiModel, AdminSampleGenerator sampleGenerator)
        {
            try
            {
                foreach (var item in sampleGenerator.GetSampleRequests(apiModel.ApiDescription))
                {
                    apiModel.SampleRequests.Add(item.Key, item.Value);
                    LogInvalidSampleAsError(apiModel, item.Value);
                }

                foreach (var item in sampleGenerator.GetSampleResponses(apiModel.ApiDescription))
                {
                    apiModel.SampleResponses.Add(item.Key, item.Value);
                    LogInvalidSampleAsError(apiModel, item.Value);
                }
            }
            catch (Exception e)
            {
                apiModel.ErrorMessages.Add(String.Format(CultureInfo.CurrentCulture,
                                                         "An exception has occurred while generating the sample. Exception message: {0}",
                                                         AdminSampleGenerator.UnwrapException(e).Message));
            }
        }
        private static void GenerateRequestModelDescription(AdminApiModel apiModel, ModelDescriptionGenerator modelGenerator, AdminSampleGenerator sampleGenerator)
        {
            ApiDescription apiDescription = apiModel.ApiDescription;

            foreach (ApiParameterDescription apiParameter in apiDescription.ParameterDescriptions)
            {
                if (apiParameter.Source == ApiParameterSource.FromBody)
                {
                    Type parameterType = apiParameter.ParameterDescriptor.ParameterType;
                    apiModel.RequestModelDescription = modelGenerator.GetOrCreateModelDescription(parameterType);
                    apiModel.RequestDocumentation    = apiParameter.Documentation;
                }
                else if (apiParameter.ParameterDescriptor != null &&
                         apiParameter.ParameterDescriptor.ParameterType == typeof(HttpRequestMessage))
                {
                    Type parameterType = sampleGenerator.ResolveHttpRequestMessageType(apiDescription);

                    if (parameterType != null)
                    {
                        apiModel.RequestModelDescription = modelGenerator.GetOrCreateModelDescription(parameterType);
                    }
                }
            }
        }