From 671f8fdb8dbcd97b01d6cdbd1c0f4ca02c227ef2 Mon Sep 17 00:00:00 2001 From: Daniel Jonathan Date: Wed, 19 Nov 2025 15:43:34 -0600 Subject: [PATCH 1/2] perf(dotnet): optimize dictionary lookups in model Equals methods Address CodeQL warning about inefficient ContainsKey usage by replacing ContainsKey + indexer pattern with TryGetValue. Changes: - Update modelGeneric.mustache template line 357 - Replace two dictionary lookups with single TryGetValue call - Applies to all generated models with AdditionalProperties Before (inefficient - 2 lookups): input.AdditionalProperties.ContainsKey(kv.Key) && Equals(kv.Value, input.AdditionalProperties[kv.Key]) After (efficient - 1 lookup): input.AdditionalProperties.TryGetValue(kv.Key, out var inputValue) && Equals(kv.Value, inputValue) This resolves the CodeQL warning from dotnet-sdk PR #156 and improves performance for all model equality comparisons across the SDK. --- config/clients/dotnet/template/modelGeneric.mustache | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/clients/dotnet/template/modelGeneric.mustache b/config/clients/dotnet/template/modelGeneric.mustache index 078ae3ed7..6a2fddaa5 100644 --- a/config/clients/dotnet/template/modelGeneric.mustache +++ b/config/clients/dotnet/template/modelGeneric.mustache @@ -354,7 +354,7 @@ {{/vendorExtensions.x-is-value-type}}this.{{name}}.SequenceEqual(input.{{name}}) ){{^-last}} && {{/-last}}{{/isContainer}}{{/vars}}{{^vars}}{{#parent}}base.Equals(input){{/parent}}{{^parent}}false{{/parent}}{{/vars}}{{^isAdditionalPropertiesTrue}};{{/isAdditionalPropertiesTrue}} {{#isAdditionalPropertiesTrue}} - && (this.AdditionalProperties.Count == input.AdditionalProperties.Count && this.AdditionalProperties.All(kv => input.AdditionalProperties.ContainsKey(kv.Key) && Equals(kv.Value, input.AdditionalProperties[kv.Key]))); + && (this.AdditionalProperties.Count == input.AdditionalProperties.Count && this.AdditionalProperties.All(kv => input.AdditionalProperties.TryGetValue(kv.Key, out var inputValue) && Equals(kv.Value, inputValue))); {{/isAdditionalPropertiesTrue}} {{/useCompareNetObjects}} } From 2b6bddae435bddb3082307de1a9a4b73771f7a0b Mon Sep 17 00:00:00 2001 From: Daniel Jonathan Date: Wed, 19 Nov 2025 16:08:45 -0600 Subject: [PATCH 2/2] perf(dotnet): optimize model Equals methods for CodeQL compliance Address two CodeQL warnings in generated model Equals methods: 1. Inefficient ContainsKey usage - Replace ContainsKey + indexer with TryGetValue - Reduces dictionary lookups from 2 to 1 per key 2. Equals should not apply "as" cast - Add GetType() check before casting - Ensures proper equality for subclasses Changes: - Line 320-321: Fix Equals(object) to use GetType() check - Line 357: Optimize AdditionalProperties comparison with TryGetValue These fixes apply to all generated models with AdditionalProperties. Resolves CodeQL warnings from dotnet-sdk PR #156. --- config/clients/dotnet/template/modelGeneric.mustache | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/config/clients/dotnet/template/modelGeneric.mustache b/config/clients/dotnet/template/modelGeneric.mustache index 6a2fddaa5..2901cc4d5 100644 --- a/config/clients/dotnet/template/modelGeneric.mustache +++ b/config/clients/dotnet/template/modelGeneric.mustache @@ -317,7 +317,8 @@ return OpenAPIClientUtils.compareLogic.Compare(this, input as {{classname}}).AreEqual; {{/useCompareNetObjects}} {{^useCompareNetObjects}} - return this.Equals(input as {{classname}}); + if (input == null || input.GetType() != this.GetType()) return false; + return this.Equals(({{classname}})input); {{/useCompareNetObjects}} }