-
Notifications
You must be signed in to change notification settings - Fork 252
/
Copy pathOpenApiReferencableExtensions.cs
106 lines (101 loc) · 4.32 KB
/
OpenApiReferencableExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.OpenApi.Exceptions;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Properties;
namespace Microsoft.OpenApi.Extensions
{
/// <summary>
/// Extension methods for resolving references on <see cref="IOpenApiReferenceable"/> elements.
/// </summary>
public static class OpenApiReferenceableExtensions
{
/// <summary>
/// Resolves a JSON Pointer with respect to an element, returning the referenced element.
/// </summary>
/// <param name="element">The referencable Open API element on which to apply the JSON pointer</param>
/// <param name="pointer">a JSON Pointer [RFC 6901](https://tools.ietf.org/html/rfc6901).</param>
/// <returns>The element pointed to by the JSON pointer.</returns>
public static IOpenApiReferenceable ResolveReference(this IOpenApiReferenceable element, JsonPointer pointer)
{
if (!pointer.Tokens.Any())
{
return element;
}
var propertyName = pointer.Tokens.FirstOrDefault();
var mapKey = pointer.Tokens.ElementAtOrDefault(1);
try
{
if (element is OpenApiHeader header)
{
return ResolveReferenceOnHeaderElement(header, propertyName, mapKey, pointer);
}
if (element is OpenApiParameter parameter)
{
return ResolveReferenceOnParameterElement(parameter, propertyName, mapKey, pointer);
}
if (element is OpenApiResponse response)
{
return ResolveReferenceOnResponseElement(response, propertyName, mapKey, pointer);
}
}
catch (KeyNotFoundException)
{
throw new OpenApiException(string.Format(SRResource.InvalidReferenceId, pointer));
}
throw new OpenApiException(string.Format(SRResource.InvalidReferenceId, pointer));
}
private static IOpenApiReferenceable ResolveReferenceOnHeaderElement(
OpenApiHeader headerElement,
string propertyName,
string mapKey,
JsonPointer pointer)
{
if (OpenApiConstants.Examples.Equals(propertyName, StringComparison.Ordinal) &&
!string.IsNullOrEmpty(mapKey) &&
headerElement?.Examples != null &&
headerElement.Examples.TryGetValue(mapKey, out var exampleElement) &&
exampleElement is IOpenApiReferenceable referenceable)
{
return referenceable;
}
throw new OpenApiException(string.Format(SRResource.InvalidReferenceId, pointer));
}
private static IOpenApiReferenceable ResolveReferenceOnParameterElement(
OpenApiParameter parameterElement,
string propertyName,
string mapKey,
JsonPointer pointer)
{
if (OpenApiConstants.Examples.Equals(propertyName, StringComparison.Ordinal) &&
!string.IsNullOrEmpty(mapKey) &&
parameterElement?.Examples != null &&
parameterElement.Examples.TryGetValue(mapKey, out var exampleElement) &&
exampleElement is IOpenApiReferenceable referenceable)
{
return referenceable;
}
throw new OpenApiException(string.Format(SRResource.InvalidReferenceId, pointer));
}
private static IOpenApiReferenceable ResolveReferenceOnResponseElement(
OpenApiResponse responseElement,
string propertyName,
string mapKey,
JsonPointer pointer)
{
switch (propertyName)
{
case OpenApiConstants.Headers when mapKey != null:
return responseElement.Headers[mapKey];
case OpenApiConstants.Links when mapKey != null:
return responseElement.Links[mapKey];
default:
throw new OpenApiException(string.Format(SRResource.InvalidReferenceId, pointer));
}
}
}
}