Enable conditional content in webapi .http files using SpecialCustomOperations#65318
Enable conditional content in webapi .http files using SpecialCustomOperations#65318
Conversation
Co-authored-by: mikekistler <85643503+mikekistler@users.noreply.github.com>
|
@copilot Is there no way to include this content conditionally? |
Co-authored-by: mikekistler <85643503+mikekistler@users.noreply.github.com>
Co-authored-by: mikekistler <85643503+mikekistler@users.noreply.github.com>
Yes! I've implemented conditional inclusion using With this approach:
|
|
@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>
Added two tests in commit f9cd978:
Both tests verify that no template directives ( |
|
@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:
You'll need to mark the PR as ready for review and tag the appropriate reviewers through the GitHub UI. |
There was a problem hiding this comment.
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
.httpfiles viaSpecialCustomOperationsin the WebApi-CSharptemplate.json. - Add tests verifying the generated
.httpfile includes the OpenAPI request by default and excludes it when--no-openapiis used. - Ensure no template directives remain in the generated
.httpfile 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. |
| "SpecialCustomOperations": { | ||
| "**/*.http": { | ||
| "operations": [ | ||
| { | ||
| "type": "conditional", | ||
| "configuration": { | ||
| "if": ["#if"], | ||
| "else": ["#else"], | ||
| "elseif": ["#elseif", "#elif"], | ||
| "endif": ["#endif"], | ||
| "trim": true, | ||
| "wholeLine": true | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| }, |
There was a problem hiding this comment.
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.
| [ConditionalFact] | ||
| [SkipOnHelix("Cert failure, https://github.com/dotnet/aspnetcore/issues/28090", Queues = "All.OSX;" + HelixConstants.Windows10Arm64 + HelixConstants.DebianArm64)] | ||
| public async Task WebApiTemplate_HttpFileIncludesOpenApiEndpoint() | ||
| { |
There was a problem hiding this comment.
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.
| [ConditionalFact] | ||
| [SkipOnHelix("Cert failure, https://github.com/dotnet/aspnetcore/issues/28090", Queues = "All.OSX;" + HelixConstants.Windows10Arm64 + HelixConstants.DebianArm64)] | ||
| public async Task WebApiTemplate_HttpFileExcludesOpenApiEndpointWhenDisabled() | ||
| { |
There was a problem hiding this comment.
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.
Enable conditional content in webapi .http files using SpecialCustomOperations
Fix template directives appearing as literal text in generated .http files
Description
The webapi template's
Company.WebApplication1.httpfile contained#if (EnableOpenAPI)and#endifdirectives that were not being processed by the template engine, resulting in these lines appearing verbatim in generated projects: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
SpecialCustomOperationsconfiguration 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 theEnableOpenAPIparameter.Changes:
SpecialCustomOperationssection totemplate.jsonto configure conditional processing for**/*.httpfiles#if,#else,#elseif, and#endifdirectives in .http files with proper boolean configuration values--no-openapiflag is usedWebApiTemplateTest.cs:WebApiTemplate_HttpFileIncludesOpenApiEndpoint: Verifies OpenAPI endpoint is included by default and no template directives remainWebApiTemplate_HttpFileExcludesOpenApiEndpointWhenDisabled: Verifies OpenAPI endpoint is excluded when--no-openapiis used and no template directives remainTesting:
--no-openapiflag: Generated .http file excludes the OpenAPI endpoint section entirely, without extraneous directive linesOriginal prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.