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: diff --git a/README.md b/README.md index 7a7a61ed..fb8d4c26 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: @@ -77,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 @@ -122,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(); @@ -161,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. 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 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