-
- Notifications
You must be signed in to change notification settings - Fork 159
Move existing integration to JADNC #1076
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
Changes from 10 commits
Commits
Show all changes
37 commits Select commit Hold shift + click to select a range
1c099e7 Copied integration and fixed compilation errors
maurei 54a023f Add models and adjusted models in OpenAPI document tests such that i …
maurei 09ab5c0 Add FlightAttendantController to used controllers
maurei 19cc14d Added client library with tests to project; made adjustmets to the mo…
maurei ec2ccaf Adjusted build script to create OpenApiClient artifact, added documen…
maurei c6d691d Adjusted docs and minor refactors
maurei ef2753e Replace resource name formatter with internal one
maurei b3cdc7a Removed JsonApiInputFormatter workaround
maurei a955616 Merge branch 'openapi' into oa/existing-integration
maurei bad9aad Moved full document test to ExistingOpenApiIntegration test folder, w…
maurei ea46194 Disabled OpenAPI for nonjson and operations controller, as these are …
maurei a0d5bff review feedback on docs
maurei 280e449 Add a very minimal example of a generated OpenAPI client for JsonApiD…
maurei 7b3d1ef Fixed inspect and cleanup code issues that do not appear when execute…
maurei ba42b77 rename existing to legacy in ExistingOpenApiIntegration folder and na…
maurei ecd971e moved OpenApiClient to OpenApi.Client
maurei 37a95b3 Configure NSwag to generate a client with access modifier set to inte…
maurei da1db37 Move OpenAPI client tests to dedicated test project, cleared up some …
maurei f71bbea Some adjustments to stay closer to 1-on-1 model mapping from the priv…
maurei 348dffa review feedback
maurei e88c762 Process inspectcode issues that do not appear when run locally
maurei 54ac898 Update package reference in build script
maurei 83ac507 Changed models to stay closer 1-to-1 mapping. Changed 'OpenApiClient…
maurei aaf59a6 process review feedback
maurei 1baed2d rename ClassName in client test project
maurei 65430a8 Updated example client project using Visual Studio defaults and updat…
520c618 Layout tweaks and fixes
b72d823 fixed failed automatic rename 'openApiOpenApi'
maurei 43cec51 Merge branch 'oa/existing-integration' of https://github.com/json-api…
maurei 605d2ce reduced changed wrt private version of this test
maurei 8c2a7b4 Changed ID type from to for Flight
maurei a83de89 ReserveCabinPersonnel -> Purser, CabinPersonnel -> CabinCrewMembers
maurei 8ceff7b fixed failed automatic rename 'openApiOpenApi'
maurei 5a4a06f Flight: replaced OperatingAirplane with Purser, added to many relatio…
maurei 7a88a64 Review feedback
maurei 4073c3f Fix client lifetime test
maurei d3ebfaf Feedback
maurei 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
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
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
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,137 @@ | ||
| # Client Generator | ||
bart-degreed marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| | ||
| You can you can generate a client library from an OpenAPI specification that describes a JsonApiDotNetCore application. For clients genearted with using [NSwag](http://stevetalkscode.co.uk/openapireference-commands) we provide an additional package that enables partial write requests. | ||
bart-degreed marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| | ||
| ## Installation | ||
| | ||
| You are required to install the following NuGet packages: | ||
| | ||
| - `JsonApiDotNetCore.OpenApiClient` | ||
| - `NSwag.ApiDescription.Client` | ||
| - `Microsoft.Extensions.ApiDescription.Cient` | ||
| - `NSwag.ApiDescription.Client` | ||
| | ||
| The following examples demonstrate how to install the `JsonApiDotNetCore.OpenApiClient` package. | ||
| | ||
| ### CLI | ||
| | ||
| ``` | ||
| dotnet add package JsonApiDotNetCore.OpenApiClient | ||
| ``` | ||
| | ||
| ### Visual Studio | ||
| | ||
| ```powershell | ||
| Install-Package JsonApiDotNetCore.OpenApiClient | ||
| ``` | ||
| | ||
| ### *.csproj | ||
| | ||
| ```xml | ||
| <ItemGroup> | ||
| <!-- Be sure to check NuGet for the latest version # --> | ||
| <PackageReference Include="JsonApiDotNetCore.OpenApiClient" Version="4.0.0" /> | ||
| </ItemGroup> | ||
| ``` | ||
| | ||
| | ||
| ## Adding an OpenApiReference | ||
| | ||
| Add a reference to your OpenAPI specification in your project file as demonstrated below. | ||
| | ||
| ```xml | ||
| <ItemGroup> | ||
| <OpenApiReference Include="swagger.json"> | ||
| <Namespace>ApiConsumer.GeneratedCode</Namespace> | ||
| <ClassName>OpenApiClient</ClassName> | ||
| <CodeGenerator>NSwagCSharp</CodeGenerator> | ||
| <Options>/UseBaseUrl:false /GenerateClientInterfaces:true</Options> | ||
| </OpenApiReference> | ||
| </ItemGroup> | ||
| ``` | ||
bart-degreed marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| | ||
| | ||
| ## Usage | ||
| | ||
| The NSwag tooling generates the OpenAPI client during a prebuild step. Once your application is built, | ||
| you can instantiate it using the class name as indicated in the project file. | ||
| | ||
| ```c# | ||
| namespace ApiConsumer | ||
| { | ||
| class Program | ||
| { | ||
| static void Main(string[] args) | ||
| { | ||
| using (HttpClient httpClient = new HttpClient()) | ||
| { | ||
| OpenApiClient openApiClient = new OpenApiClient(httpClient); | ||
| | ||
| // IntelliSense is now available on `openApiClient`! | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
| | ||
| Support for partial write requests can be enabled by leveraging the extensibility points of the generated client. | ||
| | ||
| ```c# | ||
| // Note that this class should be namespace in which NSwag generates the client. | ||
bart-degreed marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| namespace ApiConsumer.GeneratedCode | ||
| { | ||
| public partial class OpenApiClient : JsonApiClient | ||
| { | ||
| partial void UpdateJsonSerializerSettings(JsonSerializerSettings settings) | ||
| { | ||
| SetSerializerSettingsForJsonApi(settings); | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
| | ||
| You can now perform a write request by calling the `RegisterAttributesForRequest` method. Calling this method treats all attributes that contain their default value (<c>null</c> for reference types, <c>0</c> for integers, <c>false</c> for booleans, etc) as omitted unless explicitly listed to include them using the `alwaysIncludedAttributeSelectors` parameter. | ||
| | ||
| ```c# | ||
| // Program.cs | ||
| static void Main(string[] args) | ||
| { | ||
| using (HttpClient httpClient = new HttpClient()) | ||
| { | ||
| OpenApiClient openApiClient = new OpenApiClient(httpClient); | ||
| | ||
| var requestDocument = new ApiResourcePatchRequestDocument | ||
bart-degreed marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| { | ||
| Data = new ApiResourceDataInPatchRequest | ||
| { | ||
| Id = 543, | ||
| Type = ApiResourceResourceType.Airplanes, | ||
| Attributes = new ApiResourceAttributesInPatchRequest | ||
| { | ||
| someNullableAttribute = "Value" | ||
| } | ||
| } | ||
| }; | ||
| | ||
| using (apiClient.RegisterAttributesForRequestDocument<ApiResourcePatchRequestDocument, ApiResourceDataInPatchRequest>(requestDocument, apiResource => apiResource.AnotherNullableAttribute) | ||
| { | ||
| await apiClient.PatchApiResourceAsync(543, requestDocument)); | ||
| | ||
| // The request will look like this: | ||
| // | ||
| // { | ||
| // "data": { | ||
| // "type": "apiResource", | ||
| // "id": "543", | ||
| // "attributes": { | ||
| // "someNullableAttribute": "Value", | ||
| // "anotherNullableAttribute": null, | ||
| // } | ||
| // } | ||
| // } | ||
| } | ||
| | ||
| } | ||
| } | ||
| ``` | ||
| | ||
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
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
39 changes: 39 additions & 0 deletions 39 src/JsonApiDotNetCore.OpenApi/ActionDescriptorExtensions.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,39 @@ | ||
| using System.Linq; | ||
| using System.Reflection; | ||
| using Microsoft.AspNetCore.Mvc.Abstractions; | ||
| using Microsoft.AspNetCore.Mvc.Controllers; | ||
| using Microsoft.AspNetCore.Mvc.Filters; | ||
| using Microsoft.AspNetCore.Mvc.ModelBinding; | ||
| | ||
| namespace JsonApiDotNetCore.OpenApi | ||
| { | ||
| internal static class ActionDescriptorExtensions | ||
| { | ||
| public static MethodInfo GetActionMethod(this ActionDescriptor descriptor) | ||
| { | ||
| ArgumentGuard.NotNull(descriptor, nameof(descriptor)); | ||
| | ||
| return ((ControllerActionDescriptor)descriptor).MethodInfo; | ||
| } | ||
| | ||
| public static TFilterMetaData GetFilterMetadata<TFilterMetaData>(this ActionDescriptor descriptor) | ||
| where TFilterMetaData : IFilterMetadata | ||
| { | ||
| ArgumentGuard.NotNull(descriptor, nameof(descriptor)); | ||
| | ||
| IFilterMetadata filterMetadata = descriptor.FilterDescriptors.Select(filterDescriptor => filterDescriptor.Filter) | ||
| .FirstOrDefault(filter => filter is TFilterMetaData); | ||
| | ||
| return (TFilterMetaData)filterMetadata; | ||
| } | ||
| | ||
| public static ControllerParameterDescriptor GetBodyParameterDescriptor(this ActionDescriptor descriptor) | ||
| { | ||
| ArgumentGuard.NotNull(descriptor, nameof(descriptor)); | ||
| | ||
| return (ControllerParameterDescriptor)descriptor.Parameters.FirstOrDefault(parameterDescriptor => | ||
| // ReSharper disable once ConstantConditionalAccessQualifier Motivation: see https://github.com/dotnet/aspnetcore/issues/32538 | ||
| parameterDescriptor.BindingInfo?.BindingSource == BindingSource.Body); | ||
| } | ||
| } | ||
| } |
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,3 @@ | ||
| using System.Runtime.CompilerServices; | ||
| | ||
| [assembly: InternalsVisibleTo("JsonApiDotNetCore.OpenApiClient")] |
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.