From 52abc696d438a0c1bf29ec3a6ecc9208439341dd Mon Sep 17 00:00:00 2001 From: Rohit Ranjan <90008725+RohitRanjanMS@users.noreply.github.com> Date: Wed, 9 Oct 2024 13:29:07 -0700 Subject: [PATCH] Remove HttpStatusCode=OK from WriteAsJsonAsync (#2720) * WriteAsJsonAsync HttpStatusCode Fix --- sdk/release_notes.md | 1 + .../Http/HttpResponseDataExtensions.cs | 97 ++----------------- .../Http/HttpResponseDataExtensionsTests.cs | 12 +-- 3 files changed, 16 insertions(+), 94 deletions(-) diff --git a/sdk/release_notes.md b/sdk/release_notes.md index dfd76a920..178007943 100644 --- a/sdk/release_notes.md +++ b/sdk/release_notes.md @@ -9,6 +9,7 @@ - Updated `Microsoft.Azure.Functions.Worker.Sdk.Generators` reference to 1.3.4. - Setting _ToolingSuffix for TargetFrameworkVersion v9.0 - Adding support for SDK container builds with Functions base images +- Removed the default value for HttpStatusCode in WriteAsJsonAsync (#2720) ### Microsoft.Azure.Functions.Worker.Sdk.Generators 1.3.4 diff --git a/src/DotNetWorker.Core/Http/HttpResponseDataExtensions.cs b/src/DotNetWorker.Core/Http/HttpResponseDataExtensions.cs index e0f587559..e2689ecde 100644 --- a/src/DotNetWorker.Core/Http/HttpResponseDataExtensions.cs +++ b/src/DotNetWorker.Core/Http/HttpResponseDataExtensions.cs @@ -81,7 +81,7 @@ public static Task WriteStringAsync(this HttpResponseData response, string value /// /// Asynchronously writes the specified value as JSON to the response body using the default configured for this worker. - /// The response content-type will be set to application/json; charset=utf-8 and the status code set to 200. + /// The response content-type will be set to application/json; charset=utf-8. /// /// The type of object to write. /// The response to write JSON to. @@ -90,28 +90,12 @@ public static Task WriteStringAsync(this HttpResponseData response, string value /// A that represents the asynchronous operation. public static ValueTask WriteAsJsonAsync(this HttpResponseData response, T instance, CancellationToken cancellationToken = default) { - return WriteAsJsonAsync(response, instance, "application/json; charset=utf-8", HttpStatusCode.OK, cancellationToken); + return WriteAsJsonAsync(response, instance, "application/json; charset=utf-8", cancellationToken); } /// /// Asynchronously writes the specified value as JSON to the response body using the default configured for this worker. - /// The response content-type will be set to application/json; charset=utf-8 and the status code set to the provided . - /// - /// The type of object to write. - /// The response to write JSON to. - /// The instance to serialize and write as JSON. - /// The status code to set on the response. - /// A used to cancel the operation. - /// A that represents the asynchronous operation. - public static ValueTask WriteAsJsonAsync(this HttpResponseData response, T instance, HttpStatusCode statusCode, - CancellationToken cancellationToken = default) - { - return WriteAsJsonAsync(response, instance, "application/json; charset=utf-8", statusCode, cancellationToken); - } - - /// - /// Asynchronously writes the specified value as JSON to the response body using the default configured for this worker. - /// The response content-type will be set to the provided and the status code set to 200. + /// The response content-type will be set to the provided . /// /// The type of object to write. /// The response to write JSON to. @@ -128,37 +112,12 @@ public static ValueTask WriteAsJsonAsync(this HttpResponseData response, T in ObjectSerializer serializer = GetObjectSerializer(response); - return WriteAsJsonAsync(response, instance, serializer, contentType, HttpStatusCode.OK, cancellationToken); - } - - /// - /// Asynchronously writes the specified value as JSON to the response body using the default configured for this worker. - /// The response content-type will be set to the provided and the status code set to the provided . - /// - /// The type of object to write. - /// The response to write JSON to. - /// The instance to serialize and write as JSON. - /// The content-type to set on the response. - /// The status code to set on the response. - /// A used to cancel the operation. - /// A that represents the asynchronous operation. - public static ValueTask WriteAsJsonAsync(this HttpResponseData response, T instance, string contentType, HttpStatusCode statusCode, - CancellationToken cancellationToken = default) - { - if (response is null) - { - throw new ArgumentNullException(nameof(response)); - } - - ObjectSerializer serializer = GetObjectSerializer(response); - - return WriteAsJsonAsync(response, instance, serializer, contentType, statusCode, cancellationToken); + return WriteAsJsonAsync(response, instance, serializer, contentType, cancellationToken); } - /// /// Asynchronously writes the specified value as JSON to the response body using the provided . - /// The response content-type will be set to application/json; charset=utf-8 and the status code set to 200. + /// The response content-type will be set to application/json; charset=utf-8. /// /// The type of object to write. /// The response to write JSON to. @@ -168,29 +127,12 @@ public static ValueTask WriteAsJsonAsync(this HttpResponseData response, T in /// A that represents the asynchronous operation. public static ValueTask WriteAsJsonAsync(this HttpResponseData response, T instance, ObjectSerializer serializer, CancellationToken cancellationToken = default) { - return WriteAsJsonAsync(response, instance, serializer, "application/json; charset=utf-8", HttpStatusCode.OK, cancellationToken); + return WriteAsJsonAsync(response, instance, serializer, "application/json; charset=utf-8", cancellationToken); } - - /// - /// Asynchronously writes the specified value as JSON to the response body using the provided . - /// The response content-type will be set to application/json; charset=utf-8 and the status code set to the provided . - /// - /// The type of object to write. - /// The response to write JSON to. - /// The instance to serialize and write as JSON. - /// The serializer used to serialize the instance. - /// The status code to set on the response. - /// A used to cancel the operation. - /// A that represents the asynchronous operation. - public static ValueTask WriteAsJsonAsync(this HttpResponseData response, T instance, ObjectSerializer serializer, HttpStatusCode statusCode, - CancellationToken cancellationToken = default) - { - return WriteAsJsonAsync(response, instance, serializer, "application/json; charset=utf-8", statusCode, cancellationToken); - } - + /// /// Asynchronously writes the specified value as JSON to the response body using the provided . - /// The response content-type will be set to the provided and the status code set to 200. + /// The response content-type will be set to the provided . /// /// The type of object to write. /// The response to write JSON to. @@ -199,26 +141,7 @@ public static ValueTask WriteAsJsonAsync(this HttpResponseData response, T in /// The content-type to set on the response. /// A used to cancel the operation. /// A that represents the asynchronous operation. - public static ValueTask WriteAsJsonAsync(this HttpResponseData response, T instance, - ObjectSerializer serializer, string contentType, - CancellationToken cancellationToken = default) - { - return WriteAsJsonAsync(response, instance, serializer, contentType, HttpStatusCode.OK, cancellationToken); - } - - /// - /// Asynchronously writes the specified value as JSON to the response body using the provided . - /// The response content-type will be set to the provided and the status code set to the provided . - /// - /// The type of object to write. - /// The response to write JSON to. - /// The instance to serialize and write as JSON. - /// The serializer used to serialize the instance. - /// The content-type to set on the response. - /// The status code to set on the response. - /// A used to cancel the operation. - /// A that represents the asynchronous operation. - public static ValueTask WriteAsJsonAsync(this HttpResponseData response, T instance, ObjectSerializer serializer, string contentType, HttpStatusCode statusCode, CancellationToken cancellationToken = default) + public static ValueTask WriteAsJsonAsync(this HttpResponseData response, T instance, ObjectSerializer serializer, string contentType, CancellationToken cancellationToken = default) { if (response is null) { @@ -236,8 +159,6 @@ public static ValueTask WriteAsJsonAsync(this HttpResponseData response, T in } response.Headers.Add("Content-Type", contentType); - response.StatusCode = statusCode; - return serializer.SerializeAsync(response.Body, instance, typeof(T), cancellationToken); } diff --git a/test/DotNetWorkerTests/Http/HttpResponseDataExtensionsTests.cs b/test/DotNetWorkerTests/Http/HttpResponseDataExtensionsTests.cs index 09b51af49..2da456062 100644 --- a/test/DotNetWorkerTests/Http/HttpResponseDataExtensionsTests.cs +++ b/test/DotNetWorkerTests/Http/HttpResponseDataExtensionsTests.cs @@ -68,7 +68,7 @@ public async Task WriteAsJsonAsync_UsesRegisteredSerializer() public async Task WriteAsJsonAsync_ContentTypeOverload_AppliesParameters() { FunctionContext context = CreateContext(new NewtonsoftJsonObjectSerializer()); - var response = CreateResponse(context); + var response = CreateResponse(context, HttpStatusCode.Accepted); var poco = new ResponsePoco { @@ -81,7 +81,7 @@ public async Task WriteAsJsonAsync_ContentTypeOverload_AppliesParameters() string result = ReadResponseBody(response); Assert.Equal("application/json", response.Headers.GetValues("content-type").FirstOrDefault()); - Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.Equal(HttpStatusCode.Accepted, response.StatusCode); Assert.Equal("{\"jsonnetname\":\"Test\",\"jsonnetint\":42}", result); } @@ -89,7 +89,7 @@ public async Task WriteAsJsonAsync_ContentTypeOverload_AppliesParameters() public async Task WriteAsJsonAsync_StatusCodeOverload_AppliesParameters() { FunctionContext context = CreateContext(new NewtonsoftJsonObjectSerializer()); - var response = CreateResponse(context); + var response = CreateResponse(context, HttpStatusCode.BadRequest); var poco = new ResponsePoco { @@ -97,7 +97,7 @@ public async Task WriteAsJsonAsync_StatusCodeOverload_AppliesParameters() SomeInt = 42 }; - await HttpResponseDataExtensions.WriteAsJsonAsync(response, poco, HttpStatusCode.BadRequest); + await HttpResponseDataExtensions.WriteAsJsonAsync(response, poco); string result = ReadResponseBody(response); @@ -127,9 +127,9 @@ public async Task WriteAsJsonAsync_SerializerAndContentTypeOverload_AppliesParam Assert.Equal("{\"jsonnetname\":\"Test\",\"jsonnetint\":42}", result); } - private static TestHttpResponseData CreateResponse(FunctionContext context) + private static TestHttpResponseData CreateResponse(FunctionContext context, HttpStatusCode statusCode = HttpStatusCode.OK) { - var response = new TestHttpResponseData(context, HttpStatusCode.Accepted); + var response = new TestHttpResponseData(context, statusCode); response.Body = new MemoryStream(); response.Headers = new HttpHeadersCollection(); return response;