Skip to content

Commit

Permalink
#762 NullReferenceException в Document.Type
Browse files Browse the repository at this point in the history
  • Loading branch information
inyutin-maxim committed Jan 3, 2019
1 parent 777c7ea commit 8b2d84f
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 2 deletions.
21 changes: 21 additions & 0 deletions VkNet.Tests/Categories/Docs/DocsSaveTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System.Linq;
using NUnit.Framework;
using VkNet.Enums;
using VkNet.Model.Attachments;
using VkNet.Tests.Infrastructure;

namespace VkNet.Tests.Categories.Docs
Expand Down Expand Up @@ -46,5 +49,23 @@ public void Save3()

Assert.IsNotEmpty(result);
}

[Test]
public void Save_Type()
{
Url = "https://api.vk.com/method/docs.save";
ReadCategoryJsonPath("Save3");

var docUploadResult = ReadJson("Categories", Folder, "DocUploadResult");

var result = Api.Docs.Save(docUploadResult, "IMG_907");

Assert.IsNotEmpty(result);
var item = result.FirstOrDefault();
Assert.NotNull(item);
var doc = item.Instance as Document;
Assert.NotNull(doc);
Assert.AreEqual(DocumentTypeEnum.Text, doc.Type);
}
}
}
38 changes: 38 additions & 0 deletions VkNet/Enums/DocumentTypeEnum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace VkNet.Enums
{
public enum DocumentTypeEnum
{
/// <summary>
/// Текстовые документы
/// </summary>
Text = 1,
/// <summary>
/// Архивы
/// </summary>
Archive,
/// <summary>
/// gif
/// </summary>
Gif,
/// <summary>
/// Изображения
/// </summary>
Image,
/// <summary>
/// Аудио
/// </summary>
Audio,
/// <summary>
/// Видео
/// </summary>
Video,
/// <summary>
/// Электронные книги
/// </summary>
EBook,
/// <summary>
/// Неизвестно
/// </summary>
Unknown
}
}
6 changes: 4 additions & 2 deletions VkNet/Model/Attachments/Document.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using VkNet.Enums;
using VkNet.Utils;

namespace VkNet.Model.Attachments
Expand Down Expand Up @@ -33,6 +34,7 @@ public class Document : MediaAttachment
/// <summary>
/// Адрес документа, по которому его можно загрузить.
/// </summary>
[JsonProperty("url")]
public string Uri { get; set; }

/// <summary>
Expand All @@ -45,12 +47,12 @@ public class Document : MediaAttachment
/// тип документа
/// </summary>
[JsonProperty("type")]
public DocumentType Type { get; set; }
public DocumentTypeEnum Type { get; set; }

/// <summary>
/// Gets or sets the preview.
/// </summary>
public Previews Preview { get; set; }
public DocumentPreview Preview { get; set; }

/// <summary>
/// Адрес изображения с размером 100x75px (если файл графический).
Expand Down
63 changes: 63 additions & 0 deletions VkNet/Model/DocumentPreview.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using Newtonsoft.Json;
using VkNet.Utils;

namespace VkNet.Model.Attachments
{
/// <summary>
/// Информация для предварительного просмотра документа
/// </summary>
[Serializable]
public class DocumentPreview
{
/// <summary>
/// Изображения для предпросмотра.
/// </summary>
[JsonProperty("photo")]
public Photo Photo { get; set; }

/// <summary>
/// Данные о граффити
/// </summary>
[JsonProperty("graffiti")]
public Graffiti Graffiti { get; set; }

/// <summary>
/// Данные об аудиосообщении.
/// </summary>
[JsonProperty("audio_message")]
public AudioMessage AudioMessage { get; set; }

/// <summary>
/// Разобрать из json.
/// </summary>
/// <param name="response"> Ответ сервера. </param>
/// <returns> </returns>
public static DocumentPreview FromJson(VkResponse response)
{
return new DocumentPreview
{
Photo = response["photo"],
Graffiti = response["graffiti"],
AudioMessage = response["audio_message"]
};
}

/// <summary>
/// Преобразование класса <see cref="Document" /> в <see cref="VkParameters" />
/// </summary>
/// <param name="response"> Ответ сервера. </param>
/// <returns>Результат преобразования в <see cref="Document" /></returns>
public static implicit operator DocumentPreview(VkResponse response)
{
if (response == null)
{
return null;
}

return response.HasToken()
? FromJson(response)
: null;
}
}
}
17 changes: 17 additions & 0 deletions VkNet/Utils/VkResponseToManualEnumCastsGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@ namespace VkNet.Utils
{
public partial class VkResponse
{
/// <summary>
/// Преобразовать из VkResponse
/// </summary>
/// <param name="response"> Ответ. </param>
/// <returns>
/// Результат преобразования.
/// </returns>
public static implicit operator DocumentTypeEnum(VkResponse response)
{
if (response == null)
{
return DocumentTypeEnum.Unknown;
}

return Utilities.EnumFrom<DocumentTypeEnum>(value: response);
}

/// <summary>
/// Преобразовать из VkResponse
/// </summary>
Expand Down

0 comments on commit 8b2d84f

Please sign in to comment.