-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathOpenApiParameterReference.cs
More file actions
190 lines (164 loc) · 6.92 KB
/
OpenApiParameterReference.cs
File metadata and controls
190 lines (164 loc) · 6.92 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.Text.Json.Nodes;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Writers;
namespace Microsoft.OpenApi.Models.References
{
/// <summary>
/// Parameter Object Reference.
/// </summary>
public class OpenApiParameterReference : OpenApiParameter, IOpenApiReferenceableWithTarget<OpenApiParameter>
{
internal OpenApiParameter _target;
private readonly OpenApiReference _reference;
private string _description;
private bool? _explode;
private ParameterStyle? _style;
/// <summary>
/// Gets the target parameter.
/// </summary>
/// <remarks>
/// If the reference is not resolved, this will return null.
/// </remarks>
public OpenApiParameter Target
{
get
{
_target ??= Reference.HostDocument.ResolveReferenceTo<OpenApiParameter>(_reference);
if (!string.IsNullOrEmpty(_description)) _target.Description = _description;
return _target;
}
}
/// <summary>
/// Constructor initializing the reference object.
/// </summary>
/// <param name="referenceId">The reference Id.</param>
/// <param name="hostDocument">The host OpenAPI document.</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>
public OpenApiParameterReference(string referenceId, OpenApiDocument hostDocument, string externalResource = null)
{
Utils.CheckArgumentNullOrEmpty(referenceId);
_reference = new OpenApiReference()
{
Id = referenceId,
HostDocument = hostDocument,
Type = ReferenceType.Parameter,
ExternalResource = externalResource
};
Reference = _reference;
}
internal OpenApiParameterReference(OpenApiParameter target, string referenceId)
{
_target = target;
_reference = new OpenApiReference()
{
Id = referenceId,
Type = ReferenceType.Parameter,
};
}
private string _name;
/// <inheritdoc/>
public override string Name { get => !string.IsNullOrEmpty(_name) ? _name : Target?.Name; set => _name = value; }
/// <inheritdoc/>
public override string Description
{
get => string.IsNullOrEmpty(_description) ? Target?.Description : _description;
set => _description = value;
}
private bool? _required;
/// <inheritdoc/>
public override bool Required { get => _required is not null ? _required.Value : Target?.Required ?? false; set => _required = value; }
private bool? _deprecated;
/// <inheritdoc/>
public override bool Deprecated { get => _deprecated is not null ? _deprecated.Value : Target?.Deprecated ?? false; set => _deprecated = value; }
private bool? _allowEmptyValue;
/// <inheritdoc/>
public override bool AllowEmptyValue { get => _allowEmptyValue is not null ? _allowEmptyValue.Value : Target?.AllowEmptyValue ?? false; set => _allowEmptyValue = value; }
private bool? _allowReserved;
/// <inheritdoc/>
public override bool AllowReserved { get => _allowReserved is not null ? _allowReserved.Value : Target?.AllowReserved ?? false; set => _allowReserved = value; }
private OpenApiSchema _schema;
/// <inheritdoc/>
public override OpenApiSchema Schema { get => _schema is not null ? _schema : Target?.Schema; set => _schema = value; }
private JsonNode _example;
/// <inheritdoc/>
public override JsonNode Example { get => _example is not null ? _example : Target?.Example; set => _example = value; }
private IDictionary<string, OpenApiExample> _examples;
/// <inheritdoc/>
public override IDictionary<string, OpenApiExample> Examples { get => _examples is not null ? _examples : Target?.Examples; set => _examples = value; }
private ParameterLocation? _in;
/// <inheritdoc/>
public override ParameterLocation? In { get => _in is not null ? _in : Target?.In; set => _in = value; }
/// <inheritdoc/>
public override ParameterStyle? Style
{
get => _style ?? GetDefaultStyleValue();
set => _style = value;
}
/// <inheritdoc/>
public override bool Explode
{
get => _explode ?? Style == ParameterStyle.Form;
set => _explode = value;
}
private IDictionary<string, OpenApiMediaType> _content;
/// <inheritdoc/>
public override IDictionary<string, OpenApiMediaType> Content { get => _content is not null ? _content : Target?.Content; set => _content = value; }
private IDictionary<string, IOpenApiExtension> _extensions;
/// <inheritdoc/>
public override IDictionary<string, IOpenApiExtension> Extensions { get => _extensions is not null ? _extensions : Target?.Extensions; set => _extensions = value; }
/// <inheritdoc/>
public override void SerializeAsV3(IOpenApiWriter writer)
{
if (!writer.GetSettings().ShouldInlineReference(_reference))
{
_reference.SerializeAsV3(writer);
return;
}
else
{
SerializeInternal(writer, (writer, element) => element.SerializeAsV3(writer));
}
}
/// <inheritdoc/>
public override void SerializeAsV31(IOpenApiWriter writer)
{
if (!writer.GetSettings().ShouldInlineReference(_reference))
{
_reference.SerializeAsV31(writer);
return;
}
else
{
SerializeInternal(writer, (writer, element) => element.SerializeAsV31(writer));
}
}
/// <inheritdoc/>
public override void SerializeAsV2(IOpenApiWriter writer)
{
if (!writer.GetSettings().ShouldInlineReference(_reference))
{
_reference.SerializeAsV2(writer);
return;
}
else
{
SerializeInternal(writer, (writer, element) => element.SerializeAsV2(writer));
}
}
/// <inheritdoc/>
private void SerializeInternal(IOpenApiWriter writer,
Action<IOpenApiWriter, IOpenApiReferenceable> action)
{
Utils.CheckArgumentNull(writer);
action(writer, Target);
}
}
}