Skip to content

Improve OpenApiOperation ActualResponses and Responses performance #5148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 21, 2025

Conversation

lahma
Copy link
Collaborator

@lahma lahma commented Apr 20, 2025

  • use internal helpers and avoid allocating dictionaries

…ance

* use internal helpers and avoid allocating dictionaries
@lahma lahma requested a review from Copilot April 20, 2025 10:59
Copy link

@Copilot 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 improves the performance of actual OpenAPI operation response handling by introducing internal helper methods to avoid extra allocations when processing responses. Key changes include:

  • Initializing a private readonly _responses field and refactoring the Responses property in OpenApiOperation.
  • Replacing direct use of ActualResponses with new helper methods (GetActualResponses, HasActualResponse, GetActualResponse) across multiple files.
  • Updating dependent code in OpenApiDocument, TypeScript, and CSharp models to leverage the new API.

Reviewed Changes

Copilot reviewed 8 out of 10 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/NSwag.Core/OpenApiOperation.cs Refactored Responses initialization and introduced helper methods for performance improvement.
src/NSwag.Core/OpenApiDocument.cs Updated usage to call HasActualResponse with static lambdas for better performance.
src/NSwag.CodeGeneration/Models/OperationModelBase.cs Changed to use GetActualResponses for response model creation.
src/NSwag.CodeGeneration.TypeScript/Models/TypeScriptOperationModel.cs Replaced ActualResponses.Any with HasActualResponse for cleaner syntax.
src/NSwag.CodeGeneration.CSharp/Models/CSharpOperationModel.cs Updated exception type determination to use GetActualResponse.
src/NSwag.CodeGeneration.CSharp/Models/CSharpFileTemplateModel.cs Modified collection and file response detections to use HasActualResponse.
src/NSwag.CodeGeneration.CSharp/Models/CSharpClientTemplateModel.cs Refactored JSON converter check to use the new helper method.
Files not reviewed (2)
  • src/NSwag.CodeGeneration.CSharp/NSwag.CodeGeneration.CSharp.csproj: Language not supported
  • src/NSwag.Core/NSwag.Core.csproj: Language not supported
Comments suppressed due to low confidence (1)

src/NSwag.Core/OpenApiOperation.cs:191

  • [nitpick] Consider adding dedicated unit tests for the new helper methods (GetActualResponses, HasActualResponse, and GetActualResponse) to ensure they work as expected in edge cases.
        // helper to avoid extra allocations

@lahma lahma changed the title Improve actual OpenApiOperation ActualResponses and Responses performance Improve OpenApiOperation ActualResponses and Responses performance Apr 21, 2025
@lahma lahma force-pushed the improve-actual-response-reading branch from 41e7751 to 404673d Compare April 21, 2025 07:21
@lahma lahma merged commit 1ffa330 into RicoSuter:master Apr 21, 2025
3 checks passed
@lahma lahma deleted the improve-actual-response-reading branch April 21, 2025 07:37
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.

1 participant