Skip to content

Fix extraneous lines in webapi .http file using source modifiers#65318

Merged
mikekistler merged 7 commits intomainfrom
copilot/fix-extraneous-http-lines
Mar 2, 2026
Merged

Fix extraneous lines in webapi .http file using source modifiers#65318
mikekistler merged 7 commits intomainfrom
copilot/fix-extraneous-http-lines

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 4, 2026

Fix extraneous lines in webapi .http file

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

Description

The webapi template's .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.

Root Cause: The template engine only processes #if/#endif conditionals in file types it recognizes (e.g. .cs). For .http files, these directives pass through as literal text.

Solution: Use source modifiers (conditional exclude/rename) in template.json to select between two .http file variants — the same pattern already used in this template for Program.cs / Program.Main.cs /
Program.MinimalAPIs.*.cs.

Changes:

  • Company.WebApplication1.http — Removed #if/#endif directives; contains the OpenAPI endpoint (used by default)
  • Company.WebApplication1.NoOpenAPI.http — New file without the OpenAPI endpoint, renamed to Company.WebApplication1.http when --no-openapi is used
  • template.json — Added source modifiers for conditional .http file selection; removed unused SpecialCustomOperations section
  • WebApiTemplateTest.cs — Added two tests verifying .http file content with and without --no-openapi

Fixes #65317

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor

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

Copy link
Copy Markdown
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
Copy Markdown
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 thread src/ProjectTemplates/test/Templates.Mvc.Tests/WebApiTemplateTest.cs
Comment thread src/ProjectTemplates/test/Templates.Mvc.Tests/WebApiTemplateTest.cs
@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
@dotnet-policy-service dotnet-policy-service Bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Feb 17, 2026
Co-authored-by: mikekistler <85643503+mikekistler@users.noreply.github.com>
@mikekistler mikekistler changed the title Enable conditional content in webapi .http files using SpecialCustomOperations Fix extraneous lines in webapi .http file using source modifiers Feb 19, 2026
@github-actions github-actions Bot added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates and removed needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically labels Feb 19, 2026
Copy link
Copy Markdown
Contributor

@mikekistler mikekistler left a comment

Choose a reason for hiding this comment

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

Looks good. 👍

@mikekistler mikekistler requested a review from halter73 February 19, 2026 21:39
@mikekistler mikekistler merged commit 888d231 into main Mar 2, 2026
25 checks passed
@mikekistler mikekistler deleted the copilot/fix-extraneous-http-lines branch March 2, 2026 16:28
@dotnet-policy-service dotnet-policy-service Bot added this to the 11.0-preview3 milestone Mar 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Extraneous lines in new webapi project http file

4 participants