Skip to content

Commit c86fd89

Browse files
Resolve references to child properties of Response fragments
1 parent 4b50cc5 commit c86fd89

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

src/Microsoft.OpenApi/Extensions/OpenApiReferencableExtensions.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ public static IOpenApiReferenceable ResolveReference(this IOpenApiReferenceable
3838
{
3939
return ResolveReferenceOnParameterElement((OpenApiParameter)element, jsonPointer);
4040
}
41+
if (element.GetType() == typeof(OpenApiResponse))
42+
{
43+
return ResolveReferenceOnResponseElement((OpenApiResponse)element, jsonPointer);
44+
}
4145
}
4246
catch (KeyNotFoundException)
4347
{
@@ -93,5 +97,34 @@ private static IOpenApiReferenceable ResolveReferenceOnParameterElement(OpenApiP
9397
throw new OpenApiException(string.Format(SRResource.InvalidReferenceId, jsonPointer));
9498
}
9599
}
100+
101+
private static IOpenApiReferenceable ResolveReferenceOnResponseElement(OpenApiResponse responseElement, string jsonPointer)
102+
{
103+
if (string.IsNullOrEmpty(jsonPointer))
104+
{
105+
throw new OpenApiException(string.Format(SRResource.InvalidReferenceId, jsonPointer));
106+
}
107+
var jsonPointerTokens = jsonPointer.Split('/');
108+
var propertyName = jsonPointerTokens.ElementAtOrDefault(1);
109+
switch (propertyName)
110+
{
111+
case OpenApiConstants.Headers:
112+
{
113+
if (jsonPointerTokens.Length < 3)
114+
throw new OpenApiException(string.Format(SRResource.InvalidReferenceId, jsonPointer));
115+
var mapKey = jsonPointerTokens.ElementAtOrDefault(2);
116+
return responseElement.Headers[mapKey];
117+
}
118+
case OpenApiConstants.Links:
119+
{
120+
if (jsonPointerTokens.Length < 3)
121+
throw new OpenApiException(string.Format(SRResource.InvalidReferenceId, jsonPointer));
122+
var mapKey = jsonPointerTokens.ElementAtOrDefault(2);
123+
return responseElement.Links[mapKey];
124+
}
125+
default:
126+
throw new OpenApiException(string.Format(SRResource.InvalidReferenceId, jsonPointer));
127+
}
128+
}
96129
}
97130
}

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,17 @@ public class OpenApiReferencableTests
3535
}
3636
};
3737
private static readonly OpenApiRequestBody _requestBodyFragment = new OpenApiRequestBody();
38-
private static readonly OpenApiResponse _responseFragment = new OpenApiResponse();
38+
private static readonly OpenApiResponse _responseFragment = new OpenApiResponse()
39+
{
40+
Headers = new Dictionary<string, OpenApiHeader>
41+
{
42+
{ "header1", new OpenApiHeader() }
43+
},
44+
Links = new Dictionary<string, OpenApiLink>
45+
{
46+
{ "link1", new OpenApiLink() }
47+
}
48+
};
3949
private static readonly OpenApiSchema _schemaFragment = new OpenApiSchema();
4050
private static readonly OpenApiSecurityScheme _securitySchemeFragment = new OpenApiSecurityScheme();
4151
private static readonly OpenApiTag _tagFragment = new OpenApiTag();
@@ -54,6 +64,8 @@ public class OpenApiReferencableTests
5464
new object[] { _parameterFragment, "/examples/example1", _parameterFragment.Examples["example1"] },
5565
new object[] { _requestBodyFragment, "/", _requestBodyFragment },
5666
new object[] { _responseFragment, "/", _responseFragment },
67+
new object[] { _responseFragment, "/headers/header1", _responseFragment.Headers["header1"] },
68+
new object[] { _responseFragment, "/links/link1", _responseFragment.Links["link1"] },
5769
new object[] { _schemaFragment, "/", _schemaFragment},
5870
new object[] { _securitySchemeFragment, "/", _securitySchemeFragment},
5971
new object[] { _tagFragment, "/", _tagFragment},
@@ -90,7 +102,16 @@ public void ResolveReferenceCanResolveValidJsonPointers(
90102
new object[] { _parameterFragment, "/a" },
91103
new object[] { _parameterFragment, "/examples" },
92104
new object[] { _parameterFragment, "/examples/" },
93-
new object[] { _parameterFragment, "/examples/a" }
105+
new object[] { _parameterFragment, "/examples/a" },
106+
new object[] { _responseFragment, null },
107+
new object[] { _responseFragment, "" },
108+
new object[] { _responseFragment, "/a" },
109+
new object[] { _responseFragment, "/headers" },
110+
new object[] { _responseFragment, "/headers/" },
111+
new object[] { _responseFragment, "/headers/a" },
112+
new object[] { _responseFragment, "/content" },
113+
new object[] { _responseFragment, "/content/" },
114+
new object[] { _responseFragment, "/content/a" },
94115

95116
};
96117

0 commit comments

Comments
 (0)