tgstation-server  4.3.2
The /tg/station 13 server suite
OpenApiEnumVarNamesExtension.cs
Go to the documentation of this file.
1 using Microsoft.OpenApi;
2 using Microsoft.OpenApi.Interfaces;
3 using Microsoft.OpenApi.Models;
4 using Microsoft.OpenApi.Writers;
5 using System;
6 
7 namespace Tgstation.Server.Host.Core
8 {
13  {
17  readonly Type enumType;
18 
23  private OpenApiEnumVarNamesExtension(Type enumType)
24  {
25  this.enumType = enumType ?? throw new ArgumentNullException(nameof(enumType));
26  }
27 
33  public static void Apply(OpenApiSchema openApiSchema, Type enumType)
34  {
35  if (openApiSchema == null)
36  throw new ArgumentNullException(nameof(openApiSchema));
37 
38  openApiSchema.Extensions.Add("x-enum-varnames", new OpenApiEnumVarNamesExtension(enumType));
39  }
40 
42  public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
43  {
44  if (writer == null)
45  throw new ArgumentNullException(nameof(writer));
46 
47  if (specVersion != OpenApiSpecVersion.OpenApi3_0)
48  throw new InvalidOperationException("This extension only applies to OpenAPI 3.0!");
49 
50  writer.WriteStartArray();
51  foreach (var enumValue in Enum.GetValues(enumType))
52  writer.WriteValue(enumValue.ToString());
53 
54  writer.WriteEndArray();
55  }
56  }
57 }
void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
readonly Type enumType
The Type of the Enum being described.
OpenApiEnumVarNamesExtension(Type enumType)
Initializes a new instance of the OpenApiEnumVarNamesExtension .
static void Apply(OpenApiSchema openApiSchema, Type enumType)
Applies the extension to a give openApiSchema .
Implements the "x-enum-varnames" OpenAPI 3.0 extension.