Esempio n. 1
0
        private TSchemaType CreateAndAddSchema <TSchemaType>(Type type, ISchemaResolver schemaResolver)
            where TSchemaType : JsonSchema4, new()
        {
            if (type.Name == "Task`1")
            {
                type = type.GenericTypeArguments[0];
            }

            if (type.Name == "JsonResult`1")
            {
                type = type.GenericTypeArguments[0];
            }

            var info = JsonObjectTypeDescription.FromType(type);

            if (info.Type.HasFlag(JsonObjectType.Object))
            {
                if (type == typeof(object))
                {
                    return(new TSchemaType
                    {
                        Type = JsonObjectType.Object,
                        AllowAdditionalProperties = false
                    });
                }

                if (!schemaResolver.HasSchema(type))
                {
                    var schemaGenerator = new RootTypeJsonSchemaGenerator(_service, JsonSchemaGeneratorSettings);
                    schemaGenerator.Generate <JsonSchema4>(type, schemaResolver);
                }

                return(new TSchemaType
                {
                    Type = JsonObjectType.Object,
                    SchemaReference = schemaResolver.GetSchema(type)
                });
            }

            if (info.Type.HasFlag(JsonObjectType.Array))
            {
                var itemType = type.GenericTypeArguments.Length == 0 ? type.GetElementType() : type.GenericTypeArguments[0];
                return(new TSchemaType
                {
                    Type = JsonObjectType.Array,
                    Item = CreateAndAddSchema <JsonSchema4>(itemType, schemaResolver)
                });
            }

            var generator = new RootTypeJsonSchemaGenerator(_service, JsonSchemaGeneratorSettings);

            return(generator.Generate <TSchemaType>(type, schemaResolver));
        }
        /// <summary>Generates the properties for the given type and schema.</summary>
        /// <typeparam name="TSchemaType">The type of the schema type.</typeparam>
        /// <param name="type">The types.</param>
        /// <param name="schema">The properties</param>
        /// <param name="schemaResolver">The schema resolver.</param>
        protected override void GenerateObject <TSchemaType>(Type type, TSchemaType schema, ISchemaResolver schemaResolver)
        {
            if (_isRootType)
            {
                _isRootType = false;
                base.GenerateObject(type, schema, schemaResolver);
            }
            else
            {
                if (!schemaResolver.HasSchema(type))
                {
                    var schemaGenerator = new RootTypeJsonSchemaGenerator(_service, Settings);
                    schemaGenerator.Generate <JsonSchema4>(type, schemaResolver);
                }

                schema.SchemaReference = schemaResolver.GetSchema(type);
            }
        }
Esempio n. 3
0
        private SwaggerParameterExtended CreatePrimitiveParameter(ParameterInfo parameter, ISchemaResolver schemaResolver)
        {
            var attrs = parameter.GetCustomAttributes();

            var parameterGenerator = new RootTypeJsonSchemaGenerator(_service, JsonSchemaGeneratorSettings);
            var info = JsonObjectTypeDescription.FromType(parameter.ParameterType);
            var isComplexParameter = IsComplexType(info);
            var parameterType      = isComplexParameter ? typeof(string) : parameter.ParameterType; // complex types must be treated as string

            var segmentParameter = parameterGenerator.Generate <SwaggerParameterExtended>(parameterType, schemaResolver);

            segmentParameter.Name = parameter.Name;

            dynamic attr = attrs.SingleOrDefault(x => x.GetType().Name == "DescriptionAttribute");

            segmentParameter.Description = attr != null ? attr.Description : "";
            segmentParameter.IsRequired  = attrs.Any(x => x.GetType().Name == "RequiredAttribute");

            return(segmentParameter);
        }