diff --git a/src/Tgstation.Server.Api/ApiHeaders.cs b/src/Tgstation.Server.Api/ApiHeaders.cs index 3758ec9b34..fd73f49f5d 100644 --- a/src/Tgstation.Server.Api/ApiHeaders.cs +++ b/src/Tgstation.Server.Api/ApiHeaders.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Net.Http.Headers; +using System.Net.Mime; using System.Reflection; using System.Text; @@ -16,11 +17,6 @@ namespace Tgstation.Server.Api /// public sealed class ApiHeaders { - /// - /// TODO: Remove this when we upgrade to .NET Standard 2.1 - /// - public const string ApplicationJson = "application/json"; - /// /// The header key /// @@ -146,9 +142,9 @@ namespace Tgstation.Server.Api if (requestHeaders == null) throw new ArgumentNullException(nameof(requestHeaders)); - var jsonAccept = new Microsoft.Net.Http.Headers.MediaTypeHeaderValue(ApplicationJson); + var jsonAccept = new Microsoft.Net.Http.Headers.MediaTypeHeaderValue(MediaTypeNames.Application.Json); if (!requestHeaders.Accept.Any(x => x.MediaType == jsonAccept.MediaType)) - throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "Client does not accept {0}!", ApplicationJson)); + throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "Client does not accept {0}!", MediaTypeNames.Application.Json)); if (!requestHeaders.Headers.TryGetValue(HeaderNames.UserAgent, out var userAgentValues) || userAgentValues.Count == 0) throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "Missing {0} headers!", HeaderNames.UserAgent)); @@ -264,7 +260,7 @@ namespace Tgstation.Server.Api throw new InvalidOperationException("Specified instance ID in constructor and SetRequestHeaders!"); headers.Clear(); - headers.Accept.Add(new MediaTypeWithQualityHeaderValue(ApplicationJson)); + headers.Accept.Add(new MediaTypeWithQualityHeaderValue(MediaTypeNames.Application.Json)); if (IsTokenAuthentication) headers.Authorization = new AuthenticationHeaderValue(JwtAuthenticationScheme, Token); else diff --git a/src/Tgstation.Server.Client/ApiClient.cs b/src/Tgstation.Server.Client/ApiClient.cs index cf1041fc86..0e107e91f8 100644 --- a/src/Tgstation.Server.Client/ApiClient.cs +++ b/src/Tgstation.Server.Client/ApiClient.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; +using System.Net.Mime; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -157,7 +158,10 @@ namespace Tgstation.Server.Client using (var request = new HttpRequestMessage(method, fullUri)) { if (body != null) - request.Content = new StringContent(JsonConvert.SerializeObject(body, serializerSettings), Encoding.UTF8, ApiHeaders.ApplicationJson); + request.Content = new StringContent( + JsonConvert.SerializeObject(body, serializerSettings), + Encoding.UTF8, + MediaTypeNames.Application.Json); var headersToUse = tokenRefresh ? tokenRefreshHeaders! : headers; headersToUse.SetRequestHeaders(request.Headers, instanceId); diff --git a/src/Tgstation.Server.Host/Controllers/ApiController.cs b/src/Tgstation.Server.Host/Controllers/ApiController.cs index 5cd3865257..2ca7b9070f 100644 --- a/src/Tgstation.Server.Host/Controllers/ApiController.cs +++ b/src/Tgstation.Server.Host/Controllers/ApiController.cs @@ -6,6 +6,7 @@ using Serilog.Context; using System; using System.Linq; using System.Net; +using System.Net.Mime; using System.Threading.Tasks; using Tgstation.Server.Api; using Tgstation.Server.Api.Models; @@ -17,7 +18,7 @@ namespace Tgstation.Server.Host.Controllers /// /// A for API functions /// - [Produces(ApiHeaders.ApplicationJson)] + [Produces(MediaTypeNames.Application.Json)] [ApiController] public abstract class ApiController : Controller { diff --git a/src/Tgstation.Server.Host/Controllers/BridgeController.cs b/src/Tgstation.Server.Host/Controllers/BridgeController.cs index 3ee3155cd8..1a0e794e9a 100644 --- a/src/Tgstation.Server.Host/Controllers/BridgeController.cs +++ b/src/Tgstation.Server.Host/Controllers/BridgeController.cs @@ -5,9 +5,9 @@ using Newtonsoft.Json; using Serilog.Context; using System; using System.Net; +using System.Net.Mime; using System.Threading; using System.Threading.Tasks; -using Tgstation.Server.Api; using Tgstation.Server.Host.Components.Interop; using Tgstation.Server.Host.Components.Interop.Bridge; @@ -17,7 +17,7 @@ namespace Tgstation.Server.Host.Controllers /// for recieving DMAPI requests from DreamDaemon. /// [Route("Bridge")] - [Produces(ApiHeaders.ApplicationJson)] + [Produces(MediaTypeNames.Application.Json)] public class BridgeController : Controller { /// @@ -86,7 +86,7 @@ namespace Tgstation.Server.Host.Controllers var responseJson = JsonConvert.SerializeObject(response, DMApiConstants.SerializerSettings); logger.LogTrace("Bridge Response: {0}", responseJson); - return Content(responseJson, ApiHeaders.ApplicationJson); + return Content(responseJson, MediaTypeNames.Application.Json); } } } diff --git a/src/Tgstation.Server.Host/Core/SwaggerConfiguration.cs b/src/Tgstation.Server.Host/Core/SwaggerConfiguration.cs index 218740c51b..7bd6d01449 100644 --- a/src/Tgstation.Server.Host/Core/SwaggerConfiguration.cs +++ b/src/Tgstation.Server.Host/Core/SwaggerConfiguration.cs @@ -7,6 +7,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Net; +using System.Net.Mime; using Tgstation.Server.Api; using Tgstation.Server.Api.Models; using Tgstation.Server.Api.Rights; @@ -34,7 +35,7 @@ namespace Tgstation.Server.Host.Core var errorMessageContent = new Dictionary { { - ApiHeaders.ApplicationJson, + MediaTypeNames.Application.Json, new OpenApiMediaType { Schema = new OpenApiSchema @@ -90,6 +91,12 @@ namespace Tgstation.Server.Host.Core Content = errorMessageContent }); + AddDefaultResponse(HttpStatusCode.NotAcceptable, new OpenApiResponse + { + Description = "Invalid Accept header, TGS requires `Accept: application/json`.", + Content = errorMessageContent + }); + AddDefaultResponse(HttpStatusCode.InternalServerError, new OpenApiResponse { Description = ErrorCode.InternalServerError.Describe(),