Skip to content

Commit d2f54ae

Browse files
committed
chore: update examples, defaults and enum values
1 parent 3ec8065 commit d2f54ae

File tree

9 files changed

+149
-50
lines changed

9 files changed

+149
-50
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Text.Json.Nodes;
6+
using Microsoft.OpenApi.Interfaces;
7+
8+
namespace Microsoft.OpenApi.Any
9+
{
10+
/// <summary>
11+
///
12+
/// </summary>
13+
public class ExtensionDictionary : IDictionary<string, IOpenApiExtension>
14+
{
15+
private readonly Dictionary<string, IOpenApiExtension> _inner = new();
16+
17+
/// <summary>
18+
/// Allow simplified usage via object indexer
19+
/// </summary>
20+
/// <param name="key"></param>
21+
/// <returns></returns>
22+
public IOpenApiExtension this[string key]
23+
{
24+
get => _inner[key];
25+
set => _inner[key] = ConvertToExtension(value);
26+
}
27+
28+
/// <summary>
29+
/// Allow setting JsonNode directly
30+
/// </summary>
31+
/// <param name="key"></param>
32+
/// <param name="node"></param>
33+
public void Set(string key, JsonNode node)
34+
{
35+
_inner[key] = new OpenApiAny(node);
36+
}
37+
38+
/// <summary>
39+
/// Allow simplified usage via object indexer
40+
/// </summary>
41+
/// <param name="key"></param>
42+
/// <param name="node"></param>
43+
/// <returns></returns>
44+
public object this[string key, JsonNode node]
45+
{
46+
set => Set(key, node);
47+
}
48+
49+
private static IOpenApiExtension ConvertToExtension(IOpenApiExtension extension)
50+
{
51+
// Just in case you want to extend in future to convert primitives
52+
return extension;
53+
}
54+
55+
/// <summary>
56+
/// Core IDictionary implementation
57+
/// </summary>
58+
/// <param name="key"></param>
59+
/// <param name="value"></param>
60+
public void Add(string key, IOpenApiExtension value) => _inner.Add(key, ConvertToExtension(value));
61+
62+
/// <summary>
63+
/// Checks whether a key exists in the dictionary.
64+
/// </summary>
65+
/// <param name="key"></param>
66+
/// <returns></returns>
67+
public bool ContainsKey(string key) => _inner.ContainsKey(key);
68+
69+
/// <summary>
70+
/// Remove the extension value for the given key.
71+
/// </summary>
72+
/// <param name="key"></param>
73+
/// <returns></returns>
74+
public bool Remove(string key) => _inner.Remove(key);
75+
/// <summary>
76+
/// Get the extension value for the given key.
77+
/// </summary>
78+
/// <param name="key"></param>
79+
/// <param name="value"></param>
80+
/// <returns></returns>
81+
public bool TryGetValue(string key, out IOpenApiExtension value) => _inner.TryGetValue(key, out value);
82+
83+
/// <summary>
84+
/// Get the keys in the dictionary.
85+
/// </summary>
86+
public ICollection<string> Keys => _inner.Keys;
87+
88+
/// <summary>
89+
/// Get the values in the dictionary.
90+
/// </summary>
91+
public ICollection<IOpenApiExtension> Values => _inner.Values;
92+
93+
/// <summary>
94+
/// Get the number of items in the dictionary.
95+
/// </summary>
96+
public int Count => _inner.Count;
97+
98+
/// <summary>
99+
/// Checks if the dictionary is read-only.
100+
/// </summary>
101+
public bool IsReadOnly => false;
102+
103+
/// <summary>
104+
/// Add an item to the dictionary.
105+
/// </summary>
106+
/// <param name="item"></param>
107+
public void Add(KeyValuePair<string, IOpenApiExtension> item) => Add(item.Key, item.Value);
108+
109+
/// <summary>
110+
/// Clears the dictionary.
111+
/// </summary>
112+
public void Clear() => _inner.Clear();
113+
114+
/// <summary>
115+
/// Checks if the dictionary contains a key-value pair.
116+
/// </summary>
117+
/// <param name="item"></param>
118+
/// <returns></returns>
119+
public bool Contains(KeyValuePair<string, IOpenApiExtension> item) => _inner.Contains(item);
120+
public void CopyTo(KeyValuePair<string, IOpenApiExtension>[] array, int arrayIndex) =>
121+
((IDictionary<string, IOpenApiExtension>)_inner).CopyTo(array, arrayIndex);
122+
public bool Remove(KeyValuePair<string, IOpenApiExtension> item) => _inner.Remove(item.Key);
123+
public IEnumerator<KeyValuePair<string, IOpenApiExtension>> GetEnumerator() => _inner.GetEnumerator();
124+
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => _inner.GetEnumerator();
125+
}
126+
127+
}

test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiHeaderTests.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void ParseHeaderWithDefaultShouldSucceed()
3838
{
3939
Type = JsonSchemaType.Number,
4040
Format = "float",
41-
Default = new OpenApiAny(5).Node
41+
Default = 5
4242
}
4343
},
4444
options => options
@@ -67,12 +67,7 @@ public void ParseHeaderWithEnumShouldSucceed()
6767
{
6868
Type = JsonSchemaType.Number,
6969
Format = "float",
70-
Enum =
71-
{
72-
new OpenApiAny(7).Node,
73-
new OpenApiAny(8).Node,
74-
new OpenApiAny(9).Node
75-
}
70+
Enum = [ 7, 8, 9 ]
7671
}
7772
}, options => options.IgnoringCyclicReferences()
7873
.Excluding((IMemberInfo memberInfo) =>

test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiParameterTests.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ public void ParseParameterWithDefaultShouldSucceed()
237237
{
238238
Type = JsonSchemaType.Number,
239239
Format = "float",
240-
Default = new OpenApiAny(5).Node
240+
Default = 5
241241
}
242242
}, options => options.IgnoringCyclicReferences().Excluding(x => x.Schema.Default.Parent));
243243
}
@@ -264,12 +264,7 @@ public void ParseParameterWithEnumShouldSucceed()
264264
{
265265
Type = JsonSchemaType.Number,
266266
Format = "float",
267-
Enum =
268-
{
269-
new OpenApiAny(7).Node,
270-
new OpenApiAny(8).Node,
271-
new OpenApiAny(9).Node
272-
}
267+
Enum = [7, 8, 9]
273268
}
274269
};
275270

test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiSchemaTests.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,7 @@ public void ParseSchemaWithEnumShouldSucceed()
8585
{
8686
Type = JsonSchemaType.Number,
8787
Format = "float",
88-
Enum = new List<JsonNode>
89-
{
90-
new OpenApiAny(7).Node,
91-
new OpenApiAny(8).Node,
92-
new OpenApiAny(9).Node
93-
}
88+
Enum = [7, 8, 9]
9489
};
9590

9691
schema.Should().BeEquivalentTo(expected, options =>

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiSchemaTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,8 @@ public void ParseBasicSchemaWithExampleShouldSucceed()
212212
},
213213
Example = new JsonObject
214214
{
215-
["name"] = new OpenApiAny("Puma").Node,
216-
["id"] = new OpenApiAny(1).Node
215+
["name"] = "Puma",
216+
["id"] = 1
217217
}
218218
}, options => options
219219
.IgnoringCyclicReferences()

test/Microsoft.OpenApi.Tests/Models/OpenApiParameterTests.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,7 @@ public class OpenApiParameterTests
7373
Type = JsonSchemaType.Array,
7474
Items = new OpenApiSchema()
7575
{
76-
Enum =
77-
{
78-
new OpenApiAny("value1").Node,
79-
new OpenApiAny("value2").Node
80-
}
76+
Enum = ["value1", "value2"]
8177
}
8278
}
8379
};
@@ -94,11 +90,7 @@ public class OpenApiParameterTests
9490
Type = JsonSchemaType.Array,
9591
Items = new OpenApiSchema()
9692
{
97-
Enum =
98-
[
99-
new OpenApiAny("value1").Node,
100-
new OpenApiAny("value2").Node
101-
]
93+
Enum = ["value1", "value2"]
10294
}
10395
}
10496
};

test/Microsoft.OpenApi.Tests/Services/OpenApiValidatorTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public void ValidateCustomExtension()
103103
{
104104
var ruleset = ValidationRuleSet.GetDefaultRuleSet();
105105

106-
ruleset.Add(typeof(OpenApiAny),
106+
ruleset.Add(typeof(OpenApiAny),
107107
new ValidationRule<OpenApiAny>("FooExtensionRule",
108108
(context, item) =>
109109
{
@@ -147,8 +147,8 @@ public void ValidateCustomExtension()
147147
[Fact]
148148
public void RemoveRuleByName_Invalid()
149149
{
150-
Assert.Throws<ArgumentNullException>(() => new ValidationRule<OpenApiAny>(null, (vc, oaa) => { }));
151-
Assert.Throws<ArgumentNullException>(() => new ValidationRule<OpenApiAny>(string.Empty, (vc, oaa) => { }));
150+
Assert.Throws<ArgumentNullException>(() => new ValidationRule<JsonNode>(null, (vc, oaa) => { }));
151+
Assert.Throws<ArgumentNullException>(() => new ValidationRule<JsonNode>(string.Empty, (vc, oaa) => { }));
152152
}
153153

154154
[Fact]

test/Microsoft.OpenApi.Tests/Validations/OpenApiSchemaValidationTests.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,22 @@ public void ValidateEnumShouldNotHaveDataTypeMismatchForSimpleSchema()
7373
IEnumerable<OpenApiError> warnings;
7474
var schema = new OpenApiSchema()
7575
{
76-
Enum =
77-
{
78-
new OpenApiAny("1").Node,
79-
new OpenApiAny(new JsonObject()
76+
Enum =
77+
[
78+
1,
79+
new JsonObject()
8080
{
8181
["x"] = 2,
8282
["y"] = "20",
8383
["z"] = "200"
84-
}).Node,
85-
new OpenApiAny(new JsonArray() { 3 }).Node,
86-
new OpenApiAny(new JsonObject()
84+
},
85+
new JsonArray() { 3 },
86+
new JsonObject()
8787
{
8888
["x"] = 4,
8989
["y"] = 40,
90-
}).Node
91-
},
90+
}
91+
],
9292
Type = JsonSchemaType.Object,
9393
AdditionalProperties = new OpenApiSchema()
9494
{

test/Microsoft.OpenApi.Tests/Writers/OpenApiJsonWriterTests.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -325,12 +325,7 @@ public void OpenApiJsonWriterOutputsValidJsonValueWhenSchemaHasNanOrInfinityValu
325325
// Arrange
326326
var schema = new OpenApiSchema
327327
{
328-
Enum = new List<JsonNode>
329-
{
330-
new OpenApiAny("NaN").Node,
331-
new OpenApiAny("Infinity").Node,
332-
new OpenApiAny("-Infinity").Node
333-
}
328+
Enum = ["NaN", "Infinity", "-Infinity"]
334329
};
335330

336331
// Act

0 commit comments

Comments
 (0)