From d8d52c09ac18cbfe7f0f190ae22290535153835f Mon Sep 17 00:00:00 2001 From: Marcus Turewicz <24448509+marcusturewicz@users.noreply.github.com> Date: Thu, 2 Sep 2021 19:32:19 +1000 Subject: [PATCH] Add C# documentation comments (#15) * Add C# documentation comments * Add tests for error message --- .../Dates/IsoDateModelBinder.cs | 23 ++++++++++++++++ .../IsoDateModelBinderTests.cs | 26 ++++++++++++------- version.json | 2 +- 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/Standards.AspNetCore/Dates/IsoDateModelBinder.cs b/src/Standards.AspNetCore/Dates/IsoDateModelBinder.cs index e425cd4..83053ba 100644 --- a/src/Standards.AspNetCore/Dates/IsoDateModelBinder.cs +++ b/src/Standards.AspNetCore/Dates/IsoDateModelBinder.cs @@ -6,6 +6,17 @@ namespace Standards.AspNetCore { + /// + /// An ASP.NET Core model binder to enforce ISO-8601 (YYYY-MM-DD) format for objects. + /// For example, apply to a Controller action: + /// + /// [HttpGet] + /// public IActionResult Get([ModelBinder(typeof(IsoDateModelBinder))] DateTime date) { + /// return Ok("Date is in ISO format!"); + /// } + /// + /// + /// public class IsoDateModelBinder : IModelBinder { public Task BindModelAsync(ModelBindingContext bindingContext) @@ -32,6 +43,18 @@ public Task BindModelAsync(ModelBindingContext bindingContext) } } + /// + /// An ASP.NET Core model binder provider to enforce ISO-8601 (YYYY-MM-DD) format for objects. + /// For example, apply globally in ASP.NET Core Startup.cs: + /// + /// public override void ConfigureServices(IServiceCollection services) { + /// services.AddControllers(options => { + /// options.ModelBinderProviders.Insert(0, new IsoDateModelBinderProvider()); + /// }) + /// } + /// + /// + /// public class IsoDateModelBinderProvider : IModelBinderProvider { public IModelBinder GetBinder(ModelBinderProviderContext context) diff --git a/tests/Standards.AspNetCore.Tests/IsoDateModelBinderTests.cs b/tests/Standards.AspNetCore.Tests/IsoDateModelBinderTests.cs index 094f941..6e2e6eb 100644 --- a/tests/Standards.AspNetCore.Tests/IsoDateModelBinderTests.cs +++ b/tests/Standards.AspNetCore.Tests/IsoDateModelBinderTests.cs @@ -1,15 +1,11 @@ using Xunit; using Microsoft.AspNetCore.Mvc.Testing; using System.Net; -using System; -using System.Collections.Generic; -using System.Linq; using System.Threading.Tasks; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; using Standards.AspNetCore.Tests.TestServer; +using System.Text.Json; +using Microsoft.AspNetCore.Mvc; +using System.Text.Json.Serialization; namespace Standards.AspNetCore.Tests { @@ -49,17 +45,29 @@ public IsoDateModelBinderTests(WebApplicationFactory factory) [InlineData("21.08.2021", "provider", HttpStatusCode.BadRequest)] [InlineData("2021.08.21", "provider", HttpStatusCode.BadRequest)] [InlineData("2021.21.08", "provider", HttpStatusCode.BadRequest)] - public async Task AttributeTests(string date, string route, HttpStatusCode expectedStatusCode) { // Arrange using var client = _factory.CreateClient(); // Act - var response = await client.GetAsync($"/IsoDateModelBinder/{route}?date={date}"); + using var response = await client.GetAsync($"/IsoDateModelBinder/{route}?date={date}"); // Assert + if (expectedStatusCode != HttpStatusCode.OK) + { + using var responseStream = await response.Content.ReadAsStreamAsync(); + var problem = await JsonSerializer.DeserializeAsync(responseStream); + var error = JsonSerializer.Deserialize(problem.Extensions["errors"].ToString()); + Assert.Equal("Invalid date; must be in ISO-8601 format i.e. YYYY-MM-DD.", error.Date[0]); + } Assert.Equal(expectedStatusCode, response.StatusCode); } } + + public class Error + { + [JsonPropertyName("date")] + public string[] Date { get; set; } + } } \ No newline at end of file diff --git a/version.json b/version.json index 7ace867..13e59c3 100644 --- a/version.json +++ b/version.json @@ -1,6 +1,6 @@ { "$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json", - "version": "1.0.0-alpha.5", + "version": "1.0.0-beta.1", "gitCommitIdShortAutoMinimum": 7, "nugetPackageVersion": { "semVer": 2