From 67423b9442901581bdfb00ad9829c38671dee186 Mon Sep 17 00:00:00 2001 From: Christian Helle Date: Mon, 1 May 2023 22:21:29 +0200 Subject: [PATCH 1/5] Add tests for GenerateOperationHeaders setting --- src/Refitter.Tests/SwaggerPetstoreTests.cs | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/Refitter.Tests/SwaggerPetstoreTests.cs b/src/Refitter.Tests/SwaggerPetstoreTests.cs index e6014f83..440f8d3b 100644 --- a/src/Refitter.Tests/SwaggerPetstoreTests.cs +++ b/src/Refitter.Tests/SwaggerPetstoreTests.cs @@ -160,6 +160,32 @@ public async Task Can_Generate_Code_With_Correct_Usings( Assert.Equal(cancellationTokens, generateCode.Contains("using System.Threading;")); } + [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_With_OperationHeaders(SampleOpenSpecifications version, string filename) + { + var settings = new RefitGeneratorSettings(); + settings.GenerateOperationHeaders = true; + var generateCode = await GenerateCode(version, filename, settings); + generateCode.Should().Contain("[Header(\"api_key\")] string api_key"); + } + + [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_OperationHeaders(SampleOpenSpecifications version, string filename) + { + var settings = new RefitGeneratorSettings(); + settings.GenerateOperationHeaders = false; + var generateCode = await GenerateCode(version, filename, settings); + generateCode.Should().NotContain("[Header(\"api_key\")] string api_key"); + } + [Theory] [InlineData(SampleOpenSpecifications.SwaggerPetstoreJsonV3, "SwaggerPetstore.json")] #if !DEBUG From dee6a34947ff6f349c02a6399ce37fe0750af5d6 Mon Sep 17 00:00:00 2001 From: Christian Helle Date: Mon, 1 May 2023 22:21:51 +0200 Subject: [PATCH 2/5] Introduce --no-operation-headers CLI tool argument --- src/Refitter/Program.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Refitter/Program.cs b/src/Refitter/Program.cs index 1afd5010..e3641b47 100644 --- a/src/Refitter/Program.cs +++ b/src/Refitter/Program.cs @@ -73,6 +73,14 @@ "./openapi.json", "--cancellation-tokens" }); + + configuration + .AddExample( + new[] + { + "./openapi.json", + "--no-operation-headers" + }); }); return app.Run(args); @@ -118,6 +126,11 @@ public sealed class Settings : CommandSettings [CommandOption("--cancellation-tokens")] [DefaultValue(false)] public bool UseCancellationTokens { get; set; } + + [Description("Don't generate operation headers")] + [CommandOption("--no-operation-headers")] + [DefaultValue(false)] + public bool NoOperationHeaders { get; set; } } public override ValidationResult Validate(CommandContext context, Settings settings) @@ -143,6 +156,7 @@ public override async Task ExecuteAsync(CommandContext context, Settings se GenerateContracts = !settings.InterfaceOnly, ReturnIApiResponse = settings.ReturnIApiResponse, UseCancellationTokens = settings.UseCancellationTokens, + GenerateOperationHeaders = !settings.NoOperationHeaders, TypeAccessibility = settings.InternalTypeAccessibility ? TypeAccessibility.Internal : TypeAccessibility.Public From 0c875d3c424cfdf6d6c515a03d99213029a16653 Mon Sep 17 00:00:00 2001 From: Christian Helle Date: Mon, 1 May 2023 22:23:20 +0200 Subject: [PATCH 3/5] Add usage help for --no-operation-headers in README --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7a7a61ed..39e48a4d 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ EXAMPLES: 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 ARGUMENTS: [URL or input file] URL or file path to OpenAPI Specification file @@ -44,7 +45,8 @@ OPTIONS: --interface-only Don't generate contract types --use-api-response Return Task> instead of Task --internal Set the accessibility of the generated types to 'internal' - --cancellation-tokens Use cancellation tokens + --cancellation-tokens Use cancellation tokens + --no-operation-headers Don't generate operation headers ``` To generate code from an OpenAPI specifications file, run the following: From 3c8aba12f8cb050b35ee6ff7831e8890dfd48192 Mon Sep 17 00:00:00 2001 From: Christian Helle Date: Mon, 1 May 2023 22:24:18 +0200 Subject: [PATCH 4/5] Update sample code in README to include headers --- README.md | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 39e48a4d..fb8d4c26 100644 --- a/README.md +++ b/README.md @@ -79,40 +79,40 @@ namespace Your.Namespace.Of.Choice.GeneratedCode /// Update an existing pet by Id /// [Put("/pet")] - Task UpdatePet([Body]Pet body); + Task UpdatePet([Body] Pet body); /// /// Add a new pet to the store /// [Post("/pet")] - Task AddPet([Body]Pet body); + Task AddPet([Body] Pet body); /// /// Multiple status values can be provided with comma separated strings /// [Get("/pet/findByStatus")] - Task> FindPetsByStatus([Query]Status? status); + Task> FindPetsByStatus([Query(CollectionFormat.Multi)] Status? status); /// /// Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing. /// [Get("/pet/findByTags")] - Task> FindPetsByTags([Query(CollectionFormat.Multi)]ICollection tags); + Task> FindPetsByTags([Query(CollectionFormat.Multi)] IEnumerable tags); /// /// Returns a single pet /// [Get("/pet/{petId}")] - Task GetPetById(long? petId); + Task GetPetById(long petId); [Post("/pet/{petId}")] - Task UpdatePetWithForm(long? petId, [Query]string name, [Query]string status); + Task UpdatePetWithForm(long petId, [Query(CollectionFormat.Multi)] string name, [Query(CollectionFormat.Multi)] string status); [Delete("/pet/{petId}")] - Task DeletePet(long? petId); + Task DeletePet(long petId, [Header("api_key")] string api_key); [Post("/pet/{petId}/uploadImage")] - Task UploadFile(long? petId, [Query]string additionalMetadata, [Body]StreamPart body); + Task UploadFile(long petId, [Query(CollectionFormat.Multi)] string additionalMetadata, [Body(BodySerializationMethod.UrlEncoded)] Dictionary body); /// /// Returns a map of status codes to quantities @@ -124,34 +124,34 @@ namespace Your.Namespace.Of.Choice.GeneratedCode /// Place a new order in the store /// [Post("/store/order")] - Task PlaceOrder([Body]Order body); + Task PlaceOrder([Body] Order body); /// /// For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions /// [Get("/store/order/{orderId}")] - Task GetOrderById(long? orderId); + Task GetOrderById(long orderId); /// /// For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors /// [Delete("/store/order/{orderId}")] - Task DeleteOrder(long? orderId); + Task DeleteOrder(long orderId); /// /// This can only be done by the logged in user. /// [Post("/user")] - Task CreateUser([Body]User body); + Task CreateUser([Body] User body); /// /// Creates list of users with given input array /// [Post("/user/createWithList")] - Task CreateUsersWithListInput([Body]ICollection body); + Task CreateUsersWithListInput([Body] IEnumerable body); [Get("/user/login")] - Task LoginUser([Query]string username, [Query]string password); + Task LoginUser([Query(CollectionFormat.Multi)] string username, [Query(CollectionFormat.Multi)] string password); [Get("/user/logout")] Task LogoutUser(); @@ -163,7 +163,7 @@ namespace Your.Namespace.Of.Choice.GeneratedCode /// This can only be done by the logged in user. /// [Put("/user/{username}")] - Task UpdateUser(string username, [Body]User body); + Task UpdateUser(string username, [Body] User body); /// /// This can only be done by the logged in user. From 93f7d05a360ab26f3054503c1cc492da3a073738 Mon Sep 17 00:00:00 2001 From: Christian Helle Date: Mon, 1 May 2023 22:56:17 +0200 Subject: [PATCH 5/5] Bump up version to v0.5.1 --- .github/workflows/release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 59670042..bb89d174 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -7,7 +7,7 @@ on: - "release" env: - VERSION: 0.5.0 + VERSION: 0.5.1 NUGET_REPO_URL: "https://api.nuget.org/v3/index.json" jobs: