public ModelDescription Generate(Type modelType, ModelDescriptionGenerator generator)
        {
            var collectionModelDescription = generator.GetOrCreateModelDescription(_elementType);
            if (collectionModelDescription != null)
            {
                return new CollectionModelDescription
                {
                    Name = ModelNameHelper.GetModelName(modelType),
                    ModelType = modelType,
                    ElementDescription = collectionModelDescription,
                    ParameterDocumentation = collectionModelDescription.ParameterDocumentation
                };
            }

            return null;
        }
        public void Tewt_SimpleEnum()
        {
            generator = new ModelDescriptionGenerator(new HttpConfiguration());
            var description = generator.GetOrCreateModelDescription(typeof(SimpleEnum));
            Assert.IsTrue(description is EnumTypeModelDescription);
            Assert.AreEqual(description.Name, "SimpleEnum");
            Assert.AreEqual(description.ModelType, typeof(SimpleEnum));
            Assert.IsNull(description.Documentation);
            Assert.AreEqual((description as EnumTypeModelDescription).Values.Count, 2);
            var memberValues = (description as EnumTypeModelDescription).Values;
            Assert.AreEqual(memberValues[0].Name, "Option1");
            Assert.AreEqual(memberValues[0].Value, "0");
            Assert.AreEqual(memberValues[0].Documentation, null);
            Assert.AreEqual(memberValues[1].Name, "Option2");
            Assert.AreEqual(memberValues[1].Value, "1");
            Assert.AreEqual(memberValues[1].Documentation, null);

        }
 public void Test_Second()
 {
     var generator = new ModelDescriptionGenerator(new HttpConfiguration());
     var modelDescription = generator.GetOrCreateModelDescription(typeof(VipDemandDto));
     Assert.IsNotNull(modelDescription);
     Assert.AreEqual(modelDescription.Name, "VipDemandDto");
     Assert.AreEqual(modelDescription.Documentation, "VIP需求信息数据单元");
     Assert.AreEqual(modelDescription.ParameterDocumentation, null);
 }
 public void Test_First()
 {
     var generator = new ModelDescriptionGenerator(new HttpConfiguration());
     var modelDescription = generator.GetOrCreateModelDescription(typeof (Cell));
     Assert.IsNotNull(modelDescription);
     Assert.AreEqual(modelDescription.Name, "Cell");
     Assert.AreEqual(modelDescription.Documentation, null);
     Assert.AreEqual(modelDescription.ParameterDocumentation, null);
 }
        public ModelDescription Generate(Type modelType, ModelDescriptionGenerator generator)
        {
            var provider = new DocProvider();
            
            var complexModelDescription = new ComplexTypeModelDescription
            {
                Name = ModelNameHelper.GetModelName(modelType),
                ModelType = modelType,
                Documentation = generator.CreateDefaultDocumentation(modelType),
                ParameterDocumentation = provider.GetDocumentation(modelType)
            };

            generator.GeneratedModels.Add(complexModelDescription.Name, complexModelDescription);
            var hasDataContractAttribute = modelType.GetCustomAttribute<DataContractAttribute>() != null;
            var properties = modelType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (var property in properties)
            {
                if (!ModelDescriptionGenerator.ShouldDisplayMember(property, hasDataContractAttribute)) continue;
                var propertyModel = new ParameterDescription
                {
                    Name = GetMemberName(property, hasDataContractAttribute)
                };

                if (generator.DocumentationProvider != null)
                {
                    propertyModel.Documentation = generator.DocumentationProvider.GetDocumentation(property);
                }

                GenerateAnnotations(property, propertyModel);
                complexModelDescription.Properties.Add(propertyModel);
                propertyModel.TypeDescription = generator.GetOrCreateModelDescription(property.PropertyType);
            }

            var fields = modelType.GetFields(BindingFlags.Public | BindingFlags.Instance);
            foreach (var field in fields)
            {
                if (!ModelDescriptionGenerator.ShouldDisplayMember(field, hasDataContractAttribute)) continue;
                var propertyModel = new ParameterDescription
                {
                    Name = GetMemberName(field, hasDataContractAttribute)
                };

                if (generator.DocumentationProvider != null)
                {
                    propertyModel.Documentation = generator.DocumentationProvider.GetDocumentation(field);
                }

                complexModelDescription.Properties.Add(propertyModel);
                propertyModel.TypeDescription = generator.GetOrCreateModelDescription(field.FieldType);
            }

            return complexModelDescription;
        }
        public ModelDescription Generate(Type modelType, ModelDescriptionGenerator generator)
        {
            var keyModelDescription = generator.GetOrCreateModelDescription(_keyType);
            var valueModelDescription = generator.GetOrCreateModelDescription(_valueType);

            return new KeyValuePairModelDescription
            {
                Name = ModelNameHelper.GetModelName(modelType),
                ModelType = modelType,
                KeyModelDescription = keyModelDescription,
                ValueModelDescription = valueModelDescription
            };
        }
 private static ModelDescriptionGenerator InitializeModelDescriptionGenerator(HttpConfiguration config)
 {
     var modelGenerator = new ModelDescriptionGenerator(config);
     var apis = config.Services.GetApiExplorer().ApiDescriptions;
     foreach (var api in apis)
     {
         ApiParameterDescription parameterDescription;
         Type parameterType;
         if (TryGetResourceParameter(api, config, out parameterDescription, out parameterType))
         {
             modelGenerator.GetOrCreateModelDescription(parameterType);
         }
     }
     return modelGenerator;
 }
 public static ModelDescription GenerateResponseDescription(this ApiDescription apiDescription, 
     ModelDescriptionGenerator modelGenerator)
 {
     var response = apiDescription.ResponseDescription;
     var responseType = response.ResponseType ?? response.DeclaredType;
     return (responseType != null && responseType != typeof(void)) ? 
         modelGenerator.GetOrCreateModelDescription(responseType) : 
         null;
 }
        public static ModelDescription GenerateRequestModelDescription(this ApiDescription apiDescription, 
            ModelDescriptionGenerator modelGenerator, 
            HelpPageSampleGenerator sampleGenerator)
        {
            foreach (var apiParameter in apiDescription.ParameterDescriptions)
            {
                if (apiParameter.Source == ApiParameterSource.FromBody)
                {
                    var parameterType = apiParameter.ParameterDescriptor.ParameterType;
                    var result = modelGenerator.GetOrCreateModelDescription(parameterType);
                    result.ParameterDocumentation = apiParameter.Documentation;
                    return result;
                }
                if (apiParameter.ParameterDescriptor != null &&
                    apiParameter.ParameterDescriptor.ParameterType == typeof(HttpRequestMessage))
                {
                    var parameterType = sampleGenerator.ResolveHttpRequestMessageType(apiDescription);

                    if (parameterType != null)
                    {
                        var result = modelGenerator.GetOrCreateModelDescription(parameterType);
                        result.ParameterDocumentation = apiParameter.Documentation;
                        return result;
                    }
                }
            }
            return null;
        }
        public static Collection<ParameterDescription> GenerateUriParameters(
            this ApiDescription apiDescription,
            ModelDescriptionGenerator modelGenerator)
        {
            var uriParameters = new Collection<ParameterDescription>();
            foreach (var apiParameter in apiDescription.ParameterDescriptions)
            {
                if (apiParameter.Source != ApiParameterSource.FromUri) continue;
                var parameterDescriptor = apiParameter.ParameterDescriptor;
                Type parameterType = null;
                ModelDescription typeDescription = null;
                ComplexTypeModelDescription complexTypeDescription = null;
                if (parameterDescriptor != null)
                {
                    parameterType = parameterDescriptor.ParameterType;
                    typeDescription = modelGenerator.GetOrCreateModelDescription(parameterType);
                    complexTypeDescription = typeDescription as ComplexTypeModelDescription;
                }
                
                // Regular complex class Point will have properties X and Y added to UriParameters collection.
                if (complexTypeDescription != null
                    && !IsBindableWithTypeConverter(parameterType))
                {
                    foreach (var uriParameter in complexTypeDescription.Properties)
                    {
                        uriParameters.Add(uriParameter);
                    }
                }
                else if (parameterDescriptor != null)
                {
                    var uriParameter =
                        uriParameters.AddParameterDescription(apiParameter, typeDescription);

                    if (!parameterDescriptor.IsOptional)
                    {
                        uriParameter.Annotations.Add(new ParameterAnnotation
                        {
                            Documentation = "Required"
                        });
                    }

                    var defaultValue = parameterDescriptor.DefaultValue;
                    if (defaultValue != null)
                    {
                        uriParameter.Annotations.Add(new ParameterAnnotation
                        {
                            Documentation = "Default value is " + Convert.ToString(defaultValue, CultureInfo.InvariantCulture)
                        });
                    }
                }
                else
                {
                    Debug.Assert(parameterDescriptor == null);

                    // If parameterDescriptor is null, this is an undeclared route parameter which only occurs
                    // when source is FromUri. Ignored in request model and among resource parameters but listed
                    // as a simple string here.
                    var modelDescription = modelGenerator.GetOrCreateModelDescription(typeof(string));
                    uriParameters.AddParameterDescription(apiParameter, modelDescription);
                }
            }
            return uriParameters;
        }