Ejemplo n.º 1
0
        /// <summary>Generates and appends a schema from a given type.</summary>
        /// <param name="type">The type.</param>
        /// <param name="mayBeNull">if set to <c>true</c> [may be null].</param>
        /// <param name="parentAttributes">The parent attributes.</param>
        /// <returns></returns>
        public JsonSchema4 GenerateAndAppendSchemaFromType(Type type, bool mayBeNull, IEnumerable <Attribute> parentAttributes)
        {
            if (type.Name == "Task`1")
            {
                type = type.GenericTypeArguments[0];
            }

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

            if (IsFileResponse(type))
            {
                return new JsonSchema4 {
                           Type = JsonObjectType.File
                }
            }
            ;

            var typeDescription = JsonObjectTypeDescription.FromType(type, parentAttributes, _settings.DefaultEnumHandling);

            if (typeDescription.Type.HasFlag(JsonObjectType.Object) && !typeDescription.IsDictionary)
            {
                if (type == typeof(object))
                {
                    return(new JsonSchema4
                    {
                        // IsNullable is directly set on SwaggerParameter or SwaggerResponse
                        Type = _settings.NullHandling == NullHandling.JsonSchema ? JsonObjectType.Object | JsonObjectType.Null : JsonObjectType.Object,
                        AllowAdditionalProperties = false
                    });
                }

                if (!SchemaResolver.HasSchema(type, false))
                {
                    _schemaGenerator.Generate(type, SchemaResolver, SchemaDefinitionAppender);
                }

                if (mayBeNull)
                {
                    if (_settings.NullHandling == NullHandling.JsonSchema)
                    {
                        var schema = new JsonSchema4();
                        schema.OneOf.Add(new JsonSchema4 {
                            Type = JsonObjectType.Null
                        });
                        schema.OneOf.Add(new JsonSchema4 {
                            SchemaReference = SchemaResolver.GetSchema(type, false)
                        });
                        return(schema);
                    }
                    else
                    {
                        // TODO: Fix this bad design
                        // IsNullable must be directly set on SwaggerParameter or SwaggerResponse
                        return(new JsonSchema4 {
                            SchemaReference = SchemaResolver.GetSchema(type, false)
                        });
                    }
                }
                else
                {
                    return new JsonSchema4 {
                               SchemaReference = SchemaResolver.GetSchema(type, false)
                    }
                };
            }

            if (typeDescription.Type.HasFlag(JsonObjectType.Array))
            {
                var itemType = type.GenericTypeArguments.Length == 0 ? type.GetElementType() : type.GenericTypeArguments[0];
                return(new JsonSchema4
                {
                    // TODO: Fix this bad design
                    // IsNullable must be directly set on SwaggerParameter or SwaggerResponse
                    Type = _settings.NullHandling == NullHandling.JsonSchema ? JsonObjectType.Array | JsonObjectType.Null : JsonObjectType.Array,
                    Item = GenerateAndAppendSchemaFromType(itemType, false, null)
                });
            }

            return(_schemaGenerator.Generate(type, SchemaResolver, SchemaDefinitionAppender));
        }