Skip to content

Option to exclude deprecated endpoints#5316

Open
knoepdan wants to merge 3 commits intoRicoSuter:masterfrom
knoepdan:OptionToExcludeDeprecatedEndpoints
Open

Option to exclude deprecated endpoints#5316
knoepdan wants to merge 3 commits intoRicoSuter:masterfrom
knoepdan:OptionToExcludeDeprecatedEndpoints

Conversation

@knoepdan
Copy link
Contributor

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

@knoepdan
Copy link
Contributor Author

knoepdan commented Mar 2, 2026

Is there a chance this PR gets reviewed and merged?

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ExcludeDeprecated in 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.

Comment on lines 131 to 134
<NSwagIncludedOperationIds>$(NSwagIncludedOperationIds)</NSwagIncludedOperationIds>
<NSwagExcludedOperationIds>$(NSwagExcludedOperationIds)</NSwagExcludedOperationIds>
<NSwagExcludeDeprecated>$(NSwagExcludeDeprecated)</NSwagExcludeDeprecated>
<NSwagAdditionalNamespaceUsages>$(NSwagAdditionalNamespaceUsages)</NSwagAdditionalNamespaceUsages>
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines 127 to 133
<CurrentOpenApiReference>
<Command Condition="'%(NSwagExcludedOperationIds)' != ''">%(Command) /excludedOperationIds:%(NSwagExcludedOperationIds)</Command>
</CurrentOpenApiReference>
<CurrentOpenApiReference>
<Command Condition="'%(NSwagExcludeDeprecated)' != ''">%(Command) /excludeDeprecated:%(NSwagExcludeDeprecated)</Command>
</CurrentOpenApiReference>
<CurrentOpenApiReference>
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
<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>

Copilot uses AI. Check for mistakes.
Comment on lines +421 to +422
public async Task When_depreacted_endpoints_are_excluded_the_client_should_not_generate_these_endpoint()
{
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines +447 to +448
public async Task When_depreacted_endpoints_are_excluded_the_client_should_still_generate_explicitly_included_endpoints()
{
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
/// <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>
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Suggested change
/// <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>

Copilot uses AI. Check for mistakes.
Comment on lines +251 to +256
[Argument(Name = "ExcludeDeprecated", IsRequired = false, Description = "Specifies if deprecated endpoints should be generated")]
public bool ExcludeDeprecated
{
get => Settings.ExcludeDeprecated;
set => Settings.ExcludeDeprecated = value;
}
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines +53 to +58
[Argument(Name = "ExcludeDeprecated", IsRequired = false, Description = "Specifies if deprecated endpoints should be generated")]
public bool ExcludeDeprecated
{
get => Settings.ExcludeDeprecated;
set => Settings.ExcludeDeprecated = value;
}
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
@knoepdan
Copy link
Contributor Author

knoepdan commented Mar 6, 2026

Thanks CoPilot.. great job ;-) will look into it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants