Skip to content

Commit 7cd6abc

Browse files
committed
Revert ICloneable changes and use reflection for deep copying
1 parent 12fa16b commit 7cd6abc

File tree

13 files changed

+56
-83
lines changed

13 files changed

+56
-83
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System.Reflection;
5+
6+
namespace Microsoft.OpenApi.Any
7+
{
8+
/// <summary>
9+
/// Contains logic for cloning objects through copy constructors.
10+
/// </summary>
11+
public class CloneHelper
12+
{
13+
/// <summary>
14+
/// Clones an instance of <see cref="IOpenApiAny"/> object from the copy constructor
15+
/// </summary>
16+
/// <param name="obj">The object instance.</param>
17+
/// <returns>A clone copy or the object itself.</returns>
18+
public static IOpenApiAny CloneFromCopyConstructor(IOpenApiAny obj)
19+
{
20+
if (obj != null)
21+
{
22+
var t = obj.GetType();
23+
foreach (ConstructorInfo ci in t.GetConstructors())
24+
{
25+
ParameterInfo[] pi = ci.GetParameters();
26+
if (pi.Length == 1 && pi[0].ParameterType == t)
27+
{
28+
return (IOpenApiAny)ci.Invoke(new object[] { obj });
29+
}
30+
}
31+
}
32+
33+
return obj;
34+
}
35+
}
36+
}

src/Microsoft.OpenApi/Any/IOpenApiAny.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Microsoft.OpenApi.Any
99
/// <summary>
1010
/// Base interface for all the types that represent Open API Any.
1111
/// </summary>
12-
public interface IOpenApiAny : IOpenApiElement, IOpenApiExtension, ICloneable
12+
public interface IOpenApiAny : IOpenApiElement, IOpenApiExtension
1313
{
1414
/// <summary>
1515
/// Type of an <see cref="IOpenApiAny"/>.

src/Microsoft.OpenApi/Any/OpenApiArray.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,13 @@ namespace Microsoft.OpenApi.Any
1010
/// <summary>
1111
/// Open API array.
1212
/// </summary>
13-
public class OpenApiArray : List<IOpenApiAny>, IOpenApiAny, ICloneable
13+
public class OpenApiArray : List<IOpenApiAny>, IOpenApiAny
1414
{
1515
/// <summary>
1616
/// The type of <see cref="IOpenApiAny"/>
1717
/// </summary>
1818
public AnyType AnyType { get; } = AnyType.Array;
1919

20-
/// <summary>
21-
/// Implement ICloneable interface to allow for deep copying
22-
/// </summary>
23-
/// <returns>A new copy of <see cref="OpenApiArray"/></returns>
24-
public object Clone()
25-
{
26-
return new OpenApiArray();
27-
}
28-
2920
/// <summary>
3021
/// Write out contents of OpenApiArray to passed writer
3122
/// </summary>

src/Microsoft.OpenApi/Any/OpenApiNull.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,6 @@ public class OpenApiNull : IOpenApiAny
1515
/// </summary>
1616
public AnyType AnyType { get; } = AnyType.Null;
1717

18-
19-
/// <summary>
20-
/// Implement ICloneable interface to allow for deep copying
21-
/// </summary>
22-
/// <returns>A new copy of <see cref="OpenApiNull"/></returns>
23-
public object Clone()
24-
{
25-
return new OpenApiNull();
26-
}
27-
2818
/// <summary>
2919
/// Write out null representation
3020
/// </summary>

src/Microsoft.OpenApi/Any/OpenApiObject.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,6 @@ public class OpenApiObject : Dictionary<string, IOpenApiAny>, IOpenApiAny
1616
/// </summary>
1717
public AnyType AnyType { get; } = AnyType.Object;
1818

19-
/// <summary>
20-
/// Implement ICloneable interface to allow for deep copying
21-
/// </summary>
22-
/// <returns>A new copy of <see cref="OpenApiObject"/></returns>
23-
public object Clone()
24-
{
25-
return new OpenApiObject();
26-
}
27-
2819
/// <summary>
2920
/// Serialize OpenApiObject to writer
3021
/// </summary>

src/Microsoft.OpenApi/Any/OpenApiPrimitive.cs

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public OpenApiPrimitive(OpenApiPrimitive<T> openApiPrimitive)
3939
/// </summary>
4040
public AnyType AnyType { get; } = AnyType.Primitive;
4141

42-
4342
/// <summary>
4443
/// The primitive class this object represents.
4544
/// </summary>
@@ -50,40 +49,6 @@ public OpenApiPrimitive(OpenApiPrimitive<T> openApiPrimitive)
5049
/// </summary>
5150
public T Value { get; }
5251

53-
/// <summary>
54-
/// Implement ICloneable interface to allow for deep copying
55-
/// </summary>
56-
/// <returns>A new copy of <see cref="IOpenApiPrimitive"/></returns>
57-
public object Clone()
58-
{
59-
var clone = CloneFromCopyConstructor(this);
60-
if (clone == null) throw new ApplicationException("There's no copy constructor defined");
61-
return clone;
62-
}
63-
64-
/// <summary>
65-
/// Clones an instance of <see cref="IOpenApiPrimitive"/> object from the copy constructor
66-
/// </summary>
67-
/// <param name="obj">The object instance.</param>
68-
/// <returns>A clone copy.</returns>
69-
public static object CloneFromCopyConstructor(Object obj)
70-
{
71-
if (obj != null)
72-
{
73-
Type t = obj.GetType();
74-
foreach (ConstructorInfo ci in t.GetConstructors())
75-
{
76-
ParameterInfo[] pi = ci.GetParameters();
77-
if (pi.Length == 1 && pi[0].ParameterType == t)
78-
{
79-
return ci.Invoke(new object[] { obj });
80-
}
81-
}
82-
}
83-
84-
return null;
85-
}
86-
8752
/// <summary>
8853
/// Write out content of primitive element
8954
/// </summary>

src/Microsoft.OpenApi/Models/OpenApiExample.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public OpenApiExample(OpenApiExample example)
6666
{
6767
Summary = example.Summary;
6868
Description = example.Description;
69-
Value = (IOpenApiAny)example.Value.Clone();
69+
Value = CloneHelper.CloneFromCopyConstructor(example.Value);
7070
ExternalValue = example.ExternalValue;
7171
Extensions = new Dictionary<string, IOpenApiExtension>(example.Extensions);
7272
Reference = new(example.Reference);

src/Microsoft.OpenApi/Models/OpenApiHeader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public OpenApiHeader(OpenApiHeader header)
106106
Explode = header.Explode;
107107
AllowReserved = header.AllowReserved;
108108
Schema = new(header.Schema);
109-
Example = (IOpenApiAny)header.Example.Clone();
109+
Example = CloneHelper.CloneFromCopyConstructor(header.Example);
110110
Examples = new Dictionary<string, OpenApiExample>(header.Examples);
111111
Content = new Dictionary<string, OpenApiMediaType>(header.Content);
112112
Extensions = new Dictionary<string, IOpenApiExtension>(header.Extensions);

src/Microsoft.OpenApi/Models/OpenApiMediaType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public OpenApiMediaType() {}
5454
public OpenApiMediaType(OpenApiMediaType mediaType)
5555
{
5656
Schema = new(mediaType.Schema);
57-
Example = (IOpenApiAny)mediaType.Example.Clone();
57+
Example = CloneHelper.CloneFromCopyConstructor(mediaType.Example);
5858
Examples = new Dictionary<string, OpenApiExample>(mediaType.Examples);
5959
Encoding = new Dictionary<string, OpenApiEncoding>(mediaType.Encoding);
6060
Extensions = new Dictionary<string, IOpenApiExtension>(mediaType.Extensions);

src/Microsoft.OpenApi/Models/OpenApiParameter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public OpenApiParameter(OpenApiParameter parameter)
157157
AllowReserved = parameter.AllowReserved;
158158
Schema = new(parameter.Schema);
159159
Examples = new Dictionary<string, OpenApiExample>(parameter.Examples);
160-
Example = (IOpenApiAny)parameter.Example.Clone();
160+
Example = CloneHelper.CloneFromCopyConstructor(parameter.Example);
161161
Content = new Dictionary<string, OpenApiMediaType>(parameter.Content);
162162
Extensions = new Dictionary<string, IOpenApiExtension>(parameter.Extensions);
163163
AllowEmptyValue = parameter.AllowEmptyValue;

0 commit comments

Comments
 (0)