-
Notifications
You must be signed in to change notification settings - Fork 253
/
Copy pathOpenApiExtensibleExtensions.cs
40 lines (36 loc) · 1.47 KB
/
OpenApiExtensibleExtensions.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using Microsoft.OpenApi.Exceptions;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Properties;
namespace Microsoft.OpenApi.Extensions
{
/// <summary>
/// Extension methods to verify validity and add an extension to Extensions property.
/// </summary>
public static class OpenApiExtensibleExtensions
{
/// <summary>
/// Add extension into the Extensions
/// </summary>
/// <typeparam name="T"><see cref="IOpenApiExtensible"/>.</typeparam>
/// <param name="element">The extensible Open API element. </param>
/// <param name="name">The extension name.</param>
/// <param name="any">The extension value.</param>
public static void AddExtension<T>(this T element, string name, IOpenApiExtension any)
where T : IOpenApiExtensible
{
Utils.CheckArgumentNull(element);
Utils.CheckArgumentNullOrEmpty(name);
if (!name.StartsWith(OpenApiConstants.ExtensionFieldNamePrefix, StringComparison.OrdinalIgnoreCase))
{
throw new OpenApiException(string.Format(SRResource.ExtensionFieldNameMustBeginWithXDash, name));
}
element.Extensions ??= [];
element.Extensions[name] = Utils.CheckArgumentNull(any);
}
}
}