-
-
Notifications
You must be signed in to change notification settings - Fork 158
openapi: support client-generated id #1475
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
bkoelman
merged 6 commits into
json-api-dotnet:openapi-client-generated-ids
from
verdie-g:1054-client-generated-id
Mar 10, 2024
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5785553
openapi: support client-generated id
verdie-g 5cabf90
Complete tests
verdie-g 05b4ba5
Address PR comments
verdie-g 8c8f950
Address PR comments
verdie-g bf41287
Only generate post endpoints
verdie-g 8af7556
Address PR comments
verdie-g File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
164 changes: 164 additions & 0 deletions
164
test/OpenApiEndToEndTests/ClientGeneratedId/PostTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
using FluentAssertions; | ||
using FluentAssertions.Specialized; | ||
using JsonApiDotNetCore.OpenApi.Client; | ||
using Newtonsoft.Json; | ||
using OpenApiEndToEndTests.ClientGeneratedId.GeneratedCode; | ||
using OpenApiTests; | ||
using OpenApiTests.ClientGeneratedId; | ||
using TestBuildingBlocks; | ||
using Xunit; | ||
|
||
namespace OpenApiEndToEndTests.ClientGeneratedId; | ||
|
||
public sealed class PostTests : IClassFixture<IntegrationTestContext<OpenApiStartup<ClientGeneratedIdDbContext>, ClientGeneratedIdDbContext>> | ||
{ | ||
private readonly IntegrationTestContext<OpenApiStartup<ClientGeneratedIdDbContext>, ClientGeneratedIdDbContext> _testContext; | ||
private readonly ClientGeneratedIdFakers _fakers = new(); | ||
|
||
public PostTests(IntegrationTestContext<OpenApiStartup<ClientGeneratedIdDbContext>, ClientGeneratedIdDbContext> testContext) | ||
{ | ||
_testContext = testContext; | ||
|
||
testContext.UseController<PlayersController>(); | ||
testContext.UseController<GamesController>(); | ||
testContext.UseController<GroupsController>(); | ||
} | ||
|
||
[Fact] | ||
public async Task Omit_required_id() | ||
verdie-g marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
// Arrange | ||
Player player = _fakers.Player.Generate(); | ||
|
||
using HttpClient httpClient = _testContext.Factory.CreateClient(); | ||
ClientGeneratedIdClient apiClient = new(httpClient); | ||
|
||
// Act | ||
Func<Task<PlayerPrimaryResponseDocument?>> action = () => ApiResponse.TranslateAsync(() => apiClient.PostPlayerAsync(null, new PlayerPostRequestDocument | ||
{ | ||
Data = new PlayerDataInPostRequest | ||
{ | ||
Id = null!, | ||
Attributes = new PlayerAttributesInPostRequest | ||
{ | ||
Name = player.Name | ||
} | ||
} | ||
})); | ||
|
||
// Assert | ||
ExceptionAssertions<JsonSerializationException> assertion = await action.Should().ThrowExactlyAsync<JsonSerializationException>(); | ||
assertion.Which.Message.Should().Be("Cannot write a null value for property 'id'. Property requires a value. Path 'data'."); | ||
} | ||
|
||
[Fact] | ||
public async Task Pass_required_id() | ||
{ | ||
// Arrange | ||
Player player = _fakers.Player.Generate(); | ||
player.Id = Guid.NewGuid(); | ||
|
||
using HttpClient httpClient = _testContext.Factory.CreateClient(); | ||
ClientGeneratedIdClient apiClient = new(httpClient); | ||
|
||
// Act | ||
Func<Task<PlayerPrimaryResponseDocument?>> action = () => ApiResponse.TranslateAsync(() => apiClient.PostPlayerAsync(null, new PlayerPostRequestDocument | ||
{ | ||
Data = new PlayerDataInPostRequest | ||
{ | ||
Id = player.StringId!, | ||
Attributes = new PlayerAttributesInPostRequest | ||
{ | ||
Name = player.Name | ||
} | ||
} | ||
})); | ||
|
||
// Assert | ||
PlayerPrimaryResponseDocument? doc = (await action.Should().NotThrowAsync()).Subject; | ||
doc.Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public async Task Omit_allowed_id() | ||
{ | ||
// Arrange | ||
Game game = _fakers.Game.Generate(); | ||
|
||
using HttpClient httpClient = _testContext.Factory.CreateClient(); | ||
ClientGeneratedIdClient apiClient = new(httpClient); | ||
|
||
// Act | ||
Func<Task<GamePrimaryResponseDocument?>> action = () => ApiResponse.TranslateAsync(() => apiClient.PostGameAsync(null, new GamePostRequestDocument | ||
{ | ||
Data = new GameDataInPostRequest | ||
{ | ||
Id = null!, | ||
Attributes = new GameAttributesInPostRequest | ||
{ | ||
Name = game.Name, | ||
Price = (double)game.Price | ||
} | ||
} | ||
})); | ||
|
||
// Assert | ||
GamePrimaryResponseDocument? doc = (await action.Should().NotThrowAsync()).Subject; | ||
doc?.Data.Id.Should().NotBeNullOrEmpty(); | ||
} | ||
|
||
[Fact] | ||
public async Task Pass_allowed_id() | ||
{ | ||
// Arrange | ||
Game game = _fakers.Game.Generate(); | ||
game.Id = Guid.NewGuid(); | ||
|
||
using HttpClient httpClient = _testContext.Factory.CreateClient(); | ||
ClientGeneratedIdClient apiClient = new(httpClient); | ||
|
||
// Act | ||
Func<Task<GamePrimaryResponseDocument?>> action = () => ApiResponse.TranslateAsync(() => apiClient.PostGameAsync(null, new GamePostRequestDocument | ||
{ | ||
Data = new GameDataInPostRequest | ||
{ | ||
Id = game.StringId!, | ||
Attributes = new GameAttributesInPostRequest | ||
{ | ||
Name = game.Name, | ||
Price = (double)game.Price | ||
} | ||
} | ||
})); | ||
|
||
// Assert | ||
GamePrimaryResponseDocument? doc = (await action.Should().NotThrowAsync()).Subject; | ||
doc.Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public async Task Omit_forbidden_id() | ||
{ | ||
// Arrange | ||
Group group = _fakers.Group.Generate(); | ||
|
||
using HttpClient httpClient = _testContext.Factory.CreateClient(); | ||
ClientGeneratedIdClient apiClient = new(httpClient); | ||
|
||
// Act | ||
Func<Task<GroupPrimaryResponseDocument?>> action = () => ApiResponse.TranslateAsync(() => apiClient.PostGroupAsync(null, new GroupPostRequestDocument | ||
{ | ||
Data = new GroupDataInPostRequest | ||
{ | ||
Attributes = new GroupAttributesInPostRequest | ||
{ | ||
Name = group.Name | ||
} | ||
} | ||
})); | ||
|
||
// Assert | ||
GroupPrimaryResponseDocument? doc = (await action.Should().NotThrowAsync()).Subject; | ||
doc?.Data.Id.Should().NotBeNullOrEmpty(); | ||
} | ||
} | ||
verdie-g marked this conversation as resolved.
Show resolved
Hide resolved
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.