-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathOpenApiPagingExtension.cs
More file actions
95 lines (82 loc) · 3.49 KB
/
OpenApiPagingExtension.cs
File metadata and controls
95 lines (82 loc) · 3.49 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
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------
using System;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Writers;
using System.Text.Json.Nodes;
namespace Microsoft.OpenApi.MicrosoftExtensions;
/// <summary>
/// Extension element for OpenAPI to add pageable information.
/// Based of the AutoRest specification https://github.com/Azure/autorest/blob/main/docs/extensions/readme.md#x-ms-pageable
/// </summary>
public class OpenApiPagingExtension : IOpenApiExtension
{
/// <summary>
/// Name of the extension as used in the description.
/// </summary>
public static string Name => "x-ms-pageable";
/// <summary>
/// The name of the property that provides the collection of pageable items.
/// </summary>
public string ItemName
{
get; set;
} = "value";
/// <summary>
/// The name of the property that provides the next link (common: nextLink)
/// </summary>
public string NextLinkName
{
get; set;
} = "nextLink";
/// <summary>
/// The name (operationId) of the operation for retrieving the next page.
/// </summary>
public string OperationName
{
get; set;
} = string.Empty;
/// <inheritdoc />
public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
{
if (writer is null) throw new ArgumentNullException(nameof(writer));
writer.WriteStartObject();
if (!string.IsNullOrEmpty(NextLinkName))
{
writer.WriteProperty(nameof(NextLinkName).ToFirstCharacterLowerCase(), NextLinkName);
}
if (!string.IsNullOrEmpty(OperationName))
{
writer.WriteProperty(nameof(OperationName).ToFirstCharacterLowerCase(), OperationName);
}
writer.WriteProperty(nameof(ItemName).ToFirstCharacterLowerCase(), ItemName);
writer.WriteEndObject();
}
/// <summary>
/// Parse the extension from the raw IOpenApiAny object.
/// </summary>
/// <param name="source">The source element to parse.</param>
/// <returns>The <see cref="OpenApiPagingExtension"/>.</returns>
/// <exception cref="ArgumentOutOfRangeException">When the source element is not an object</exception>
public static OpenApiPagingExtension Parse(JsonNode source)
{
if (source is not JsonObject rawObject) return null;
var extension = new OpenApiPagingExtension();
if (rawObject.TryGetPropertyValue(nameof(NextLinkName).ToFirstCharacterLowerCase(), out var nextLinkName) && nextLinkName is JsonValue nextLinkNameValue && nextLinkNameValue.TryGetValue<string>(out var nextLinkNameStr))
{
extension.NextLinkName = nextLinkNameStr;
}
if (rawObject.TryGetPropertyValue(nameof(OperationName).ToFirstCharacterLowerCase(), out var opName) && opName is JsonValue opNameValue && opNameValue.TryGetValue<string>(out var opNameStr))
{
extension.OperationName = opNameStr;
}
if (rawObject.TryGetPropertyValue(nameof(ItemName).ToFirstCharacterLowerCase(), out var itemName) && itemName is JsonValue itemNameValue && itemNameValue.TryGetValue<string>(out var itemNameStr))
{
extension.ItemName = itemNameStr;
}
return extension;
}
}