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

Added GenerateClients options for seperate generation #436

Merged
merged 3 commits into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ The following is an example `.refitter` file
"interfaceName": "MyApiClient" // Optional. Default=ApiClient
},
"generateContracts": true, // Optional. Default=true
"generateClients": true, // Optional. Default=true
"generateXmlDocCodeComments": true, // Optional. Default=true
"generateStatusCodeComments": true, // Optional. Default=true
"addAutoGeneratedHeader": true, // Optional. Default=true
Expand Down Expand Up @@ -256,6 +257,7 @@ The following is an example `.refitter` file
- `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`
- `generateClients`: - a boolean indicating whether clients should be generated. A use case for this is to seperate clients and contracts in two generation
- `generateXmlDocCodeComments` - a boolean indicating whether XML doc comments should be generated. Default is `true`
- `generateStatusCodeComments` - a boolean indicating whether the XML docs for `ApiException` and `IApiResponse` contain detailed descriptions for every documented status code. Default is `true`
- `addAutoGeneratedHeader` - a boolean indicating whether XML doc comments should be generated. Default is `true`
Expand Down
2 changes: 1 addition & 1 deletion src/Refitter.Core/RefitGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public string Generate()
? document.Info!.Title.Sanitize()
: settings.Naming.InterfaceName;
return new StringBuilder()
.AppendLine(generatedCode.SourceCode)
.AppendLine(settings.GenerateClients ? generatedCode.SourceCode : string.Empty)
.AppendLine()
.AppendLine(settings.GenerateContracts ? contracts : string.Empty)
.AppendLine(settings.ApizrSettings != null
Expand Down
5 changes: 5 additions & 0 deletions src/Refitter.Core/Settings/RefitGeneratorSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public class RefitGeneratorSettings
/// </summary>
public bool GenerateContracts { get; set; } = true;

/// <summary>
/// Gets or sets a value indicating whether clients should be generated.
/// </summary>
public bool GenerateClients { get; set; } = true;

/// <summary>
/// Gets or sets a value indicating whether XML doc comments should be generated.
/// </summary>
Expand Down
14 changes: 14 additions & 0 deletions src/Refitter.Tests/SwaggerPetstoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ public async Task Can_Generate_Code_Without_Contracts(SampleOpenSpecifications v
generateCode.Should().NotContain("class Pet");
}

[Theory]
[InlineData(SampleOpenSpecifications.SwaggerPetstoreJsonV3, "SwaggerPetstore.json")]
[InlineData(SampleOpenSpecifications.SwaggerPetstoreYamlV3, "SwaggerPetstore.yaml")]
[InlineData(SampleOpenSpecifications.SwaggerPetstoreJsonV2, "SwaggerPetstore.json")]
[InlineData(SampleOpenSpecifications.SwaggerPetstoreYamlV2, "SwaggerPetstore.yaml")]
public async Task Can_Generate_Code_Without_Clients(SampleOpenSpecifications version, string filename)
{
var generateCode = await GenerateCode(
version,
filename,
new RefitGeneratorSettings { GenerateXmlDocCodeComments = false, GenerateClients = false });
generateCode.Should().NotContain("ISwaggerPetstore");
}

[Theory]
[InlineData(SampleOpenSpecifications.SwaggerPetstoreJsonV3, "SwaggerPetstore.json")]
[InlineData(SampleOpenSpecifications.SwaggerPetstoreYamlV3, "SwaggerPetstore.yaml")]
Expand Down
1 change: 1 addition & 0 deletions src/Refitter/GenerateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public override async Task<int> ExecuteAsync(CommandContext context, Settings se
AddAutoGeneratedHeader = !settings.NoAutoGeneratedHeader,
AddAcceptHeaders = !settings.NoAcceptHeaders,
GenerateContracts = !settings.InterfaceOnly,
GenerateClients = !settings.ContractOnly,
ReturnIApiResponse = settings.ReturnIApiResponse,
ReturnIObservable = settings.ReturnIObservable,
UseCancellationTokens = settings.UseCancellationTokens,
Expand Down
7 changes: 7 additions & 0 deletions src/Refitter/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ private static int Main(string[] args)
"./IGeneratedCode.cs",
"--interface-only");

configuration
.AddExample(
"./openapi.json",
"--output",
"./GeneratedContracts.cs",
"--contract-only");

configuration
.AddExample(
"./openapi.json",
Expand Down
1 change: 1 addition & 0 deletions src/Refitter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ EXAMPLES:
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 --output ./GeneratedContracts.cs --contract-only
refitter ./openapi.json --use-api-response
refitter ./openapi.json --cancellation-tokens
refitter ./openapi.json --no-operation-headers
Expand Down
5 changes: 5 additions & 0 deletions src/Refitter/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public sealed class Settings : CommandSettings
[DefaultValue(false)]
public bool InterfaceOnly { get; set; }

[Description("Don't generate clients")]
[CommandOption("--contract-only")]
[DefaultValue(false)]
public bool ContractOnly { get; set; }

[Description("Return Task<IApiResponse<T>> instead of Task<T>")]
[CommandOption("--use-api-response")]
[DefaultValue(false)]
Expand Down
Loading