Skip to content

Commit 81f5487

Browse files
committed
Implement ICloneable semantically for deep cloning
1 parent c61e1cb commit 81f5487

28 files changed

+347
-375
lines changed

src/Microsoft.OpenApi/Models/OpenApiCallback.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,19 @@ public class OpenApiCallback : IOpenApiSerializable, IOpenApiReferenceable, IOpe
3434
/// This object MAY be extended with Specification Extensions.
3535
/// </summary>
3636
public IDictionary<string, IOpenApiExtension> Extensions { get; set; } = new Dictionary<string, IOpenApiExtension>();
37-
38-
/// <summary>
39-
/// Parameter-less constructor
40-
/// </summary>
41-
public OpenApiCallback() { }
42-
37+
4338
/// <summary>
4439
/// Initializes a copy of an <see cref="OpenApiCallback"/> object
4540
/// </summary>
46-
public OpenApiCallback(OpenApiCallback callback)
41+
public object Clone()
4742
{
48-
PathItems = callback.PathItems;
49-
UnresolvedReference = callback.UnresolvedReference;
50-
Reference = callback.Reference;
51-
Extensions = callback.Extensions;
43+
return new OpenApiCallback
44+
{
45+
PathItems = PathItems,
46+
UnresolvedReference = UnresolvedReference,
47+
Reference = Reference,
48+
Extensions = Extensions
49+
};
5250
}
5351

5452
/// <summary>

src/Microsoft.OpenApi/Models/OpenApiComponents.cs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,26 +68,24 @@ public class OpenApiComponents : IOpenApiSerializable, IOpenApiExtensible
6868
/// </summary>
6969
public IDictionary<string, IOpenApiExtension> Extensions { get; set; } = new Dictionary<string, IOpenApiExtension>();
7070

71-
/// <summary>
72-
/// Parameter-less constructor
73-
/// </summary>
74-
public OpenApiComponents() { }
75-
7671
/// <summary>
7772
/// Initializes a copy of an <see cref="OpenApiComponents"/> object
7873
/// </summary>
79-
public OpenApiComponents(OpenApiComponents components)
74+
public object Clone()
8075
{
81-
Schemas = components.Schemas;
82-
Responses = components.Responses;
83-
Parameters = components.Parameters;
84-
Examples = components.Examples;
85-
RequestBodies = components.RequestBodies;
86-
Headers = components.Headers;
87-
SecuritySchemes = components.SecuritySchemes;
88-
Links = components.Links;
89-
Callbacks = components.Callbacks;
90-
Extensions = components.Extensions;
76+
return new OpenApiComponents
77+
{
78+
Schemas = Schemas,
79+
Responses = Responses,
80+
Parameters = Parameters,
81+
Examples = Examples,
82+
RequestBodies = RequestBodies,
83+
Headers = Headers,
84+
SecuritySchemes = SecuritySchemes,
85+
Links = Links,
86+
Callbacks = Callbacks,
87+
Extensions = Extensions
88+
};
9189
}
9290

9391
/// <summary>

src/Microsoft.OpenApi/Models/OpenApiContact.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,20 @@ public class OpenApiContact : IOpenApiSerializable, IOpenApiExtensible
3535
/// </summary>
3636
public IDictionary<string, IOpenApiExtension> Extensions { get; set; } = new Dictionary<string, IOpenApiExtension>();
3737

38-
/// <summary>
39-
/// Parameter-less constructor
40-
/// </summary>
41-
public OpenApiContact() { }
42-
4338
/// <summary>
4439
/// Initializes a copy of an <see cref="OpenApiContact"/> instance
4540
/// </summary>
46-
public OpenApiContact(OpenApiContact contact)
41+
public object Clone()
4742
{
48-
Name = contact.Name;
49-
Url = contact.Url;
50-
Email = contact.Email;
51-
Extensions = contact.Extensions;
43+
return new OpenApiContact
44+
{
45+
Name = Name.Clone() as string,
46+
Url = Url,
47+
Email = Email.Clone() as string,
48+
Extensions = Extensions
49+
};
5250
}
53-
51+
5452
/// <summary>
5553
/// Serialize <see cref="OpenApiContact"/> to Open Api v3.0
5654
/// </summary>

src/Microsoft.OpenApi/Models/OpenApiDiscriminator.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,16 @@ public class OpenApiDiscriminator : IOpenApiSerializable
2222
/// </summary>
2323
public IDictionary<string, string> Mapping { get; set; } = new Dictionary<string, string>();
2424

25-
/// <summary>
26-
/// Parameter-less constructor
27-
/// </summary>
28-
public OpenApiDiscriminator() { }
29-
3025
/// <summary>
3126
/// Initializes a copy of an <see cref="OpenApiDiscriminator"/> instance
3227
/// </summary>
33-
public OpenApiDiscriminator(OpenApiDiscriminator discriminator)
28+
public object Clone()
3429
{
35-
PropertyName = discriminator.PropertyName;
36-
Mapping = discriminator.Mapping;
30+
return new OpenApiDiscriminator
31+
{
32+
PropertyName = PropertyName.Clone() as string,
33+
Mapping = Mapping
34+
};
3735
}
3836

3937
/// <summary>

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,19 @@ public OpenApiDocument() {}
7070
/// <summary>
7171
/// Initializes a copy of an an <see cref="OpenApiDocument"/> object
7272
/// </summary>
73-
public OpenApiDocument(OpenApiDocument document)
73+
public object Clone()
7474
{
75-
Workspace = document.Workspace;
76-
Info = document.Info;
77-
Servers = document.Servers;
78-
Paths = document.Paths;
79-
Components = document.Components;
80-
SecurityRequirements = document.SecurityRequirements;
81-
Tags = document.Tags;
82-
ExternalDocs = document.ExternalDocs;
83-
Extensions = document.Extensions;
75+
return new OpenApiDocument
76+
{
77+
Workspace = Workspace,
78+
Info = Info,
79+
Servers = Servers,
80+
Components = Components,
81+
SecurityRequirements = SecurityRequirements,
82+
Tags = Tags,
83+
ExternalDocs = ExternalDocs,
84+
Extensions = Extensions
85+
};
8486
}
8587

8688
/// <summary>

src/Microsoft.OpenApi/Models/OpenApiEncoding.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,20 @@ public class OpenApiEncoding : IOpenApiSerializable, IOpenApiExtensible
5353
/// </summary>
5454
public IDictionary<string, IOpenApiExtension> Extensions { get; set; } = new Dictionary<string, IOpenApiExtension>();
5555

56-
/// <summary>
57-
/// Parameter-less constructor
58-
/// </summary>
59-
public OpenApiEncoding() {}
60-
6156
/// <summary>
6257
/// Initializes a copy of an <see cref="OpenApiEncoding"/> object
6358
/// </summary>
64-
public OpenApiEncoding(OpenApiEncoding encoding)
59+
public object Clone()
6560
{
66-
ContentType = encoding.ContentType;
67-
Headers = encoding.Headers;
68-
Style = encoding.Style;
69-
Explode = encoding.Explode;
70-
AllowReserved = encoding.AllowReserved;
71-
Extensions = encoding.Extensions;
61+
return new OpenApiEncoding
62+
{
63+
ContentType = ContentType.Clone() as string,
64+
Headers = Headers,
65+
Style = Style,
66+
Explode = Explode,
67+
AllowReserved = AllowReserved,
68+
Extensions = Extensions
69+
};
7270
}
7371

7472
/// <summary>

src/Microsoft.OpenApi/Models/OpenApiExample.cs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,21 @@ public class OpenApiExample : IOpenApiSerializable, IOpenApiReferenceable, IOpen
5454
/// </summary>
5555
public bool UnresolvedReference { get; set; } = false;
5656

57-
/// <summary>
58-
/// Parameter-less constructor
59-
/// </summary>
60-
public OpenApiExample() {}
61-
6257
/// <summary>
6358
/// Initializes a copy of <see cref="OpenApiExample"/> object
6459
/// </summary>
65-
public OpenApiExample(OpenApiExample example)
60+
public object Clone()
6661
{
67-
Summary = example.Summary;
68-
Description = example.Description;
69-
Value = example.Value;
70-
ExternalValue = example.ExternalValue;
71-
Extensions = example.Extensions;
72-
Reference = example.Reference;
73-
UnresolvedReference = example.UnresolvedReference;
62+
return new OpenApiExample
63+
{
64+
Summary = Summary,
65+
Description = Description,
66+
Value = Value,
67+
ExternalValue = ExternalValue,
68+
Extensions = Extensions,
69+
Reference = Reference,
70+
UnresolvedReference = UnresolvedReference
71+
};
7472
}
7573

7674
/// <summary>

src/Microsoft.OpenApi/Models/OpenApiExternalDocs.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,17 @@ public class OpenApiExternalDocs : IOpenApiSerializable, IOpenApiExtensible
2929
/// </summary>
3030
public IDictionary<string, IOpenApiExtension> Extensions { get; set; } = new Dictionary<string, IOpenApiExtension>();
3131

32-
/// <summary>
33-
/// Parameter-less constructor
34-
/// </summary>
35-
public OpenApiExternalDocs() {}
36-
3732
/// <summary>
3833
/// Initializes a copy of an <see cref="OpenApiExternalDocs"/> object
3934
/// </summary>
40-
public OpenApiExternalDocs(OpenApiExternalDocs externalDocs)
35+
public Object Clone()
4136
{
42-
Description = externalDocs.Description;
43-
Url = externalDocs.Url;
44-
Extensions = externalDocs.Extensions;
37+
return new OpenApiExternalDocs
38+
{
39+
Description = Description.Clone() as string,
40+
Url = Url,
41+
Extensions = Extensions
42+
};
4543
}
4644

4745
/// <summary>

src/Microsoft.OpenApi/Models/OpenApiHeader.cs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4+
using System;
45
using System.Collections.Generic;
56
using Microsoft.OpenApi.Any;
67
using Microsoft.OpenApi.Extensions;
@@ -86,30 +87,28 @@ public class OpenApiHeader : IOpenApiSerializable, IOpenApiReferenceable, IOpenA
8687
/// </summary>
8788
public IDictionary<string, IOpenApiExtension> Extensions { get; set; } = new Dictionary<string, IOpenApiExtension>();
8889

89-
/// <summary>
90-
/// Parameter-less constructor
91-
/// </summary>
92-
public OpenApiHeader() {}
93-
9490
/// <summary>
9591
/// Initializes a copy of an <see cref="OpenApiHeader"/> object
9692
/// </summary>
97-
public OpenApiHeader(OpenApiHeader header)
93+
public Object Clone()
9894
{
99-
UnresolvedReference = header.UnresolvedReference;
100-
Reference = header.Reference;
101-
Description = header.Description;
102-
Required = header.Required;
103-
Deprecated = header.Deprecated;
104-
AllowEmptyValue = header.AllowEmptyValue;
105-
Style = header.Style;
106-
Explode = header.Explode;
107-
AllowReserved = header.AllowReserved;
108-
Schema = header.Schema;
109-
Example = header.Example;
110-
Examples = header.Examples;
111-
Content = header.Content;
112-
Extensions = header.Extensions;
95+
return new OpenApiHeader
96+
{
97+
UnresolvedReference = UnresolvedReference,
98+
Reference = Reference,
99+
Description = Description.Clone() as string,
100+
Required = Required,
101+
Deprecated = Deprecated,
102+
AllowEmptyValue = AllowEmptyValue,
103+
Style = Style,
104+
Explode = Explode,
105+
AllowReserved = AllowReserved,
106+
Schema = Schema,
107+
Example = Example,
108+
Examples = Examples,
109+
Content = Content,
110+
Extensions = Extensions
111+
};
113112
}
114113

115114
/// <summary>

src/Microsoft.OpenApi/Models/OpenApiInfo.cs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,21 @@ public class OpenApiInfo : IOpenApiSerializable, IOpenApiExtensible
4949
/// </summary>
5050
public IDictionary<string, IOpenApiExtension> Extensions { get; set; } = new Dictionary<string, IOpenApiExtension>();
5151

52-
/// <summary>
53-
/// Parameter-less constructor
54-
/// </summary>
55-
public OpenApiInfo() {}
56-
5752
/// <summary>
5853
/// Initializes a copy of an <see cref="OpenApiInfo"/> object
5954
/// </summary>
60-
public OpenApiInfo(OpenApiInfo info)
55+
public Object Clone()
6156
{
62-
Title = info.Title;
63-
Description = info.Description;
64-
Version = info.Version;
65-
TermsOfService = info.TermsOfService;
66-
Contact = info.Contact;
67-
License = info.License;
68-
Extensions = info.Extensions;
57+
return new OpenApiInfo
58+
{
59+
Title = Title.Clone() as string,
60+
Description = Description.Clone() as string,
61+
Version = Version.Clone() as string,
62+
TermsOfService = TermsOfService,
63+
Contact = Contact,
64+
License = License,
65+
Extensions = Extensions
66+
};
6967
}
7068

7169
/// <summary>

0 commit comments

Comments
 (0)