-
Notifications
You must be signed in to change notification settings - Fork 242
/
Copy pathOpenApiXml.cs
117 lines (96 loc) · 3.61 KB
/
OpenApiXml.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
// 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>
/// XML Object.
/// </summary>
public class OpenApiXml : IOpenApiSerializable, IOpenApiExtensible
{
/// <summary>
/// Replaces the name of the element/attribute used for the described schema property.
/// </summary>
public string Name { get; set; }
/// <summary>
/// The URI of the namespace definition. Value MUST be in the form of an absolute URI.
/// </summary>
public Uri Namespace { get; set; }
/// <summary>
/// The prefix to be used for the name
/// </summary>
public string Prefix { get; set; }
/// <summary>
/// Declares whether the property definition translates to an attribute instead of an element.
/// Default value is false.
/// </summary>
public bool Attribute { get; set; }
/// <summary>
/// Signifies whether the array is wrapped.
/// Default value is false.
/// </summary>
public bool Wrapped { get; set; }
/// <summary>
/// Specification Extensions.
/// </summary>
public IDictionary<string, IOpenApiExtension> Extensions { get; set; } = new Dictionary<string, IOpenApiExtension>();
/// <summary>
/// Parameterless constructor
/// </summary>
public OpenApiXml() { }
/// <summary>
/// Initializes a copy of an <see cref="OpenApiXml"/> object
/// </summary>
public OpenApiXml(OpenApiXml xml)
{
Name = xml?.Name ?? Name;
Namespace = xml?.Namespace ?? Namespace;
Prefix = xml?.Prefix ?? Prefix;
Attribute = xml?.Attribute ?? Attribute;
Wrapped = xml?.Wrapped ?? Wrapped;
Extensions = xml?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(xml.Extensions) : null;
}
/// <summary>
/// Serialize <see cref="OpenApiXml"/> to Open Api v3.0
/// </summary>
public void SerializeAsV31(IOpenApiWriter writer)
{
Write(writer, OpenApiSpecVersion.OpenApi3_1);
}
/// <summary>
/// Serialize <see cref="OpenApiXml"/> to Open Api v3.0
/// </summary>
public void SerializeAsV3(IOpenApiWriter writer)
{
Write(writer, OpenApiSpecVersion.OpenApi3_0);
}
/// <summary>
/// Serialize <see cref="OpenApiXml"/> to Open Api v2.0
/// </summary>
public void SerializeAsV2(IOpenApiWriter writer)
{
Write(writer, OpenApiSpecVersion.OpenApi2_0);
}
private void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
{
Utils.CheckArgumentNull(writer);;
writer.WriteStartObject();
// name
writer.WriteProperty(OpenApiConstants.Name, Name);
// namespace
writer.WriteProperty(OpenApiConstants.Namespace, Namespace?.AbsoluteUri);
// prefix
writer.WriteProperty(OpenApiConstants.Prefix, Prefix);
// attribute
writer.WriteProperty(OpenApiConstants.Attribute, Attribute, false);
// wrapped
writer.WriteProperty(OpenApiConstants.Wrapped, Wrapped, false);
// extensions
writer.WriteExtensions(Extensions, specVersion);
writer.WriteEndObject();
}
}
}