tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
OpenApiEnumVarNamesExtension.cs
Go to the documentation of this file.
1using System;
2
3using Microsoft.OpenApi;
4using Microsoft.OpenApi.Interfaces;
5using Microsoft.OpenApi.Models;
6using Microsoft.OpenApi.Writers;
7
9{
14 {
18 readonly Type enumType;
19
25 {
26 this.enumType = enumType ?? throw new ArgumentNullException(nameof(enumType));
27 }
28
34 public static void Apply(OpenApiSchema openApiSchema, Type enumType)
35 {
36 ArgumentNullException.ThrowIfNull(openApiSchema);
37
38 openApiSchema.Extensions.Add("x-enum-varnames", new OpenApiEnumVarNamesExtension(enumType));
39 }
40
42 public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
43 {
44 ArgumentNullException.ThrowIfNull(writer);
45
46 if (specVersion != OpenApiSpecVersion.OpenApi3_0)
47 throw new InvalidOperationException("This extension only applies to OpenAPI 3.0!");
48
49 writer.WriteStartArray();
50 foreach (var enumValue in Enum.GetValues(enumType))
51 {
52 var enumName = enumValue.ToString()!;
53 var field = enumType.GetField(enumName)!;
54 if (field.IsDefined(typeof(ObsoleteAttribute), false))
55 enumName = $"DEPRECATED_{enumName}";
56
57 writer.WriteValue(enumName);
58 }
59
60 writer.WriteEndArray();
61 }
62 }
63}
Implements the "x-enum-varnames" OpenAPI 3.0 extension.
OpenApiEnumVarNamesExtension(Type enumType)
Initializes a new instance of the OpenApiEnumVarNamesExtension class.
void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
static void Apply(OpenApiSchema openApiSchema, Type enumType)
Applies the extension to a give openApiSchema .
readonly Type enumType
The Type of the Enum being described.