Skip to content

OpenAPI: Generalization of naming conventions #1123

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

Merged
merged 15 commits into from
Dec 21, 2021
Merged
44 changes: 44 additions & 0 deletions src/JsonApiDotNetCore.OpenApi.Client/ApiException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using JetBrains.Annotations;

// We cannot rely on generating ApiException as soon as we are generating multiple clients, see https://github.com/RicoSuter/NSwag/issues/2839#issuecomment-776647377.
// Instead, we take the generated code as is and use it for the various clients.
#nullable disable
// @formatter:off
// ReSharper disable All
namespace JsonApiDotNetCore.OpenApi.Client.Exceptions
{
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
internal class ApiException : Exception
{
public int StatusCode { get; }

public string Response { get; }

public IReadOnlyDictionary<string, IEnumerable<string>> Headers { get; }

public ApiException(string message, int statusCode, string response, IReadOnlyDictionary<string, IEnumerable<string>> headers, Exception innerException)
: base(
message + "\n\nStatus: " + statusCode + "\nResponse: \n" +
(response == null ? "(null)" : response.Substring(0, response.Length >= 512 ? 512 : response.Length)), innerException)
{
StatusCode = statusCode;
Response = response;
Headers = headers;
}

public override string ToString()
{
return $"HTTP Response: \n\n{Response}\n\n{base.ToString()}";
}}

internal sealed class ApiException<TResult> : ApiException
{
public TResult Result { get; }

public ApiException(string message, int statusCode, string response, IReadOnlyDictionary<string, IEnumerable<string>> headers, TResult result,
Exception innerException)
: base(message, statusCode, response, headers, innerException)
{
Result = result;
}}
}
2 changes: 1 addition & 1 deletion test/OpenApiClientTests/LegacyClient/ApiResponse.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using JsonApiDotNetCore.OpenApi.Client;
using OpenApiClientTests.LegacyClient.GeneratedCode;
using JsonApiDotNetCore.OpenApi.Client.Exceptions;

#pragma warning disable AV1008 // Class should not be static

Expand Down
1 change: 1 addition & 0 deletions test/OpenApiClientTests/LegacyClient/ResponseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Net;
using FluentAssertions;
using FluentAssertions.Specialized;
using JsonApiDotNetCore.OpenApi.Client.Exceptions;
using OpenApiClientTests.LegacyClient.GeneratedCode;
using Xunit;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using FluentAssertions;
using OpenApiClientTests.NamingConvention.KebabCase.GeneratedCode;
using Xunit;

namespace OpenApiClientTests.NamingConvention.KebabCase
{
public sealed class GeneratedTypesTests
{
[Fact]
public void Generated_code_is_named_as_expected()
{
nameof(KebabCaseClient.GetSupermarketCollectionAsync).Should().NotBeNull();
nameof(KebabCaseClient.GetSupermarketCollectionAsync).Should().NotBeNull();
nameof(KebabCaseClient.PostSupermarketAsync).Should().NotBeNull();
nameof(KebabCaseClient.GetSupermarketAsync).Should().NotBeNull();
nameof(KebabCaseClient.GetSupermarketAsync).Should().NotBeNull();
nameof(KebabCaseClient.PatchSupermarketAsync).Should().NotBeNull();
nameof(KebabCaseClient.DeleteSupermarketAsync).Should().NotBeNull();
nameof(KebabCaseClient.GetSupermarketBackupStoreManagerAsync).Should().NotBeNull();
nameof(KebabCaseClient.GetSupermarketBackupStoreManagerAsync).Should().NotBeNull();
nameof(KebabCaseClient.GetSupermarketBackupStoreManagerRelationshipAsync).Should().NotBeNull();
nameof(KebabCaseClient.GetSupermarketBackupStoreManagerRelationshipAsync).Should().NotBeNull();
nameof(KebabCaseClient.PatchSupermarketBackupStoreManagerRelationshipAsync).Should().NotBeNull();
nameof(KebabCaseClient.GetSupermarketCashiersAsync).Should().NotBeNull();
nameof(KebabCaseClient.GetSupermarketCashiersAsync).Should().NotBeNull();
nameof(KebabCaseClient.GetSupermarketCashiersRelationshipAsync).Should().NotBeNull();
nameof(KebabCaseClient.GetSupermarketCashiersRelationshipAsync).Should().NotBeNull();
nameof(KebabCaseClient.PostSupermarketCashiersRelationshipAsync).Should().NotBeNull();
nameof(KebabCaseClient.PatchSupermarketCashiersRelationshipAsync).Should().NotBeNull();
nameof(KebabCaseClient.DeleteSupermarketCashiersRelationshipAsync).Should().NotBeNull();
nameof(KebabCaseClient.GetSupermarketStoreManagerAsync).Should().NotBeNull();
nameof(KebabCaseClient.GetSupermarketStoreManagerAsync).Should().NotBeNull();
nameof(KebabCaseClient.GetSupermarketStoreManagerRelationshipAsync).Should().NotBeNull();
nameof(KebabCaseClient.GetSupermarketStoreManagerRelationshipAsync).Should().NotBeNull();
nameof(KebabCaseClient.PatchSupermarketStoreManagerRelationshipAsync).Should().NotBeNull();

nameof(SupermarketCollectionResponseDocument).Should().NotBeNull();
nameof(LinksInResourceCollectionDocument).Should().NotBeNull();
nameof(JsonapiObject).Should().NotBeNull();
nameof(SupermarketDataInResponse).Should().NotBeNull();
nameof(SupermarketResourceType).Should().NotBeNull();
nameof(SupermarketAttributesInResponse.NameOfCity).Should().NotBeNull();
nameof(SupermarketRelationshipsInResponse.StoreManager).Should().NotBeNull();
nameof(SupermarketRelationshipsInResponse.BackupStoreManager).Should().NotBeNull();
nameof(LinksInResourceObject).Should().NotBeNull();
nameof(SupermarketType).Should().NotBeNull();
nameof(KebabCaseClient.GetSupermarketAsync).Should().NotBeNull();
nameof(ToOneStaffMemberInResponse).Should().NotBeNull();
nameof(NullableToOneStaffMemberInResponse).Should().NotBeNull();
nameof(ToManyStaffMemberInResponse).Should().NotBeNull();
nameof(LinksInRelationshipObject).Should().NotBeNull();
nameof(StaffMemberIdentifier).Should().NotBeNull();
nameof(StaffMemberResourceType).Should().NotBeNull();
nameof(StaffMemberResourceType.StaffMembers).Should().NotBeNull();
nameof(SupermarketPrimaryResponseDocument).Should().NotBeNull();
nameof(LinksInResourceDocument).Should().NotBeNull();
nameof(StaffMemberSecondaryResponseDocument).Should().NotBeNull();
nameof(StaffMemberDataInResponse).Should().NotBeNull();
nameof(StaffMemberAttributesInResponse).Should().NotBeNull();
nameof(NullableStaffMemberSecondaryResponseDocument).Should().NotBeNull();
nameof(StaffMemberCollectionResponseDocument).Should().NotBeNull();
nameof(StaffMemberIdentifierResponseDocument).Should().NotBeNull();
nameof(LinksInResourceIdentifierDocument).Should().NotBeNull();
nameof(NullableStaffMemberIdentifierResponseDocument).Should().NotBeNull();
nameof(StaffMemberIdentifierCollectionResponseDocument).Should().NotBeNull();
nameof(LinksInResourceIdentifierCollectionDocument).Should().NotBeNull();
nameof(SupermarketPostRequestDocument).Should().NotBeNull();
nameof(SupermarketDataInPostRequest).Should().NotBeNull();
nameof(SupermarketAttributesInPostRequest).Should().NotBeNull();
nameof(SupermarketRelationshipsInPostRequest).Should().NotBeNull();
nameof(ToOneStaffMemberInRequest).Should().NotBeNull();
nameof(NullableToOneStaffMemberInRequest).Should().NotBeNull();
nameof(ToManyStaffMemberInRequest).Should().NotBeNull();
nameof(SupermarketPatchRequestDocument).Should().NotBeNull();
nameof(SupermarketDataInPatchRequest).Should().NotBeNull();
nameof(SupermarketAttributesInPatchRequest).Should().NotBeNull();
nameof(SupermarketRelationshipsInPatchRequest).Should().NotBeNull();
}
}
}
30 changes: 15 additions & 15 deletions test/OpenApiTests/NamingConvention/KebabCase/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@
},
"meta": {
"type": "object",
"additionalProperties": {}
"additionalProperties": { }
}
},
"additionalProperties": false
Expand Down Expand Up @@ -880,7 +880,7 @@
"type": "array"
}
],
"items": {}
"items": { }
},
"nullable": true
},
Expand All @@ -903,7 +903,7 @@
},
"meta": {
"type": "object",
"additionalProperties": {}
"additionalProperties": { }
},
"jsonapi": {
"$ref": "#/components/schemas/jsonapi-object"
Expand Down Expand Up @@ -933,7 +933,7 @@
},
"meta": {
"type": "object",
"additionalProperties": {}
"additionalProperties": { }
},
"jsonapi": {
"$ref": "#/components/schemas/jsonapi-object"
Expand Down Expand Up @@ -984,7 +984,7 @@
},
"meta": {
"type": "object",
"additionalProperties": {}
"additionalProperties": { }
}
},
"additionalProperties": false
Expand Down Expand Up @@ -1017,7 +1017,7 @@
},
"meta": {
"type": "object",
"additionalProperties": {}
"additionalProperties": { }
},
"jsonapi": {
"$ref": "#/components/schemas/jsonapi-object"
Expand Down Expand Up @@ -1050,7 +1050,7 @@
},
"meta": {
"type": "object",
"additionalProperties": {}
"additionalProperties": { }
}
},
"additionalProperties": false
Expand Down Expand Up @@ -1086,7 +1086,7 @@
},
"meta": {
"type": "object",
"additionalProperties": {}
"additionalProperties": { }
},
"jsonapi": {
"$ref": "#/components/schemas/jsonapi-object"
Expand All @@ -1109,7 +1109,7 @@
},
"meta": {
"type": "object",
"additionalProperties": {}
"additionalProperties": { }
},
"jsonapi": {
"$ref": "#/components/schemas/jsonapi-object"
Expand Down Expand Up @@ -1138,7 +1138,7 @@
},
"meta": {
"type": "object",
"additionalProperties": {}
"additionalProperties": { }
},
"jsonapi": {
"$ref": "#/components/schemas/jsonapi-object"
Expand Down Expand Up @@ -1203,7 +1203,7 @@
},
"meta": {
"type": "object",
"additionalProperties": {}
"additionalProperties": { }
},
"jsonapi": {
"$ref": "#/components/schemas/jsonapi-object"
Expand Down Expand Up @@ -1279,7 +1279,7 @@
},
"meta": {
"type": "object",
"additionalProperties": {}
"additionalProperties": { }
}
},
"additionalProperties": false
Expand Down Expand Up @@ -1320,7 +1320,7 @@
},
"meta": {
"type": "object",
"additionalProperties": {}
"additionalProperties": { }
},
"jsonapi": {
"$ref": "#/components/schemas/jsonapi-object"
Expand Down Expand Up @@ -1425,7 +1425,7 @@
},
"meta": {
"type": "object",
"additionalProperties": {}
"additionalProperties": { }
}
},
"additionalProperties": false
Expand Down Expand Up @@ -1456,7 +1456,7 @@
},
"meta": {
"type": "object",
"additionalProperties": {}
"additionalProperties": { }
}
},
"additionalProperties": false
Expand Down
33 changes: 33 additions & 0 deletions test/TestBuildingBlocks/JsonElementAssertions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Text.Json;
using FluentAssertions.Execution;

namespace TestBuildingBlocks
{
public sealed class JsonElementAssertions : JsonElementAssertions<JsonElementAssertions>
{
internal JsonElementAssertions(JsonElement subject)
: base(subject)
{
}
}

public class JsonElementAssertions<TAssertions>
where TAssertions : JsonElementAssertions<TAssertions>
{
/// <summary>
/// - Gets the object which value is being asserted.
/// </summary>
private JsonElement Subject { get; }

protected JsonElementAssertions(JsonElement subject)
{
Subject = subject;
}

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.");
}
}
}
2 changes: 1 addition & 1 deletion test/TestBuildingBlocks/TestBuildingBlocks.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>$(TargetFrameworkName)</TargetFramework>
</PropertyGroup>
Expand Down