-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathOpenApiExample.cs
More file actions
134 lines (113 loc) · 4.95 KB
/
OpenApiExample.cs
File metadata and controls
134 lines (113 loc) · 4.95 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System.Collections.Generic;
using System.Text.Json.Nodes;
using Microsoft.OpenApi.Helpers;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Writers;
namespace Microsoft.OpenApi.Models
{
/// <summary>
/// Example Object.
/// </summary>
public class OpenApiExample : IOpenApiReferenceable, IOpenApiExtensible
{
/// <summary>
/// Short description for the example.
/// </summary>
public virtual string Summary { get; set; }
/// <summary>
/// Long description for the example.
/// CommonMark syntax MAY be used for rich text representation.
/// </summary>
public virtual string Description { get; set; }
/// <summary>
/// Embedded literal example. The value field and externalValue field are mutually
/// exclusive. To represent examples of media types that cannot naturally represented
/// in JSON or YAML, use a string value to contain the example, escaping where necessary.
/// </summary>
public virtual JsonNode Value { get; set; }
/// <summary>
/// A URL that points to the literal example.
/// This provides the capability to reference examples that cannot easily be
/// included in JSON or YAML documents.
/// The value field and externalValue field are mutually exclusive.
/// </summary>
public virtual string ExternalValue { get; set; }
/// <summary>
/// This object MAY be extended with Specification Extensions.
/// </summary>
public virtual IDictionary<string, IOpenApiExtension> Extensions { get; set; } = new Dictionary<string, IOpenApiExtension>();
/// <summary>
/// Reference object.
/// </summary>
public virtual OpenApiReference Reference { get; set; }
/// <summary>
/// Indicates object is a placeholder reference to an actual object and does not contain valid data.
/// </summary>
public virtual bool UnresolvedReference { get; set; } = false;
/// <summary>
/// Parameter-less constructor
/// </summary>
public OpenApiExample() { }
/// <summary>
/// Initializes a copy of <see cref="OpenApiExample"/> object
/// </summary>
public OpenApiExample(OpenApiExample example)
{
Summary = example?.Summary ?? Summary;
Description = example?.Description ?? Description;
Value = example?.Value != null ? JsonNodeCloneHelper.Clone(example.Value) : null;
ExternalValue = example?.ExternalValue ?? ExternalValue;
Extensions = example?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(example.Extensions) : null;
Reference = example?.Reference != null ? new(example.Reference) : null;
UnresolvedReference = example?.UnresolvedReference ?? UnresolvedReference;
}
/// <summary>
/// Serialize <see cref="OpenApiExample"/> to Open Api v3.1
/// </summary>
/// <param name="writer"></param>
public virtual void SerializeAsV31(IOpenApiWriter writer)
{
SerializeInternal(writer, OpenApiSpecVersion.OpenApi3_1);
}
/// <summary>
/// Serialize <see cref="OpenApiExample"/> to Open Api v3.0
/// </summary>
/// <param name="writer"></param>
public virtual void SerializeAsV3(IOpenApiWriter writer)
{
SerializeInternal(writer, OpenApiSpecVersion.OpenApi3_0);
}
/// <summary>
/// Writes out existing examples in a mediatype object
/// </summary>
/// <param name="writer"></param>
/// <param name="version"></param>
public void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version)
{
Utils.CheckArgumentNull(writer);
writer.WriteStartObject();
// summary
writer.WriteProperty(OpenApiConstants.Summary, Summary);
// description
writer.WriteProperty(OpenApiConstants.Description, Description);
// value
writer.WriteOptionalObject(OpenApiConstants.Value, Value, (w, v) => w.WriteAny(v));
// externalValue
writer.WriteProperty(OpenApiConstants.ExternalValue, ExternalValue);
// extensions
writer.WriteExtensions(Extensions, version);
writer.WriteEndObject();
}
/// <summary>
/// Serialize <see cref="OpenApiExample"/> to Open Api v2.0
/// </summary>
public virtual void SerializeAsV2(IOpenApiWriter writer)
{
// Example object of this form does not exist in V2.
// V2 Example object requires knowledge of media type and exists only
// in Response object, so it will be serialized as a part of the Response object.
}
}
}