-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathOpenApiResponse.cs
More file actions
175 lines (140 loc) · 6.43 KB
/
OpenApiResponse.cs
File metadata and controls
175 lines (140 loc) · 6.43 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
// 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>
/// Response object.
/// </summary>
public class OpenApiResponse : IOpenApiExtensible, IOpenApiResponse
{
/// <inheritdoc/>
public string? Description { get; set; }
/// <inheritdoc/>
public IDictionary<string, IOpenApiHeader>? Headers { get; set; }
/// <inheritdoc/>
public IDictionary<string, OpenApiMediaType>? Content { get; set; }
/// <inheritdoc/>
public IDictionary<string, IOpenApiLink>? Links { get; set; }
/// <inheritdoc/>
public IDictionary<string, IOpenApiExtension>? Extensions { get; set; }
/// <summary>
/// Parameterless constructor
/// </summary>
public OpenApiResponse() { }
/// <summary>
/// Initializes a copy of <see cref="IOpenApiResponse"/> object
/// </summary>
internal OpenApiResponse(IOpenApiResponse response)
{
Utils.CheckArgumentNull(response);
Description = response.Description ?? Description;
Headers = response.Headers != null ? new Dictionary<string, IOpenApiHeader>(response.Headers) : null;
Content = response.Content != null ? new Dictionary<string, OpenApiMediaType>(response.Content) : null;
Links = response.Links != null ? new Dictionary<string, IOpenApiLink>(response.Links) : null;
Extensions = response.Extensions != null ? new Dictionary<string, IOpenApiExtension>(response.Extensions) : null;
}
/// <summary>
/// Serialize <see cref="OpenApiResponse"/> to Open Api v3.1
/// </summary>
public virtual void SerializeAsV31(IOpenApiWriter writer)
{
SerializeInternal(writer, OpenApiSpecVersion.OpenApi3_1, (writer, element) => element.SerializeAsV31(writer));
}
/// <summary>
/// Serialize <see cref="OpenApiResponse"/> to Open Api v3.0.
/// </summary>
public virtual void SerializeAsV3(IOpenApiWriter writer)
{
SerializeInternal(writer, OpenApiSpecVersion.OpenApi3_0, (writer, element) => element.SerializeAsV3(writer));
}
private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version,
Action<IOpenApiWriter, IOpenApiSerializable> callback)
{
Utils.CheckArgumentNull(writer);
writer.WriteStartObject();
// description
writer.WriteRequiredProperty(OpenApiConstants.Description, Description);
// headers
writer.WriteOptionalMap(OpenApiConstants.Headers, Headers, callback);
// content
writer.WriteOptionalMap(OpenApiConstants.Content, Content, callback);
// links
writer.WriteOptionalMap(OpenApiConstants.Links, Links, callback);
// extension
writer.WriteExtensions(Extensions, version);
writer.WriteEndObject();
}
/// <summary>
/// Serialize to OpenAPI V2 document without using reference.
/// </summary>
public virtual void SerializeAsV2(IOpenApiWriter writer)
{
Utils.CheckArgumentNull(writer);
writer.WriteStartObject();
// description
writer.WriteRequiredProperty(OpenApiConstants.Description, Description);
var extensionsClone = Extensions is not null ? new Dictionary<string, IOpenApiExtension>(Extensions) : null;
if (Content != null)
{
var mediatype = Content.FirstOrDefault();
if (mediatype.Value != null)
{
// schema
writer.WriteOptionalObject(OpenApiConstants.Schema, mediatype.Value.Schema, (w, s) => s.SerializeAsV2(w));
// examples
if (Content.Values.Any(m => m.Example != null))
{
writer.WritePropertyName(OpenApiConstants.Examples);
writer.WriteStartObject();
foreach (var mediaTypePair in Content)
{
if (mediaTypePair.Value.Example != null)
{
writer.WritePropertyName(mediaTypePair.Key);
writer.WriteAny(mediaTypePair.Value.Example);
}
}
writer.WriteEndObject();
}
if (Content.Values.Any(m => m.Examples != null && m.Examples.Any()))
{
writer.WritePropertyName(OpenApiConstants.ExamplesExtension);
writer.WriteStartObject();
foreach (var example in Content
.Select(static x => x.Value.Examples)
.OfType<Dictionary<string, IOpenApiExample>>()
.SelectMany(static x => x))
{
writer.WritePropertyName(example.Key);
example.Value.SerializeAsV2(writer);
}
writer.WriteEndObject();
}
writer.WriteExtensions(mediatype.Value.Extensions, OpenApiSpecVersion.OpenApi2_0);
if (mediatype.Value.Extensions is not null)
{
foreach (var key in mediatype.Value.Extensions.Keys)
{
// The extension will already have been serialized as part of the call above,
// so remove it from the cloned collection so we don't write it again.
extensionsClone?.Remove(key);
}
}
}
}
// headers
writer.WriteOptionalMap(OpenApiConstants.Headers, Headers, (w, h) => h.SerializeAsV2(w));
// extension
writer.WriteExtensions(extensionsClone, OpenApiSpecVersion.OpenApi2_0);
writer.WriteEndObject();
}
/// <inheritdoc/>
public IOpenApiResponse CreateShallowCopy()
{
return new OpenApiResponse(this);
}
}
}