Repository navigation
Expand file tree
/
Copy pathOpenApiEncoding.cs
More file actions
207 lines (179 loc) · 8.18 KB
/
Copy pathOpenApiEncoding.cs
File metadata and controls
207 lines (179 loc) · 8.18 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.Linq;
namespace Microsoft.OpenApi
{
/// <summary>
/// ExternalDocs object.
/// </summary>
public class OpenApiEncoding : IOpenApiSerializable, IOpenApiExtensible
{
/// <summary>
/// Explode backing variable
/// </summary>
private bool? _explode;
/// <summary>
/// The Content-Type for encoding a specific property.
/// The value can be a specific media type (e.g. application/json),
/// a wildcard media type (e.g. image/*), or a comma-separated list of the two types.
/// </summary>
public string? ContentType { get; set; }
/// <summary>
/// A map allowing additional information to be provided as headers.
/// </summary>
public IDictionary<string, IOpenApiHeader>? Headers { get; set; }
/// <summary>
/// A map of property names to their encoding information.
/// The key is the property name and the value is the encoding object.
/// </summary>
public IDictionary<string, OpenApiEncoding>? Encoding { get; set; }
/// <summary>
/// Encoding object for array items.
/// </summary>
public OpenApiEncoding? ItemEncoding { get; set; }
/// <summary>
/// Encoding objects for tuple-style arrays.
/// </summary>
public IList<OpenApiEncoding>? PrefixEncoding { get; set; }
/// <summary>
/// Describes how a specific property value will be serialized depending on its type.
/// </summary>
public ParameterStyle? Style { get; set; }
/// <summary>
/// When this is true, property values of type array or object generate separate parameters
/// for each value of the array, or key-value-pair of the map. For other types of properties
/// this property has no effect. When style is form, the default value is true.
/// For all other styles, the default value is false.
/// This property SHALL be ignored if the request body media type is not application/x-www-form-urlencoded.
/// </summary>
public bool? Explode
{
get => _explode ?? Style == ParameterStyle.Form;
set => _explode = value;
}
/// <summary>
/// Determines whether the parameter value SHOULD allow reserved characters,
/// as defined by RFC3986 :/?#[]@!$&'()*+,;= to be included without percent-encoding.
/// The default value is false. This property SHALL be ignored
/// if the request body media type is not application/x-www-form-urlencoded.
/// </summary>
public bool? AllowReserved { get; set; }
/// <summary>
/// This object MAY be extended with Specification Extensions.
/// </summary>
public IDictionary<string, IOpenApiExtension>? Extensions { get; set; }
/// <summary>
/// Parameter-less constructor
/// </summary>
public OpenApiEncoding() { }
/// <summary>
/// Initializes a copy of an <see cref="OpenApiEncoding"/> object
/// </summary>
public OpenApiEncoding(OpenApiEncoding encoding)
{
ContentType = encoding?.ContentType ?? ContentType;
Headers = encoding?.Headers != null ? new Dictionary<string, IOpenApiHeader>(encoding.Headers) : null;
Encoding = encoding?.Encoding != null ? new Dictionary<string, OpenApiEncoding>(encoding.Encoding, StringComparer.Ordinal) : null;
ItemEncoding = encoding?.ItemEncoding != null ? new OpenApiEncoding(encoding.ItemEncoding) : null;
PrefixEncoding = encoding?.PrefixEncoding != null ? new List<OpenApiEncoding>(encoding.PrefixEncoding) : null;
Style = encoding?.Style ?? Style;
Explode = encoding?._explode;
AllowReserved = encoding?.AllowReserved ?? AllowReserved;
Extensions = encoding?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(encoding.Extensions) : null;
}
/// <summary>
/// Serialize <see cref="OpenApiEncoding"/> to Open Api v3.2
/// </summary>
/// <param name="writer"></param>
public virtual void SerializeAsV32(IOpenApiWriter writer)
{
SerializeInternal(writer, OpenApiSpecVersion.OpenApi3_2, (writer, element) => element.SerializeAsV32(writer));
}
/// <summary>
/// Serialize <see cref="OpenApiEncoding"/> to Open Api v3.1
/// </summary>
/// <param name="writer"></param>
public virtual void SerializeAsV31(IOpenApiWriter writer)
{
SerializeInternal(writer, OpenApiSpecVersion.OpenApi3_1, (writer, element) => element.SerializeAsV31(writer));
}
/// <summary>
/// Serialize <see cref="OpenApiEncoding"/> to Open Api v3.0
/// </summary>
/// <param name="writer"></param>
public virtual void SerializeAsV3(IOpenApiWriter writer)
{
SerializeInternal(writer, OpenApiSpecVersion.OpenApi3_0, (writer, element) => element.SerializeAsV3(writer));
}
/// <summary>
/// Serialize <see cref="OpenApiExternalDocs"/> to Open Api v3.0.
/// </summary>
private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version,
Action<IOpenApiWriter, IOpenApiSerializable> callback)
{
Utils.CheckArgumentNull(writer);
writer.WriteStartObject();
// contentType
writer.WriteProperty(OpenApiConstants.ContentType, ContentType);
// headers
writer.WriteOptionalMap(OpenApiConstants.Headers, Headers, callback);
// encoding - serialize as native field in v3.2+, as extension in earlier versions
if (Encoding != null)
{
if (version >= OpenApiSpecVersion.OpenApi3_2)
{
writer.WriteOptionalMap(OpenApiConstants.Encoding, Encoding, callback);
}
else
{
writer.WriteOptionalMap(OpenApiConstants.ExtensionFieldNamePrefix + "oai-" + OpenApiConstants.Encoding, Encoding, callback);
}
}
// itemEncoding - serialize as native field in v3.2+, as extension in earlier versions
if (ItemEncoding != null)
{
if (version >= OpenApiSpecVersion.OpenApi3_2)
{
writer.WriteOptionalObject(OpenApiConstants.ItemEncoding, ItemEncoding, callback);
}
else
{
writer.WriteOptionalObject(OpenApiConstants.ExtensionFieldNamePrefix + "oai-" + OpenApiConstants.ItemEncoding, ItemEncoding, callback);
}
}
// prefixEncoding - serialize as native field in v3.2+, as extension in earlier versions
if (PrefixEncoding != null)
{
if (version >= OpenApiSpecVersion.OpenApi3_2)
{
writer.WriteOptionalCollection(OpenApiConstants.PrefixEncoding, PrefixEncoding, callback);
}
else
{
writer.WriteOptionalCollection(OpenApiConstants.ExtensionFieldNamePrefix + "oai-" + OpenApiConstants.PrefixEncoding, PrefixEncoding, callback);
}
}
// style
writer.WriteProperty(OpenApiConstants.Style, Style?.GetDisplayName());
// explode
if (_explode.HasValue)
{
writer.WriteProperty(OpenApiConstants.Explode, Explode);
}
// allowReserved
writer.WriteProperty(OpenApiConstants.AllowReserved, AllowReserved, false);
// extensions
writer.WriteExtensions(Extensions, version);
writer.WriteEndObject();
}
/// <summary>
/// Serialize <see cref="OpenApiExternalDocs"/> to Open Api v2.0.
/// </summary>
public virtual void SerializeAsV2(IOpenApiWriter writer)
{
// nothing here
}
}
}