Skip to content

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

Closed
wants to merge 1 commit into from

Conversation

maurei
Copy link
Member

@maurei maurei commented Oct 28, 2021

Closed in favour of #1123

@maurei maurei marked this pull request as draft October 28, 2021 11:12
@maurei maurei force-pushed the openapi-naming-convention branch from ce76eb2 to 03af5ab Compare October 28, 2021 14:07
Copy link
Contributor

@bart-degreed bart-degreed left a 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.

Comment on lines 20 to 21
message + "\n\nStatus: " + statusCode + "\nResponse: \n" +
(response == null ? "(null)" : response.Substring(0, response.Length >= 512 ? 512 : response.Length)), innerException)
Copy link
Contributor

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?

Copy link
Member Author

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());
Copy link
Contributor

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/";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// Assert
resourceCollection.Links.First.Should().Be($"{HostPrefix}supermarkets");
resourceCollection.Links.Self.Should().Be($"{HostPrefix}supermarkets");
resourceCollection.Data.Count.Should().Be(1);
Copy link
Contributor

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())
Copy link
Contributor

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.
Copy link
Contributor

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

Comment on lines 22 to 24
#pragma warning disable AV1210
catch
#pragma warning restore AV1210
Copy link
Contributor

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));
Copy link
Contributor

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();
Copy link
Contributor

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?

Comment on lines +66 to +72
// @formatter:wrap_chained_method_calls chop_always
// @formatter:keep_existing_linebreaks true
Copy link
Contributor

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

@maurei maurei force-pushed the openapi-naming-convention branch from 03af5ab to 0e6f5a0 Compare October 29, 2021 09:16
Copy link
Contributor

@bart-degreed bart-degreed left a 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" />
Copy link
Contributor

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.

Copy link
Member Author

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
Copy link
Contributor

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);
Copy link
Contributor

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";
Copy link
Contributor

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.

Copy link
Contributor

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.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing '

Copy link
Contributor

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"/>
Copy link
Contributor

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)"/>
Copy link
Contributor

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.

Copy link
Member Author

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.

@bart-degreed bart-degreed changed the title Generalization of naming conventions OpenAPI: Generalization of naming conventions Nov 1, 2021
@maurei maurei force-pushed the openapi-naming-convention branch 2 times, most recently from 453ab4f to 6b788bb Compare December 6, 2021 14:18
@maurei maurei force-pushed the openapi-naming-convention branch from 6b788bb to 68f3d6c Compare December 6, 2021 14:19
@maurei maurei force-pushed the openapi-naming-convention branch from 68f3d6c to 3fe4e2e Compare December 6, 2021 14:26
@maurei maurei closed this Dec 6, 2021
@maurei maurei deleted the openapi-naming-convention branch December 6, 2021 14:40
@maurei
Copy link
Member Author

maurei commented Dec 6, 2021

See #1123

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants