diff --git a/WebApi.Hal.Build/WebApi.Hal.Commons.props b/WebApi.Hal.Build/WebApi.Hal.Commons.props index 9d0eb5e..a3001e3 100644 --- a/WebApi.Hal.Build/WebApi.Hal.Commons.props +++ b/WebApi.Hal.Build/WebApi.Hal.Commons.props @@ -6,9 +6,9 @@ Jake Ginnivan Enabled Hypermedia support in Asp.net Web API using the HAL mediatype - Copyright © Jake Ginnvan 2012 + Copyright © Jake Ginnvan 2020 https://github.com/JakeGinnivan/WebApi.Hal - $(PackageProjectUrl)/blob/master/LICENSE.md + $(PackageProjectUrl)/blob/master/LICENSE.md $(PackageProjectUrl)/releases false diff --git a/WebApi.Hal.Tests/HalResourceListTests.cs b/WebApi.Hal.Tests/HalResourceListTests.cs index 2da5723..c62d9e9 100644 --- a/WebApi.Hal.Tests/HalResourceListTests.cs +++ b/WebApi.Hal.Tests/HalResourceListTests.cs @@ -3,6 +3,7 @@ using System.IO; using System.Net.Http; using Assent; +using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using WebApi.Hal.Tests.Representations; using Xunit; @@ -54,7 +55,7 @@ public void organisation_list_get_json_test() { // arrange var mediaFormatter = new JsonHalMediaTypeOutputFormatter( - new JsonSerializerSettings { Formatting = Formatting.Indented }, ArrayPool.Shared); + new JsonSerializerSettings { Formatting = Formatting.Indented }, ArrayPool.Shared, new MvcOptions()); // act using (var stream = new StringWriter()) @@ -73,7 +74,7 @@ public void one_item_organisation_list_get_json_test() { // arrange var mediaFormatter = new JsonHalMediaTypeOutputFormatter( - new JsonSerializerSettings { Formatting = Formatting.Indented }, ArrayPool.Shared); + new JsonSerializerSettings { Formatting = Formatting.Indented }, ArrayPool.Shared, new MvcOptions()); var content = new StringContent(string.Empty); var type = oneitemrepresentation.GetType(); @@ -89,4 +90,4 @@ public void one_item_organisation_list_get_json_test() } } } -} \ No newline at end of file +} diff --git a/WebApi.Hal.Tests/HalResourceMixedContentTest.cs b/WebApi.Hal.Tests/HalResourceMixedContentTest.cs index 985dcd7..a3c2b67 100644 --- a/WebApi.Hal.Tests/HalResourceMixedContentTest.cs +++ b/WebApi.Hal.Tests/HalResourceMixedContentTest.cs @@ -7,9 +7,7 @@ using Assent; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.Internal; using Microsoft.AspNetCore.Mvc.ModelBinding; -using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata; using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.ObjectPool; using Newtonsoft.Json; @@ -41,7 +39,7 @@ public void peopledetail_get_json_test() { // arrange var mediaFormatter = new JsonHalMediaTypeOutputFormatter( - new JsonSerializerSettings { Formatting = Formatting.Indented }, ArrayPool.Shared); + new JsonSerializerSettings { Formatting = Formatting.Indented }, ArrayPool.Shared, new MvcOptions()); // act using (var stream = new StringWriter()) @@ -71,7 +69,7 @@ public void peopledetail_non_empty_resource_list_get_json_test() }; var mediaFormatter = new JsonHalMediaTypeOutputFormatter( - new JsonSerializerSettings { Formatting = Formatting.Indented }, ArrayPool.Shared); + new JsonSerializerSettings { Formatting = Formatting.Indented }, ArrayPool.Shared, new MvcOptions()); // act using (var stream = new StringWriter()) @@ -96,7 +94,7 @@ public void peopledetail_empty_resource_list_get_json_test() }; var mediaFormatter = new JsonHalMediaTypeOutputFormatter( - new JsonSerializerSettings { Formatting = Formatting.Indented }, ArrayPool.Shared); + new JsonSerializerSettings { Formatting = Formatting.Indented }, ArrayPool.Shared, new MvcOptions()); // act using (var stream = new StringWriter()) @@ -137,7 +135,7 @@ public async Task peopledetail_post_json_props_test() new JsonSerializerSettings { Formatting = Formatting.Indented }, ArrayPool.Shared, new DefaultObjectPoolProvider(), - new MvcOptions(), new MvcJsonOptions()); + new MvcOptions(), new MvcNewtonsoftJsonOptions()); var type = typeof(OrganisationWithPeopleDetailRepresentation); const string json = @" @@ -181,7 +179,7 @@ public async Task peopledetail_post_json_links_test() ArrayPool.Shared, new DefaultObjectPoolProvider(), new MvcOptions(), - new MvcJsonOptions()); + new MvcNewtonsoftJsonOptions()); var type = typeof(OrganisationWithPeopleRepresentation); const string json = @" @@ -243,7 +241,7 @@ public async Task peopledetail_post_json_embedded_singles_test() ArrayPool.Shared, new DefaultObjectPoolProvider(), new MvcOptions(), - new MvcJsonOptions()); + new MvcNewtonsoftJsonOptions()); var type = typeof(OrganisationWithPeopleDetailRepresentation); const string json = @" @@ -294,7 +292,7 @@ public async Task peopledetail_post_json_embedded_arrays_test() ArrayPool.Shared, new DefaultObjectPoolProvider(), new MvcOptions(), - new MvcJsonOptions()); + new MvcNewtonsoftJsonOptions()); var type = typeof(OrganisationWithPeopleDetailRepresentation); const string json = @" @@ -349,7 +347,7 @@ public async Task peopledetail_post_json_embedded_null_test() ArrayPool.Shared, new DefaultObjectPoolProvider(), new MvcOptions(), - new MvcJsonOptions()); + new MvcNewtonsoftJsonOptions()); var type = typeof(OrganisationWithPeopleDetailRepresentation); const string json = @" @@ -402,7 +400,7 @@ public async Task simplelist_post_json_test() ArrayPool.Shared, new DefaultObjectPoolProvider(), new MvcOptions(), - new MvcJsonOptions()); + new MvcNewtonsoftJsonOptions()); var type = typeof(MySimpleList); const string json = @" @@ -448,16 +446,9 @@ public async Task simplelist_post_json_test() } } - public static IModelMetadataProvider CreateDefaultProvider() + public static ModelMetadataProvider CreateDefaultProvider() { - var detailsProviders = new IMetadataDetailsProvider[] - { - new DefaultBindingMetadataProvider(), - new DefaultValidationMetadataProvider() - }; - - var compositeDetailsProvider = new DefaultCompositeMetadataDetailsProvider(detailsProviders); - return new DefaultModelMetadataProvider(compositeDetailsProvider); + return new EmptyModelMetadataProvider(); } } } diff --git a/WebApi.Hal.Tests/HalResourceTest.cs b/WebApi.Hal.Tests/HalResourceTest.cs index 7f5828b..74e8c21 100644 --- a/WebApi.Hal.Tests/HalResourceTest.cs +++ b/WebApi.Hal.Tests/HalResourceTest.cs @@ -1,6 +1,7 @@ using System.Buffers; using System.IO; using Assent; +using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using WebApi.Hal.Tests.Representations; using Xunit; @@ -21,7 +22,7 @@ public void organisation_get_json_test() { // arrange var mediaFormatter = new JsonHalMediaTypeOutputFormatter( - new JsonSerializerSettings { Formatting = Formatting.Indented }, ArrayPool.Shared); + new JsonSerializerSettings { Formatting = Formatting.Indented }, ArrayPool.Shared, new MvcOptions()); // act using (var stream = new StringWriter()) @@ -40,7 +41,7 @@ public void organisation_get_json_with_app_path_test() { // arrange var mediaFormatter = new JsonHalMediaTypeOutputFormatter( - new JsonSerializerSettings { Formatting = Formatting.Indented }, ArrayPool.Shared); + new JsonSerializerSettings { Formatting = Formatting.Indented }, ArrayPool.Shared, new MvcOptions()); var resourceWithAppPath = new OrganisationWithAppPathRepresentation(1, "Org Name"); // act @@ -60,7 +61,7 @@ public void organisation_get_json_with_no_href_test() { // arrange var mediaFormatter = new JsonHalMediaTypeOutputFormatter( - new JsonSerializerSettings { Formatting = Formatting.Indented }, ArrayPool.Shared); + new JsonSerializerSettings { Formatting = Formatting.Indented }, ArrayPool.Shared, new MvcOptions()); var resourceWithAppPath = new OrganisationWithNoHrefRepresentation(1, "Org Name"); // act @@ -80,7 +81,7 @@ public void organisation_get_json_with_link_title_test() { // arrange var mediaFormatter = new JsonHalMediaTypeOutputFormatter( - new JsonSerializerSettings { Formatting = Formatting.Indented }, ArrayPool.Shared); + new JsonSerializerSettings { Formatting = Formatting.Indented }, ArrayPool.Shared, new MvcOptions()); var resourceWithAppPath = new OrganisationWithLinkTitleRepresentation(1, "Org Name"); // act @@ -113,4 +114,4 @@ public void organisation_get_xml_test() } } } -} \ No newline at end of file +} diff --git a/WebApi.Hal.Tests/HalResourceWithPeopleTest.cs b/WebApi.Hal.Tests/HalResourceWithPeopleTest.cs index cb1c8b8..a7560a7 100644 --- a/WebApi.Hal.Tests/HalResourceWithPeopleTest.cs +++ b/WebApi.Hal.Tests/HalResourceWithPeopleTest.cs @@ -1,6 +1,7 @@ using System.Buffers; using System.IO; using Assent; +using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using WebApi.Hal.Tests.Representations; using Xunit; @@ -21,7 +22,7 @@ public void organisation_get_json_test() { // arrange var mediaFormatter = new JsonHalMediaTypeOutputFormatter( - new JsonSerializerSettings { Formatting = Formatting.Indented }, ArrayPool.Shared); + new JsonSerializerSettings { Formatting = Formatting.Indented }, ArrayPool.Shared, new MvcOptions()); // act using (var stream = new StringWriter()) @@ -53,4 +54,4 @@ public void organisation_get_xml_test() } } } -} \ No newline at end of file +} diff --git a/WebApi.Hal.Tests/HypermediaContainerTests.cs b/WebApi.Hal.Tests/HypermediaContainerTests.cs index 318567b..99142e0 100644 --- a/WebApi.Hal.Tests/HypermediaContainerTests.cs +++ b/WebApi.Hal.Tests/HypermediaContainerTests.cs @@ -2,6 +2,7 @@ using System.IO; using System.Linq; using Assent; +using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using WebApi.Hal.Tests.HypermediaAppenders; using WebApi.Hal.Tests.Representations; @@ -29,7 +30,7 @@ public void CanUseRegisterExtensionMethod() // arrange var mediaFormatter = new JsonHalMediaTypeOutputFormatter( - new JsonSerializerSettings { Formatting = Formatting.Indented }, ArrayPool.Shared, config); + new JsonSerializerSettings { Formatting = Formatting.Indented }, ArrayPool.Shared, new MvcOptions(), config); // act using (var stream = new StringWriter()) diff --git a/WebApi.Hal.Tests/ResolvingHalResourceTest.cs b/WebApi.Hal.Tests/ResolvingHalResourceTest.cs index 9884df9..43f14fd 100644 --- a/WebApi.Hal.Tests/ResolvingHalResourceTest.cs +++ b/WebApi.Hal.Tests/ResolvingHalResourceTest.cs @@ -1,6 +1,7 @@ using System.Buffers; using System.IO; using Assent; +using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using WebApi.Hal.Tests.HypermediaAppenders; using WebApi.Hal.Tests.Representations; @@ -51,7 +52,7 @@ public void ProperlySerializesRepresentationToJson() var config = builder.Build(); var mediaFormatter = new JsonHalMediaTypeOutputFormatter( - new JsonSerializerSettings { Formatting = Formatting.Indented }, ArrayPool.Shared, config); + new JsonSerializerSettings { Formatting = Formatting.Indented }, ArrayPool.Shared, new MvcOptions(), config); // act using (var stream = new StringWriter()) @@ -72,7 +73,7 @@ public void ProperlySerializesRepresentationWithoutLinksToJson() var builder = Hypermedia.CreateBuilder(); var config = builder.Build(); var mediaFormatter = new JsonHalMediaTypeOutputFormatter( - new JsonSerializerSettings { Formatting = Formatting.Indented }, ArrayPool.Shared, config); + new JsonSerializerSettings { Formatting = Formatting.Indented }, ArrayPool.Shared, new MvcOptions(), config); // act using (var stream = new StringWriter()) @@ -86,4 +87,4 @@ public void ProperlySerializesRepresentationWithoutLinksToJson() } } } -} \ No newline at end of file +} diff --git a/WebApi.Hal.Tests/WebApi.Hal.Tests.csproj b/WebApi.Hal.Tests/WebApi.Hal.Tests.csproj index 675d8f4..411068f 100644 --- a/WebApi.Hal.Tests/WebApi.Hal.Tests.csproj +++ b/WebApi.Hal.Tests/WebApi.Hal.Tests.csproj @@ -3,7 +3,7 @@ - netcoreapp2.0 + netcoreapp3.1 3.0.0-pre-1 Jake Ginnivan, DotNetMedia Organizaion Copyright © Jake Ginnvan 2017 @@ -11,11 +11,11 @@ - - - - - + + + + + all runtime; build; native; contentfiles; analyzers diff --git a/WebApi.Hal.Web/FormattersMvcOptionsSetup.cs b/WebApi.Hal.Web/FormattersMvcOptionsSetup.cs index 12d7ff4..3309ebb 100644 --- a/WebApi.Hal.Web/FormattersMvcOptionsSetup.cs +++ b/WebApi.Hal.Web/FormattersMvcOptionsSetup.cs @@ -14,11 +14,11 @@ public class FormattersMvcOptionsSetup : IConfigureOptions private readonly JsonSerializerSettings _jsonSerializerSettings; private readonly ArrayPool _charPool; private readonly ObjectPoolProvider _objectPoolProvider; - private readonly MvcJsonOptions _mvcJsonOptions; + private readonly MvcNewtonsoftJsonOptions _mvcJsonOptions; public FormattersMvcOptionsSetup( ILoggerFactory loggerFactory, - IOptions jsonOptions, + IOptions jsonOptions, ArrayPool charPool, ObjectPoolProvider objectPoolProvider) { @@ -37,7 +37,7 @@ public class FormattersMvcOptionsSetup : IConfigureOptions public void Configure(MvcOptions options) { options.OutputFormatters.Add(new XmlHalMediaTypeOutputFormatter()); - options.OutputFormatters.Add(new JsonHalMediaTypeOutputFormatter(_jsonSerializerSettings, _charPool)); + options.OutputFormatters.Add(new JsonHalMediaTypeOutputFormatter(_jsonSerializerSettings, _charPool, options)); // Register JsonPatchInputFormatter before JsonInputFormatter, otherwise // JsonInputFormatter would consume "application/json-patch+json" requests diff --git a/WebApi.Hal.Web/Startup.cs b/WebApi.Hal.Web/Startup.cs index fad8a12..5843990 100644 --- a/WebApi.Hal.Web/Startup.cs +++ b/WebApi.Hal.Web/Startup.cs @@ -5,8 +5,10 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; +using Microsoft.OpenApi.Models; using Swashbuckle.AspNetCore.Swagger; using WebApi.Hal.Web.Data; @@ -14,7 +16,7 @@ namespace WebApi.Hal.Web { public class Startup { - public Startup(IHostingEnvironment hostingEnvironment) + public Startup(IWebHostEnvironment hostingEnvironment) { Configuration = new ConfigurationBuilder() .SetBasePath(hostingEnvironment.ContentRootPath) @@ -30,14 +32,15 @@ public void ConfigureServices(IServiceCollection services) { services .AddMvcCore() - .AddJsonFormatters( + .AddNewtonsoftJson( options => { - options.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore; - options.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; + options.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore; + options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; }) .AddApiExplorer() - .SetCompatibilityVersion(CompatibilityVersion.Version_2_1); + .SetCompatibilityVersion(CompatibilityVersion.Latest); + services.AddControllers(); services.TryAddEnumerable( ServiceDescriptor.Transient, FormattersMvcOptionsSetup>()); @@ -46,20 +49,22 @@ public void ConfigureServices(IServiceCollection services) services.AddScoped(); services.AddScoped(); + services.AddLogging(options => + { + options.AddConsole(); + }); + services.AddSwaggerGen( options => { - options.DescribeAllEnumsAsStrings(); - options.SwaggerDoc("v1", new Info {Title = "WebApi.Hal Demo API", Version = "v1"}); + options.SwaggerDoc("v1", new OpenApiInfo {Title = "WebApi.Hal Demo API", Version = "v1"}); options.CustomSchemaIds(type => type.FullName); }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, BeerDbContext beerDbContext) + public void Configure(IApplicationBuilder app, IWebHostEnvironment env, BeerDbContext beerDbContext) { - loggerFactory.AddConsole(); - if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); @@ -70,7 +75,12 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebApi.Hal Demo API V1"); }); - app.UseMvc(); + app.UseRouting(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + }); } } } diff --git a/WebApi.Hal.Web/WebApi.Hal.Web.csproj b/WebApi.Hal.Web/WebApi.Hal.Web.csproj index 3674842..3cd5fca 100644 --- a/WebApi.Hal.Web/WebApi.Hal.Web.csproj +++ b/WebApi.Hal.Web/WebApi.Hal.Web.csproj @@ -3,7 +3,7 @@ - netcoreapp2.1 + netcoreapp3.1 WebApi.Hal.Web @@ -12,10 +12,9 @@ - - - - + + + diff --git a/WebApi.Hal.sln b/WebApi.Hal.sln index 50466b0..836e75d 100644 --- a/WebApi.Hal.sln +++ b/WebApi.Hal.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.26430.16 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30204.135 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApi.Hal", "WebApi.Hal\WebApi.Hal.csproj", "{8A01C801-D7E6-4ACB-A843-7C3EFB7833F7}" EndProject @@ -12,6 +12,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Licence.md = Licence.md README.md = README.md Readme.txt = Readme.txt + WebApi.Hal.Build\WebApi.Hal.Commons.props = WebApi.Hal.Build\WebApi.Hal.Commons.props EndProjectSection EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApi.Hal.Web", "WebApi.Hal.Web\WebApi.Hal.Web.csproj", "{29ADB701-A036-4195-8478-2FE393CC7A74}" diff --git a/WebApi.Hal/JsonHalMediaTypeInputFormatter.cs b/WebApi.Hal/JsonHalMediaTypeInputFormatter.cs index 437c2d5..27dd578 100644 --- a/WebApi.Hal/JsonHalMediaTypeInputFormatter.cs +++ b/WebApi.Hal/JsonHalMediaTypeInputFormatter.cs @@ -9,13 +9,13 @@ namespace WebApi.Hal { - public class JsonHalMediaTypeInputFormatter : JsonInputFormatter + public class JsonHalMediaTypeInputFormatter : NewtonsoftJsonInputFormatter { private readonly LinksConverter _linksConverter = new LinksConverter(); private readonly ResourceConverter _resourceConverter; private readonly EmbeddedResourceConverter _embeddedResourceConverter = new EmbeddedResourceConverter(); - public JsonHalMediaTypeInputFormatter(ILogger logger, JsonSerializerSettings serializerSettings, ArrayPool charPool, ObjectPoolProvider objectPoolProvider, IHypermediaResolver hypermediaResolver, MvcOptions mvcOptions, MvcJsonOptions mvcJsonOptions) + public JsonHalMediaTypeInputFormatter(ILogger logger, JsonSerializerSettings serializerSettings, ArrayPool charPool, ObjectPoolProvider objectPoolProvider, IHypermediaResolver hypermediaResolver, MvcOptions mvcOptions, MvcNewtonsoftJsonOptions mvcJsonOptions) : base(logger, serializerSettings, charPool, objectPoolProvider, mvcOptions, mvcJsonOptions) { if (hypermediaResolver == null) @@ -27,7 +27,7 @@ public JsonHalMediaTypeInputFormatter(ILogger logger, JsonSerializerSettings ser Initialize(); } - public JsonHalMediaTypeInputFormatter(ILogger logger, JsonSerializerSettings serializerSettings, ArrayPool charPool, ObjectPoolProvider objectPoolProvider, MvcOptions mvcOptions, MvcJsonOptions mvcJsonOptions) + public JsonHalMediaTypeInputFormatter(ILogger logger, JsonSerializerSettings serializerSettings, ArrayPool charPool, ObjectPoolProvider objectPoolProvider, MvcOptions mvcOptions, MvcNewtonsoftJsonOptions mvcJsonOptions) : base(logger, serializerSettings, charPool, objectPoolProvider, mvcOptions, mvcJsonOptions) { _resourceConverter = new ResourceConverter(SerializerSettings); diff --git a/WebApi.Hal/JsonHalMediaTypeOutputFormatter.cs b/WebApi.Hal/JsonHalMediaTypeOutputFormatter.cs index bcc6caf..ee751e3 100644 --- a/WebApi.Hal/JsonHalMediaTypeOutputFormatter.cs +++ b/WebApi.Hal/JsonHalMediaTypeOutputFormatter.cs @@ -1,6 +1,7 @@ using System; using System.Buffers; -using System.Reflection; +using System.IO; +using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Formatters; using Microsoft.Net.Http.Headers; using Newtonsoft.Json; @@ -8,7 +9,7 @@ namespace WebApi.Hal { - public class JsonHalMediaTypeOutputFormatter : JsonOutputFormatter + public class JsonHalMediaTypeOutputFormatter : NewtonsoftJsonOutputFormatter { private const string _mediaTypeHeaderValueName = "application/hal+json"; @@ -19,9 +20,10 @@ public class JsonHalMediaTypeOutputFormatter : JsonOutputFormatter public JsonHalMediaTypeOutputFormatter( JsonSerializerSettings serializerSettings, - ArrayPool charPool, + ArrayPool charPool, + MvcOptions mvcOptions, IHypermediaResolver hypermediaResolver) : - base(serializerSettings, charPool) + base(serializerSettings, charPool, mvcOptions) { if (hypermediaResolver == null) { @@ -34,13 +36,19 @@ public class JsonHalMediaTypeOutputFormatter : JsonOutputFormatter public JsonHalMediaTypeOutputFormatter( JsonSerializerSettings serializerSettings, - ArrayPool charPool) : - base(serializerSettings, charPool) + ArrayPool charPool, + MvcOptions mvcOptions) : + base(serializerSettings, charPool, mvcOptions) { _resourceConverter = new ResourceConverter(SerializerSettings); Initialize(); } + public void WriteObject(TextWriter stream, object value) + { + CreateJsonSerializer().Serialize(stream, value); + } + private void Initialize() { SupportedMediaTypes.Add(new MediaTypeHeaderValue(_mediaTypeHeaderValueName)); diff --git a/WebApi.Hal/WebApi.Hal.csproj b/WebApi.Hal/WebApi.Hal.csproj index 9e0b4b9..199dfd3 100644 --- a/WebApi.Hal/WebApi.Hal.csproj +++ b/WebApi.Hal/WebApi.Hal.csproj @@ -3,23 +3,23 @@ - netstandard2.0 - 3.1.0 - 3.1.0 - Copyright © Jake Ginnivan 2018 + netcoreapp3.1 + 4.0.0 + 4.0.0 + Copyright © Jake Ginnivan 2020 Adds support for the Hal Media Type (and Hypermedia) to Asp.net https://github.com/JakeGinnivan/WebApi.Hal true + 4.0.0 update to support .net core 3.1 3.10 updates to support multithreaded usage 3.0.0 first .net standard release - - - + + diff --git a/WebApi.Hal/XmlHalMediaTypeOutputFormatter.cs b/WebApi.Hal/XmlHalMediaTypeOutputFormatter.cs index 143a77f..6664ec1 100644 --- a/WebApi.Hal/XmlHalMediaTypeOutputFormatter.cs +++ b/WebApi.Hal/XmlHalMediaTypeOutputFormatter.cs @@ -14,7 +14,7 @@ namespace WebApi.Hal { - public class XmlHalMediaTypeOutputFormatter : OutputFormatter + public class XmlHalMediaTypeOutputFormatter : TextOutputFormatter { private const string _mediaTypeHeaderValueName = "application/hal+xml"; @@ -23,7 +23,7 @@ public XmlHalMediaTypeOutputFormatter() Initialize(); } - public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext context) + public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding) { if (context == null) { @@ -32,9 +32,8 @@ public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext co var response = context.HttpContext.Response; var contentType = context.ContentType; - var encoding = (contentType.HasValue ? (Encoding)Enum.Parse(typeof(Encoding), contentType.Value) : null) ?? Encoding.UTF8; var memoryStream = new MemoryStream(); - using (var textWriter = context.WriterFactory(memoryStream, encoding)) + using (var textWriter = context.WriterFactory(memoryStream, selectedEncoding)) { WriteObject(textWriter, context.Object); await textWriter.FlushAsync(); @@ -59,7 +58,7 @@ public void WriteObject(TextWriter writer, object value) var settings = new XmlWriterSettings { Indent = true, - Encoding = Encoding.UTF8 + Encoding = writer.Encoding }; using (var xmlWriter = XmlWriter.Create(writer, settings)) { @@ -71,7 +70,8 @@ public void WriteObject(TextWriter writer, object value) private void Initialize() { SupportedMediaTypes.Add(new MediaTypeHeaderValue(_mediaTypeHeaderValueName)); - } + SupportedEncodings.Add(Encoding.UTF8); + } /// /// ReadHalResource will