-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathBaseOpenApiReferenceHolder.cs
More file actions
128 lines (122 loc) · 4.42 KB
/
BaseOpenApiReferenceHolder.cs
File metadata and controls
128 lines (122 loc) · 4.42 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Writers;
namespace Microsoft.OpenApi.Models.References;
/// <summary>
/// Base class for OpenApiReferenceHolder.
/// </summary>
/// <typeparam name="T">The concrete class implementation type for the model.</typeparam>
/// <typeparam name="V">The interface type for the model.</typeparam>
public abstract class BaseOpenApiReferenceHolder<T, V> : IOpenApiReferenceHolder<T, V> where T : class, IOpenApiReferenceable, V where V : IOpenApiSerializable
{
/// <summary>
/// The resolved target object.
/// </summary>
protected T _target;
/// <inheritdoc/>
public virtual T Target
{
get
{
_target ??= Reference.HostDocument?.ResolveReferenceTo<T>(Reference);
return _target;
}
}
/// <summary>
/// Copy constructor
/// </summary>
/// <param name="source">The parameter reference to copy</param>
protected BaseOpenApiReferenceHolder(BaseOpenApiReferenceHolder<T, V> source)
{
Utils.CheckArgumentNull(source);
Reference = source.Reference != null ? new(source.Reference) : null;
UnresolvedReference = source.UnresolvedReference;
//no need to copy summary and description as if they are not overridden, they will be fetched from the target
//if they are, the reference copy will handle it
}
private protected BaseOpenApiReferenceHolder(T target, string referenceId, ReferenceType referenceType)
{
_target = target;
Reference = new OpenApiReference()
{
Id = referenceId,
Type = referenceType,
};
}
/// <summary>
/// Constructor initializing the reference object.
/// </summary>
/// <param name="referenceId">The reference Id.</param>
/// <param name="hostDocument">The host OpenAPI document.</param>
/// <param name="referenceType">The reference type.</param>
/// <param name="externalResource">Optional: External resource in the reference.
/// It may be:
/// 1. a absolute/relative file path, for example: ../commons/pet.json
/// 2. a Url, for example: http://localhost/pet.json
/// </param>
protected BaseOpenApiReferenceHolder(string referenceId, OpenApiDocument hostDocument, ReferenceType referenceType, string externalResource = null)
{
Utils.CheckArgumentNullOrEmpty(referenceId);
Reference = new OpenApiReference()
{
Id = referenceId,
HostDocument = hostDocument,
Type = referenceType,
ExternalResource = externalResource
};
}
/// <inheritdoc/>
public bool UnresolvedReference { get; set; }
/// <inheritdoc/>
public OpenApiReference Reference { get; set; }
/// <inheritdoc/>
public abstract V CopyReferenceAsTargetElementWithOverrides(V source);
/// <inheritdoc/>
public virtual void SerializeAsV3(IOpenApiWriter writer)
{
if (!writer.GetSettings().ShouldInlineReference(Reference))
{
Reference.SerializeAsV3(writer);
}
else
{
SerializeInternal(writer, (writer, element) => element.SerializeAsV3(writer));
}
}
/// <inheritdoc/>
public virtual void SerializeAsV31(IOpenApiWriter writer)
{
if (!writer.GetSettings().ShouldInlineReference(Reference))
{
Reference.SerializeAsV31(writer);
}
else
{
SerializeInternal(writer, (writer, element) => CopyReferenceAsTargetElementWithOverrides(element).SerializeAsV31(writer));
}
}
/// <inheritdoc/>
public virtual void SerializeAsV2(IOpenApiWriter writer)
{
if (!writer.GetSettings().ShouldInlineReference(Reference))
{
Reference.SerializeAsV2(writer);
}
else
{
SerializeInternal(writer, (writer, element) => element.SerializeAsV2(writer));
}
}
/// <summary>
/// Serialize the reference as a reference or the target object.
/// This method is used to accelerate the serialization methods implementations.
/// </summary>
/// <param name="writer">The OpenApiWriter.</param>
/// <param name="action">The action to serialize the target object.</param>
private protected void SerializeInternal(IOpenApiWriter writer,
Action<IOpenApiWriter, V> action)
{
Utils.CheckArgumentNull(writer);
action(writer, Target);
}
}