This repository was archived by the owner on Nov 11, 2025. It is now read-only.
forked from microsoft/OpenAPI.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenApiEncoding.cs
More file actions
148 lines (126 loc) · 5.53 KB
/
OpenApiEncoding.cs
File metadata and controls
148 lines (126 loc) · 5.53 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
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>
/// 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;
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);
// 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
}
}
}