-
-
Notifications
You must be signed in to change notification settings - Fork 158
OpenAPI: Generalization of naming conventions #1098
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
ce76eb2
to
03af5ab
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done a quick pass over the changes via GitHub. Haven't looked into json-path assertions or the project-level changes yet.
message + "\n\nStatus: " + statusCode + "\nResponse: \n" + | ||
(response == null ? "(null)" : response.Substring(0, response.Length >= 512 ? 512 : response.Length)), innerException) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use string interpolation, this is optimized at compile-time in .NET 6.
Related: why cut off the message beyond the first 512 bytes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a copy paste from the generated code. I don't think we should refactor that
await dbContext.SaveChangesAsync(); | ||
}); | ||
|
||
KebabCaseClient apiClient = new(_testContext.Factory.CreateClient()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
using
public sealed class KebabCaseRoundTripTests | ||
: IClassFixture<IntegrationTestContext<KebabCaseNamingConventionStartup<NamingConventionDbContext>, NamingConventionDbContext>> | ||
{ | ||
private const string HostPrefix = "http://localhost/"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should not end in /
, so you can reuse it for absolute/relative paths.
See https://github.com/json-api-dotnet/JsonApiDotNetCore/blob/nullable-reference-types/test/JsonApiDotNetCoreTests/IntegrationTests/Links/AbsoluteLinksWithNamespaceTests.cs#L21
// Assert | ||
resourceCollection.Links.First.Should().Be($"{HostPrefix}supermarkets"); | ||
resourceCollection.Links.Self.Should().Be($"{HostPrefix}supermarkets"); | ||
resourceCollection.Data.Count.Should().Be(1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should().HaveCount()
{ | ||
internal sealed class NamingConventionFakers : FakerContainer | ||
{ | ||
private readonly Lazy<Faker<Supermarket>> _lazySupermarketFaker = new(() => new Faker<Supermarket>().UseSeed(GetFakerSeed()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copy formatter directive from other fakers and align line breaks likewise
where TAssertions : JsonElementAssertions<TAssertions> | ||
{ | ||
/// <summary> | ||
/// - Gets the object which value is being asserted. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
whose value, and no leading minus
#pragma warning disable AV1210 | ||
catch | ||
#pragma warning restore AV1210 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't you reason about which errors are to be expected here and just catch those?
|
||
private static string Capitalize(string term) | ||
{ | ||
return string.Concat(term[0].ToString().ToUpper(), term.AsSpan(1)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why use Span<T>
here?
// @formatter:keep_existing_linebreaks restore | ||
// @formatter:wrap_chained_method_calls restore | ||
|
||
return _namingPolicy.ConvertName(pascalCaseSchemaId); | ||
} | ||
|
||
// Used for a fixed set of types, such as jsonapi-object, links-in-many-resource-document etc. | ||
return _formatter.FormatResourceName(type).Singularize(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only thing that can be expected from FormatResourceName
is that applies the casing convention and pluralizes. Given you now have access to the naming policy and don't want plural, why still use this method?
// @formatter:wrap_chained_method_calls chop_always | ||
// @formatter:keep_existing_linebreaks true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move these down to where these are intended for
03af5ab
to
0e6f5a0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More feedback
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\JsonApiDotNetCore.OpenApi.Client\JsonApiDotNetCore.OpenApi.Client.csproj" /> | ||
<ProjectReference Include="..\OpenApiTests\OpenApiTests.csproj" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This dependency can (and should) be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can't be removed: this project depends on the models, fakers and DbContexts defined in that project.
using Microsoft.EntityFrameworkCore; | ||
using OpenApiTests.LegacyOpenApiIntegration; | ||
|
||
namespace OpenApiTests.NamingConvention.KebabCase |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be ...NamingConventions...
(plural), as there will be multiple.
|
||
await WriteToSwaggerDocumentsFolderAsync(content); | ||
|
||
return JsonDocument.Parse(content); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This leaks memory. See dotnet/runtime#49207.
public async Task Kebab_naming_policy_is_applied_to_get_collection_endpoint() | ||
{ | ||
// Arrange | ||
const string expectedReferenceIdForResponseDocument = "supermarket-collection-response-document"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most of these constants are used only once, I think its easier to read when they are inlined.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Related to this, I've come up with a different approach for these assertions, which I'll elaborate on later.
public void HaveProperty(string propertyName, string because = "", params object[] becauseArgs) | ||
{ | ||
Execute.Assertion.ForCondition(Subject.TryGetProperty(propertyName, out _)).BecauseOf(because, becauseArgs) | ||
.FailWith($"Expected element to have property with name '{propertyName}, but did not find it."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing '
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
string escapedJson = Subject.ToString()?.Replace("{", "{{").Replace("}", "}}");
Execute.Assertion.ForCondition(Subject.TryGetProperty(propertyName, out _))
.FailWith($"Expected JSON element '{escapedJson}' to contain a property named '{propertyName}'.");
And you can remove the because/becauseArgs parameters because you're not using them. And should rename this method to ContainsProperty.
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="$(AspNetCoreVersion)"/> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)"/> | ||
<PackageReference Include="BlushingPenguin.JsonPath" Version="$(BlushingPenguinVersion)"/> | ||
<PackageReference Include="Microsoft.OpenApi.Readers" Version="1.2.3"/> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can be removed.
<PackageReference Include="xunit.runner.visualstudio" Version="$(XUnitVersion)" PrivateAssets="All" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="BlushingPenguin.JsonPath" Version="$(BlushingPenguinVersion)"/> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this reference can be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use the SelectToken
method in this project.
453ab4f
to
6b788bb
Compare
6b788bb
to
68f3d6c
Compare
68f3d6c
to
3fe4e2e
Compare
See #1123 |
Closed in favour of #1123