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 using .refitter file from CLI #127

Merged
merged 6 commits into from
Aug 24, 2023

Conversation

christianhelle
Copy link
Owner

@christianhelle christianhelle commented Aug 24, 2023

This resolves #121

The changes here introduces using the .refitter file as a settings file directly from the CLI tool. Using a settings file ignores all other CLI argument

Usage

$ refitter ./openapi.json --settings-file openapi.refitter

CLI tool help text

USAGE:
    refitter [URL or input file] [OPTIONS]

EXAMPLES:
    refitter ./openapi.json
    refitter https://petstore3.swagger.io/api/v3/openapi.yaml
    refitter ./openapi.json --settings-file ./openapi.refitter
    refitter ./openapi.json --namespace "Your.Namespace.Of.Choice.GeneratedCode" --output ./GeneratedCode.cs
    refitter ./openapi.json --namespace "Your.Namespace.Of.Choice.GeneratedCode" --internal
    refitter ./openapi.json --output ./IGeneratedCode.cs --interface-only
    refitter ./openapi.json --use-api-response
    refitter ./openapi.json --cancellation-tokens
    refitter ./openapi.json --no-operation-headers
    refitter ./openapi.json --no-accept-headers
    refitter ./openapi.json --use-iso-date-format
    refitter ./openapi.json --additional-namespace "Your.Additional.Namespace" --additional-namespace "Your.Other.Additional.Namespace"
    refitter ./openapi.json --multiple-interfaces ByEndpoint

ARGUMENTS:
    [URL or input file]    URL or file path to OpenAPI Specification file

OPTIONS:
                                      DEFAULT
    -h, --help                                         Prints help information
    -s, --settings-file                                Path to .refitter settings file. Specifying this will ignore all other settings
    -n, --namespace                   GeneratedCode    Default namespace to use for generated types
    -o, --output                      Output.cs        Path to Output file
        --no-auto-generated-header                     Don't add <auto-generated> header to output file
        --no-accept-headers                            Don't add <Accept> header to output file
        --interface-only                               Don't generate contract types
        --use-api-response                             Return Task<IApiResponse<T>> instead of Task<T>
        --internal                                     Set the accessibility of the generated types to 'internal'
        --cancellation-tokens                          Use cancellation tokens
        --no-operation-headers                         Don't generate operation headers
        --no-logging                                   Don't log errors or collect telemetry
        --additional-namespace                         Add additional namespace to generated types
        --use-iso-date-format                          Explicitly format date query string parameters in ISO 8601 standard date format using delimiters (2023-06-15)
        --multiple-interfaces                          Generate a Refit interface for each endpoint. May be one of ByEndpoint, ByTag                                                                                                                    

Settings file format

The following is an example .refitter file

{
  "openApiPath": "/path/to/your/openAPI", // Required
  "namespace": "Org.System.Service.Api.GeneratedCode", // Optional. Default=GeneratedCode
  "naming": {
    "useOpenApiTitle": false, // Optional. Default=true
    "interfaceName": "MyApiClient" // Optional. Default=ApiClient
  },
  "generateContracts": true, // Optional. Default=true
  "generateXmlDocCodeComments": true, // Optional. Default=true
  "addAutoGeneratedHeader": true, // Optional. Default=true
  "addAcceptHeaders": true, // Optional. Default=true
  "returnIApiResponse": false, // Optional. Default=false
  "generateOperationHeaders": true, // Optional. Default=true
  "typeAccessibility": "Public", // Optional. Values=Public|Internal. Default=Public
  "useCancellationTokens": false, // Optional. Default=false
  "useIsoDateFormat": false, // Optional. Default=false
  "multipleInterfaces": "ByEndpoint", // Optional. May be one of "ByEndpoint" or "ByTag"
  "additionalNamespaces": [ // Optional
    "Namespace1",
    "Namespace2"
  ]
}
  • openApiPath - points to the OpenAPI Specifications file. This can be the path to a file stored on disk, relative to the .refitter file. This can also be a URL to a remote file that will be downloaded over HTTP/HTTPS
  • namespace - the namespace used in the generated code. If not specified, this defaults to GeneratedCode
  • naming.useOpenApiTitle - a boolean indicating whether the OpenApi title should be used. Default is true
  • naming.interfaceName - the name of the generated interface. The generated code will automatically prefix this with I so if this set to MyApiClient then the generated interface is called IMyApiClient. Default is ApiClient
  • generateContracts - a boolean indicating whether contracts should be generated. A use case for this is several API clients use the same contracts. Default is true
  • generateXmlDocCodeComments - a boolean indicating whether XML doc comments should be generated. Default is true
  • addAutoGeneratedHeader - a boolean indicating whether XML doc comments should be generated. Default is true
  • addAcceptHeaders - a boolean indicating whether to add accept headers [Headers("Accept: application/json")]. Default is true
  • returnIApiResponse - a boolean indicating whether to return IApiResponse<T> objects. Default is false
  • generateOperationHeaders - a boolean indicating whether to use operation headers in the generated methods. Default is true
  • typeAccessibility - the generated type accessibility. Possible values are Public and Internal. Default is Public
  • useCancellationTokens - Use cancellation tokens in the generated methods. Default is false
  • useIsoDateFormat - Set to true to explicitly format date query string parameters in ISO 8601 standard date format using delimiters (for example: 2023-06-15). Default is false
  • multipleInterfaces - Set to ByEndpoint to generate an interface for each endpoint, or ByTag to group Endpoints by their Tag (like SwaggerUI groups them).
  • additionalNamespaces - A collection of additional namespaces to include in the generated file. A use case for this is when you want to reuse contracts from a different namespace than the generated code. Default is empty

A new attribute has been added to the Settings.cs file to allow the use of a .refitter settings file. The attribute is named SettingsFilePath and if it's specified, it will ignore all other settings. This provides more control for users who wish to utilize a predefined settings file.
In order to provide an option for users to adjust tool settings through a JSON file, added a process of JSON deserialization. If a settings file path is provided, the tool now reads the file, deserialize the JSON content into the `RefitGeneratorSettings` object, and updates the `OpenApiPath`. This gives users more flexibility to manage their tool configurations.
The configuration in the main program has been extended to include a local example of an openapi refitter. Now, it can process a local openapi.json file using specified refitter settings. This enhancement aims to make it easier for developers to test and debug the API refitting process in their local environment. The source path and settings files' parameters are adjustable depending on developer's configuration.
The change adds an extra option "ByEndpoint" to the configuration in the 'Refitter' application. This new option will allow users to generate multiple interfaces based on different endpoints. It extends the functionality of the "--multiple-interfaces" option and provides more granular control over interface generation from OpenAPI specification.
Added description of the new "--settings-file" option to the examples and options sections of the README file. This change reflects the addition of a new feature which allows users to specify a settings file, ignoring all other settings if specified. This feature increases flexibility and eases configuration under certain conditions.
@christianhelle christianhelle added the enhancement New feature, bug fix, or request label Aug 24, 2023
@christianhelle christianhelle self-assigned this Aug 24, 2023
@codecov
Copy link

codecov bot commented Aug 24, 2023

Codecov Report

Merging #127 (6eb5a31) into main (2eb8a11) will not change coverage.
Report is 2 commits behind head on main.
The diff coverage is n/a.

@@           Coverage Diff           @@
##             main     #127   +/-   ##
=======================================
  Coverage   98.29%   98.29%           
=======================================
  Files          32       32           
  Lines         997      997           
=======================================
  Hits          980      980           
  Misses          6        6           
  Partials       11       11           
Flag Coverage Δ
unittests 98.29% <ø> (ø)

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

@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@christianhelle christianhelle marked this pull request as ready for review August 24, 2023 13:14
@christianhelle christianhelle merged commit 6c25e93 into main Aug 24, 2023
@christianhelle christianhelle deleted the use-refitter-file-from-cli-tool branch August 24, 2023 19:15
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.

Add support for using .refitter file from CLI
1 participant