Skip to content

Commit 4b50cc5

Browse files
Further unit tests covering ResolveReference method on IOpenApiReferencable interface
1 parent 4eb1c6c commit 4b50cc5

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

src/Microsoft.OpenApi/Extensions/OpenApiReferencableExtensions.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,15 @@ public static IOpenApiReferenceable ResolveReference(this IOpenApiReferenceable
4343
{
4444
throw new OpenApiException(string.Format(SRResource.InvalidReferenceId, jsonPointer));
4545
}
46-
throw new NotImplementedException();
46+
throw new OpenApiException(string.Format(SRResource.InvalidReferenceId, jsonPointer));
4747
}
4848

4949
private static IOpenApiReferenceable ResolveReferenceOnHeaderElement(OpenApiHeader headerElement, string jsonPointer)
5050
{
51+
if (string.IsNullOrEmpty(jsonPointer))
52+
{
53+
throw new OpenApiException(string.Format(SRResource.InvalidReferenceId, jsonPointer));
54+
}
5155
var jsonPointerTokens = jsonPointer.Split('/');
5256
var propertyName = jsonPointerTokens.ElementAtOrDefault(1);
5357
switch (propertyName)
@@ -56,6 +60,8 @@ private static IOpenApiReferenceable ResolveReferenceOnHeaderElement(OpenApiHead
5660
return headerElement.Schema;
5761
case OpenApiConstants.Examples:
5862
{
63+
if (jsonPointerTokens.Length < 3)
64+
throw new OpenApiException(string.Format(SRResource.InvalidReferenceId, jsonPointer));
5965
var mapKey = jsonPointerTokens.ElementAtOrDefault(2);
6066
return headerElement.Examples[mapKey];
6167
}
@@ -66,6 +72,10 @@ private static IOpenApiReferenceable ResolveReferenceOnHeaderElement(OpenApiHead
6672

6773
private static IOpenApiReferenceable ResolveReferenceOnParameterElement(OpenApiParameter parameterElement, string jsonPointer)
6874
{
75+
if (string.IsNullOrEmpty(jsonPointer))
76+
{
77+
throw new OpenApiException(string.Format(SRResource.InvalidReferenceId, jsonPointer));
78+
}
6979
var jsonPointerTokens = jsonPointer.Split('/');
7080
var propertyName = jsonPointerTokens.ElementAtOrDefault(1);
7181
switch (propertyName)
@@ -74,6 +84,8 @@ private static IOpenApiReferenceable ResolveReferenceOnParameterElement(OpenApiP
7484
return parameterElement.Schema;
7585
case OpenApiConstants.Examples:
7686
{
87+
if (jsonPointerTokens.Length < 3)
88+
throw new OpenApiException(string.Format(SRResource.InvalidReferenceId, jsonPointer));
7789
var mapKey = jsonPointerTokens.ElementAtOrDefault(2);
7890
return parameterElement.Examples[mapKey];
7991
}

test/Microsoft.OpenApi.Tests/Workspaces/OpenApiReferencableTests.cs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class OpenApiReferencableTests
4040
private static readonly OpenApiSecurityScheme _securitySchemeFragment = new OpenApiSecurityScheme();
4141
private static readonly OpenApiTag _tagFragment = new OpenApiTag();
4242

43-
public static IEnumerable<object[]> ReferencableElementsCanResolveReferencesTestData =>
43+
public static IEnumerable<object[]> ResolveReferenceCanResolveValidJsonPointersTestData =>
4444
new List<object[]>
4545
{
4646
new object[] { _callbackFragment, "/", _callbackFragment },
@@ -60,36 +60,46 @@ public class OpenApiReferencableTests
6060
};
6161

6262
[Theory]
63-
[MemberData(nameof(ReferencableElementsCanResolveReferencesTestData))]
64-
public void ReferencableElementsCanResolveReferences(
63+
[MemberData(nameof(ResolveReferenceCanResolveValidJsonPointersTestData))]
64+
public void ResolveReferenceCanResolveValidJsonPointers(
6565
IOpenApiReferenceable element,
66-
string pointer,
66+
string jsonPointer,
6767
IOpenApiElement expectedResolvedElement)
6868
{
6969
// Act
70-
var actualResolvedElement = element.ResolveReference(pointer);
70+
var actualResolvedElement = element.ResolveReference(jsonPointer);
7171

7272
// Assert
7373
Assert.Same(expectedResolvedElement, actualResolvedElement);
7474
}
7575

76-
public static IEnumerable<object[]> ParameterElementShouldThrowOnInvalidReferenceIdTestData =>
76+
public static IEnumerable<object[]> ResolveReferenceShouldThrowOnInvalidReferenceIdTestData =>
7777
new List<object[]>
7878
{
79-
new object[] { "" },
80-
new object[] { "a" },
81-
new object[] { "examples" },
82-
new object[] { "examples/" },
83-
new object[] { "examples/a" },
79+
new object[] { _callbackFragment, null },
80+
new object[] { _callbackFragment, "" },
81+
new object[] { _callbackFragment, "/a" },
82+
new object[] { _headerFragment, null },
83+
new object[] { _headerFragment, "" },
84+
new object[] { _headerFragment, "/a" },
85+
new object[] { _headerFragment, "/examples" },
86+
new object[] { _headerFragment, "/examples/" },
87+
new object[] { _headerFragment, "/examples/a" },
88+
new object[] { _parameterFragment, null },
89+
new object[] { _parameterFragment, "" },
90+
new object[] { _parameterFragment, "/a" },
91+
new object[] { _parameterFragment, "/examples" },
92+
new object[] { _parameterFragment, "/examples/" },
93+
new object[] { _parameterFragment, "/examples/a" }
8494

8595
};
8696

8797
[Theory]
88-
[MemberData(nameof(ParameterElementShouldThrowOnInvalidReferenceIdTestData))]
89-
public void ParameterElementShouldThrowOnInvalidReferenceId(string jsonPointer)
98+
[MemberData(nameof(ResolveReferenceShouldThrowOnInvalidReferenceIdTestData))]
99+
public void ResolveReferenceShouldThrowOnInvalidReferenceId(IOpenApiReferenceable element, string jsonPointer)
90100
{
91101
// Act
92-
Action resolveReference = () => _parameterFragment.ResolveReference(jsonPointer);
102+
Action resolveReference = () => element.ResolveReference(jsonPointer);
93103

94104
// Assert
95105
var exception = Assert.Throws<OpenApiException>(resolveReference);

0 commit comments

Comments
 (0)