Skip to content

Commit c4b2d17

Browse files
committed
Add client and swagger doc tests
fixup2
1 parent 2e847ab commit c4b2d17

File tree

7 files changed

+175
-17
lines changed

7 files changed

+175
-17
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using JetBrains.Annotations;
2+
3+
// We cannot rely on generating ApiException as soon as we are generating multiple clients, see https://github.com/RicoSuter/NSwag/issues/2839#issuecomment-776647377.
4+
// Instead, we take the generated code as is and use it for the various clients.
5+
#nullable disable
6+
// @formatter:off
7+
// ReSharper disable All
8+
namespace JsonApiDotNetCore.OpenApi.Client.Exceptions
9+
{
10+
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
11+
internal class ApiException : Exception
12+
{
13+
public int StatusCode { get; }
14+
15+
public string Response { get; }
16+
17+
public IReadOnlyDictionary<string, IEnumerable<string>> Headers { get; }
18+
19+
public ApiException(string message, int statusCode, string response, IReadOnlyDictionary<string, IEnumerable<string>> headers, Exception innerException)
20+
: base(
21+
message + "\n\nStatus: " + statusCode + "\nResponse: \n" +
22+
(response == null ? "(null)" : response.Substring(0, response.Length >= 512 ? 512 : response.Length)), innerException)
23+
{
24+
StatusCode = statusCode;
25+
Response = response;
26+
Headers = headers;
27+
}
28+
29+
public override string ToString()
30+
{
31+
return $"HTTP Response: \n\n{Response}\n\n{base.ToString()}";
32+
}}
33+
34+
internal sealed class ApiException<TResult> : ApiException
35+
{
36+
public TResult Result { get; }
37+
38+
public ApiException(string message, int statusCode, string response, IReadOnlyDictionary<string, IEnumerable<string>> headers, TResult result,
39+
Exception innerException)
40+
: base(message, statusCode, response, headers, innerException)
41+
{
42+
Result = result;
43+
}}
44+
}

test/OpenApiClientTests/LegacyClient/ApiResponse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using JsonApiDotNetCore.OpenApi.Client;
2-
using OpenApiClientTests.LegacyClient.GeneratedCode;
2+
using JsonApiDotNetCore.OpenApi.Client.Exceptions;
33

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

test/OpenApiClientTests/LegacyClient/ResponseTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Net;
33
using FluentAssertions;
44
using FluentAssertions.Specialized;
5+
using JsonApiDotNetCore.OpenApi.Client.Exceptions;
56
using OpenApiClientTests.LegacyClient.GeneratedCode;
67
using Xunit;
78

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using FluentAssertions;
2+
using OpenApiClientTests.NamingConvention.KebabCase.GeneratedCode;
3+
using Xunit;
4+
5+
namespace OpenApiClientTests.NamingConvention.KebabCase
6+
{
7+
public sealed class GeneratedTypesTests
8+
{
9+
[Fact]
10+
public void Generated_code_is_named_as_expected()
11+
{
12+
nameof(KebabCaseClient.GetSupermarketCollectionAsync).Should().NotBeNull();
13+
nameof(KebabCaseClient.GetSupermarketCollectionAsync).Should().NotBeNull();
14+
nameof(KebabCaseClient.PostSupermarketAsync).Should().NotBeNull();
15+
nameof(KebabCaseClient.GetSupermarketAsync).Should().NotBeNull();
16+
nameof(KebabCaseClient.GetSupermarketAsync).Should().NotBeNull();
17+
nameof(KebabCaseClient.PatchSupermarketAsync).Should().NotBeNull();
18+
nameof(KebabCaseClient.DeleteSupermarketAsync).Should().NotBeNull();
19+
nameof(KebabCaseClient.GetSupermarketBackupStoreManagerAsync).Should().NotBeNull();
20+
nameof(KebabCaseClient.GetSupermarketBackupStoreManagerAsync).Should().NotBeNull();
21+
nameof(KebabCaseClient.GetSupermarketBackupStoreManagerRelationshipAsync).Should().NotBeNull();
22+
nameof(KebabCaseClient.GetSupermarketBackupStoreManagerRelationshipAsync).Should().NotBeNull();
23+
nameof(KebabCaseClient.PatchSupermarketBackupStoreManagerRelationshipAsync).Should().NotBeNull();
24+
nameof(KebabCaseClient.GetSupermarketCashiersAsync).Should().NotBeNull();
25+
nameof(KebabCaseClient.GetSupermarketCashiersAsync).Should().NotBeNull();
26+
nameof(KebabCaseClient.GetSupermarketCashiersRelationshipAsync).Should().NotBeNull();
27+
nameof(KebabCaseClient.GetSupermarketCashiersRelationshipAsync).Should().NotBeNull();
28+
nameof(KebabCaseClient.PostSupermarketCashiersRelationshipAsync).Should().NotBeNull();
29+
nameof(KebabCaseClient.PatchSupermarketCashiersRelationshipAsync).Should().NotBeNull();
30+
nameof(KebabCaseClient.DeleteSupermarketCashiersRelationshipAsync).Should().NotBeNull();
31+
nameof(KebabCaseClient.GetSupermarketStoreManagerAsync).Should().NotBeNull();
32+
nameof(KebabCaseClient.GetSupermarketStoreManagerAsync).Should().NotBeNull();
33+
nameof(KebabCaseClient.GetSupermarketStoreManagerRelationshipAsync).Should().NotBeNull();
34+
nameof(KebabCaseClient.GetSupermarketStoreManagerRelationshipAsync).Should().NotBeNull();
35+
nameof(KebabCaseClient.PatchSupermarketStoreManagerRelationshipAsync).Should().NotBeNull();
36+
37+
nameof(SupermarketCollectionResponseDocument).Should().NotBeNull();
38+
nameof(LinksInResourceCollectionDocument).Should().NotBeNull();
39+
nameof(JsonapiObject).Should().NotBeNull();
40+
nameof(SupermarketDataInResponse).Should().NotBeNull();
41+
nameof(SupermarketResourceType).Should().NotBeNull();
42+
nameof(SupermarketAttributesInResponse.NameOfCity).Should().NotBeNull();
43+
nameof(SupermarketRelationshipsInResponse.StoreManager).Should().NotBeNull();
44+
nameof(SupermarketRelationshipsInResponse.BackupStoreManager).Should().NotBeNull();
45+
nameof(LinksInResourceObject).Should().NotBeNull();
46+
nameof(SupermarketType).Should().NotBeNull();
47+
nameof(KebabCaseClient.GetSupermarketAsync).Should().NotBeNull();
48+
nameof(ToOneStaffMemberInResponse).Should().NotBeNull();
49+
nameof(NullableToOneStaffMemberInResponse).Should().NotBeNull();
50+
nameof(ToManyStaffMemberInResponse).Should().NotBeNull();
51+
nameof(LinksInRelationshipObject).Should().NotBeNull();
52+
nameof(StaffMemberIdentifier).Should().NotBeNull();
53+
nameof(StaffMemberResourceType).Should().NotBeNull();
54+
nameof(StaffMemberResourceType.StaffMembers).Should().NotBeNull();
55+
nameof(SupermarketPrimaryResponseDocument).Should().NotBeNull();
56+
nameof(LinksInResourceDocument).Should().NotBeNull();
57+
nameof(StaffMemberSecondaryResponseDocument).Should().NotBeNull();
58+
nameof(StaffMemberDataInResponse).Should().NotBeNull();
59+
nameof(StaffMemberAttributesInResponse).Should().NotBeNull();
60+
nameof(NullableStaffMemberSecondaryResponseDocument).Should().NotBeNull();
61+
nameof(StaffMemberCollectionResponseDocument).Should().NotBeNull();
62+
nameof(StaffMemberIdentifierResponseDocument).Should().NotBeNull();
63+
nameof(LinksInResourceIdentifierDocument).Should().NotBeNull();
64+
nameof(NullableStaffMemberIdentifierResponseDocument).Should().NotBeNull();
65+
nameof(StaffMemberIdentifierCollectionResponseDocument).Should().NotBeNull();
66+
nameof(LinksInResourceIdentifierCollectionDocument).Should().NotBeNull();
67+
nameof(SupermarketPostRequestDocument).Should().NotBeNull();
68+
nameof(SupermarketDataInPostRequest).Should().NotBeNull();
69+
nameof(SupermarketAttributesInPostRequest).Should().NotBeNull();
70+
nameof(SupermarketRelationshipsInPostRequest).Should().NotBeNull();
71+
nameof(ToOneStaffMemberInRequest).Should().NotBeNull();
72+
nameof(NullableToOneStaffMemberInRequest).Should().NotBeNull();
73+
nameof(ToManyStaffMemberInRequest).Should().NotBeNull();
74+
nameof(SupermarketPatchRequestDocument).Should().NotBeNull();
75+
nameof(SupermarketDataInPatchRequest).Should().NotBeNull();
76+
nameof(SupermarketAttributesInPatchRequest).Should().NotBeNull();
77+
nameof(SupermarketRelationshipsInPatchRequest).Should().NotBeNull();
78+
}
79+
}
80+
}

test/OpenApiTests/NamingConvention/KebabCase/swagger.json

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@
734734
},
735735
"meta": {
736736
"type": "object",
737-
"additionalProperties": {}
737+
"additionalProperties": { }
738738
}
739739
},
740740
"additionalProperties": false
@@ -880,7 +880,7 @@
880880
"type": "array"
881881
}
882882
],
883-
"items": {}
883+
"items": { }
884884
},
885885
"nullable": true
886886
},
@@ -903,7 +903,7 @@
903903
},
904904
"meta": {
905905
"type": "object",
906-
"additionalProperties": {}
906+
"additionalProperties": { }
907907
},
908908
"jsonapi": {
909909
"$ref": "#/components/schemas/jsonapi-object"
@@ -933,7 +933,7 @@
933933
},
934934
"meta": {
935935
"type": "object",
936-
"additionalProperties": {}
936+
"additionalProperties": { }
937937
},
938938
"jsonapi": {
939939
"$ref": "#/components/schemas/jsonapi-object"
@@ -984,7 +984,7 @@
984984
},
985985
"meta": {
986986
"type": "object",
987-
"additionalProperties": {}
987+
"additionalProperties": { }
988988
}
989989
},
990990
"additionalProperties": false
@@ -1017,7 +1017,7 @@
10171017
},
10181018
"meta": {
10191019
"type": "object",
1020-
"additionalProperties": {}
1020+
"additionalProperties": { }
10211021
},
10221022
"jsonapi": {
10231023
"$ref": "#/components/schemas/jsonapi-object"
@@ -1050,7 +1050,7 @@
10501050
},
10511051
"meta": {
10521052
"type": "object",
1053-
"additionalProperties": {}
1053+
"additionalProperties": { }
10541054
}
10551055
},
10561056
"additionalProperties": false
@@ -1086,7 +1086,7 @@
10861086
},
10871087
"meta": {
10881088
"type": "object",
1089-
"additionalProperties": {}
1089+
"additionalProperties": { }
10901090
},
10911091
"jsonapi": {
10921092
"$ref": "#/components/schemas/jsonapi-object"
@@ -1109,7 +1109,7 @@
11091109
},
11101110
"meta": {
11111111
"type": "object",
1112-
"additionalProperties": {}
1112+
"additionalProperties": { }
11131113
},
11141114
"jsonapi": {
11151115
"$ref": "#/components/schemas/jsonapi-object"
@@ -1138,7 +1138,7 @@
11381138
},
11391139
"meta": {
11401140
"type": "object",
1141-
"additionalProperties": {}
1141+
"additionalProperties": { }
11421142
},
11431143
"jsonapi": {
11441144
"$ref": "#/components/schemas/jsonapi-object"
@@ -1203,7 +1203,7 @@
12031203
},
12041204
"meta": {
12051205
"type": "object",
1206-
"additionalProperties": {}
1206+
"additionalProperties": { }
12071207
},
12081208
"jsonapi": {
12091209
"$ref": "#/components/schemas/jsonapi-object"
@@ -1279,7 +1279,7 @@
12791279
},
12801280
"meta": {
12811281
"type": "object",
1282-
"additionalProperties": {}
1282+
"additionalProperties": { }
12831283
}
12841284
},
12851285
"additionalProperties": false
@@ -1320,7 +1320,7 @@
13201320
},
13211321
"meta": {
13221322
"type": "object",
1323-
"additionalProperties": {}
1323+
"additionalProperties": { }
13241324
},
13251325
"jsonapi": {
13261326
"$ref": "#/components/schemas/jsonapi-object"
@@ -1425,7 +1425,7 @@
14251425
},
14261426
"meta": {
14271427
"type": "object",
1428-
"additionalProperties": {}
1428+
"additionalProperties": { }
14291429
}
14301430
},
14311431
"additionalProperties": false
@@ -1456,7 +1456,7 @@
14561456
},
14571457
"meta": {
14581458
"type": "object",
1459-
"additionalProperties": {}
1459+
"additionalProperties": { }
14601460
}
14611461
},
14621462
"additionalProperties": false
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Text.Json;
2+
using FluentAssertions.Execution;
3+
4+
namespace TestBuildingBlocks
5+
{
6+
public sealed class JsonElementAssertions : JsonElementAssertions<JsonElementAssertions>
7+
{
8+
internal JsonElementAssertions(JsonElement subject)
9+
: base(subject)
10+
{
11+
}
12+
}
13+
14+
public class JsonElementAssertions<TAssertions>
15+
where TAssertions : JsonElementAssertions<TAssertions>
16+
{
17+
/// <summary>
18+
/// - Gets the object which value is being asserted.
19+
/// </summary>
20+
private JsonElement Subject { get; }
21+
22+
protected JsonElementAssertions(JsonElement subject)
23+
{
24+
Subject = subject;
25+
}
26+
27+
public void HaveProperty(string propertyName, string because = "", params object[] becauseArgs)
28+
{
29+
Execute.Assertion.ForCondition(Subject.TryGetProperty(propertyName, out _)).BecauseOf(because, becauseArgs)
30+
.FailWith($"Expected element to have property with name '{propertyName}, but did not find it.");
31+
}
32+
}
33+
}

test/TestBuildingBlocks/TestBuildingBlocks.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFramework>$(TargetFrameworkName)</TargetFramework>
44
</PropertyGroup>

0 commit comments

Comments
 (0)