Skip to content

Adds support for Copy Constructors #853

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Microsoft.OpenApi/Any/IOpenApiAny.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
using Microsoft.OpenApi.Interfaces;

namespace Microsoft.OpenApi.Any
{
/// <summary>
/// Base interface for all the types that represent Open API Any.
/// </summary>
public interface IOpenApiAny : IOpenApiElement, IOpenApiExtension
public interface IOpenApiAny : IOpenApiElement, IOpenApiExtension, ICloneable
{
/// <summary>
/// Type of an <see cref="IOpenApiAny"/>.
Expand Down
12 changes: 11 additions & 1 deletion src/Microsoft.OpenApi/Any/OpenApiArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,30 @@
// Licensed under the MIT license.

using Microsoft.OpenApi.Writers;
using System;
using System.Collections.Generic;

namespace Microsoft.OpenApi.Any
{
/// <summary>
/// Open API array.
/// </summary>
public class OpenApiArray : List<IOpenApiAny>, IOpenApiAny
public class OpenApiArray : List<IOpenApiAny>, IOpenApiAny, ICloneable
{
/// <summary>
/// The type of <see cref="IOpenApiAny"/>
/// </summary>
public AnyType AnyType { get; } = AnyType.Array;

/// <summary>
/// Implement ICloneable interface to allow for deep copying
/// </summary>
/// <returns>A new copy of <see cref="OpenApiArray"/></returns>
public object Clone()
{
return new OpenApiArray();
}

/// <summary>
/// Write out contents of OpenApiArray to passed writer
/// </summary>
Expand Down
10 changes: 10 additions & 0 deletions src/Microsoft.OpenApi/Any/OpenApiNull.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ public class OpenApiNull : IOpenApiAny
/// </summary>
public AnyType AnyType { get; } = AnyType.Null;


/// <summary>
/// Implement ICloneable interface to allow for deep copying
/// </summary>
/// <returns>A new copy of <see cref="OpenApiNull"/></returns>
public object Clone()
{
return new OpenApiNull();
}

/// <summary>
/// Write out null representation
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions src/Microsoft.OpenApi/Any/OpenApiObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ public class OpenApiObject : Dictionary<string, IOpenApiAny>, IOpenApiAny
/// </summary>
public AnyType AnyType { get; } = AnyType.Object;

/// <summary>
/// Implement ICloneable interface to allow for deep copying
/// </summary>
/// <returns>A new copy of <see cref="OpenApiObject"/></returns>
public object Clone()
{
return new OpenApiObject();
}

/// <summary>
/// Serialize OpenApiObject to writer
/// </summary>
Expand Down
45 changes: 45 additions & 0 deletions src/Microsoft.OpenApi/Any/OpenApiPrimitive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.

using System;
using System.Reflection;
using System.Text;
using Microsoft.OpenApi.Exceptions;
using Microsoft.OpenApi.Properties;
Expand All @@ -24,11 +25,21 @@ public OpenApiPrimitive(T value)
Value = value;
}

/// <summary>
/// Initializes a copy of an <see cref="IOpenApiPrimitive"/> object
/// </summary>
/// <param name="openApiPrimitive"></param>
public OpenApiPrimitive(OpenApiPrimitive<T> openApiPrimitive)
{
Value = openApiPrimitive.Value;
}

/// <summary>
/// The kind of <see cref="IOpenApiAny"/>.
/// </summary>
public AnyType AnyType { get; } = AnyType.Primitive;


/// <summary>
/// The primitive class this object represents.
/// </summary>
Expand All @@ -39,6 +50,40 @@ public OpenApiPrimitive(T value)
/// </summary>
public T Value { get; }

/// <summary>
/// Implement ICloneable interface to allow for deep copying
/// </summary>
/// <returns>A new copy of <see cref="IOpenApiPrimitive"/></returns>
public object Clone()
{
var clone = CloneFromCopyConstructor(this);
if (clone == null) throw new ApplicationException("There's no copy constructor defined");
return clone;
}

/// <summary>
/// Clones an instance of <see cref="IOpenApiPrimitive"/> object from the copy constructor
/// </summary>
/// <param name="obj">The object instance.</param>
/// <returns>A clone copy.</returns>
public static object CloneFromCopyConstructor(Object obj)
{
if (obj != null)
{
Type t = obj.GetType();
foreach (ConstructorInfo ci in t.GetConstructors())
{
ParameterInfo[] pi = ci.GetParameters();
if (pi.Length == 1 && pi[0].ParameterType == t)
{
return ci.Invoke(new object[] { obj });
}
}
}

return null;
}

/// <summary>
/// Write out content of primitive element
/// </summary>
Expand Down
16 changes: 16 additions & 0 deletions src/Microsoft.OpenApi/Models/OpenApiCallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ public class OpenApiCallback : IOpenApiSerializable, IOpenApiReferenceable, IOpe
/// </summary>
public IDictionary<string, IOpenApiExtension> Extensions { get; set; } = new Dictionary<string, IOpenApiExtension>();

/// <summary>
/// Parameter-less constructor
/// </summary>
public OpenApiCallback() { }

/// <summary>
/// Initializes a copy of an <see cref="OpenApiCallback"/> object
/// </summary>
public OpenApiCallback(OpenApiCallback callback)
{
PathItems = new(callback.PathItems);
UnresolvedReference = callback.UnresolvedReference;
Reference = new(callback.Reference);
Extensions = new Dictionary<string, IOpenApiExtension>(callback.Extensions);
}

/// <summary>
/// Add a <see cref="OpenApiPathItem"/> into the <see cref="PathItems"/>.
/// </summary>
Expand Down
22 changes: 22 additions & 0 deletions src/Microsoft.OpenApi/Models/OpenApiComponents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,28 @@ public class OpenApiComponents : IOpenApiSerializable, IOpenApiExtensible
/// </summary>
public IDictionary<string, IOpenApiExtension> Extensions { get; set; } = new Dictionary<string, IOpenApiExtension>();

/// <summary>
/// Parameter-less constructor
/// </summary>
public OpenApiComponents() { }

/// <summary>
/// Initializes a copy of an <see cref="OpenApiComponents"/> object
/// </summary>
public OpenApiComponents(OpenApiComponents components)
{
Schemas = new Dictionary<string, OpenApiSchema>(components.Schemas);
Responses = new Dictionary<string, OpenApiResponse>(components.Responses);
Parameters = new Dictionary<string, OpenApiParameter>(components.Parameters);
Examples = new Dictionary<string, OpenApiExample>(components.Examples);
RequestBodies = new Dictionary<string, OpenApiRequestBody>(components.RequestBodies);
Headers = new Dictionary<string, OpenApiHeader>(components.Headers);
SecuritySchemes = new Dictionary<string, OpenApiSecurityScheme>(components.SecuritySchemes);
Links = new Dictionary<string, OpenApiLink>(components.Links);
Callbacks = new Dictionary<string, OpenApiCallback>(components.Callbacks);
Extensions = new Dictionary<string, IOpenApiExtension>(components.Extensions);
}

/// <summary>
/// Serialize <see cref="OpenApiComponents"/> to Open Api v3.0.
/// </summary>
Expand Down
16 changes: 16 additions & 0 deletions src/Microsoft.OpenApi/Models/OpenApiContact.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ public class OpenApiContact : IOpenApiSerializable, IOpenApiExtensible
/// </summary>
public IDictionary<string, IOpenApiExtension> Extensions { get; set; } = new Dictionary<string, IOpenApiExtension>();

/// <summary>
/// Parameter-less constructor
/// </summary>
public OpenApiContact() { }

/// <summary>
/// Initializes a copy of an <see cref="OpenApiContact"/> instance
/// </summary>
public OpenApiContact(OpenApiContact contact)
{
Name = contact.Name;
Url = new Uri(contact.Url.OriginalString);
Email = contact.Email;
Extensions = new Dictionary<string, IOpenApiExtension>(contact.Extensions);
}

/// <summary>
/// Serialize <see cref="OpenApiContact"/> to Open Api v3.0
/// </summary>
Expand Down
14 changes: 14 additions & 0 deletions src/Microsoft.OpenApi/Models/OpenApiDiscriminator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ public class OpenApiDiscriminator : IOpenApiSerializable
/// </summary>
public IDictionary<string, string> Mapping { get; set; } = new Dictionary<string, string>();

/// <summary>
/// Parameter-less constructor
/// </summary>
public OpenApiDiscriminator() { }

/// <summary>
/// Initializes a copy of an <see cref="OpenApiDiscriminator"/> instance
/// </summary>
public OpenApiDiscriminator(OpenApiDiscriminator discriminator)
{
PropertyName = discriminator.PropertyName;
Mapping = new Dictionary<string, string>(discriminator.Mapping);
}

/// <summary>
/// Serialize <see cref="OpenApiDiscriminator"/> to Open Api v3.0
/// </summary>
Expand Down
23 changes: 21 additions & 2 deletions src/Microsoft.OpenApi/Models/OpenApiDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Exceptions;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Services;
Expand Down Expand Up @@ -64,6 +62,27 @@ public class OpenApiDocument : IOpenApiSerializable, IOpenApiExtensible
/// </summary>
public IDictionary<string, IOpenApiExtension> Extensions { get; set; } = new Dictionary<string, IOpenApiExtension>();

/// <summary>
/// Parameter-less constructor
/// </summary>
public OpenApiDocument() {}

/// <summary>
/// Initializes a copy of an an <see cref="OpenApiDocument"/> object
/// </summary>
public OpenApiDocument(OpenApiDocument document)
{
Workspace = new(document.Workspace);
Info = new(document.Info);
Servers = new List<OpenApiServer>(document.Servers);
Paths = new(document.Paths);
Components = new(document.Components);
SecurityRequirements = new List<OpenApiSecurityRequirement>(document.SecurityRequirements);
Tags = new List<OpenApiTag>(document.Tags);
ExternalDocs = new(document.ExternalDocs);
Extensions = new Dictionary<string, IOpenApiExtension>(document.Extensions);
}

/// <summary>
/// Serialize <see cref="OpenApiDocument"/> to the latest patch of OpenAPI object V3.0.
/// </summary>
Expand Down
18 changes: 18 additions & 0 deletions src/Microsoft.OpenApi/Models/OpenApiEncoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,24 @@ public class OpenApiEncoding : IOpenApiSerializable, IOpenApiExtensible
/// </summary>
public IDictionary<string, IOpenApiExtension> Extensions { get; set; } = new Dictionary<string, IOpenApiExtension>();

/// <summary>
/// Parameter-less constructor
/// </summary>
public OpenApiEncoding() {}

/// <summary>
/// Initializes a copy of an <see cref="OpenApiEncoding"/> object
/// </summary>
public OpenApiEncoding(OpenApiEncoding encoding)
{
ContentType = encoding.ContentType;
Headers = new Dictionary<string, OpenApiHeader>(encoding.Headers);
Style = encoding.Style;
Explode = encoding.Explode;
AllowReserved = encoding.AllowReserved;
Extensions = new Dictionary<string, IOpenApiExtension>(encoding.Extensions);
}

/// <summary>
/// Serialize <see cref="OpenApiExternalDocs"/> to Open Api v3.0.
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions src/Microsoft.OpenApi/Models/OpenApiError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ public OpenApiError(string pointer, string message)
Message = message;
}

/// <summary>
/// Initializes a copy of an <see cref="OpenApiError"/> object
/// </summary>
public OpenApiError(OpenApiError error)
{
Pointer = error.Pointer;
Message = error.Message;
}

/// <summary>
/// Message explaining the error.
/// </summary>
Expand Down
19 changes: 19 additions & 0 deletions src/Microsoft.OpenApi/Models/OpenApiExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,25 @@ public class OpenApiExample : IOpenApiSerializable, IOpenApiReferenceable, IOpen
/// </summary>
public bool UnresolvedReference { get; set; } = false;

/// <summary>
/// Parameter-less constructor
/// </summary>
public OpenApiExample() {}

/// <summary>
/// Initializes a copy of <see cref="OpenApiExample"/> object
/// </summary>
public OpenApiExample(OpenApiExample example)
{
Summary = example.Summary;
Description = example.Description;
Value = (IOpenApiAny)example.Value.Clone();
ExternalValue = example.ExternalValue;
Extensions = new Dictionary<string, IOpenApiExtension>(example.Extensions);
Reference = new(example.Reference);
UnresolvedReference = example.UnresolvedReference;
}

/// <summary>
/// Serialize <see cref="OpenApiExample"/> to Open Api v3.0
/// </summary>
Expand Down
15 changes: 15 additions & 0 deletions src/Microsoft.OpenApi/Models/OpenApiExternalDocs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ public class OpenApiExternalDocs : IOpenApiSerializable, IOpenApiExtensible
/// </summary>
public IDictionary<string, IOpenApiExtension> Extensions { get; set; } = new Dictionary<string, IOpenApiExtension>();

/// <summary>
/// Parameter-less constructor
/// </summary>
public OpenApiExternalDocs() {}

/// <summary>
/// Initializes a copy of an <see cref="OpenApiExternalDocs"/> object
/// </summary>
public OpenApiExternalDocs(OpenApiExternalDocs externalDocs)
{
Description = externalDocs.Description;
Url = new Uri(externalDocs.Url.OriginalString);
Extensions = new Dictionary<string, IOpenApiExtension>(externalDocs.Extensions);
}

/// <summary>
/// Serialize <see cref="OpenApiExternalDocs"/> to Open Api v3.0.
/// </summary>
Expand Down
Loading