-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathOpenApiRequestBodyReference.cs
More file actions
104 lines (93 loc) · 3.74 KB
/
OpenApiRequestBodyReference.cs
File metadata and controls
104 lines (93 loc) · 3.74 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models.Interfaces;
using Microsoft.OpenApi.Writers;
namespace Microsoft.OpenApi.Models.References
{
/// <summary>
/// Request Body Object Reference.
/// </summary>
public class OpenApiRequestBodyReference : BaseOpenApiReferenceHolder<OpenApiRequestBody, IOpenApiRequestBody>, IOpenApiRequestBody
{
/// <summary>
/// Constructor initializing the reference object.
/// </summary>
/// <param name="referenceId">The reference Id.</param>
/// <param name="hostDocument">The host OpenAPI document.</param>
/// <param name="externalResource">Optional: External resource in the reference.
/// It may be:
/// 1. a absolute/relative file path, for example: ../commons/pet.json
/// 2. a Url, for example: http://localhost/pet.json
/// </param>
public OpenApiRequestBodyReference(string referenceId, OpenApiDocument hostDocument = null, string externalResource = null):base(referenceId, hostDocument, ReferenceType.RequestBody, externalResource)
{
}
/// <summary>
/// Copy constructor
/// </summary>
/// <param name="openApiRequestBodyReference">The reference to copy</param>
private OpenApiRequestBodyReference(OpenApiRequestBodyReference openApiRequestBodyReference):base(openApiRequestBodyReference)
{
}
/// <inheritdoc/>
public string Description
{
get => string.IsNullOrEmpty(Reference?.Description) ? Target?.Description : Reference.Description;
set
{
if (Reference is not null)
{
Reference.Description = value;
}
}
}
/// <inheritdoc/>
public IDictionary<string, OpenApiMediaType> Content { get => Target?.Content; }
/// <inheritdoc/>
public bool Required { get => Target?.Required ?? false; }
/// <inheritdoc/>
public IDictionary<string, IOpenApiExtension> Extensions { get => Target?.Extensions; }
/// <inheritdoc/>
public override IOpenApiRequestBody CopyReferenceAsTargetElementWithOverrides(IOpenApiRequestBody source)
{
return source is OpenApiRequestBody ? new OpenApiRequestBody(this) : source;
}
/// <inheritdoc/>
public override void SerializeAsV2(IOpenApiWriter writer)
{
// doesn't exist in v2
}
/// <inheritdoc/>
public IOpenApiParameter ConvertToBodyParameter(IOpenApiWriter writer)
{
if (writer.GetSettings().ShouldInlineReference(Reference))
{
return Target.ConvertToBodyParameter(writer);
}
else
{
return new OpenApiParameterReference(Reference.Id, Reference.HostDocument);
}
}
/// <inheritdoc/>
public IEnumerable<IOpenApiParameter> ConvertToFormDataParameters(IOpenApiWriter writer)
{
if (writer.GetSettings().ShouldInlineReference(Reference))
{
return Target.ConvertToFormDataParameters(writer);
}
if (Content == null || !Content.Any())
return [];
return Content.First().Value.Schema.Properties.Select(x => new OpenApiParameterReference(x.Key, Reference.HostDocument));
}
/// <inheritdoc/>
public IOpenApiRequestBody CreateShallowCopy()
{
return new OpenApiRequestBodyReference(this);
}
}
}