Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 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
36 changes: 36 additions & 0 deletions src/Microsoft.OpenApi/Any/CloneHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System.Reflection;

namespace Microsoft.OpenApi.Any
{
/// <summary>
/// Contains logic for cloning objects through copy constructors.
/// </summary>
public class CloneHelper
{
/// <summary>
/// Clones an instance of <see cref="IOpenApiAny"/> object from the copy constructor
/// </summary>
/// <param name="obj">The object instance.</param>
/// <returns>A clone copy or the object itself.</returns>
public static IOpenApiAny CloneFromCopyConstructor(IOpenApiAny obj)
{
if (obj != null)
{
var t = obj.GetType();
foreach (ConstructorInfo ci in t.GetConstructors())
{
ParameterInfo[] pi = ci.GetParameters();
if (pi.Length == 1 && pi[0].ParameterType == t)
{
return (IOpenApiAny)ci.Invoke(new object[] { obj });
}
}
}

return obj;
}
}
}
1 change: 1 addition & 0 deletions src/Microsoft.OpenApi/Any/IOpenApiAny.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
using Microsoft.OpenApi.Interfaces;

namespace Microsoft.OpenApi.Any
Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.OpenApi/Any/OpenApiArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.

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

namespace Microsoft.OpenApi.Any
Expand Down
10 changes: 10 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,6 +25,15 @@ 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>
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 = CloneHelper.CloneFromCopyConstructor(example.Value);
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
26 changes: 26 additions & 0 deletions src/Microsoft.OpenApi/Models/OpenApiHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,32 @@ public class OpenApiHeader : IOpenApiSerializable, IOpenApiReferenceable, IOpenA
/// </summary>
public IDictionary<string, IOpenApiExtension> Extensions { get; set; } = new Dictionary<string, IOpenApiExtension>();

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

/// <summary>
/// Initializes a copy of an <see cref="OpenApiHeader"/> object
/// </summary>
public OpenApiHeader(OpenApiHeader header)
{
UnresolvedReference = header.UnresolvedReference;
Reference = new(header.Reference);
Description = header.Description;
Required = header.Required;
Deprecated = header.Deprecated;
AllowEmptyValue = header.AllowEmptyValue;
Style = header.Style;
Explode = header.Explode;
AllowReserved = header.AllowReserved;
Schema = new(header.Schema);
Example = CloneHelper.CloneFromCopyConstructor(header.Example);
Examples = new Dictionary<string, OpenApiExample>(header.Examples);
Content = new Dictionary<string, OpenApiMediaType>(header.Content);
Extensions = new Dictionary<string, IOpenApiExtension>(header.Extensions);
}

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

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

/// <summary>
/// Initializes a copy of an <see cref="OpenApiInfo"/> object
/// </summary>
public OpenApiInfo(OpenApiInfo info)
{
Title = info.Title;
Description = info.Description;
Version = info.Version;
TermsOfService = info.TermsOfService;
Contact = new(info.Contact);
License = new(info.License);
Extensions = new Dictionary<string, IOpenApiExtension>(info.Extensions);
}

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

/// <summary>
/// Parameterless constructor
/// </summary>
public OpenApiLicense() {}

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

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