|
| 1 | +# Client Generator |
| 2 | + |
| 3 | +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. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +You are required to install the following NuGet packages: |
| 8 | + |
| 9 | +- `JsonApiDotNetCore.OpenApiClient` |
| 10 | +- `NSwag.ApiDescription.Client` |
| 11 | +- `Microsoft.Extensions.ApiDescription.Cient` |
| 12 | +- `NSwag.ApiDescription.Client` |
| 13 | + |
| 14 | +The following examples demonstrate how to install the `JsonApiDotNetCore.OpenApiClient` package. |
| 15 | + |
| 16 | +### CLI |
| 17 | + |
| 18 | +``` |
| 19 | +dotnet add package JsonApiDotNetCore.OpenApiClient |
| 20 | +``` |
| 21 | + |
| 22 | +### Visual Studio |
| 23 | + |
| 24 | +```powershell |
| 25 | +Install-Package JsonApiDotNetCore.OpenApiClient |
| 26 | +``` |
| 27 | + |
| 28 | +### *.csproj |
| 29 | + |
| 30 | +```xml |
| 31 | +<ItemGroup> |
| 32 | + <!-- Be sure to check NuGet for the latest version # --> |
| 33 | + <PackageReference Include="JsonApiDotNetCore.OpenApiClient" Version="4.0.0" /> |
| 34 | +</ItemGroup> |
| 35 | +``` |
| 36 | + |
| 37 | + |
| 38 | +## Adding an OpenApiReference |
| 39 | + |
| 40 | +Add a reference to your OpenAPI specification in your project file as demonstrated below. |
| 41 | + |
| 42 | +```xml |
| 43 | +<ItemGroup> |
| 44 | + <OpenApiReference Include="swagger.json"> |
| 45 | + <Namespace>ApiConsumer.GeneratedCode</Namespace> |
| 46 | + <ClassName>OpenApiClient</ClassName> |
| 47 | + <CodeGenerator>NSwagCSharp</CodeGenerator> |
| 48 | + <Options>/UseBaseUrl:false /GenerateClientInterfaces:true</Options> |
| 49 | + </OpenApiReference> |
| 50 | +</ItemGroup> |
| 51 | +``` |
| 52 | + |
| 53 | + |
| 54 | +## Usage |
| 55 | + |
| 56 | +The NSwag tooling generates the OpenAPI client during a prebuild step. Once your application is built, |
| 57 | +you can instantiate it using the class name as indicated in the project file. |
| 58 | + |
| 59 | +```c# |
| 60 | +namespace ApiConsumer |
| 61 | +{ |
| 62 | + class Program |
| 63 | + { |
| 64 | + static void Main(string[] args) |
| 65 | + { |
| 66 | + using (HttpClient httpClient = new HttpClient()) |
| 67 | + { |
| 68 | + OpenApiClient openApiClient = new OpenApiClient(httpClient); |
| 69 | + |
| 70 | + // IntelliSense is now available on `openApiClient`! |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +Support for partial write requests can be enabled by leveraging the extensibility points of the generated client. |
| 78 | + |
| 79 | +```c# |
| 80 | +// Note that this class should be namespace in which NSwag generates the client. |
| 81 | +namespace ApiConsumer.GeneratedCode |
| 82 | +{ |
| 83 | + public partial class OpenApiClient : JsonApiClient |
| 84 | + { |
| 85 | + partial void UpdateJsonSerializerSettings(JsonSerializerSettings settings) |
| 86 | + { |
| 87 | + SetSerializerSettingsForJsonApi(settings); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +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. |
| 94 | + |
| 95 | +```c# |
| 96 | +// Program.cs |
| 97 | +static void Main(string[] args) |
| 98 | +{ |
| 99 | + using (HttpClient httpClient = new HttpClient()) |
| 100 | + { |
| 101 | + OpenApiClient openApiClient = new OpenApiClient(httpClient); |
| 102 | + |
| 103 | + var requestDocument = new ApiResourcePatchRequestDocument |
| 104 | + { |
| 105 | + Data = new ApiResourceDataInPatchRequest |
| 106 | + { |
| 107 | + Id = 543, |
| 108 | + Type = ApiResourceResourceType.Airplanes, |
| 109 | + Attributes = new ApiResourceAttributesInPatchRequest |
| 110 | + { |
| 111 | + someNullableAttribute = "Value" |
| 112 | + } |
| 113 | + } |
| 114 | + }; |
| 115 | + |
| 116 | + using (apiClient.RegisterAttributesForRequestDocument<ApiResourcePatchRequestDocument, ApiResourceDataInPatchRequest>(requestDocument, apiResource => apiResource.AnotherNullableAttribute) |
| 117 | + { |
| 118 | + await apiClient.PatchApiResourceAsync(543, requestDocument)); |
| 119 | + |
| 120 | + // The request will look like this: |
| 121 | + // |
| 122 | + // { |
| 123 | + // "data": { |
| 124 | + // "type": "apiResource", |
| 125 | + // "id": "543", |
| 126 | + // "attributes": { |
| 127 | + // "someNullableAttribute": "Value", |
| 128 | + // "anotherNullableAttribute": null, |
| 129 | + // } |
| 130 | + // } |
| 131 | + // } |
| 132 | + } |
| 133 | + |
| 134 | + } |
| 135 | +} |
| 136 | +``` |
| 137 | + |
0 commit comments