Skip to content

Commit 6a3e948

Browse files
Merge pull request #853 from microsoft/mk/add-copy-constructors-for-models
Adds support for Copy Constructors
2 parents e055dde + 87180dd commit 6a3e948

40 files changed

+730
-11
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 OpenApiAnyCloneHelper
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/OpenApiArray.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license.
33

44
using Microsoft.OpenApi.Writers;
5+
using System;
56
using System.Collections.Generic;
67

78
namespace Microsoft.OpenApi.Any
@@ -16,6 +17,19 @@ public class OpenApiArray : List<IOpenApiAny>, IOpenApiAny
1617
/// </summary>
1718
public AnyType AnyType { get; } = AnyType.Array;
1819

20+
/// <summary>
21+
/// Parameterless constructor
22+
/// </summary>
23+
public OpenApiArray() { }
24+
25+
/// <summary>
26+
/// Initializes a copy of <see cref="OpenApiArray"/> object
27+
/// </summary>
28+
public OpenApiArray(OpenApiArray array)
29+
{
30+
AnyType = array.AnyType;
31+
}
32+
1933
/// <summary>
2034
/// Write out contents of OpenApiArray to passed writer
2135
/// </summary>

src/Microsoft.OpenApi/Any/OpenApiNull.cs

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

18+
/// <summary>
19+
/// Parameterless constructor
20+
/// </summary>
21+
public OpenApiNull() { }
22+
23+
/// <summary>
24+
/// Initializes a copy of <see cref="OpenApiNull"/> object
25+
/// </summary>
26+
public OpenApiNull(OpenApiNull openApiNull)
27+
{
28+
AnyType = openApiNull.AnyType;
29+
}
30+
1831
/// <summary>
1932
/// Write out null representation
2033
/// </summary>

src/Microsoft.OpenApi/Any/OpenApiObject.cs

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

19+
/// <summary>
20+
/// Parameterless constructor
21+
/// </summary>
22+
public OpenApiObject() { }
23+
24+
/// <summary>
25+
/// Initializes a copy of <see cref="OpenApiObject"/> object
26+
/// </summary>
27+
public OpenApiObject(OpenApiObject obj)
28+
{
29+
AnyType = obj.AnyType;
30+
}
31+
1932
/// <summary>
2033
/// Serialize OpenApiObject to writer
2134
/// </summary>

src/Microsoft.OpenApi/Any/OpenApiPrimitive.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ public OpenApiPrimitive(T value)
2424
Value = value;
2525
}
2626

27+
/// <summary>
28+
/// Initializes a copy of an <see cref="IOpenApiPrimitive"/> object
29+
/// </summary>
30+
/// <param name="openApiPrimitive"></param>
31+
public OpenApiPrimitive(OpenApiPrimitive<T> openApiPrimitive)
32+
{
33+
Value = openApiPrimitive.Value;
34+
}
35+
2736
/// <summary>
2837
/// The kind of <see cref="IOpenApiAny"/>.
2938
/// </summary>

src/Microsoft.OpenApi/Models/OpenApiCallback.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,22 @@ public class OpenApiCallback : IOpenApiSerializable, IOpenApiReferenceable, IOpe
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 OpenApiCallback() { }
42+
43+
/// <summary>
44+
/// Initializes a copy of an <see cref="OpenApiCallback"/> object
45+
/// </summary>
46+
public OpenApiCallback(OpenApiCallback callback)
47+
{
48+
PathItems = new(callback.PathItems);
49+
UnresolvedReference = callback.UnresolvedReference;
50+
Reference = new(callback.Reference);
51+
Extensions = new Dictionary<string, IOpenApiExtension>(callback.Extensions);
52+
}
53+
3854
/// <summary>
3955
/// Add a <see cref="OpenApiPathItem"/> into the <see cref="PathItems"/>.
4056
/// </summary>

src/Microsoft.OpenApi/Models/OpenApiComponents.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,28 @@ 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+
76+
/// <summary>
77+
/// Initializes a copy of an <see cref="OpenApiComponents"/> object
78+
/// </summary>
79+
public OpenApiComponents(OpenApiComponents components)
80+
{
81+
Schemas = new Dictionary<string, OpenApiSchema>(components.Schemas);
82+
Responses = new Dictionary<string, OpenApiResponse>(components.Responses);
83+
Parameters = new Dictionary<string, OpenApiParameter>(components.Parameters);
84+
Examples = new Dictionary<string, OpenApiExample>(components.Examples);
85+
RequestBodies = new Dictionary<string, OpenApiRequestBody>(components.RequestBodies);
86+
Headers = new Dictionary<string, OpenApiHeader>(components.Headers);
87+
SecuritySchemes = new Dictionary<string, OpenApiSecurityScheme>(components.SecuritySchemes);
88+
Links = new Dictionary<string, OpenApiLink>(components.Links);
89+
Callbacks = new Dictionary<string, OpenApiCallback>(components.Callbacks);
90+
Extensions = new Dictionary<string, IOpenApiExtension>(components.Extensions);
91+
}
92+
7193
/// <summary>
7294
/// Serialize <see cref="OpenApiComponents"/> to Open Api v3.0.
7395
/// </summary>

src/Microsoft.OpenApi/Models/OpenApiContact.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,22 @@ 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+
43+
/// <summary>
44+
/// Initializes a copy of an <see cref="OpenApiContact"/> instance
45+
/// </summary>
46+
public OpenApiContact(OpenApiContact contact)
47+
{
48+
Name = contact.Name;
49+
Url = new Uri(contact.Url.OriginalString);
50+
Email = contact.Email;
51+
Extensions = new Dictionary<string, IOpenApiExtension>(contact.Extensions);
52+
}
53+
3854
/// <summary>
3955
/// Serialize <see cref="OpenApiContact"/> to Open Api v3.0
4056
/// </summary>

src/Microsoft.OpenApi/Models/OpenApiDiscriminator.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@ 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+
30+
/// <summary>
31+
/// Initializes a copy of an <see cref="OpenApiDiscriminator"/> instance
32+
/// </summary>
33+
public OpenApiDiscriminator(OpenApiDiscriminator discriminator)
34+
{
35+
PropertyName = discriminator.PropertyName;
36+
Mapping = new Dictionary<string, string>(discriminator.Mapping);
37+
}
38+
2539
/// <summary>
2640
/// Serialize <see cref="OpenApiDiscriminator"/> to Open Api v3.0
2741
/// </summary>

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Linq;
7-
using System.Runtime.CompilerServices;
8-
using Microsoft.OpenApi.Any;
97
using Microsoft.OpenApi.Exceptions;
108
using Microsoft.OpenApi.Interfaces;
119
using Microsoft.OpenApi.Services;
@@ -64,6 +62,27 @@ public class OpenApiDocument : IOpenApiSerializable, IOpenApiExtensible
6462
/// </summary>
6563
public IDictionary<string, IOpenApiExtension> Extensions { get; set; } = new Dictionary<string, IOpenApiExtension>();
6664

65+
/// <summary>
66+
/// Parameter-less constructor
67+
/// </summary>
68+
public OpenApiDocument() {}
69+
70+
/// <summary>
71+
/// Initializes a copy of an an <see cref="OpenApiDocument"/> object
72+
/// </summary>
73+
public OpenApiDocument(OpenApiDocument document)
74+
{
75+
Workspace = new(document.Workspace);
76+
Info = new(document.Info);
77+
Servers = new List<OpenApiServer>(document.Servers);
78+
Paths = new(document.Paths);
79+
Components = new(document.Components);
80+
SecurityRequirements = new List<OpenApiSecurityRequirement>(document.SecurityRequirements);
81+
Tags = new List<OpenApiTag>(document.Tags);
82+
ExternalDocs = new(document.ExternalDocs);
83+
Extensions = new Dictionary<string, IOpenApiExtension>(document.Extensions);
84+
}
85+
6786
/// <summary>
6887
/// Serialize <see cref="OpenApiDocument"/> to the latest patch of OpenAPI object V3.0.
6988
/// </summary>

0 commit comments

Comments
 (0)