Skip to content

Commit 7db3ea7

Browse files
Refactoring of OpenApiReferencable units; add parameterised tests replacing multiple existing tests
1 parent 6c69605 commit 7db3ea7

File tree

2 files changed

+45
-70
lines changed

2 files changed

+45
-70
lines changed

src/Microsoft.OpenApi/Extensions/OpenApiReferencableExtensions.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static class OpenApiReferencableExtensions
2626
/// <returns>An IEnumerable of errors. This function will never return null.</returns>
2727
public static IOpenApiReferenceable ResolveReference(this IOpenApiReferenceable element, string jsonPointer)
2828
{
29-
if (jsonPointer == string.Empty)
29+
if (jsonPointer == "/")
3030
return element;
3131
try
3232
{
@@ -45,16 +45,14 @@ public static IOpenApiReferenceable ResolveReference(this IOpenApiReferenceable
4545
private static IOpenApiReferenceable ResolveReferenceOnParameterElement(OpenApiParameter parameterElement, string jsonPointer)
4646
{
4747
var jsonPointerTokens = jsonPointer.Split('/');
48-
var propertyName = jsonPointerTokens.First();
48+
var propertyName = jsonPointerTokens.ElementAtOrDefault(1);
4949
switch (propertyName)
5050
{
5151
case OpenApiConstants.Schema:
5252
return parameterElement.Schema;
5353
case OpenApiConstants.Examples:
5454
{
55-
var mapKey = jsonPointerTokens.ElementAtOrDefault(1);
56-
if (!(jsonPointerTokens.Length >= 2 && parameterElement.Examples.ContainsKey(mapKey)))
57-
throw new OpenApiException(string.Format(SRResource.InvalidReferenceId, jsonPointer));
55+
var mapKey = jsonPointerTokens.ElementAtOrDefault(2);
5856
return parameterElement.Examples[mapKey];
5957
}
6058
default:

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

Lines changed: 42 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -15,76 +15,62 @@ namespace Microsoft.OpenApi.Tests.Workspaces
1515

1616
public class OpenApiReferencableTests
1717
{
18-
public static IEnumerable<object[]> ReferencableElementResolvesEmptyJsonPointerToItselfTestData =>
19-
new List<object[]>
18+
private static readonly OpenApiCallback _callbackFragment = new OpenApiCallback();
19+
private static readonly OpenApiExample _exampleFragment = new OpenApiExample();
20+
private static readonly OpenApiLink _linkFragment = new OpenApiLink();
21+
private static readonly OpenApiHeader _headerFragment = new OpenApiHeader();
22+
private static readonly OpenApiParameter _parameterFragment = new OpenApiParameter
2023
{
21-
new object[] { new OpenApiCallback() },
22-
new object[] { new OpenApiExample() },
23-
new object[] { new OpenApiHeader() },
24-
new object[] { new OpenApiLink() },
25-
new object[] { new OpenApiParameter() },
26-
new object[] { new OpenApiRequestBody() },
27-
new object[] { new OpenApiResponse() },
28-
new object[] { new OpenApiSchema() },
29-
new object[] { new OpenApiSecurityScheme() },
30-
new object[] { new OpenApiTag() }
31-
24+
Schema = new OpenApiSchema(),
25+
Examples = new Dictionary<string, OpenApiExample>
26+
{
27+
{ "example1", new OpenApiExample() }
28+
}
3229
};
30+
private static readonly OpenApiRequestBody _requestBodyFragment = new OpenApiRequestBody();
31+
private static readonly OpenApiResponse _responseFragment = new OpenApiResponse();
32+
private static readonly OpenApiSchema _schemaFragment = new OpenApiSchema();
33+
private static readonly OpenApiSecurityScheme _securitySchemeFragment = new OpenApiSecurityScheme();
34+
private static readonly OpenApiTag _tagFragment = new OpenApiTag();
3335

34-
[Theory]
35-
[MemberData(nameof(ReferencableElementResolvesEmptyJsonPointerToItselfTestData))]
36-
public void ReferencableElementResolvesEmptyJsonPointerToItself(IOpenApiReferenceable referencableElement)
37-
{
38-
// Arrange - above
39-
40-
// Act
41-
var resolvedReference = referencableElement.ResolveReference(string.Empty);
42-
43-
// Assert
44-
Assert.Same(referencableElement, resolvedReference);
45-
}
46-
47-
[Fact]
48-
public void ParameterElementCanResolveReferenceToSchemaProperty()
36+
public static IEnumerable<object[]> ReferencableElementsCanResolveReferencesTestData =>
37+
new List<object[]>
4938
{
50-
// Arrange
51-
var parameterElement = new OpenApiParameter
52-
{
53-
Schema = new OpenApiSchema()
54-
};
55-
56-
// Act
57-
var resolvedReference = parameterElement.ResolveReference("schema");
58-
59-
// Assert
60-
Assert.Same(parameterElement.Schema, resolvedReference);
61-
}
39+
new object[] { _callbackFragment, "/", _callbackFragment },
40+
new object[] { _exampleFragment, "/", _exampleFragment },
41+
new object[] { _linkFragment, "/", _linkFragment },
42+
new object[] { _headerFragment, "/", _headerFragment },
43+
new object[] { _parameterFragment, "/", _parameterFragment },
44+
new object[] { _parameterFragment, "/schema", _parameterFragment.Schema },
45+
new object[] { _parameterFragment, "/examples/example1", _parameterFragment.Examples["example1"] },
46+
new object[] { _requestBodyFragment, "/", _requestBodyFragment },
47+
new object[] { _responseFragment, "/", _responseFragment },
48+
new object[] { _schemaFragment, "/", _schemaFragment},
49+
new object[] { _securitySchemeFragment, "/", _securitySchemeFragment},
50+
new object[] { _tagFragment, "/", _tagFragment},
51+
};
6252

63-
[Fact]
64-
public void ParameterElementCanResolveReferenceToExampleTmpDbgImproveMyName()
53+
[Theory]
54+
[MemberData(nameof(ReferencableElementsCanResolveReferencesTestData))]
55+
public void ReferencableElementsCanResolveReferences(
56+
IOpenApiReferenceable element,
57+
string pointer,
58+
IOpenApiElement expectedResolvedElement)
6559
{
66-
// Arrange
67-
var parameterElement = new OpenApiParameter
68-
{
69-
Examples = new Dictionary<string, OpenApiExample>()
70-
{
71-
{ "example1", new OpenApiExample() }
72-
},
73-
};
74-
7560
// Act
76-
var resolvedReference = parameterElement.ResolveReference("examples/example1");
61+
var actualResolvedElement = element.ResolveReference(pointer);
7762

7863
// Assert
79-
Assert.Same(parameterElement.Examples["example1"], resolvedReference);
64+
Assert.Same(expectedResolvedElement, actualResolvedElement);
8065
}
8166

8267
public static IEnumerable<object[]> ParameterElementShouldThrowOnInvalidReferenceIdTestData =>
8368
new List<object[]>
8469
{
85-
new object[] { "/" },
70+
new object[] { "" },
8671
new object[] { "a" },
8772
new object[] { "examples" },
73+
new object[] { "examples/" },
8874
new object[] { "examples/a" },
8975

9076
};
@@ -93,21 +79,12 @@ public void ParameterElementCanResolveReferenceToExampleTmpDbgImproveMyName()
9379
[MemberData(nameof(ParameterElementShouldThrowOnInvalidReferenceIdTestData))]
9480
public void ParameterElementShouldThrowOnInvalidReferenceId(string jsonPointer)
9581
{
96-
// Arrange
97-
var parameterElement = new OpenApiParameter
98-
{
99-
Examples = new Dictionary<string, OpenApiExample>()
100-
{
101-
{ "example1", new OpenApiExample() }
102-
},
103-
};
104-
10582
// Act
106-
Action resolveReference = () => parameterElement.ResolveReference(jsonPointer);
83+
Action resolveReference = () => _parameterFragment.ResolveReference(jsonPointer);
10784

10885
// Assert
10986
var exception = Assert.Throws<OpenApiException>(resolveReference);
110-
Assert.Equal(String.Format(SRResource.InvalidReferenceId, jsonPointer), exception.Message);
87+
Assert.Equal(string.Format(SRResource.InvalidReferenceId, jsonPointer), exception.Message);
11188
}
11289
}
11390
}

0 commit comments

Comments
 (0)