Skip to content

Commit 6c69605

Browse files
Throw exception when we try to reference a type that cannot be referenced on OpenApiParameter
1 parent afcbce9 commit 6c69605

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

src/Microsoft.OpenApi/Extensions/OpenApiReferencableExtensions.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Linq;
7+
using Microsoft.OpenApi.Exceptions;
78
using Microsoft.OpenApi.Interfaces;
89
using Microsoft.OpenApi.Models;
10+
using Microsoft.OpenApi.Properties;
911
using Microsoft.OpenApi.Services;
1012
using Microsoft.OpenApi.Validations;
1113

@@ -26,27 +28,37 @@ public static IOpenApiReferenceable ResolveReference(this IOpenApiReferenceable
2628
{
2729
if (jsonPointer == string.Empty)
2830
return element;
29-
if (element.GetType() == typeof(OpenApiParameter))
31+
try
3032
{
31-
return ResolveReferenceOnParameterElement((OpenApiParameter)element, jsonPointer);
33+
if (element.GetType() == typeof(OpenApiParameter))
34+
{
35+
return ResolveReferenceOnParameterElement((OpenApiParameter)element, jsonPointer);
36+
}
37+
}
38+
catch (KeyNotFoundException)
39+
{
40+
throw new OpenApiException(string.Format(SRResource.InvalidReferenceId, jsonPointer));
3241
}
3342
throw new NotImplementedException();
3443
}
3544

3645
private static IOpenApiReferenceable ResolveReferenceOnParameterElement(OpenApiParameter parameterElement, string jsonPointer)
3746
{
3847
var jsonPointerTokens = jsonPointer.Split('/');
39-
switch (jsonPointerTokens.First())
48+
var propertyName = jsonPointerTokens.First();
49+
switch (propertyName)
4050
{
4151
case OpenApiConstants.Schema:
4252
return parameterElement.Schema;
4353
case OpenApiConstants.Examples:
4454
{
45-
var mapKey = jsonPointerTokens.ElementAt(1);
55+
var mapKey = jsonPointerTokens.ElementAtOrDefault(1);
56+
if (!(jsonPointerTokens.Length >= 2 && parameterElement.Examples.ContainsKey(mapKey)))
57+
throw new OpenApiException(string.Format(SRResource.InvalidReferenceId, jsonPointer));
4658
return parameterElement.Examples[mapKey];
4759
}
4860
default:
49-
throw new NotImplementedException();
61+
throw new OpenApiException(string.Format(SRResource.InvalidReferenceId, jsonPointer));
5062
}
5163
}
5264
}

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4+
using System;
45
using System.Collections.Generic;
6+
using Microsoft.OpenApi.Exceptions;
57
using Microsoft.OpenApi.Extensions;
68
using Microsoft.OpenApi.Interfaces;
79
using Microsoft.OpenApi.Models;
10+
using Microsoft.OpenApi.Properties;
811
using Xunit;
912

1013
namespace Microsoft.OpenApi.Tests.Workspaces
@@ -75,5 +78,36 @@ public void ParameterElementCanResolveReferenceToExampleTmpDbgImproveMyName()
7578
// Assert
7679
Assert.Same(parameterElement.Examples["example1"], resolvedReference);
7780
}
81+
82+
public static IEnumerable<object[]> ParameterElementShouldThrowOnInvalidReferenceIdTestData =>
83+
new List<object[]>
84+
{
85+
new object[] { "/" },
86+
new object[] { "a" },
87+
new object[] { "examples" },
88+
new object[] { "examples/a" },
89+
90+
};
91+
92+
[Theory]
93+
[MemberData(nameof(ParameterElementShouldThrowOnInvalidReferenceIdTestData))]
94+
public void ParameterElementShouldThrowOnInvalidReferenceId(string jsonPointer)
95+
{
96+
// Arrange
97+
var parameterElement = new OpenApiParameter
98+
{
99+
Examples = new Dictionary<string, OpenApiExample>()
100+
{
101+
{ "example1", new OpenApiExample() }
102+
},
103+
};
104+
105+
// Act
106+
Action resolveReference = () => parameterElement.ResolveReference(jsonPointer);
107+
108+
// Assert
109+
var exception = Assert.Throws<OpenApiException>(resolveReference);
110+
Assert.Equal(String.Format(SRResource.InvalidReferenceId, jsonPointer), exception.Message);
111+
}
78112
}
79113
}

0 commit comments

Comments
 (0)