Option to exclude deprecated endpoints#5316
Option to exclude deprecated endpoints#5316knoepdan wants to merge 3 commits intoRicoSuter:masterfrom
Conversation
|
Is there a chance this PR gets reviewed and merged? |
There was a problem hiding this comment.
Pull request overview
Adds a new excludeDeprecated option to NSwag client generation to allow omitting deprecated operations (while still allowing explicit inclusion via includedOperationIds), reducing generated client size and discouraging usage of deprecated endpoints.
Changes:
- Introduces
ExcludeDeprecatedin shared generator settings and applies filtering during operation selection. - Exposes the option via CLI commands and NSwagStudio UI, and wires it into MSBuild (C# OpenApiReference) arguments.
- Updates samples/docs and extends C# snapshot-based tests to cover deprecated endpoint handling.
Reviewed changes
Copilot reviewed 34 out of 35 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
| src/NSwagStudio/Views/CodeGenerators/SwaggerToTypeScriptClientGeneratorView.xaml | Adds UI checkbox for excluding deprecated endpoints in TS client generation. |
| src/NSwagStudio/Views/CodeGenerators/SwaggerToCSharpControllerGeneratorView.xaml | Adds UI checkbox for excluding deprecated endpoints in C# controller generation. |
| src/NSwagStudio/Views/CodeGenerators/SwaggerToCSharpClientGeneratorView.xaml | Adds UI checkbox for excluding deprecated endpoints in C# client generation. |
| src/NSwag.Sample.NET90Minimal/nswag.json | Adds excludeDeprecated setting to sample config. |
| src/NSwag.Sample.NET90/nswag.json | Adds excludeDeprecated setting to sample config. |
| src/NSwag.Sample.NET80Minimal/nswag.json | Adds excludeDeprecated setting to sample config. |
| src/NSwag.Sample.NET80/nswag.json | Adds excludeDeprecated setting to sample config. |
| src/NSwag.Sample.NET100Minimal/nswag.json | Adds excludeDeprecated setting to sample config. |
| src/NSwag.Sample.NET100/nswag.json | Adds excludeDeprecated setting to sample config. |
| src/NSwag.Commands/Commands/CodeGeneration/OpenApiToTypeScriptClientCommand.cs | Exposes ExcludeDeprecated as a CLI argument for TS generation. |
| src/NSwag.Commands/Commands/CodeGeneration/OpenApiToCSharpCommandBase.cs | Exposes ExcludeDeprecated as a CLI argument for C# generation commands. |
| src/NSwag.CodeGeneration/ClientGeneratorBaseSettings.cs | Adds ExcludeDeprecated setting to base generator settings. |
| src/NSwag.CodeGeneration/ClientGeneratorBase.cs | Implements filtering of deprecated operations during operation collection. |
| src/NSwag.CodeGeneration.CSharp.Tests/Snapshots/CSharpClientSettingsTests.When_parameter_name_is_reserved_keyword_then_it_is_appended_with_at.verified.txt | Snapshot updated due to deprecated operation now emitting [Obsolete]. |
| src/NSwag.CodeGeneration.CSharp.Tests/Snapshots/CSharpClientSettingsTests.When_include_operation_ids_is_provided_then_only_selected_operations_are_included_in_generated_code_includedOperationIds=3.verified.txt | Snapshot updated due to deprecated operation now emitting [Obsolete]. |
| src/NSwag.CodeGeneration.CSharp.Tests/Snapshots/CSharpClientSettingsTests.When_exclude_operation_ids_is_provided_then_selected_operations_should_be_excluded_from_generated_code_excludedOperationIds=2.verified.txt | Snapshot updated due to deprecated operation now emitting [Obsolete]. |
| src/NSwag.CodeGeneration.CSharp.Tests/Snapshots/CSharpClientSettingsTests.When_exclude_operation_ids_is_provided_then_selected_operations_should_be_excluded_from_generated_code_excludedOperationIds=1.verified.txt | Snapshot updated due to deprecated operation now emitting [Obsolete]. |
| src/NSwag.CodeGeneration.CSharp.Tests/Snapshots/CSharpClientSettingsTests.When_depreacted_endpoints_are_excluded_the_client_should_still_generate_explicitly_included_endpoints.verified.txt | New snapshot covering “exclude deprecated but allow explicit include” behavior. |
| src/NSwag.CodeGeneration.CSharp.Tests/Snapshots/CSharpClientSettingsTests.When_depreacted_endpoints_are_excluded_the_client_should_not_generate_these_endpoint.verified.txt | New snapshot covering “exclude deprecated endpoints” behavior. |
| src/NSwag.CodeGeneration.CSharp.Tests/Snapshots/CSharpClientSettingsTests.When_custom_http_client_type_is_specified_then_an_instance_of_that_type_is_used.verified.txt | Snapshot updated due to deprecated operation now emitting [Obsolete]. |
| src/NSwag.CodeGeneration.CSharp.Tests/Snapshots/CSharpClientSettingsTests.When_code_is_generated_then_by_default_the_system_httpclient_is_used.verified.txt | Snapshot updated due to deprecated operation now emitting [Obsolete]. |
| src/NSwag.CodeGeneration.CSharp.Tests/Snapshots/CSharpClientSettingsTests.When_client_interface_generation_is_enabled_and_suppressed_then_client_interface_is_not_generated.verified.txt | Snapshot updated due to deprecated operation now emitting [Obsolete]. |
| src/NSwag.CodeGeneration.CSharp.Tests/Snapshots/CSharpClientSettingsTests.When_client_class_generation_is_enabled_and_suppressed_then_client_class_is_not_generated.verified.txt | Snapshot updated due to deprecated operation now emitting [Obsolete]. |
| src/NSwag.CodeGeneration.CSharp.Tests/Snapshots/CSharpClientSettingsTests.When_client_base_interface_is_specified_with_access_modifier_then_client_interface_extends_it_and_has_correct_access_modifier.verified.txt | Snapshot updated due to deprecated operation now emitting [Obsolete]. |
| src/NSwag.CodeGeneration.CSharp.Tests/Snapshots/CSharpClientSettingsTests.When_client_base_interface_is_specified_then_client_interface_extends_it.verified.txt | Snapshot updated due to deprecated operation now emitting [Obsolete]. |
| src/NSwag.CodeGeneration.CSharp.Tests/Snapshots/CSharpClientSettingsTests.When_client_base_interface_is_not_specified_then_client_interface_should_have_no_base_interface_and_has_correct_access_modifier.verified.txt | Snapshot updated due to deprecated operation now emitting [Obsolete]. |
| src/NSwag.CodeGeneration.CSharp.Tests/Snapshots/CSharpClientSettingsTests.When_client_base_interface_is_not_specified_then_client_interface_should_have_no_base_interface.verified.txt | Snapshot updated due to deprecated operation now emitting [Obsolete]. |
| src/NSwag.CodeGeneration.CSharp.Tests/Snapshots/CSharpClientSettingsTests.When_UseHttpRequestMessageCreationMethod_is_set_then_CreateRequestMessage_is_generated.verified.txt | Snapshot updated due to deprecated operation now emitting [Obsolete]. |
| src/NSwag.CodeGeneration.CSharp.Tests/Snapshots/CSharpClientSettingsTests.When_ConfigurationClass_is_set_then_correct_ctor_is_generated.verified.txt | Snapshot updated due to deprecated operation now emitting [Obsolete]. |
| src/NSwag.CodeGeneration.CSharp.Tests/Snapshots/CSharpClientSettingsTests.WhenUsingBaseUrl_ButNoProperty_ThenPropertyIsNotUsedAndFieldIsGenerated.verified.txt | Snapshot updated due to deprecated operation now emitting [Obsolete]. |
| src/NSwag.CodeGeneration.CSharp.Tests/CSharpClientSettingsTests.cs | Marks one controller action as [Obsolete] and adds tests for ExcludeDeprecated. |
| src/NSwag.ApiDescription.Client/NSwag.ApiDescription.Client.targets | Adds MSBuild command-line switch forwarding for /excludeDeprecated. |
| src/NSwag.ApiDescription.Client/NSwag.ApiDescription.Client.props | Adds MSBuild metadata default for NSwagExcludeDeprecated (currently only for project refs). |
| docs/tutorials/GenerateProxyClientWithCLI/sample.nswag | Adds a sample .nswag config including excludeDeprecated. |
| docs/tutorials/GenerateProxyClientWithCLI/generate-proxy-client.md | Documents excludeDeprecated in the tutorial sample JSON. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| <NSwagIncludedOperationIds>$(NSwagIncludedOperationIds)</NSwagIncludedOperationIds> | ||
| <NSwagExcludedOperationIds>$(NSwagExcludedOperationIds)</NSwagExcludedOperationIds> | ||
| <NSwagExcludeDeprecated>$(NSwagExcludeDeprecated)</NSwagExcludeDeprecated> | ||
| <NSwagAdditionalNamespaceUsages>$(NSwagAdditionalNamespaceUsages)</NSwagAdditionalNamespaceUsages> |
There was a problem hiding this comment.
NSwagExcludeDeprecated is added to the OpenApiProjectReference item defaults, but GenerateNSwagCSharp consumes %(NSwagExcludeDeprecated) from OpenApiReference items as well. For consistency with NSwagIncludedOperationIds/NSwagExcludedOperationIds, add the same metadata default to the <OpenApiReference> section too; otherwise setting $(NSwagExcludeDeprecated) won’t flow into OpenApiReference items unless users set the item metadata explicitly.
| <CurrentOpenApiReference> | ||
| <Command Condition="'%(NSwagExcludedOperationIds)' != ''">%(Command) /excludedOperationIds:%(NSwagExcludedOperationIds)</Command> | ||
| </CurrentOpenApiReference> | ||
| <CurrentOpenApiReference> | ||
| <Command Condition="'%(NSwagExcludeDeprecated)' != ''">%(Command) /excludeDeprecated:%(NSwagExcludeDeprecated)</Command> | ||
| </CurrentOpenApiReference> | ||
| <CurrentOpenApiReference> |
There was a problem hiding this comment.
This block introduces tab-indented XML, which is inconsistent with the surrounding spacing/indentation in this targets file. Please reformat these lines to match the existing indentation style (spaces) to avoid noisy diffs and keep the file consistent.
| <CurrentOpenApiReference> | |
| <Command Condition="'%(NSwagExcludedOperationIds)' != ''">%(Command) /excludedOperationIds:%(NSwagExcludedOperationIds)</Command> | |
| </CurrentOpenApiReference> | |
| <CurrentOpenApiReference> | |
| <Command Condition="'%(NSwagExcludeDeprecated)' != ''">%(Command) /excludeDeprecated:%(NSwagExcludeDeprecated)</Command> | |
| </CurrentOpenApiReference> | |
| <CurrentOpenApiReference> | |
| <CurrentOpenApiReference> | |
| <Command Condition="'%(NSwagExcludedOperationIds)' != ''">%(Command) /excludedOperationIds:%(NSwagExcludedOperationIds)</Command> | |
| </CurrentOpenApiReference> | |
| <CurrentOpenApiReference> | |
| <Command Condition="'%(NSwagExcludeDeprecated)' != ''">%(Command) /excludeDeprecated:%(NSwagExcludeDeprecated)</Command> | |
| </CurrentOpenApiReference> | |
| <CurrentOpenApiReference> |
| public async Task When_depreacted_endpoints_are_excluded_the_client_should_not_generate_these_endpoint() | ||
| { |
There was a problem hiding this comment.
Typo in test name: “depreacted” → “deprecated”. Renaming this test will also require renaming the corresponding snapshot file(s) so the Verify snapshot naming stays in sync.
| public async Task When_depreacted_endpoints_are_excluded_the_client_should_still_generate_explicitly_included_endpoints() | ||
| { |
There was a problem hiding this comment.
Typo in test name: “depreacted” → “deprecated”. Renaming this test will also require renaming the corresponding snapshot file(s) so the Verify snapshot naming stays in sync.
| /// <summary>Gets or sets the operations that should be excluded from the generated client.</summary> | ||
| public string[] ExcludedOperationIds { get; set; } | ||
|
|
||
| /// <summary>Gets or sets the value indicating if deprecated endpoints shall be rendered</summary> |
There was a problem hiding this comment.
The XML doc comment for ExcludeDeprecated is misleading: it says deprecated endpoints “shall be rendered”, but the setting name/behavior is to exclude deprecated operations from generation when true. Update the summary to clearly state that deprecated endpoints are excluded/skipped when enabled (and consider adding a trailing period for consistency with surrounding docs).
| /// <summary>Gets or sets the value indicating if deprecated endpoints shall be rendered</summary> | |
| /// <summary>Gets or sets a value indicating whether deprecated endpoints are excluded from the generated client.</summary> |
| [Argument(Name = "ExcludeDeprecated", IsRequired = false, Description = "Specifies if deprecated endpoints should be generated")] | ||
| public bool ExcludeDeprecated | ||
| { | ||
| get => Settings.ExcludeDeprecated; | ||
| set => Settings.ExcludeDeprecated = value; | ||
| } |
There was a problem hiding this comment.
The CLI argument description contradicts the option name: ExcludeDeprecated suggests deprecated endpoints are excluded, but the description says they “should be generated”. Please reword to something like “Specifies whether deprecated endpoints should be excluded from generation.” so the CLI help is unambiguous.
| [Argument(Name = "ExcludeDeprecated", IsRequired = false, Description = "Specifies if deprecated endpoints should be generated")] | ||
| public bool ExcludeDeprecated | ||
| { | ||
| get => Settings.ExcludeDeprecated; | ||
| set => Settings.ExcludeDeprecated = value; | ||
| } |
There was a problem hiding this comment.
The CLI argument description contradicts the option name: ExcludeDeprecated implies deprecated endpoints are excluded, but the description says they “should be generated”. Please reword the description to clearly state that deprecated endpoints are skipped/excluded when the flag is enabled.
|
Thanks CoPilot.. great job ;-) will look into it |
I added a new option "excludeDeprecated" to exclude deprecated endpoints from client generation.
My motivation for this change comes from the experience I have within my own company where we have a huge api that is used internally by many different applications but also exposed to the outside world. We have a policy that we never consume deprecated endpoints ourselves. However, sometimes this policy is hard to follow (sometimes depending on the repo and it's settings). Excluding every deprecated endpoint would help us a lot as we would get compile time errors, once "excludeDeprecated" is set to true. Furthermore, there it size of the clients would be removed, especially for Typescript and some complexity for the developer is removed.
PS: A lot of the changes were similar as a checkin from 4 months ago that added the settings "includedOperationIds" and "excludedOperationIds". Helped me a lot and I think that functionality will also help us. Thanks Amine. But I still think this would be helpful