-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathOpenApiMediaType.cs
More file actions
150 lines (129 loc) · 5.68 KB
/
OpenApiMediaType.cs
File metadata and controls
150 lines (129 loc) · 5.68 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Nodes;
using Microsoft.OpenApi.Helpers;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models.Interfaces;
using Microsoft.OpenApi.Writers;
namespace Microsoft.OpenApi.Models
{
/// <summary>
/// Media Type Object.
/// </summary>
public class OpenApiMediaType : IOpenApiSerializable, IOpenApiExtensible
{
/// <summary>
/// The schema defining the type used for the request body.
/// </summary>
public IOpenApiSchema? Schema { get; set; }
/// <summary>
/// Example of the media type.
/// The example object SHOULD be in the correct format as specified by the media type.
/// </summary>
public JsonNode? Example { get; set; }
/// <summary>
/// Examples of the media type.
/// Each example object SHOULD match the media type and specified schema if present.
/// </summary>
public IDictionary<string, IOpenApiExample>? Examples { get; set; }
/// <summary>
/// A map between a property name and its encoding information.
/// The key, being the property name, MUST exist in the schema as a property.
/// The encoding object SHALL only apply to requestBody objects
/// when the media type is multipart or application/x-www-form-urlencoded.
/// </summary>
public IDictionary<string, OpenApiEncoding>? Encoding { get; set; }
/// <summary>
/// Serialize <see cref="OpenApiExternalDocs"/> to Open Api v3.0.
/// </summary>
public IDictionary<string, IOpenApiExtension>? Extensions { get; set; }
/// <summary>
/// Parameterless constructor
/// </summary>
public OpenApiMediaType() { }
/// <summary>
/// Initializes a copy of an <see cref="OpenApiMediaType"/> object
/// </summary>
public OpenApiMediaType(OpenApiMediaType? mediaType)
{
Schema = mediaType?.Schema?.CreateShallowCopy();
Example = mediaType?.Example != null ? JsonNodeCloneHelper.Clone(mediaType.Example) : null;
Examples = mediaType?.Examples != null ? new Dictionary<string, IOpenApiExample>(mediaType.Examples) : null;
Encoding = mediaType?.Encoding != null ? new Dictionary<string, OpenApiEncoding>(mediaType.Encoding) : null;
Extensions = mediaType?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(mediaType.Extensions) : null;
}
/// <summary>
/// Serialize <see cref="OpenApiMediaType"/> to Open Api v3.1.
/// </summary>
public void SerializeAsV31(IOpenApiWriter writer)
{
SerializeInternal(writer, OpenApiSpecVersion.OpenApi3_1, (w, element) => element.SerializeAsV31(w));
}
/// <summary>
/// Serialize <see cref="OpenApiMediaType"/> to Open Api v3.0.
/// </summary>
public void SerializeAsV3(IOpenApiWriter writer)
{
SerializeInternal(writer, OpenApiSpecVersion.OpenApi3_0, (w, element) => element.SerializeAsV3(w));
}
/// <summary>
/// Serialize <see cref="OpenApiMediaType"/> to Open Api v3.0.
/// </summary>
private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version,
Action<IOpenApiWriter, IOpenApiSerializable> callback)
{
Utils.CheckArgumentNull(writer);
writer.WriteStartObject();
// schema
writer.WriteOptionalObject(OpenApiConstants.Schema, Schema, callback);
// example
writer.WriteOptionalObject(OpenApiConstants.Example, Example, (w, e) => w.WriteAny(e));
// examples
if (Examples != null && Examples.Any())
{
SerializeExamples(writer, Examples);
}
// encoding
writer.WriteOptionalMap(OpenApiConstants.Encoding, Encoding, callback);
// extensions
writer.WriteExtensions(Extensions, version);
writer.WriteEndObject();
}
/// <summary>
/// Serialize <see cref="OpenApiMediaType"/> to Open Api v2.0.
/// </summary>
public void SerializeAsV2(IOpenApiWriter writer)
{
// Media type does not exist in V2.
}
private static void SerializeExamples(IOpenApiWriter writer, IDictionary<string, IOpenApiExample> examples)
{
/* Special case for writing out empty arrays as valid response examples
* Check if there is any example with an empty array as its value and set the flag `hasEmptyArray` to true
* */
var hasEmptyArray = examples.Values.Any( static example =>
example.Value is JsonArray arr && arr.Count == 0
);
if (hasEmptyArray)
{
writer.WritePropertyName(OpenApiConstants.Examples);
writer.WriteStartObject();
foreach (var kvp in examples.Where(static kvp => kvp.Value.Value is JsonArray arr && arr.Count == 0))
{
writer.WritePropertyName(kvp.Key);
writer.WriteStartObject();
writer.WriteRequiredObject(OpenApiConstants.Value, kvp.Value.Value, (w, v) => w.WriteAny(v));
writer.WriteEndObject();
}
writer.WriteEndObject();
}
else
{
writer.WriteOptionalMap(OpenApiConstants.Examples, examples, (w, e) => e.SerializeAsV3(w));
}
}
}
}