Skip to content

Commit

Permalink
Add C# documentation comments (#15)
Browse files Browse the repository at this point in the history
* Add C# documentation comments

* Add tests for error message
  • Loading branch information
marcusturewicz authored Sep 2, 2021
1 parent f744875 commit d8d52c0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
23 changes: 23 additions & 0 deletions src/Standards.AspNetCore/Dates/IsoDateModelBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@

namespace Standards.AspNetCore
{
/// <summary>
/// An ASP.NET Core model binder to enforce ISO-8601 (YYYY-MM-DD) format for <see cref="System.DateTime"/> objects.
/// <example>For example, apply to a Controller action:
/// <code>
/// [HttpGet]
/// public IActionResult Get([ModelBinder(typeof(IsoDateModelBinder))] DateTime date) {
/// return Ok("Date is in ISO format!");
/// }
/// </code>
/// </example>
/// </summary>
public class IsoDateModelBinder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
Expand All @@ -32,6 +43,18 @@ public Task BindModelAsync(ModelBindingContext bindingContext)
}
}

/// <summary>
/// An ASP.NET Core model binder provider to enforce ISO-8601 (YYYY-MM-DD) format for <see cref="System.DateTime"/> objects.
/// <example>For example, apply globally in ASP.NET Core Startup.cs:
/// <code>
/// public override void ConfigureServices(IServiceCollection services) {
/// services.AddControllers(options => {
/// options.ModelBinderProviders.Insert(0, new IsoDateModelBinderProvider());
/// })
/// }
/// </code>
/// </example>
/// </summary>
public class IsoDateModelBinderProvider : IModelBinderProvider
{
public IModelBinder GetBinder(ModelBinderProviderContext context)
Expand Down
26 changes: 17 additions & 9 deletions tests/Standards.AspNetCore.Tests/IsoDateModelBinderTests.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -49,17 +45,29 @@ public IsoDateModelBinderTests(WebApplicationFactory<Startup> 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<ProblemDetails>(responseStream);
var error = JsonSerializer.Deserialize<Error>(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; }
}
}
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit d8d52c0

Please sign in to comment.