Skip to content

Commit d940bf6

Browse files
committed
Revert dictionary clone helper logic in the copy constructors to unblock kiota
1 parent 2acc209 commit d940bf6

17 files changed

+59
-53
lines changed

src/Microsoft.OpenApi/Any/OpenApiAnyCloneHelper.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// Licensed under the MIT license.
33

44
using System.Reflection;
5+
using Microsoft.OpenApi.Helpers;
6+
using System.Text.Json.Serialization;
7+
using System.Text.Json;
58

69
namespace Microsoft.OpenApi.Any
710
{
@@ -27,7 +30,7 @@ public static IOpenApiAny CloneFromCopyConstructor(IOpenApiAny obj)
2730
{
2831
return (IOpenApiAny)ci.Invoke(new object[] { obj });
2932
}
30-
}
33+
}
3134
}
3235

3336
return obj;

src/Microsoft.OpenApi/Helpers/DictionaryCloneHelper.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ internal static class DictionaryCloneHelper
1919
/// <param name="dictionary">The target dictionary to clone.</param>
2020
/// <returns>The cloned dictionary.</returns>
2121
internal static Dictionary<T, U> Clone<T, U>(IDictionary<T, U> dictionary)
22-
{
22+
{
2323
if (dictionary is null) return null;
24-
24+
2525
var clonedDictionary = new Dictionary<T, U>(dictionary.Keys.Count);
2626
var clonedObjects = new Dictionary<object, object>();
27-
27+
2828
foreach (var keyValuePair in dictionary)
2929
{
3030
// If the object has already been cloned, use the cloned object instead of cloning it again
@@ -36,11 +36,11 @@ internal static Dictionary<T, U> Clone<T, U>(IDictionary<T, U> dictionary)
3636
{
3737
// Create instance of the specified type using the constructor matching the specified parameter types.
3838
clonedDictionary[keyValuePair.Key] = (U)Activator.CreateInstance(keyValuePair.Value.GetType(), keyValuePair.Value);
39-
39+
4040
// Add the cloned object to the dictionary of cloned objects
4141
clonedObjects.Add(keyValuePair.Value, clonedDictionary[keyValuePair.Key]);
42-
}
43-
}
42+
}
43+
}
4444

4545
return clonedDictionary;
4646
}

src/Microsoft.OpenApi/Microsoft.OpenApi.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
<PropertyGroup Condition="'$(TF_BUILD)' == 'true'">
3434
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
3535
</PropertyGroup>
36+
<ItemGroup>
37+
<PackageReference Include="System.Text.Json" Version="7.0.0" />
38+
</ItemGroup>
3639

3740
<ItemGroup>
3841
<Compile Update="Properties\SRResource.Designer.cs">

src/Microsoft.OpenApi/Models/OpenApiComponents.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,15 @@ public OpenApiComponents() { }
7777
/// </summary>
7878
public OpenApiComponents(OpenApiComponents components)
7979
{
80-
Schemas = DictionaryCloneHelper.Clone(components?.Schemas);
81-
Responses = DictionaryCloneHelper.Clone(components?.Responses);
82-
Parameters = DictionaryCloneHelper.Clone(components?.Parameters);
83-
Examples = DictionaryCloneHelper.Clone(components?.Examples);
84-
RequestBodies = DictionaryCloneHelper.Clone(components?.RequestBodies);
85-
Headers = DictionaryCloneHelper.Clone(components?.Headers);
86-
SecuritySchemes = DictionaryCloneHelper.Clone(components?.SecuritySchemes);
87-
Links = DictionaryCloneHelper.Clone(components?.Links);
88-
Callbacks = DictionaryCloneHelper.Clone(components?.Callbacks);
80+
Schemas = components?.Schemas != null ? new Dictionary<string, OpenApiSchema>(components.Schemas) : null;
81+
Responses = components?.Responses != null ? new Dictionary<string, OpenApiResponse>(components.Responses) : null;
82+
Parameters = components?.Parameters != null ? new Dictionary<string, OpenApiParameter>(components.Parameters) : null;
83+
Examples = components?.Examples != null ? new Dictionary<string, OpenApiExample>(components.Examples) : null;
84+
RequestBodies = components?.RequestBodies != null ? new Dictionary<string, OpenApiRequestBody>(components.RequestBodies) : null;
85+
Headers = components?.Headers != null ? new Dictionary<string, OpenApiHeader>(components.Headers) : null;
86+
SecuritySchemes = components?.SecuritySchemes != null ? new Dictionary<string, OpenApiSecurityScheme>(components.SecuritySchemes) : null;
87+
Links = components?.Links != null ? new Dictionary<string, OpenApiLink>(components.Links) : null;
88+
Callbacks = components?.Callbacks != null ? new Dictionary<string, OpenApiCallback>(components.Callbacks) : null;
8989
Extensions = components?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(components.Extensions) : null;
9090
}
9191

src/Microsoft.OpenApi/Models/OpenApiEncoding.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public OpenApiEncoding() {}
6565
public OpenApiEncoding(OpenApiEncoding encoding)
6666
{
6767
ContentType = encoding?.ContentType ?? ContentType;
68-
Headers = DictionaryCloneHelper.Clone(encoding?.Headers);
68+
Headers = encoding?.Headers != null ? new Dictionary<string, OpenApiHeader>(encoding.Headers) : null;
6969
Style = encoding?.Style ?? Style;
7070
Explode = encoding?.Explode ?? Explode;
7171
AllowReserved = encoding?.AllowReserved ?? AllowReserved;

src/Microsoft.OpenApi/Models/OpenApiHeader.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ public OpenApiHeader(OpenApiHeader header)
108108
AllowReserved = header?.AllowReserved ?? AllowReserved;
109109
Schema = header?.Schema != null ? new(header?.Schema) : null;
110110
Example = OpenApiAnyCloneHelper.CloneFromCopyConstructor(header?.Example);
111-
Examples = DictionaryCloneHelper.Clone(header?.Examples);
112-
Content = DictionaryCloneHelper.Clone(header?.Content);
111+
Examples = header?.Examples != null ? new Dictionary<string, OpenApiExample>(header.Examples) : null;
112+
Content = header?.Content != null ? new Dictionary<string, OpenApiMediaType>(header.Content) : null;
113113
Extensions = header?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(header.Extensions) : null;
114114
}
115115

src/Microsoft.OpenApi/Models/OpenApiMediaType.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ public OpenApiMediaType(OpenApiMediaType mediaType)
5656
{
5757
Schema = mediaType?.Schema != null ? new(mediaType?.Schema) : null;
5858
Example = OpenApiAnyCloneHelper.CloneFromCopyConstructor(mediaType?.Example);
59-
Examples = DictionaryCloneHelper.Clone(mediaType?.Examples);
60-
Encoding = DictionaryCloneHelper.Clone(mediaType?.Encoding);
59+
Examples = mediaType?.Examples != null ? new Dictionary<string, OpenApiExample>(mediaType.Examples) : null;
60+
Encoding = mediaType?.Encoding != null ? new Dictionary<string, OpenApiEncoding>(mediaType.Encoding) : null;
6161
Extensions = mediaType?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(mediaType.Extensions) : null;
6262
}
6363

src/Microsoft.OpenApi/Models/OpenApiOperation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public OpenApiOperation(OpenApiOperation operation)
125125
Parameters = operation?.Parameters != null ? new List<OpenApiParameter>(operation.Parameters) : null;
126126
RequestBody = operation?.RequestBody != null ? new(operation?.RequestBody) : null;
127127
Responses = operation?.Responses != null ? new(operation?.Responses) : null;
128-
Callbacks = DictionaryCloneHelper.Clone(operation?.Callbacks);
128+
Callbacks = operation?.Callbacks != null ? new Dictionary<string, OpenApiCallback>(operation.Callbacks) : null;
129129
Deprecated = operation?.Deprecated ?? Deprecated;
130130
Security = operation?.Security != null ? new List<OpenApiSecurityRequirement>(operation.Security) : null;
131131
Servers = operation?.Servers != null ? new List<OpenApiServer>(operation.Servers) : null;

src/Microsoft.OpenApi/Models/OpenApiParameter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,9 @@ public OpenApiParameter(OpenApiParameter parameter)
163163
Explode = parameter?.Explode ?? Explode;
164164
AllowReserved = parameter?.AllowReserved ?? AllowReserved;
165165
Schema = parameter?.Schema != null ? new(parameter?.Schema) : null;
166-
Examples = DictionaryCloneHelper.Clone(parameter?.Examples);
166+
Examples = parameter?.Examples != null ? new Dictionary<string, OpenApiExample>(parameter.Examples) : null;
167167
Example = OpenApiAnyCloneHelper.CloneFromCopyConstructor(parameter?.Example);
168-
Content = DictionaryCloneHelper.Clone(parameter?.Content);
168+
Content = parameter?.Content != null ? new Dictionary<string, OpenApiMediaType>(parameter.Content) : null;
169169
Extensions = parameter?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(parameter.Extensions) : null;
170170
AllowEmptyValue = parameter?.AllowEmptyValue ?? AllowEmptyValue;
171171
Deprecated = parameter?.Deprecated ?? Deprecated;

src/Microsoft.OpenApi/Models/OpenApiPathItem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public OpenApiPathItem(OpenApiPathItem pathItem)
7979
{
8080
Summary = pathItem?.Summary ?? Summary;
8181
Description = pathItem?.Description ?? Description;
82-
Operations = DictionaryCloneHelper.Clone(pathItem?.Operations);
82+
Operations = pathItem?.Operations != null ? new Dictionary<OperationType, OpenApiOperation>(pathItem.Operations) : null;
8383
Servers = pathItem?.Servers != null ? new List<OpenApiServer>(pathItem.Servers) : null;
8484
Parameters = pathItem?.Parameters != null ? new List<OpenApiParameter>(pathItem.Parameters) : null;
8585
Extensions = pathItem?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(pathItem.Extensions) : null;

0 commit comments

Comments
 (0)