Skip to content

Commit 9022ba6

Browse files
committed
fix: override base class indexer to set OpenApiAny values
1 parent 5384afa commit 9022ba6

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Text.Json.Nodes;
7+
using Microsoft.OpenApi.Interfaces;
8+
9+
namespace Microsoft.OpenApi.Any
10+
{
11+
/// <summary>
12+
/// A dictionary of OpenApi extensions.
13+
/// </summary>
14+
public class OpenApiExtensionDictionary : Dictionary<string, IOpenApiExtension>
15+
{
16+
/// <summary>
17+
/// Override the base class indexer to return OpenApiAny.
18+
/// </summary>
19+
/// <param name="key"></param>
20+
/// <returns></returns>
21+
public new OpenApiAny this[string key]
22+
{
23+
get => (OpenApiAny)base[key];
24+
set => base[key] = ConvertIfJsonNode(value);
25+
}
26+
27+
/// <summary>
28+
/// Adds an extension to the dictionary.
29+
/// </summary>
30+
/// <param name="key"></param>
31+
/// <param name="value"></param>
32+
public void Add(string key, object value)
33+
{
34+
base.Add(key, ConvertIfJsonNode(value));
35+
}
36+
37+
private static IOpenApiExtension ConvertIfJsonNode(object? value)
38+
{
39+
return value switch
40+
{
41+
IOpenApiExtension extension => extension,
42+
JsonNode node => (OpenApiAny)node,
43+
_ => throw new InvalidOperationException($"Cannot convert value of type '{value?.GetType().Name}' to IOpenApiExtension.")
44+
};
45+
}
46+
47+
/// <summary>
48+
/// Test the OpenApiExtensionDictionary and base class implementations.
49+
/// </summary>
50+
public static void TestExtensions()
51+
{
52+
var jsonNode = new JsonObject();
53+
var extensions = new OpenApiExtensionDictionary
54+
{
55+
["x-key"] = jsonNode
56+
};
57+
extensions.Add("x-key", jsonNode);
58+
var extensions2 = new Dictionary<string, IOpenApiExtension>
59+
{
60+
["x-key"] = (OpenApiAny)jsonNode
61+
};
62+
extensions2.Add("x-key", (OpenApiAny)jsonNode);
63+
}
64+
}
65+
}

src/Microsoft.OpenApi/Interfaces/IOpenApiExtensible.cs

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

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

67
namespace Microsoft.OpenApi.Interfaces
78
{
@@ -13,6 +14,6 @@ public interface IOpenApiExtensible : IOpenApiElement
1314
/// <summary>
1415
/// Specification extensions.
1516
/// </summary>
16-
Dictionary<string, IOpenApiExtension>? Extensions { get; set; }
17+
OpenApiExtensionDictionary? Extensions { get; set; }
1718
}
1819
}

src/Microsoft.OpenApi/Models/OpenApiCallback.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Text.Json.Nodes;
7+
using Microsoft.OpenApi.Any;
68
using Microsoft.OpenApi.Expressions;
79
using Microsoft.OpenApi.Interfaces;
810
using Microsoft.OpenApi.Models.Interfaces;
@@ -22,7 +24,7 @@ public class OpenApiCallback : IOpenApiExtensible, IOpenApiCallback
2224
/// <summary>
2325
/// This object MAY be extended with Specification Extensions.
2426
/// </summary>
25-
public Dictionary<string, IOpenApiExtension>? Extensions { get; set; }
27+
public OpenApiExtensionDictionary? Extensions { get; set; }
2628

2729
/// <summary>
2830
/// Parameter-less constructor
@@ -90,6 +92,7 @@ internal void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion versio
9092

9193
// extensions
9294
writer.WriteExtensions(Extensions, version);
95+
Extensions!["x-extensions"] = new JsonObject(); //this works
9396

9497
writer.WriteEndObject();
9598
}

0 commit comments

Comments
 (0)