Skip to content
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

Add support for generating multiple files per Refit interface #502

Merged
merged 6 commits into from
Oct 2, 2024

Conversation

christianhelle
Copy link
Owner

This pull request includes several significant changes to the Refitter.Core project, primarily focusing on adding support for generating multiple Refit interface files, renaming constants, and improving code generation. The changes also include updates to project dependencies and minor formatting adjustments.

Example 1 - Generate multiple files with multiple interfaces by endpoint

The following settings file:

{
  "openApiPath": "https://petstore3.swagger.io/api/v3/openapi.json",
  "namespace": "Petstore",
  "contractsNamespace": "Petstore.Contracts",
  "outputFolder": "Client",
  "contractsOutputFolder": "Contracts/",
  "generateMultipleFiles": true,
  "naming": {
    "useOpenApiTitle": false,
    "interfaceName": "SwaggerPetstore"
  },
  "dependencyInjectionSettings": {
    "baseUrl": "https://petstore3.swagger.io/api/v3",
    "transientErrorHandler": "HttpResilience",
    "maxRetryCount": 3,
    "firstBackoffRetryInSeconds": 0.5
  },
  "codeGeneratorSettings": {
    "dateType": "System.DateTime",
    "dateTimeType": "System.DateTime",
    "arrayType": "System.Collections.Generic.IList"
  },
  "operationNameGenerator": "default",
  "typeAccessibility": "public",
  "multipleInterfaces": "ByEndpoint"
}

Outputs the following:

Refitter v1.0.0.0 (local build)
Support key: ptbddgt

OpenAPI statistics:
 - Path Items: 13
 - Operations: 19
 - Parameters: 17
 - Request Bodies: 9
 - Responses: 19
 - Links: 0
 - Callbacks: 0
 - Schemas: 73

Output: C:\projects\christianhelle\refitter\test\MultipleFiles\Client\IUpdatePetEndpoint.cs (1580 bytes)
Output: C:\projects\christianhelle\refitter\test\MultipleFiles\Client\IAddPetEndpoint.cs (1315 bytes)
Output: C:\projects\christianhelle\refitter\test\MultipleFiles\Client\IFindPetsByStatusEndpoint.cs (1411 bytes)
Output: C:\projects\christianhelle\refitter\test\MultipleFiles\Client\IFindPetsByTagsEndpoint.cs (1424 bytes)
Output: C:\projects\christianhelle\refitter\test\MultipleFiles\Client\IGetPetByIdEndpoint.cs (1413 bytes)
Output: C:\projects\christianhelle\refitter\test\MultipleFiles\Client\IUpdatePetWithFormEndpoint.cs (1484 bytes)
Output: C:\projects\christianhelle\refitter\test\MultipleFiles\Client\IDeletePetEndpoint.cs (1242 bytes)
Output: C:\projects\christianhelle\refitter\test\MultipleFiles\Client\IUploadFileEndpoint.cs (1331 bytes)
Output: C:\projects\christianhelle\refitter\test\MultipleFiles\Client\IGetInventoryEndpoint.cs (937 bytes)
Output: C:\projects\christianhelle\refitter\test\MultipleFiles\Client\IPlaceOrderEndpoint.cs (1244 bytes)
Output: C:\projects\christianhelle\refitter\test\MultipleFiles\Client\IGetOrderByIdEndpoint.cs (1550 bytes)
Output: C:\projects\christianhelle\refitter\test\MultipleFiles\Client\IDeleteOrderEndpoint.cs (1549 bytes)
Output: C:\projects\christianhelle\refitter\test\MultipleFiles\Client\ICreateUserEndpoint.cs (949 bytes)
Output: C:\projects\christianhelle\refitter\test\MultipleFiles\Client\ICreateUsersWithListInputEndpoint.cs (1003 bytes)
Output: C:\projects\christianhelle\refitter\test\MultipleFiles\Client\ILoginUserEndpoint.cs (1399 bytes)
Output: C:\projects\christianhelle\refitter\test\MultipleFiles\Client\ILogoutUserEndpoint.cs (839 bytes)
Output: C:\projects\christianhelle\refitter\test\MultipleFiles\Client\IGetUserByNameEndpoint.cs (1435 bytes)
Output: C:\projects\christianhelle\refitter\test\MultipleFiles\Client\IUpdateUserEndpoint.cs (1048 bytes)
Output: C:\projects\christianhelle\refitter\test\MultipleFiles\Client\IDeleteUserEndpoint.cs (1443 bytes)
Output: C:\projects\christianhelle\refitter\test\MultipleFiles\Contracts\Contracts.cs (13958 bytes)
Output: C:\projects\christianhelle\refitter\test\MultipleFiles\Client\DependencyInjection.cs (14102 bytes)
Duration: 00:00:03.1165880

Example 2 - Generate multiple files for multiple interfaces by tag

The following settings file:

{
  "openApiPath": "https://petstore3.swagger.io/api/v3/openapi.json",
  "namespace": "Petstore",
  "contractsNamespace": "Petstore.Contracts",
  "outputFolder": "Client",
  "contractsOutputFolder": "Contracts/",
  "generateMultipleFiles": true,
  "naming": {
    "useOpenApiTitle": false,
    "interfaceName": "SwaggerPetstore"
  },
  "dependencyInjectionSettings": {
    "baseUrl": "https://petstore3.swagger.io/api/v3",
    "transientErrorHandler": "HttpResilience",
    "maxRetryCount": 3,
    "firstBackoffRetryInSeconds": 0.5
  },
  "codeGeneratorSettings": {
    "dateType": "System.DateTime",
    "dateTimeType": "System.DateTime",
    "arrayType": "System.Collections.Generic.IList"
  },
  "operationNameGenerator": "default",
  "typeAccessibility": "public",
  "multipleInterfaces": "ByTag"
}

Outputs the following

Refitter v1.0.0.0 (local build)
Support key: ptbddgt

OpenAPI statistics:
 - Path Items: 13
 - Operations: 19
 - Parameters: 17
 - Request Bodies: 9
 - Responses: 19
 - Links: 0
 - Callbacks: 0
 - Schemas: 73

Output: C:\projects\christianhelle\refitter\test\MultipleFiles\Client\IPetApi.cs (7865 bytes)
Output: C:\projects\christianhelle\refitter\test\MultipleFiles\Client\IStoreApi.cs (3835 bytes)
Output: C:\projects\christianhelle\refitter\test\MultipleFiles\Client\IUserApi.cs (5233 bytes)
Output: C:\projects\christianhelle\refitter\test\MultipleFiles\Contracts\Contracts.cs (13958 bytes)
Output: C:\projects\christianhelle\refitter\test\MultipleFiles\Client\DependencyInjection.cs (2530 bytes)
Duration: 00:00:02.9782556

Renaming and Refactoring:

  • Renamed FilenameConstants to TypenameConstants and updated the constants to exclude the .cs extension in src/Refitter.Core/FilenameConstants.cs.
  • Updated GeneratedCode record to include a computed Filename property based on TypeName in src/Refitter.Core/GeneratedCode.cs.

Code Generation Enhancements:

  • Changed GenerateCode method in IRefitInterfaceGenerator and its implementations to return IEnumerable<GeneratedCode> instead of a single RefitGeneratedCode in multiple files, including src/Refitter.Core/IRefitInterfaceGenerator.cs, src/Refitter.Core/RefitInterfaceGenerator.cs, src/Refitter.Core/RefitMultipleInterfaceByTagGenerator.cs, and src/Refitter.Core/RefitMultipleInterfaceGenerator.cs. [1] [2] [3] [4]
  • Modified GenerateClient method to handle multiple GeneratedCode objects and updated the Generate and GenerateMultipleFiles methods accordingly in src/Refitter.Core/RefitGenerator.cs. [1] [2] [3] [4] [5]

Dependency Updates:

  • Added System.Text.Json package reference to src/Refitter.Core/Refitter.Core.csproj and src/Refitter.Tests/Build/ProjectFileContents.cs. [1] [2]

Minor Changes:

  • Fixed a minor issue with the version display in src/Refitter/GenerateCommand.cs.
  • Adjusted the check for ContractsOutputFolder in WriteMultipleFiles method to use the updated TypenameConstants.Contracts in src/Refitter/GenerateCommand.cs.

Related issues:

@christianhelle christianhelle added the enhancement New feature, bug fix, or request label Oct 2, 2024
@christianhelle christianhelle self-assigned this Oct 2, 2024
@christianhelle christianhelle changed the title Add support for generating multiple Refit interface files Add support for generating multiple files per Refit interface Oct 2, 2024
Copy link

sonarcloud bot commented Oct 2, 2024

Copy link

codecov bot commented Oct 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.13%. Comparing base (1010796) to head (db9c811).
Report is 13 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #502      +/-   ##
==========================================
- Coverage   97.40%   90.13%   -7.28%     
==========================================
  Files          51       51              
  Lines        2508     2756     +248     
==========================================
+ Hits         2443     2484      +41     
- Misses         42      242     +200     
- Partials       23       30       +7     
Flag Coverage Δ
unittests 90.13% <ø> (-7.28%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@christianhelle christianhelle merged commit cec86df into main Oct 2, 2024
7 of 8 checks passed
@christianhelle christianhelle deleted the multiple-files-per-interface branch October 3, 2024 10:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature, bug fix, or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant