Skip to content

Enable conditional content in webapi .http files using SpecialCustomOperations#65318

Open
Copilot wants to merge 5 commits intomainfrom
copilot/fix-extraneous-http-lines
Open

Enable conditional content in webapi .http files using SpecialCustomOperations#65318
Copilot wants to merge 5 commits intomainfrom
copilot/fix-extraneous-http-lines

Conversation

Copy link
Contributor

Copilot AI commented Feb 4, 2026

Enable conditional content in webapi .http files using SpecialCustomOperations

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Fix template directives appearing as literal text in generated .http files

Description

The webapi template's Company.WebApplication1.http file contained #if (EnableOpenAPI) and #endif directives that were not being processed by the template engine, resulting in these lines appearing verbatim in generated projects:

@buggy_HostAddress = http://localhost:5157

#if (EnableOpenAPI)                                      <<< literal text
GET {{buggy_HostAddress}}/openapi/v1.json
Accept: application/json

###
#endif                                                   <<< literal text

GET {{buggy_HostAddress}}/weatherforecast/

Root Cause:
The .http file used conditional directives, but the template engine wasn't configured to process these directives in .http files, causing them to appear as literal text.

Solution:
Added SpecialCustomOperations configuration to the template.json file to enable the template engine to properly process conditional directives in .http files. This allows the OpenAPI endpoint to be conditionally included based on the EnableOpenAPI parameter.

Changes:

  • Added SpecialCustomOperations section to template.json to configure conditional processing for **/*.http files
  • Configured the template engine to recognize #if, #else, #elseif, and #endif directives in .http files with proper boolean configuration values
  • The OpenAPI endpoint is now properly included when OpenAPI is enabled (default) and excluded when --no-openapi flag is used
  • Added two automated tests in WebApiTemplateTest.cs:
    • WebApiTemplate_HttpFileIncludesOpenApiEndpoint: Verifies OpenAPI endpoint is included by default and no template directives remain
    • WebApiTemplate_HttpFileExcludesOpenApiEndpointWhenDisabled: Verifies OpenAPI endpoint is excluded when --no-openapi is used and no template directives remain

Testing:

  • ✅ With OpenAPI enabled (default): Generated .http file includes the OpenAPI endpoint cleanly, without extraneous directive lines
  • ✅ With --no-openapi flag: Generated .http file excludes the OpenAPI endpoint section entirely, without extraneous directive lines
  • ✅ Both scenarios produce clean .http files with properly processed conditionals
  • ✅ Automated tests verify the behavior and prevent regression
Original prompt

This section details on the original issue you should resolve

<issue_title>Extraneous lines in new webapi project http file</issue_title>
<issue_description>In .NET 11, preview 1, a new webapi project created with dotnet new:

dotnet new webapi

contains a .http file with extraneous lines:

@buggy_HostAddress = http://localhost:5157

#if (EnableOpenAPI)                                      <<< extraneous
GET {{buggy_HostAddress}}/openapi/v1.json
Accept: application/json

###
#endif                                                   <<< extraneous

GET {{buggy_HostAddress}}/weatherforecast/
Accept: application/json

###

This problem was very likely introduced by this recent PR: #64951</issue_description>

<agent_instructions>@copilot please investigate this issue and submit a PR with a fix.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: mikekistler <85643503+mikekistler@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix extraneous lines in new webapi project http file Remove unprocessed template directives from webapi .http file Feb 4, 2026
Copilot AI requested a review from mikekistler February 4, 2026 01:57
@mikekistler
Copy link
Contributor

@copilot Is there no way to include this content conditionally?

Copilot AI and others added 2 commits February 4, 2026 03:24
Co-authored-by: mikekistler <85643503+mikekistler@users.noreply.github.com>
Co-authored-by: mikekistler <85643503+mikekistler@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 4, 2026

@copilot Is there no way to include this content conditionally?

Yes! I've implemented conditional inclusion using SpecialCustomOperations in the template.json configuration. This tells the template engine to process #if/#endif directives in .http files.

With this approach:

  • When OpenAPI is enabled (default), the generated .http file includes the OpenAPI endpoint
  • When --no-openapi is used, the OpenAPI endpoint section is completely omitted
  • No extraneous directive lines appear in either case

Changes in commits 0b5cb4b and 7c3f259.

Copilot AI changed the title Remove unprocessed template directives from webapi .http file Enable conditional content in webapi .http files using SpecialCustomOperations Feb 4, 2026
@mikekistler
Copy link
Contributor

@copilot Can you add a test that verifies the behavior both with and without --no-openapi

Co-authored-by: mikekistler <85643503+mikekistler@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 4, 2026

@copilot Can you add a test that verifies the behavior both with and without --no-openapi

Added two tests in commit f9cd978:

  • WebApiTemplate_HttpFileIncludesOpenApiEndpoint: Verifies OpenAPI endpoint is included by default
  • WebApiTemplate_HttpFileExcludesOpenApiEndpointWhenDisabled: Verifies OpenAPI endpoint is excluded with --no-openapi

Both tests verify that no template directives (#if/#endif) remain in the generated .http files and that the weatherforecast endpoint is always present.

@mikekistler
Copy link
Contributor

@copilot This looks good. Please move this PR as ready for review and tag the appropriate folks to review.

Copy link
Contributor Author

Copilot AI commented Feb 4, 2026

@copilot This looks good. Please move this PR as ready for review and tag the appropriate folks to review.

I don't have the ability to change the PR status or tag reviewers directly. The PR is complete with:

  • Implementation using SpecialCustomOperations (commits 0b5cb4b, 7c3f259)
  • Automated tests (commit f9cd978)
  • All code reviews and security checks passed

You'll need to mark the PR as ready for review and tag the appropriate reviewers through the GitHub UI.

Copy link
Contributor

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

Fixes conditional template directives (#if (EnableOpenAPI) / #endif) being emitted as literal text in generated webapi .http files by enabling conditional processing for **/*.http files in the WebApi C# template and adding regression tests.

Changes:

  • Configure the template engine to process conditional directives in .http files via SpecialCustomOperations in the WebApi-CSharp template.json.
  • Add tests verifying the generated .http file includes the OpenAPI request by default and excludes it when --no-openapi is used.
  • Ensure no template directives remain in the generated .http file in either configuration.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/ProjectTemplates/test/Templates.Mvc.Tests/WebApiTemplateTest.cs Adds regression coverage for .http generation with OpenAPI enabled/disabled.
src/ProjectTemplates/Web.ProjectTemplates/content/WebApi-CSharp/.template.config/template.json Enables conditional directive processing for **/*.http files so #if/#endif blocks are evaluated rather than emitted.

Comment on lines +423 to +439
"SpecialCustomOperations": {
"**/*.http": {
"operations": [
{
"type": "conditional",
"configuration": {
"if": ["#if"],
"else": ["#else"],
"elseif": ["#elseif", "#elif"],
"endif": ["#endif"],
"trim": true,
"wholeLine": true
}
}
]
}
},
Copy link

Copilot AI Feb 4, 2026

Choose a reason for hiding this comment

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

Optional: this file’s top-level keys are otherwise lower camelCase (e.g., primaryOutputs, defaultName). SpecialCustomOperations introduces a different casing style; if the template schema/engine supports specialCustomOperations, consider using that casing for consistency and better schema/tooling alignment.

Copilot uses AI. Check for mistakes.
Comment on lines +282 to +285
[ConditionalFact]
[SkipOnHelix("Cert failure, https://github.com/dotnet/aspnetcore/issues/28090", Queues = "All.OSX;" + HelixConstants.Windows10Arm64 + HelixConstants.DebianArm64)]
public async Task WebApiTemplate_HttpFileIncludesOpenApiEndpoint()
{
Copy link

Copilot AI Feb 4, 2026

Choose a reason for hiding this comment

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

Test method name doesn’t follow the naming pattern used throughout this file (most C# webapi tests include CSharp in the method name, e.g., WebApiTemplateCSharp_WithoutOpenAPI). Consider renaming to something like WebApiTemplateCSharp_HttpFileIncludesOpenApiEndpoint to make the language/scenario consistent and easier to scan.

Copilot generated this review using guidance from repository custom instructions.
Comment on lines +302 to +305
[ConditionalFact]
[SkipOnHelix("Cert failure, https://github.com/dotnet/aspnetcore/issues/28090", Queues = "All.OSX;" + HelixConstants.Windows10Arm64 + HelixConstants.DebianArm64)]
public async Task WebApiTemplate_HttpFileExcludesOpenApiEndpointWhenDisabled()
{
Copy link

Copilot AI Feb 4, 2026

Choose a reason for hiding this comment

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

Test method name doesn’t follow the naming pattern used throughout this file (most C# webapi tests include CSharp in the method name). Consider renaming to something like WebApiTemplateCSharp_HttpFileExcludesOpenApiEndpointWhenDisabled for consistency and clarity.

Copilot generated this review using guidance from repository custom instructions.
@mikekistler mikekistler requested a review from a team February 4, 2026 19:51
@github-actions github-actions bot added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label Feb 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Extraneous lines in new webapi project http file

2 participants