Repository navigation
Expand file tree
/
Copy pathOpenApiExample.cs
More file actions
121 lines (101 loc) · 4.22 KB
/
Copy pathOpenApiExample.cs
File metadata and controls
121 lines (101 loc) · 4.22 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System.Collections.Generic;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Writers;
namespace Microsoft.OpenApi.Models
{
/// <summary>
/// Example Object.
/// </summary>
public class OpenApiExample : IOpenApiSerializable, IOpenApiReferenceable, IOpenApiExtensible
{
/// <summary>
/// Short description for the example.
/// </summary>
public string Summary { get; set; }
/// <summary>
/// Long description for the example.
/// CommonMark syntax MAY be used for rich text representation.
/// </summary>
public 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 IOpenApiAny 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 string ExternalValue { get; set; }
/// <summary>
/// This object MAY be extended with Specification Extensions.
/// </summary>
public IDictionary<string, IOpenApiExtension> Extensions { get; set; } = new Dictionary<string, IOpenApiExtension>();
/// <summary>
/// Reference object.
/// </summary>
public OpenApiReference Reference { get; set; }
/// <summary>
/// Indicates object is a placeholder reference to an actual object and does not contain valid data.
/// </summary>
public bool UnresolvedReference { get; set; } = false;
/// <summary>
/// Serialize <see cref="OpenApiExample"/> to Open Api v3.0
/// </summary>
public void SerializeAsV3(IOpenApiWriter writer)
{
if (writer == null)
{
throw Error.ArgumentNull(nameof(writer));
}
if (Reference != null)
{
Reference.SerializeAsV3(writer);
return;
}
SerializeAsV3WithoutReference(writer);
}
/// <summary>
/// Serialize to OpenAPI V3 document without using reference.
/// </summary>
public void SerializeAsV3WithoutReference(IOpenApiWriter 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, OpenApiSpecVersion.OpenApi3_0);
writer.WriteEndObject();
}
/// <summary>
/// Serialize <see cref="OpenApiExample"/> to Open Api v2.0
/// </summary>
public 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.
}
/// <summary>
/// Serialize to OpenAPI V2 document without using reference.
/// </summary>
public void SerializeAsV2WithoutReference(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.
}
}
}