forked from microsoft/OpenAPI.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenApiMediaType.cs
More file actions
118 lines (99 loc) · 4.33 KB
/
OpenApiMediaType.cs
File metadata and controls
118 lines (99 loc) · 4.33 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
// 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;
namespace Microsoft.OpenApi
{
/// <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 virtual 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 virtual 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())
{
writer.WriteOptionalMap(OpenApiConstants.Examples, Examples, callback);
}
// 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 virtual void SerializeAsV2(IOpenApiWriter writer)
{
// Media type does not exist in V2.
}
}
}