Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 12, 2025

Refactor RequestDelegateGenerator to use partial class pattern

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

Restructure RequestDelegateGenerator to match ValidationsGenerator and OpenApiGenerator patterns

Description

Refactors RequestDelegateGenerator to use the same partial class structure as ValidationsGenerator (src/Validation/gen) and XmlCommentGenerator (src/OpenApi/gen).

Changes

  • RequestDelegateGenerator.cs — Now contains only the Initialize method (pipeline setup)
  • RequestDelegateGenerator.Parser.cs (new) — Parsing logic:
    • IsEndpointInvocation — Predicate for identifying Map* invocations
    • TransformEndpoint — Creates endpoint models from syntax contexts
  • RequestDelegateGenerator.Emitter.cs (new) — Emission logic:
    • EmitInterceptorDefinition — Generates interceptor method code
    • EmitHttpVerbs, EmitEndpointHelpers, EmitHelperTypes — Collect and emit supporting code
    • Emit — Final source output

Structure

Before:
  RequestDelegateGenerator.cs (300+ lines, all inline)

After:
  RequestDelegateGenerator.cs        (~60 lines, pipeline only)
  RequestDelegateGenerator.Parser.cs (~30 lines)
  RequestDelegateGenerator.Emitter.cs (~260 lines)

No functional changes — existing tests pass unchanged.

Original prompt

Rewrite the RequestDelegateGenerator implementation to mimic the structure of the ValidationsGenerator in src/Validation/gen and the OpenAPI generator in src/OpenApi/gen.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…alidationsGenerator and OpenApiGenerator

Co-authored-by: captainsafia <1857993+captainsafia@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor RequestDelegateGenerator structure to match ValidationsGenerator Refactor RequestDelegateGenerator to use partial class pattern Dec 12, 2025
Copilot AI requested a review from captainsafia December 12, 2025 18:23
@captainsafia captainsafia marked this pull request as ready for review December 15, 2025 19:06
Copilot AI review requested due to automatic review settings December 15, 2025 19:06
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

This PR restructures the RequestDelegateGenerator to follow the partial class pattern used by other generators in the repository (ValidationsGenerator and OpenApiGenerator). The generator is split into three focused files: the main file containing the pipeline setup (Initialize), a parser file with syntax predicate and transformation logic, and an emitter file with all code generation methods.

Key changes:

  • Separated parsing and emission logic into dedicated partial class files
  • Converted inline lambdas to named methods for better organization
  • Maintained all existing functionality without behavioral changes

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
RequestDelegateGenerator.cs Simplified to contain only pipeline setup; extracted parsing and emission logic
RequestDelegateGenerator.Parser.cs New file with predicate and transform methods for endpoint detection
RequestDelegateGenerator.Emitter.cs New file with all code generation methods previously inline in Initialize
Comments suppressed due to low confidence (1)

src/Http/Http.Extensions/gen/Microsoft.AspNetCore.Http.RequestDelegateGenerator/RequestDelegateGenerator.cs:1

  • The logical condition differs from the original code. The original used || (OR), but this uses && (AND). This changes the behavior: the original wrote pattern, when the method was NOT MapFallback OR when there were NOT exactly 2 arguments. The new code writes pattern, only when BOTH conditions are true. This should be || to match the original logic.
// Licensed to the .NET Foundation under one or more agreements.

}
var code = RequestDelegateGeneratorSources.GetGeneratedRouteBuilderExtensionsSource(
endpoints: stringWriter.ToString(),
helperMethods: helperMethods ?? string.Empty,
Copy link

Copilot AI Dec 15, 2025

Choose a reason for hiding this comment

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

The null-coalescing operator is redundant here since the calling code already ensures helperMethods is never null (see line 59 where it's passed as helperMethods ?? string.Empty). Consider removing this defensive check or the one at the call site.

Suggested change
helperMethods: helperMethods ?? string.Empty,
helperMethods: helperMethods,

Copilot uses AI. Check for mistakes.
@captainsafia captainsafia merged commit fab9e0e into main Dec 16, 2025
25 checks passed
@captainsafia captainsafia deleted the copilot/refactor-request-delegate-generator branch December 16, 2025 00:06
@dotnet-policy-service dotnet-policy-service bot added this to the 11.0-preview1 milestone Dec 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants