-
Notifications
You must be signed in to change notification settings - Fork 253
/
Copy pathOpenApiExternalDocs.cs
93 lines (78 loc) · 3.13 KB
/
OpenApiExternalDocs.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
// 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>
/// ExternalDocs object.
/// </summary>
public class OpenApiExternalDocs : IOpenApiSerializable, IOpenApiExtensible
{
/// <summary>
/// A short description of the target documentation.
/// </summary>
public string? Description { get; set; }
/// <summary>
/// REQUIRED. The URL for the target documentation. Value MUST be in the format of a URL.
/// </summary>
public Uri? Url { get; set; }
private Lazy<IDictionary<string, IOpenApiExtension>>? _extensions = new(() => new Dictionary<string, IOpenApiExtension>(StringComparer.Ordinal));
/// <summary>
/// This object MAY be extended with Specification Extensions.
/// </summary>
public IDictionary<string, IOpenApiExtension>? Extensions
{
get => _extensions?.Value;
set => _extensions = value is null ? null : new(() => value);
}
/// <summary>
/// Parameter-less constructor
/// </summary>
public OpenApiExternalDocs() { }
/// <summary>
/// Initializes a copy of an <see cref="OpenApiExternalDocs"/> object
/// </summary>
public OpenApiExternalDocs(OpenApiExternalDocs externalDocs)
{
Description = externalDocs?.Description ?? Description;
Url = externalDocs?.Url != null ? new Uri(externalDocs.Url.OriginalString, UriKind.RelativeOrAbsolute) : null;
Extensions = externalDocs?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(externalDocs.Extensions) : null;
}
/// <summary>
/// Serialize <see cref="OpenApiExternalDocs"/> to Open Api v3.1.
/// </summary>
public void SerializeAsV31(IOpenApiWriter writer)
{
WriteInternal(writer, OpenApiSpecVersion.OpenApi3_1);
}
/// <summary>
/// Serialize <see cref="OpenApiExternalDocs"/> to Open Api v3.0.
/// </summary>
public void SerializeAsV3(IOpenApiWriter writer)
{
WriteInternal(writer, OpenApiSpecVersion.OpenApi3_0);
}
/// <summary>
/// Serialize <see cref="OpenApiExternalDocs"/> to Open Api v2.0.
/// </summary>
public void SerializeAsV2(IOpenApiWriter writer)
{
WriteInternal(writer, OpenApiSpecVersion.OpenApi2_0);
}
private void WriteInternal(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
{
Utils.CheckArgumentNull(writer);
writer.WriteStartObject();
// description
writer.WriteProperty(OpenApiConstants.Description, Description);
// url
writer.WriteProperty(OpenApiConstants.Url, Url?.OriginalString);
// extensions
writer.WriteExtensions(Extensions, specVersion);
writer.WriteEndObject();
}
}
}