Skip to content

Commit 84fddba

Browse files
committed
- Modify / Rewrite majority of V3 reader tests
- Restructure the tests to resemble the deserializers. Future tests can now be written per object type.
1 parent a2337db commit 84fddba

File tree

59 files changed

+1818
-2272
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1818
-2272
lines changed

src/Microsoft.OpenApi.Readers/OpenApiError.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,40 @@ namespace Microsoft.OpenApi.Readers
1212
/// </summary>
1313
public class OpenApiError
1414
{
15-
private readonly string _message;
16-
private readonly string _pointer;
15+
/// <summary>
16+
/// Message explaining the error.
17+
/// </summary>
18+
public string Message { get; set; }
19+
20+
/// <summary>
21+
/// Pointer to the location of the error.
22+
/// </summary>
23+
public string Pointer { get; set; }
1724

1825
/// <summary>
1926
/// Initializes the <see cref="OpenApiError"/> class using the message and pointer from the given exception.
2027
/// </summary>
2128
public OpenApiError(OpenApiException exception)
2229
{
23-
_message = exception.Message;
24-
_pointer = exception.Pointer;
30+
Message = exception.Message;
31+
Pointer = exception.Pointer;
2532
}
2633

2734
/// <summary>
2835
/// Initializes the <see cref="OpenApiError"/> class.
2936
/// </summary>
3037
public OpenApiError(string pointer, string message)
3138
{
32-
this._pointer = pointer;
33-
this._message = message;
39+
this.Pointer = pointer;
40+
this.Message = message;
3441
}
3542

3643
/// <summary>
3744
/// Gets the string representation of <see cref="OpenApiError"/>.
3845
/// </summary>
39-
/// <returns></returns>
4046
public override string ToString()
4147
{
42-
return _message + (!string.IsNullOrEmpty(_pointer) ? " at " + _pointer : "");
48+
return Message + (!string.IsNullOrEmpty(Pointer) ? " at " + Pointer : "");
4349
}
4450
}
4551
}

src/Microsoft.OpenApi.Readers/ParseNodes/MapNode.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public MapNode(ParsingContext context, OpenApiDiagnostic diagnostic, YamlMapping
3535
{
3636
if (node == null)
3737
{
38-
throw new OpenApiException($"Expected map");
38+
throw new OpenApiException("Expected map");
3939
}
4040

4141
this.node = node;
@@ -72,8 +72,12 @@ public override Dictionary<string, T> CreateMap<T>(Func<MapNode, T> map)
7272
n => new
7373
{
7474
key = n.Key.GetScalarValue(),
75-
value = map(new MapNode(Context, Diagnostic, n.Value as YamlMappingNode))
75+
value = n.Value as YamlMappingNode == null ?
76+
default(T) :
77+
map(new MapNode(Context, Diagnostic, n.Value as YamlMappingNode))
78+
7679
});
80+
7781
return nodes.ToDictionary(k => k.key, v => v.value);
7882
}
7983

src/Microsoft.OpenApi.Readers/V2/OpenApiV2Deserializer.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ private static void ParseMap<T>(
3636

3737
private static void ReportMissing(ParseNode node, IList<string> required)
3838
{
39-
foreach (var error in required.Select(r => new OpenApiError("", $"{r} is a required property")).ToList())
39+
foreach (var error in required.Select(
40+
r => new OpenApiError(
41+
node.Context.GetLocation(),
42+
$"{r} is a required property"))
43+
.ToList())
4044
{
4145
node.Diagnostic.Errors.Add(error);
4246
}

src/Microsoft.OpenApi.Readers/V3/OpenApiMediaTypeDeserializer.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,18 @@ internal static partial class OpenApiV3Deserializer
4949

5050
public static OpenApiMediaType LoadMediaType(ParseNode node)
5151
{
52-
var mapNode = node.CheckMapNode("contentType");
52+
var mapNode = node.CheckMapNode("content");
5353

5454
if (!mapNode.Any())
5555
{
5656
return null;
5757
}
5858

59-
var contentType = new OpenApiMediaType();
59+
var mediaType = new OpenApiMediaType();
6060

61-
ParseMap(mapNode, contentType, MediaTypeFixedFields, MediaTypePatternFields);
61+
ParseMap(mapNode, mediaType, MediaTypeFixedFields, MediaTypePatternFields);
6262

63-
return contentType;
63+
return mediaType;
6464
}
6565
}
6666
}

src/Microsoft.OpenApi.Readers/V3/OpenApiV3Deserializer.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ private static void ReportMissing(ParseNode node, IList<string> required)
7070
}
7171

7272
foreach (var error in required.Select(
73-
r => new OpenApiError("", $"{r} is a required property of {node.Context.GetLocation()}"))
73+
r => new OpenApiError(
74+
node.Context.GetLocation(),
75+
$"{r} is a required property"))
7476
.ToList())
7577
{
7678
node.Diagnostic.Errors.Add(error);

src/Microsoft.OpenApi.Readers/YamlHelper.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public static YamlNode ParseYamlString(string yamlString)
2828
var reader = new StringReader(yamlString);
2929
var yamlStream = new YamlStream();
3030
yamlStream.Load(reader);
31+
3132
var yamlDocument = yamlStream.Documents.First();
3233
return yamlDocument.RootNode;
3334
}

src/Microsoft.OpenApi/Expressions/RuntimeExpression.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Microsoft.OpenApi.Expressions
1212
/// <summary>
1313
/// Base class for the Open API runtime expression.
1414
/// </summary>
15-
public abstract class RuntimeExpression
15+
public abstract class RuntimeExpression : IEquatable<RuntimeExpression>
1616
{
1717
/// <summary>
1818
/// The dollar sign prefix for a runtime expression.
@@ -77,5 +77,29 @@ public static RuntimeExpression Build(string expression)
7777

7878
throw new OpenApiException(String.Format(SRResource.RuntimeExpressionHasInvalidFormat, expression));
7979
}
80+
81+
/// <summary>
82+
/// GetHashCode implementation for IEquatable.
83+
/// </summary>
84+
public override int GetHashCode()
85+
{
86+
return Expression.GetHashCode();
87+
}
88+
89+
/// <summary>
90+
/// Equals implementation for IEquatable.
91+
/// </summary>
92+
public override bool Equals(object obj)
93+
{
94+
return Equals(obj as RuntimeExpression);
95+
}
96+
97+
/// <summary>
98+
/// Equals implementation for object of the same type.
99+
/// </summary>
100+
public bool Equals(RuntimeExpression obj)
101+
{
102+
return obj != null && obj.Expression == Expression;
103+
}
80104
}
81105
}

src/Microsoft.OpenApi/Models/OpenApiConstants.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
namespace Microsoft.OpenApi.Models
99
{
10+
/// <summary>
11+
/// Constants used in the Open API document.
12+
/// </summary>
1013
public static class OpenApiConstants
1114
{
1215
public const string OpenApi = "openapi";

src/Microsoft.OpenApi/Models/OpenApiReference.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,9 @@ private string GetReferenceTypeNameAsV2(ReferenceType type)
209209
return OpenApiConstants.SecurityDefinitions;
210210

211211
default:
212-
throw new OpenApiException(string.Format(SRResource.ReferenceTypeNotSupportedV2, type));
212+
// If the reference type is not supported in V2, simply return null
213+
// to indicate that the reference is not pointing to any object.
214+
return null;
213215
}
214216
}
215217
}

src/Microsoft.OpenApi/Properties/SRResource.Designer.cs

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)