From 8b2d84fde29f14c86b19d2deefcf572c19b8df61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BD=D1=8E=D1=82=D0=B8=D0=BD=20=D0=9C=D0=B0=D0=BA?= =?UTF-8?q?=D1=81=D0=B8=D0=BC=20=D0=9D=D0=B8=D0=BA=D0=BE=D0=BB=D0=B0=D0=B5?= =?UTF-8?q?=D0=B2=D0=B8=D1=87?= Date: Fri, 4 Jan 2019 00:20:58 +0300 Subject: [PATCH] =?UTF-8?q?#762=20NullReferenceException=20=D0=B2=20Docume?= =?UTF-8?q?nt.Type?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- VkNet.Tests/Categories/Docs/DocsSaveTests.cs | 21 +++++++ VkNet/Enums/DocumentTypeEnum.cs | 38 +++++++++++ VkNet/Model/Attachments/Document.cs | 6 +- VkNet/Model/DocumentPreview.cs | 63 +++++++++++++++++++ .../VkResponseToManualEnumCastsGenerator.cs | 17 +++++ 5 files changed, 143 insertions(+), 2 deletions(-) create mode 100644 VkNet/Enums/DocumentTypeEnum.cs create mode 100644 VkNet/Model/DocumentPreview.cs diff --git a/VkNet.Tests/Categories/Docs/DocsSaveTests.cs b/VkNet.Tests/Categories/Docs/DocsSaveTests.cs index 71c4dac2f..ba877ad3b 100644 --- a/VkNet.Tests/Categories/Docs/DocsSaveTests.cs +++ b/VkNet.Tests/Categories/Docs/DocsSaveTests.cs @@ -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 @@ -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); + } } } \ No newline at end of file diff --git a/VkNet/Enums/DocumentTypeEnum.cs b/VkNet/Enums/DocumentTypeEnum.cs new file mode 100644 index 000000000..5d5f724b6 --- /dev/null +++ b/VkNet/Enums/DocumentTypeEnum.cs @@ -0,0 +1,38 @@ +namespace VkNet.Enums +{ + public enum DocumentTypeEnum + { + /// + /// Текстовые документы + /// + Text = 1, + /// + /// Архивы + /// + Archive, + /// + /// gif + /// + Gif, + /// + /// Изображения + /// + Image, + /// + /// Аудио + /// + Audio, + /// + /// Видео + /// + Video, + /// + /// Электронные книги + /// + EBook, + /// + /// Неизвестно + /// + Unknown + } +} \ No newline at end of file diff --git a/VkNet/Model/Attachments/Document.cs b/VkNet/Model/Attachments/Document.cs index 9af23bcfe..cfc1203e9 100644 --- a/VkNet/Model/Attachments/Document.cs +++ b/VkNet/Model/Attachments/Document.cs @@ -1,6 +1,7 @@ using System; using Newtonsoft.Json; using Newtonsoft.Json.Converters; +using VkNet.Enums; using VkNet.Utils; namespace VkNet.Model.Attachments @@ -33,6 +34,7 @@ public class Document : MediaAttachment /// /// Адрес документа, по которому его можно загрузить. /// + [JsonProperty("url")] public string Uri { get; set; } /// @@ -45,12 +47,12 @@ public class Document : MediaAttachment /// тип документа /// [JsonProperty("type")] - public DocumentType Type { get; set; } + public DocumentTypeEnum Type { get; set; } /// /// Gets or sets the preview. /// - public Previews Preview { get; set; } + public DocumentPreview Preview { get; set; } /// /// Адрес изображения с размером 100x75px (если файл графический). diff --git a/VkNet/Model/DocumentPreview.cs b/VkNet/Model/DocumentPreview.cs new file mode 100644 index 000000000..69fd010b9 --- /dev/null +++ b/VkNet/Model/DocumentPreview.cs @@ -0,0 +1,63 @@ +using System; +using Newtonsoft.Json; +using VkNet.Utils; + +namespace VkNet.Model.Attachments +{ + /// + /// Информация для предварительного просмотра документа + /// + [Serializable] + public class DocumentPreview + { + /// + /// Изображения для предпросмотра. + /// + [JsonProperty("photo")] + public Photo Photo { get; set; } + + /// + /// Данные о граффити + /// + [JsonProperty("graffiti")] + public Graffiti Graffiti { get; set; } + + /// + /// Данные об аудиосообщении. + /// + [JsonProperty("audio_message")] + public AudioMessage AudioMessage { get; set; } + + /// + /// Разобрать из json. + /// + /// Ответ сервера. + /// + public static DocumentPreview FromJson(VkResponse response) + { + return new DocumentPreview + { + Photo = response["photo"], + Graffiti = response["graffiti"], + AudioMessage = response["audio_message"] + }; + } + + /// + /// Преобразование класса в + /// + /// Ответ сервера. + /// Результат преобразования в + public static implicit operator DocumentPreview(VkResponse response) + { + if (response == null) + { + return null; + } + + return response.HasToken() + ? FromJson(response) + : null; + } + } +} \ No newline at end of file diff --git a/VkNet/Utils/VkResponseToManualEnumCastsGenerator.cs b/VkNet/Utils/VkResponseToManualEnumCastsGenerator.cs index 8f51ee4e0..e0e9521f7 100644 --- a/VkNet/Utils/VkResponseToManualEnumCastsGenerator.cs +++ b/VkNet/Utils/VkResponseToManualEnumCastsGenerator.cs @@ -5,6 +5,23 @@ namespace VkNet.Utils { public partial class VkResponse { + /// + /// Преобразовать из VkResponse + /// + /// Ответ. + /// + /// Результат преобразования. + /// + public static implicit operator DocumentTypeEnum(VkResponse response) + { + if (response == null) + { + return DocumentTypeEnum.Unknown; + } + + return Utilities.EnumFrom(value: response); + } + /// /// Преобразовать из VkResponse ///