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 --no-operation-headers CLI tool argument #25

Merged
merged 5 commits into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from all 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: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
34 changes: 18 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -44,7 +45,8 @@ OPTIONS:
--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
--cancellation-tokens Use cancellation tokens
--no-operation-headers Don't generate operation headers
```

To generate code from an OpenAPI specifications file, run the following:
Expand Down Expand Up @@ -77,40 +79,40 @@ namespace Your.Namespace.Of.Choice.GeneratedCode
/// Update an existing pet by Id
/// </summary>
[Put("/pet")]
Task<Pet> UpdatePet([Body]Pet body);
Task<Pet> UpdatePet([Body] Pet body);

/// <summary>
/// Add a new pet to the store
/// </summary>
[Post("/pet")]
Task<Pet> AddPet([Body]Pet body);
Task<Pet> AddPet([Body] Pet body);

/// <summary>
/// Multiple status values can be provided with comma separated strings
/// </summary>
[Get("/pet/findByStatus")]
Task<ICollection<Pet>> FindPetsByStatus([Query]Status? status);
Task<ICollection<Pet>> FindPetsByStatus([Query(CollectionFormat.Multi)] Status? status);

/// <summary>
/// Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
/// </summary>
[Get("/pet/findByTags")]
Task<ICollection<Pet>> FindPetsByTags([Query(CollectionFormat.Multi)]ICollection<string> tags);
Task<ICollection<Pet>> FindPetsByTags([Query(CollectionFormat.Multi)] IEnumerable<string> tags);

/// <summary>
/// Returns a single pet
/// </summary>
[Get("/pet/{petId}")]
Task<Pet> GetPetById(long? petId);
Task<Pet> 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<ApiResponse> UploadFile(long? petId, [Query]string additionalMetadata, [Body]StreamPart body);
Task<ApiResponse> UploadFile(long petId, [Query(CollectionFormat.Multi)] string additionalMetadata, [Body(BodySerializationMethod.UrlEncoded)] Dictionary<string, object> body);

/// <summary>
/// Returns a map of status codes to quantities
Expand All @@ -122,34 +124,34 @@ namespace Your.Namespace.Of.Choice.GeneratedCode
/// Place a new order in the store
/// </summary>
[Post("/store/order")]
Task<Order> PlaceOrder([Body]Order body);
Task<Order> PlaceOrder([Body] Order body);

/// <summary>
/// For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions
/// </summary>
[Get("/store/order/{orderId}")]
Task<Order> GetOrderById(long? orderId);
Task<Order> GetOrderById(long orderId);

/// <summary>
/// For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors
/// </summary>
[Delete("/store/order/{orderId}")]
Task DeleteOrder(long? orderId);
Task DeleteOrder(long orderId);

/// <summary>
/// This can only be done by the logged in user.
/// </summary>
[Post("/user")]
Task CreateUser([Body]User body);
Task CreateUser([Body] User body);

/// <summary>
/// Creates list of users with given input array
/// </summary>
[Post("/user/createWithList")]
Task<User> CreateUsersWithListInput([Body]ICollection<User> body);
Task<User> CreateUsersWithListInput([Body] IEnumerable<User> body);

[Get("/user/login")]
Task<string> LoginUser([Query]string username, [Query]string password);
Task<string> LoginUser([Query(CollectionFormat.Multi)] string username, [Query(CollectionFormat.Multi)] string password);

[Get("/user/logout")]
Task LogoutUser();
Expand All @@ -161,7 +163,7 @@ namespace Your.Namespace.Of.Choice.GeneratedCode
/// This can only be done by the logged in user.
/// </summary>
[Put("/user/{username}")]
Task UpdateUser(string username, [Body]User body);
Task UpdateUser(string username, [Body] User body);

/// <summary>
/// This can only be done by the logged in user.
Expand Down
26 changes: 26 additions & 0 deletions src/Refitter.Tests/SwaggerPetstoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions src/Refitter/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@
"./openapi.json",
"--cancellation-tokens"
});

configuration
.AddExample(
new[]
{
"./openapi.json",
"--no-operation-headers"
});
});
return app.Run(args);

Expand Down Expand Up @@ -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)
Expand All @@ -143,6 +156,7 @@ public override async Task<int> ExecuteAsync(CommandContext context, Settings se
GenerateContracts = !settings.InterfaceOnly,
ReturnIApiResponse = settings.ReturnIApiResponse,
UseCancellationTokens = settings.UseCancellationTokens,
GenerateOperationHeaders = !settings.NoOperationHeaders,
TypeAccessibility = settings.InternalTypeAccessibility
? TypeAccessibility.Internal
: TypeAccessibility.Public
Expand Down