-
Notifications
You must be signed in to change notification settings - Fork 240
/
OpenApiInfo.cs
161 lines (128 loc) · 5.48 KB
/
OpenApiInfo.cs
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Writers;
namespace Microsoft.OpenApi.Models
{
/// <summary>
/// Open API Info Object, it provides the metadata about the Open API.
/// </summary>
public class OpenApiInfo : IOpenApiSerializable, IOpenApiExtensible
{
/// <summary>
/// REQUIRED. The title of the application.
/// </summary>
public string Title { get; set; }
/// <summary>
/// A short summary of the API.
/// </summary>
public string Summary { get; set; }
/// <summary>
/// A short description of the application.
/// </summary>
public string Description { get; set; }
/// <summary>
/// REQUIRED. The version of the OpenAPI document.
/// </summary>
public string Version { get; set; }
/// <summary>
/// A URL to the Terms of Service for the API. MUST be in the format of a URL.
/// </summary>
public Uri TermsOfService { get; set; }
/// <summary>
/// The contact information for the exposed API.
/// </summary>
public OpenApiContact Contact { get; set; }
/// <summary>
/// The license information for the exposed API.
/// </summary>
public OpenApiLicense License { get; set; }
/// <summary>
/// This object MAY be extended with Specification Extensions.
/// </summary>
public IDictionary<string, IOpenApiExtension> Extensions { get; set; } = new Dictionary<string, IOpenApiExtension>();
/// <summary>
/// Parameter-less constructor
/// </summary>
public OpenApiInfo() { }
/// <summary>
/// Initializes a copy of an <see cref="OpenApiInfo"/> object
/// </summary>
public OpenApiInfo(OpenApiInfo info)
{
Title = info?.Title ?? Title;
Summary = info?.Summary ?? Summary;
Description = info?.Description ?? Description;
Version = info?.Version ?? Version;
TermsOfService = info?.TermsOfService ?? TermsOfService;
Contact = info?.Contact != null ? new(info?.Contact) : null;
License = info?.License != null ? new(info?.License) : null;
Extensions = info?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(info.Extensions) : null;
}
/// <summary>
/// Serialize <see cref="OpenApiInfo"/> to Open Api v3.1
/// </summary>
public void SerializeAsV31(IOpenApiWriter writer)
{
SerializeInternal(writer, OpenApiSpecVersion.OpenApi3_1, (writer, element) => element.SerializeAsV31(writer));
// summary - present in 3.1
writer.WriteProperty(OpenApiConstants.Summary, Summary);
writer.WriteEndObject();
}
/// <summary>
/// Serialize <see cref="OpenApiInfo"/> to Open Api v3.0
/// </summary>
public void SerializeAsV3(IOpenApiWriter writer)
{
SerializeInternal(writer, OpenApiSpecVersion.OpenApi3_0, (writer, element) => element.SerializeAsV3(writer));
writer.WriteEndObject();
}
/// <summary>
/// Serialize <see cref="OpenApiInfo"/> to Open Api v3.0
/// </summary>
private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version, Action<IOpenApiWriter, IOpenApiSerializable> callback)
{
Utils.CheckArgumentNull(writer);;
writer.WriteStartObject();
// title
writer.WriteProperty(OpenApiConstants.Title, Title);
// description
writer.WriteProperty(OpenApiConstants.Description, Description);
// termsOfService
writer.WriteProperty(OpenApiConstants.TermsOfService, TermsOfService?.OriginalString);
// contact object
writer.WriteOptionalObject(OpenApiConstants.Contact, Contact, callback);
// license object
writer.WriteOptionalObject(OpenApiConstants.License, License, callback);
// version
writer.WriteProperty(OpenApiConstants.Version, Version);
// specification extensions
writer.WriteExtensions(Extensions, version);
}
/// <summary>
/// Serialize <see cref="OpenApiInfo"/> to Open Api v2.0
/// </summary>
public void SerializeAsV2(IOpenApiWriter writer)
{
Utils.CheckArgumentNull(writer);;
writer.WriteStartObject();
// title
writer.WriteProperty(OpenApiConstants.Title, Title);
// description
writer.WriteProperty(OpenApiConstants.Description, Description);
// termsOfService
writer.WriteProperty(OpenApiConstants.TermsOfService, TermsOfService?.OriginalString);
// contact object
writer.WriteOptionalObject(OpenApiConstants.Contact, Contact, (w, c) => c.SerializeAsV2(w));
// license object
writer.WriteOptionalObject(OpenApiConstants.License, License, (w, l) => l.SerializeAsV2(w));
// version
writer.WriteProperty(OpenApiConstants.Version, Version);
// specification extensions
writer.WriteExtensions(Extensions, OpenApiSpecVersion.OpenApi2_0);
writer.WriteEndObject();
}
}
}