Skip to content

Conversation

@commonsensesoftware
Copy link
Collaborator

Single-quote OData strings

  • 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

Fixes up OData route templates during API exploration for OData strings that are within the parentheses syntax. This typically applies to composite keys and functions.

Comment on lines +538 to +545
foreach ( var parameter in function.Parameters )
{
if ( parameter.Type.IsString() )
{
names ??= [];
names.Add( parameterMappings[parameter.Name] );
}
}

Check notice

Code scanning / CodeQL

Missed opportunity to use Where Note

This foreach loop
implicitly filters its target sequence
- consider filtering the sequence explicitly using '.Where(...)'.

Copilot Autofix

AI about 12 hours ago

In general terms, to fix this, replace the explicit in-loop if-statement filtering (if (condition) { ... }) with filtration of the enumeration using LINQ's Where before entering the loop. Specifically, for the loop at line 538:

foreach ( var parameter in function.Parameters )
{
    if ( parameter.Type.IsString() )
    {
        names ??= [];
        names.Add( parameterMappings[parameter.Name] );
    }
}

should become:

foreach ( var parameter in function.Parameters.Where(p => p.Type.IsString()) )
{
    names ??= [];
    names.Add( parameterMappings[parameter.Name] );
}

This makes it clear that we're only iterating over parameters with Type.IsString(). To implement the change, you'll need to add using System.Linq; if it's not already present. Only code shown may be modified: you may add the import at the top if and only if it's missing in the lines shown.

Suggested changeset 1
src/AspNetCore/OData/src/Asp.Versioning.OData.ApiExplorer/ApiExplorer/ODataApiDescriptionProvider.cs

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/AspNetCore/OData/src/Asp.Versioning.OData.ApiExplorer/ApiExplorer/ODataApiDescriptionProvider.cs b/src/AspNetCore/OData/src/Asp.Versioning.OData.ApiExplorer/ApiExplorer/ODataApiDescriptionProvider.cs
--- a/src/AspNetCore/OData/src/Asp.Versioning.OData.ApiExplorer/ApiExplorer/ODataApiDescriptionProvider.cs
+++ b/src/AspNetCore/OData/src/Asp.Versioning.OData.ApiExplorer/ApiExplorer/ODataApiDescriptionProvider.cs
@@ -17,6 +17,7 @@
 using System.Diagnostics.CodeAnalysis;
 using System.Runtime.CompilerServices;
 using System.Text;
+using System.Linq;
 using static ODataMetadataOptions;
 using static System.StringComparison;
 using Opts = Microsoft.Extensions.Options.Options;
@@ -535,13 +536,10 @@
         IDictionary<string, string> parameterMappings,
         ref HashSet<string>? names )
     {
-        foreach ( var parameter in function.Parameters )
+        foreach ( var parameter in function.Parameters.Where(p => p.Type.IsString()) )
         {
-            if ( parameter.Type.IsString() )
-            {
-                names ??= [];
-                names.Add( parameterMappings[parameter.Name] );
-            }
+            names ??= [];
+            names.Add( parameterMappings[parameter.Name] );
         }
     }
 
EOF
@@ -17,6 +17,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Text;
using System.Linq;
using static ODataMetadataOptions;
using static System.StringComparison;
using Opts = Microsoft.Extensions.Options.Options;
@@ -535,13 +536,10 @@
IDictionary<string, string> parameterMappings,
ref HashSet<string>? names )
{
foreach ( var parameter in function.Parameters )
foreach ( var parameter in function.Parameters.Where(p => p.Type.IsString()) )
{
if ( parameter.Type.IsString() )
{
names ??= [];
names.Add( parameterMappings[parameter.Name] );
}
names ??= [];
names.Add( parameterMappings[parameter.Name] );
}
}

Copilot is powered by AI and may make mistakes. Always verify output.
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.

Quote Strings in OData Route Templates

2 participants