Пример #1
0
        public static Operation ToOperation(this SwaggerRouteData routeData)
        {
            var operation = routeData.OperationModel.ToDataType <Operation>();

            operation.Nickname         = SwaggerConfig.NicknameConvention(routeData);
            operation.Summary          = routeData.OperationSummary;
            operation.Method           = routeData.OperationMethod;
            operation.Notes            = routeData.OperationNotes;
            operation.Parameters       = routeData.OperationParameters.Select(p => p.ToParameter());
            operation.ResponseMessages = routeData.OperationResponseMessages.Any() ? routeData.OperationResponseMessages.OrderBy(r => r.Code) : null;
            operation.Produces         = routeData.OperationProduces.Any() ? routeData.OperationProduces.OrderBy(p => p) : null;
            operation.Consumes         = routeData.OperationConsumes.Any() ? routeData.OperationConsumes.OrderBy(c => c) : null;

            return(operation);
        }
Пример #2
0
 public ObjectSchema(Type t, Model sModel, bool isDefinition)
 {
     if (isDefinition)
     {
         Type        = "object";
         Required    = (sModel.Required as IList <string>)?.Select(x => x.ToCamelCase()).ToList();
         Description = sModel.Description;
         Properties  = new Dictionary <string, Schema>();
         foreach (var member in sModel.Properties)
         {
             Properties.Add(member.Key.ToCamelCase(), GenerateSchemaForProperty(member.Value));
         }
     }
     else
     {
         Ref = DefinitionsRefLocation + SwaggerConfig.ModelIdConvention(t);
     }
 }
Пример #3
0
        public static T ToDataType <T>(this Type type, bool isTopLevel = false)
            where T : DataType, new()
        {
            var dataType = new T();

            if (type == null)
            {
                dataType.Type = "void";

                return(dataType);
            }

            if (Primitive.IsPrimitive(type))
            {
                var primitive = Primitive.FromType(type);

                dataType.Format = primitive.Format;
                dataType.Type   = primitive.Type;

                return(dataType);
            }

            if (type.IsContainer())
            {
                dataType.Type = "array";

                var itemsType = type.GetElementType() ?? type.GetGenericArguments().FirstOrDefault();

                if (Primitive.IsPrimitive(itemsType))
                {
                    var primitive = Primitive.FromType(itemsType);

                    dataType.Items = new Items
                    {
                        Type   = primitive.Type,
                        Format = primitive.Format
                    };

                    return(dataType);
                }

                dataType.Items = new Items {
                    Ref = "#/definitions/" + SwaggerConfig.ModelIdConvention(itemsType)
                };

                return(dataType);
            }

            if (type.IsEnum)
            {
                dataType.Type = "string";//SwaggerConfig.ModelIdConvention(Enum.GetUnderlyingType(type)).ToLower();
                return(dataType);
            }
            else if (isTopLevel)
            {
                dataType.Ref = "#/definitions/" + SwaggerConfig.ModelIdConvention(type);
                return(dataType);
            }

            dataType.Type = SwaggerConfig.ModelIdConvention(type);

            return(dataType);
        }
Пример #4
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);
        }
Пример #5
0
        public static T ToDataType <T>(this Type type, bool isTopLevel = false)
            where T : DataType, new()
        {
            var dataType = new T();

            if (SwaggerTypeMapping.IsMappedType(type))
            {
                type = SwaggerTypeMapping.GetMappedType(type);
            }

            if (type == null)
            {
                dataType.Type = "void";

                return(dataType);
            }

            if (type.IsNullable())
            {
                type = Nullable.GetUnderlyingType(type);
            }
            if (Primitive.IsPrimitive(type))
            {
                var primitive = Primitive.FromType(type);

                dataType.Format = primitive.Format;
                dataType.Type   = primitive.Type;

                return(dataType);
            }


            if (type.IsContainer())
            {
                dataType.Type = "array";

                var itemsType = type.GetElementType() ?? type.GetTypeInfo().GetGenericArguments().FirstOrDefault();

                if (Primitive.IsPrimitive(itemsType))
                {
                    var primitive = Primitive.FromType(itemsType);

                    dataType.Items = new Item
                    {
                        Type   = primitive.Type,
                        Format = primitive.Format
                    };

                    return(dataType);
                }

                dataType.Items = new Item {
                    Ref = SwaggerConfig.ModelIdConvention(itemsType)
                };

                return(dataType);
            }

            if (isTopLevel)
            {
                dataType.Ref = SwaggerConfig.ModelIdConvention(type);
                return(dataType);
            }

            dataType.Type = SwaggerConfig.ModelIdConvention(type);

            return(dataType);
        }