Skip to content

Commit

Permalink
Excise remaining code using Newtonsoft.Json
Browse files Browse the repository at this point in the history
  • Loading branch information
idg10 committed Jan 5, 2023
1 parent 0101d19 commit d5bba5c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,14 @@ namespace Menes.Converters
public class UriConverter : IOpenApiConverter
{
private readonly OpenApiSchemaValidator validator;
private readonly IOpenApiConfiguration configuration;

/// <summary>
/// Initializes a new instance of the <see cref="UriConverter"/> class.
/// </summary>
/// <param name="validator">The <see cref="OpenApiSchemaValidator"/>.</param>
/// <param name="configuration">The OpenAPI host configuration.</param>
public UriConverter(OpenApiSchemaValidator validator, IOpenApiConfiguration configuration)
public UriConverter(OpenApiSchemaValidator validator)
{
this.validator = validator;
this.configuration = configuration;
}

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,14 @@ namespace Menes.Converters
public class UuidConverter : IOpenApiConverter
{
private readonly OpenApiSchemaValidator validator;
private readonly IOpenApiConfiguration configuration;

/// <summary>
/// Initializes a new instance of the <see cref="UuidConverter"/> class.
/// </summary>
/// <param name="validator">The <see cref="OpenApiSchemaValidator"/>.</param>
/// <param name="configuration">The OpenAPI host configuration.</param>
public UuidConverter(OpenApiSchemaValidator validator, IOpenApiConfiguration configuration)
public UuidConverter(OpenApiSchemaValidator validator)
{
this.validator = validator;
this.configuration = configuration;
}

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public class OpenApiSchemaValidator
.ToList();

private static readonly JsonDocument JsonNullDoc = JsonDocument.Parse("null");
private static JsonElement NullElement => JsonNullDoc.RootElement;

private readonly ILogger<OpenApiSchemaValidator> logger;

Expand All @@ -47,6 +46,8 @@ public OpenApiSchemaValidator(ILogger<OpenApiSchemaValidator> logger)
this.logger = logger;
}

private static JsonElement NullElement => JsonNullDoc.RootElement;

/// <summary>Validates the given data, throwing an <see cref="OpenApiInvalidFormatException"/> augmented with <see cref="OpenApiProblemDetailsExtensions"/> detailing the errors.</summary>
/// <param name="data">The data.</param>
/// <param name="schema">The schema.</param>
Expand Down
53 changes: 31 additions & 22 deletions Solutions/Menes.PetStore.Specs/Steps/Steps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ namespace Menes.PetStore.Specs.Steps
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NUnit.Framework;
using TechTalk.SpecFlow;

Expand All @@ -24,6 +24,7 @@ public class Steps
private static readonly Uri BaseUri = new("http://localhost:7071");

private readonly ScenarioContext scenarioContext;
private JsonObject? data;

public Steps(ScenarioContext scenarioContext)
{
Expand Down Expand Up @@ -80,8 +81,8 @@ public Task WhenIRequestAListOfPetsWithALimitOf(int limit)
[When("I request a list of pets using the value of '(.*)' from the previous response object")]
public Task WhenIRequestAListOfPetsUsingTheValueOfFromThePreviousResponseObject(string propertyPath)
{
JToken token = this.GetRequiredTokenFromResponseObject(propertyPath);
string value = token.Value<string>()!;
JsonNode token = this.GetRequiredTokenFromResponseObject(propertyPath);
string value = token.GetValue<string>()!;
return this.SendGetRequest(value);
}

Expand Down Expand Up @@ -112,9 +113,9 @@ public void ThenTheResponseShouldNotContainTheHeader(string headerName)
[Then("the response object should have an integer property called '(.*)' with value (.*)")]
public void ThenTheResponseShouldHaveSetTo(string propertyPath, int expectedValue)
{
JToken actualToken = this.GetRequiredTokenFromResponseObject(propertyPath);
JsonNode actualToken = this.GetRequiredTokenFromResponseObject(propertyPath);

int actualValue = actualToken.Value<int>();
int actualValue = actualToken.GetValue<int>();
Assert.AreEqual(expectedValue, actualValue);
}

Expand All @@ -127,26 +128,25 @@ public void ThenTheResponseObjectShouldHaveAPropertyCalled(string propertyPath)
[Then("the response object should have a string property called '(.*)' with value '(.*)'")]
public void ThenTheResponseObjectShouldHaveAStringPropertyCalledWithValue(string propertyPath, string expectedValue)
{
JToken actualToken = this.GetRequiredTokenFromResponseObject(propertyPath);
JsonNode actualToken = this.GetRequiredTokenFromResponseObject(propertyPath);

string? actualValue = actualToken.Value<string>();
string? actualValue = actualToken.GetValue<string>();
Assert.AreEqual(expectedValue, actualValue);
}

[Then("the response object should not have a property called '(.*)'")]
public void ThenTheResponseObjectShouldNotHaveAPropertyCalled(string propertyPath)
{
JObject data = this.scenarioContext.Get<JObject>();
JToken? token = data.SelectToken(propertyPath);
JsonNode? token = this.GetTokenFromResponseObject(propertyPath);
Assert.IsNull(token);
}

[Then("the response object should have an array property called '(.*)' containing (.*) entries")]
public void ThenTheResponseObjectShouldHaveAnArrayPropertyCalledContainingEntries(string propertyPath, int expectedEntryCount)
{
JToken actualToken = this.GetRequiredTokenFromResponseObject(propertyPath);
JToken[] tokenArray = actualToken.ToArray();
Assert.AreEqual(expectedEntryCount, tokenArray.Length);
JsonNode actualToken = this.GetRequiredTokenFromResponseObject(propertyPath);
JsonArray tokenArray = actualToken.AsArray();
Assert.AreEqual(expectedEntryCount, tokenArray.Count);
}

private async Task SendGetRequest(string path)
Expand All @@ -159,15 +159,13 @@ private async Task SendGetRequest(string path)

if (!string.IsNullOrEmpty(content))
{
var data = JObject.Parse(content);

this.scenarioContext.Set(data);
this.data = (JsonObject)JsonNode.Parse(content)!;
}
}

private async Task SendPostRequest(string path, object body)
{
string requestContentString = JsonConvert.SerializeObject(body);
string requestContentString = JsonSerializer.Serialize(body);
var requestContent = new StringContent(requestContentString, Encoding.UTF8, "application/json");

HttpResponseMessage response = await HttpClient.PostAsync(new Uri(BaseUri, path), requestContent).ConfigureAwait(false);
Expand All @@ -178,16 +176,27 @@ private async Task SendPostRequest(string path, object body)

if (!string.IsNullOrEmpty(responseContent))
{
var data = JObject.Parse(responseContent);
this.data = (JsonObject)JsonNode.Parse(responseContent)!;
}
}

this.scenarioContext.Set(data);
private JsonNode? GetTokenFromResponseObject(string propertyPath)
{
JsonNode? node = this.data;

// There's no JsonPath in .NET as of .NET 7.0, so just do
// a dead simple dotted path expansion to make do.
foreach (string propertyName in propertyPath.Split('.'))
{
node = node?[propertyName];
}

return node;
}

private JToken GetRequiredTokenFromResponseObject(string propertyPath)
private JsonNode GetRequiredTokenFromResponseObject(string propertyPath)
{
JObject data = this.scenarioContext.Get<JObject>();
JToken? token = data.SelectToken(propertyPath);
JsonNode? token = this.GetTokenFromResponseObject(propertyPath);
Assert.IsNotNull(token);
return token!;
}
Expand Down
5 changes: 2 additions & 3 deletions Solutions/Menes.Specs/Steps/OpenApiParameterParsingSteps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Menes.Specs.Steps
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

using Corvus.Testing.SpecFlow;
Expand All @@ -23,8 +24,6 @@ namespace Menes.Specs.Steps
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers;

using Newtonsoft.Json;

using NUnit.Framework;

using TechTalk.SpecFlow;
Expand Down Expand Up @@ -1077,7 +1076,7 @@ private static object GetResultFromStringAndType(string expectedResultAsString,
"System.DateTimeOffset" => DateTimeOffset.Parse(expectedResultAsString),
"System.Guid" => Guid.Parse(expectedResultAsString),
"System.Uri" => new Uri(expectedResultAsString, UriKind.RelativeOrAbsolute),
"ObjectWithIdAndName" => JsonConvert.DeserializeObject<ObjectWithIdAndName>(expectedResultAsString)!,
"ObjectWithIdAndName" => JsonSerializer.Deserialize<ObjectWithIdAndName>(expectedResultAsString, new JsonSerializerOptions(JsonSerializerDefaults.Web))!,
_ => Convert.ChangeType(expectedResultAsString, Type.GetType(expectedType)!),
};
}
Expand Down

0 comments on commit d5bba5c

Please sign in to comment.