From f550d65897feef04c7ec37f826614ad2fad8c121 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Thu, 25 Jun 2020 15:40:04 +0200 Subject: [PATCH] Append flags format to flags enum types in swagger --- .../IPC/Integration/EnumFlagsSchemaFilter.cs | 42 +++++++++++++++++++ ArchiSteamFarm/IPC/Startup.cs | 1 + 2 files changed, 43 insertions(+) create mode 100644 ArchiSteamFarm/IPC/Integration/EnumFlagsSchemaFilter.cs diff --git a/ArchiSteamFarm/IPC/Integration/EnumFlagsSchemaFilter.cs b/ArchiSteamFarm/IPC/Integration/EnumFlagsSchemaFilter.cs new file mode 100644 index 000000000..60d86ba97 --- /dev/null +++ b/ArchiSteamFarm/IPC/Integration/EnumFlagsSchemaFilter.cs @@ -0,0 +1,42 @@ +// _ _ _ ____ _ _____ +// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___ +// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \ +// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | | +// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_| +// | +// Copyright 2015-2020 Ɓukasz "JustArchi" Domeradzki +// Contact: JustArchi@JustArchi.net +// | +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// | +// http://www.apache.org/licenses/LICENSE-2.0 +// | +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using JetBrains.Annotations; +using Microsoft.OpenApi.Models; +using Swashbuckle.AspNetCore.SwaggerGen; + +namespace ArchiSteamFarm.IPC.Integration { + [UsedImplicitly] + internal sealed class EnumFlagsSchemaFilter : ISchemaFilter { + public void Apply([NotNull] OpenApiSchema schema, [NotNull] SchemaFilterContext context) { + if ((schema == null) || (context == null)) { + throw new ArgumentNullException(nameof(schema) + " || " + nameof(context)); + } + + if (!context.Type.IsEnum || !context.Type.IsDefined(typeof(FlagsAttribute), false)) { + return; + } + + schema.Format = "flags"; + } + } +} diff --git a/ArchiSteamFarm/IPC/Startup.cs b/ArchiSteamFarm/IPC/Startup.cs index 2b9c98102..71b4ca89b 100644 --- a/ArchiSteamFarm/IPC/Startup.cs +++ b/ArchiSteamFarm/IPC/Startup.cs @@ -168,6 +168,7 @@ namespace ArchiSteamFarm.IPC { ); options.EnableAnnotations(true); + options.SchemaFilter(); options.SwaggerDoc( SharedInfo.ASF, new OpenApiInfo {