This class allows you to customize how you want a type to be represented in the swagger output. For example, if a service serializes/deserializes DateTime as a string when being sent/received, there should be a SwaggerTypeMapping from DateTime to string in order to make the swagger output match what is actually expected.
Exemplo n.º 1
0
        public static Schema GetSchema(ISwaggerModelCatalog modelCatalog, Type t, bool isDefinition)
        {
            if (SwaggerTypeMapping.IsMappedType(t))
            {
                t = SwaggerTypeMapping.GetMappedType(t);
            }
            var model  = modelCatalog.GetModelForType(t);
            var schema = new Schema();

            if (model != null)
            {
                schema = model.GetSchema(isDefinition);
            }
            else if (Primitive.IsPrimitive(t))
            {
                var primitive = Primitive.FromType(t);

                schema.Type = primitive.Type.ToLower();
                if (!string.IsNullOrEmpty(primitive.Format))
                {
                    schema.Format = primitive.Format.ToLower();
                }
            }
            return(schema);
        }
Exemplo n.º 2
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 (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 Item
                    {
                        Type   = primitive.Type,
                        Format = primitive.Format
                    };

                    return(dataType);
                }

                dataType.Items = itemsType.ToItem();

                return(dataType);
            }

            if (isTopLevel)
            {
                dataType.Ref = "#/definitions/" + SwaggerBuilderConfig.ModelIdConvention(type);
                return(dataType);
            }

            dataType.Type = SwaggerBuilderConfig.ModelIdConvention(type);

            return(dataType);
        }