Skip to content

feat: enable setting extensions with JsonNode values #2332

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

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
12 changes: 12 additions & 0 deletions src/Microsoft.OpenApi/Any/OpenApiAny.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,17 @@ public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
{
writer.WriteAny(Node);
}

/// <summary>
/// Implicit conversion from JsonNode to an OpenApiAny.
/// </summary>
/// <param name="jsonNode"></param>
public static implicit operator OpenApiAny(JsonNode jsonNode) => new(jsonNode);

/// <summary>
/// Implicit conversion from OpenApiAny to a JsonNode.
/// </summary>
/// <param name="openApiAny"></param>
public static implicit operator JsonNode(OpenApiAny openApiAny) => openApiAny.Node;
}
}
58 changes: 58 additions & 0 deletions src/Microsoft.OpenApi/Any/OpenApiExtensionDictionary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
using System.Collections.Generic;
using System.Text.Json.Nodes;
using Microsoft.OpenApi.Interfaces;

namespace Microsoft.OpenApi.Any
{
/// <summary>
/// A dictionary of OpenApi extensions.
/// </summary>
public class OpenApiExtensionDictionary : Dictionary<string, IOpenApiExtension>
{
/// <summary>
/// Initializes a copy of <see cref="OpenApiExtensionDictionary"/> object
/// </summary>
/// <param name="extensions"></param>
public OpenApiExtensionDictionary(OpenApiExtensionDictionary extensions) : base(dictionary: extensions) { }

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

/// <summary>
/// Override the base class indexer to return OpenApiAny.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public new OpenApiAny this[string key]
{
get => (OpenApiAny)base[key];
set => base[key] = ConvertIfJsonNode(value)!;
}

/// <summary>
/// Adds an extension to the dictionary.
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void Add(string key, object value)
{
base.Add(key, ConvertIfJsonNode(value)!);
}

private static IOpenApiExtension? ConvertIfJsonNode(object? value)
{
return value switch
{
IOpenApiExtension extension => extension,
JsonNode node => (OpenApiAny)node,
_ => null
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT license.

using System;
using System.Collections.Generic;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Exceptions;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models;
Expand Down Expand Up @@ -32,9 +32,8 @@ public static void AddExtension<T>(this T element, string name, IOpenApiExtensio
{
throw new OpenApiException(string.Format(SRResource.ExtensionFieldNameMustBeginWithXDash, name));
}

element.Extensions ??= [];
element.Extensions[name] = Utils.CheckArgumentNull(any);
element.Extensions[name] = (OpenApiAny)Utils.CheckArgumentNull(any);
}
}
}
3 changes: 2 additions & 1 deletion src/Microsoft.OpenApi/Interfaces/IOpenApiExtensible.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.

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

namespace Microsoft.OpenApi.Interfaces
{
Expand All @@ -13,6 +14,6 @@ public interface IOpenApiExtensible : IOpenApiElement
/// <summary>
/// Specification extensions.
/// </summary>
Dictionary<string, IOpenApiExtension>? Extensions { get; set; }
OpenApiExtensionDictionary? Extensions { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System.Collections.Generic;
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using Microsoft.OpenApi.Any;

namespace Microsoft.OpenApi.Interfaces;

Expand All @@ -10,6 +13,5 @@ public interface IOpenApiReadOnlyExtensible
/// <summary>
/// Specification extensions.
/// </summary>
Dictionary<string, IOpenApiExtension>? Extensions { get; }

OpenApiExtensionDictionary? Extensions { get; }
}
6 changes: 4 additions & 2 deletions src/Microsoft.OpenApi/Models/OpenApiCallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System;
using System.Collections.Generic;
using System.Text.Json.Nodes;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Expressions;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models.Interfaces;
Expand All @@ -22,7 +24,7 @@ public class OpenApiCallback : IOpenApiExtensible, IOpenApiCallback
/// <summary>
/// This object MAY be extended with Specification Extensions.
/// </summary>
public Dictionary<string, IOpenApiExtension>? Extensions { get; set; }
public OpenApiExtensionDictionary? Extensions { get; set; }

/// <summary>
/// Parameter-less constructor
Expand All @@ -36,7 +38,7 @@ internal OpenApiCallback(IOpenApiCallback callback)
{
Utils.CheckArgumentNull(callback);
PathItems = callback?.PathItems != null ? new(callback.PathItems) : null;
Extensions = callback?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(callback.Extensions) : null;
Extensions = callback?.Extensions != null ? new OpenApiExtensionDictionary(callback.Extensions) : null;
}

/// <summary>
Expand Down
5 changes: 3 additions & 2 deletions src/Microsoft.OpenApi/Models/OpenApiComponents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models.Interfaces;
using Microsoft.OpenApi.Models.References;
Expand Down Expand Up @@ -69,7 +70,7 @@ public class OpenApiComponents : IOpenApiSerializable, IOpenApiExtensible
/// <summary>
/// This object MAY be extended with Specification Extensions.
/// </summary>
public Dictionary<string, IOpenApiExtension>? Extensions { get; set; }
public OpenApiExtensionDictionary? Extensions { get; set; }

/// <summary>
/// Parameter-less constructor
Expand All @@ -91,7 +92,7 @@ public OpenApiComponents(OpenApiComponents? components)
Links = components?.Links != null ? new Dictionary<string, IOpenApiLink>(components.Links) : null;
Callbacks = components?.Callbacks != null ? new Dictionary<string, IOpenApiCallback>(components.Callbacks) : null;
PathItems = components?.PathItems != null ? new Dictionary<string, IOpenApiPathItem>(components.PathItems) : null;
Extensions = components?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(components.Extensions) : null;
Extensions = components?.Extensions != null ? new OpenApiExtensionDictionary(components.Extensions) : null;
}

/// <summary>
Expand Down
5 changes: 3 additions & 2 deletions src/Microsoft.OpenApi/Models/OpenApiContact.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

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

Expand Down Expand Up @@ -32,7 +33,7 @@ public class OpenApiContact : IOpenApiSerializable, IOpenApiExtensible
/// <summary>
/// This object MAY be extended with Specification Extensions.
/// </summary>
public Dictionary<string, IOpenApiExtension>? Extensions { get; set; }
public OpenApiExtensionDictionary? Extensions { get; set; }

/// <summary>
/// Parameter-less constructor
Expand All @@ -47,7 +48,7 @@ public OpenApiContact(OpenApiContact contact)
Name = contact?.Name ?? Name;
Url = contact?.Url != null ? new Uri(contact.Url.OriginalString, UriKind.RelativeOrAbsolute) : null;
Email = contact?.Email ?? Email;
Extensions = contact?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(contact.Extensions) : null;
Extensions = contact?.Extensions != null ? new OpenApiExtensionDictionary(contact.Extensions) : null;
}

/// <summary>
Expand Down
5 changes: 3 additions & 2 deletions src/Microsoft.OpenApi/Models/OpenApiDiscriminator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.

using System.Collections.Generic;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models.References;
using Microsoft.OpenApi.Writers;
Expand All @@ -26,7 +27,7 @@ public class OpenApiDiscriminator : IOpenApiSerializable, IOpenApiExtensible
/// <summary>
/// This object MAY be extended with Specification Extensions.
/// </summary>
public Dictionary<string, IOpenApiExtension>? Extensions { get; set; }
public OpenApiExtensionDictionary? Extensions { get; set; }

/// <summary>
/// Parameter-less constructor
Expand All @@ -40,7 +41,7 @@ public OpenApiDiscriminator(OpenApiDiscriminator discriminator)
{
PropertyName = discriminator?.PropertyName ?? PropertyName;
Mapping = discriminator?.Mapping != null ? new Dictionary<string, OpenApiSchemaReference>(discriminator.Mapping) : null;
Extensions = discriminator?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(discriminator.Extensions) : null;
Extensions = discriminator?.Extensions != null ? new OpenApiExtensionDictionary(discriminator.Extensions) : null;
}

/// <summary>
Expand Down
5 changes: 3 additions & 2 deletions src/Microsoft.OpenApi/Models/OpenApiDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models.Interfaces;
Expand Down Expand Up @@ -103,7 +104,7 @@ public HashSet<OpenApiTag>? Tags
/// <summary>
/// This object MAY be extended with Specification Extensions.
/// </summary>
public Dictionary<string, IOpenApiExtension>? Extensions { get; set; }
public OpenApiExtensionDictionary? Extensions { get; set; }

/// <inheritdoc />
public Dictionary<string, object>? Metadata { get; set; }
Expand Down Expand Up @@ -139,7 +140,7 @@ public OpenApiDocument(OpenApiDocument? document)
Security = document?.Security != null ? [.. document.Security] : null;
Tags = document?.Tags != null ? new HashSet<OpenApiTag>(document.Tags, OpenApiTagComparer.Instance) : null;
ExternalDocs = document?.ExternalDocs != null ? new(document.ExternalDocs) : null;
Extensions = document?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(document.Extensions) : null;
Extensions = document?.Extensions != null ? new OpenApiExtensionDictionary(document.Extensions) : null;
Metadata = document?.Metadata != null ? new Dictionary<string, object>(document.Metadata) : null;
BaseUri = document?.BaseUri != null ? document.BaseUri : new(OpenApiConstants.BaseRegistryUri + Guid.NewGuid());
}
Expand Down
5 changes: 3 additions & 2 deletions src/Microsoft.OpenApi/Models/OpenApiEncoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models.Interfaces;
Expand Down Expand Up @@ -52,7 +53,7 @@ public class OpenApiEncoding : IOpenApiSerializable, IOpenApiExtensible
/// <summary>
/// This object MAY be extended with Specification Extensions.
/// </summary>
public Dictionary<string, IOpenApiExtension>? Extensions { get; set; }
public OpenApiExtensionDictionary? Extensions { get; set; }

/// <summary>
/// Parameter-less constructor
Expand All @@ -69,7 +70,7 @@ public OpenApiEncoding(OpenApiEncoding encoding)
Style = encoding?.Style ?? Style;
Explode = encoding?.Explode ?? Explode;
AllowReserved = encoding?.AllowReserved ?? AllowReserved;
Extensions = encoding?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(encoding.Extensions) : null;
Extensions = encoding?.Extensions != null ? new OpenApiExtensionDictionary(encoding.Extensions) : null;
}

/// <summary>
Expand Down
5 changes: 3 additions & 2 deletions src/Microsoft.OpenApi/Models/OpenApiExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Generic;
using System.Text.Json.Nodes;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Helpers;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models.Interfaces;
Expand All @@ -28,7 +29,7 @@ public class OpenApiExample : IOpenApiExtensible, IOpenApiExample
public JsonNode? Value { get; set; }

/// <inheritdoc/>
public Dictionary<string, IOpenApiExtension>? Extensions { get; set; }
public OpenApiExtensionDictionary? Extensions { get; set; }

/// <summary>
/// Parameter-less constructor
Expand All @@ -46,7 +47,7 @@ internal OpenApiExample(IOpenApiExample example)
Description = example.Description ?? Description;
Value = example.Value != null ? JsonNodeCloneHelper.Clone(example.Value) : null;
ExternalValue = example.ExternalValue ?? ExternalValue;
Extensions = example.Extensions != null ? new Dictionary<string, IOpenApiExtension>(example.Extensions) : null;
Extensions = example.Extensions != null ? new OpenApiExtensionDictionary(example.Extensions) : null;
}

/// <inheritdoc/>
Expand Down
7 changes: 4 additions & 3 deletions src/Microsoft.OpenApi/Models/OpenApiExtensibleDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

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

Expand All @@ -28,15 +29,15 @@ protected OpenApiExtensibleDictionary():this([]) { }
/// <param name="extensions">The dictionary of <see cref="IOpenApiExtension"/>.</param>
protected OpenApiExtensibleDictionary(
Dictionary<string, T> dictionary,
Dictionary<string, IOpenApiExtension>? extensions = null) : base(dictionary is null ? [] : dictionary)
OpenApiExtensionDictionary? extensions = null) : base(dictionary is null ? [] : dictionary)
{
Extensions = extensions != null ? new Dictionary<string, IOpenApiExtension>(extensions) : [];
Extensions = extensions != null ? new OpenApiExtensionDictionary(extensions) : [];
}

/// <summary>
/// This object MAY be extended with Specification Extensions.
/// </summary>
public Dictionary<string, IOpenApiExtension>? Extensions { get; set; }
public OpenApiExtensionDictionary? Extensions { get; set; }


/// <summary>
Expand Down
5 changes: 3 additions & 2 deletions src/Microsoft.OpenApi/Models/OpenApiExternalDocs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

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

Expand All @@ -26,7 +27,7 @@ public class OpenApiExternalDocs : IOpenApiSerializable, IOpenApiExtensible
/// <summary>
/// This object MAY be extended with Specification Extensions.
/// </summary>
public Dictionary<string, IOpenApiExtension>? Extensions { get; set; }
public OpenApiExtensionDictionary? Extensions { get; set; }

/// <summary>
/// Parameter-less constructor
Expand All @@ -40,7 +41,7 @@ public OpenApiExternalDocs(OpenApiExternalDocs externalDocs)
{
Description = externalDocs?.Description ?? Description;
Url = externalDocs?.Url != null ? new Uri(externalDocs.Url.OriginalString, UriKind.RelativeOrAbsolute) : null;
Extensions = externalDocs?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(externalDocs.Extensions) : null;
Extensions = externalDocs?.Extensions != null ? new OpenApiExtensionDictionary(externalDocs.Extensions) : null;
}

/// <summary>
Expand Down
5 changes: 3 additions & 2 deletions src/Microsoft.OpenApi/Models/OpenApiHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Text.Json.Nodes;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Helpers;
using Microsoft.OpenApi.Interfaces;
Expand Down Expand Up @@ -53,7 +54,7 @@ public class OpenApiHeader : IOpenApiHeader, IOpenApiExtensible
public Dictionary<string, OpenApiMediaType>? Content { get; set; }

/// <inheritdoc/>
public Dictionary<string, IOpenApiExtension>? Extensions { get; set; }
public OpenApiExtensionDictionary? Extensions { get; set; }

/// <summary>
/// Parameter-less constructor
Expand All @@ -77,7 +78,7 @@ internal OpenApiHeader(IOpenApiHeader header)
Example = header.Example != null ? JsonNodeCloneHelper.Clone(header.Example) : null;
Examples = header.Examples != null ? new Dictionary<string, IOpenApiExample>(header.Examples) : null;
Content = header.Content != null ? new Dictionary<string, OpenApiMediaType>(header.Content) : null;
Extensions = header.Extensions != null ? new Dictionary<string, IOpenApiExtension>(header.Extensions) : null;
Extensions = header.Extensions != null ? new OpenApiExtensionDictionary(header.Extensions) : null;
}

/// <summary>
Expand Down
Loading
Loading