/// <summary>
 /// Initializes a new instance of the <see cref="SwaggerModelDataBuilder{T}"/> class.
 /// </summary>
 public SwaggerModelDataBuilder()
 {
     Data = new SwaggerModelData(typeof(T));
 }
예제 #2
0
        public static IEnumerable <Model> ToModel(this SwaggerModelData model, IEnumerable <SwaggerModelData> knownModels = null)
        {
            var classProperties = model.Properties.Where(x => !Primitive.IsPrimitive(x.Type) && !x.Type.IsEnum && !x.Type.IsGenericType);

            var modelsData = knownModels ?? Enumerable.Empty <SwaggerModelData>();

            foreach (var swaggerModelPropertyData in classProperties)
            {
                var properties = GetPropertiesFromType(swaggerModelPropertyData.Type);

                var modelDataForClassProperty =
                    modelsData.FirstOrDefault(x => x.ModelType == swaggerModelPropertyData.Type);

                var id = modelDataForClassProperty == null
                    ? swaggerModelPropertyData.Type.Name
                    : SwaggerConfig.ModelIdConvention(modelDataForClassProperty.ModelType);

                var description = modelDataForClassProperty == null
                    ? swaggerModelPropertyData.Description
                    : modelDataForClassProperty.Description;

                var required = modelDataForClassProperty == null
                    ? properties.Where(p => p.Required || p.Type.IsImplicitlyRequired())
                               .Select(p => p.Name)
                               .OrderBy(name => name)
                               .ToList()
                    : modelDataForClassProperty.Properties
                               .Where(p => p.Required || p.Type.IsImplicitlyRequired())
                               .Select(p => p.Name)
                               .OrderBy(name => name)
                               .ToList();

                var modelproperties = modelDataForClassProperty == null
                    ? properties
                                      .OrderBy(x => x.Name)
                                      .ToDictionary(p => p.Name, ToModelProperty)
                    : modelDataForClassProperty.Properties
                                      .OrderBy(x => x.Name)
                                      .ToDictionary(p => p.Name, ToModelProperty);

                yield return(new Model
                {
                    Id = id,
                    Description = description,
                    Required = required,
                    Properties = modelproperties
                });
            }

            var topLevelModel = new Model
            {
                Id          = SwaggerConfig.ModelIdConvention(model.ModelType),
                Description = model.Description,
                Required    = model.Properties
                              .Where(p => p.Required || p.Type.IsImplicitlyRequired())
                              .Select(p => p.Name)
                              //.OrderBy(name => name)
                              .ToList(),
                Properties = model.Properties
                             //.OrderBy(p => p.Name)
                             .ToDictionary(p => p.Name, ToModelProperty)

                             // TODO: SubTypes and Discriminator
            };

            yield return(topLevelModel);
        }