Extract custom attributes to CustomSwaggerAttribute

This also allows plugins to inherit from it and add custom attributes while utilizing the same ASF mechanism for extending the schema
This commit is contained in:
Archi
2021-06-30 22:53:01 +02:00
parent d7cbf0ced2
commit 19bd497362
5 changed files with 40 additions and 12 deletions

View File

@@ -41,9 +41,9 @@ namespace ArchiSteamFarm.IPC.Integration {
return;
}
context.MemberInfo.GetCustomAttribute<SwaggerItemsMinMaxAttribute>()?.Apply(schema);
context.MemberInfo.GetCustomAttribute<SwaggerSteamIdentifierAttribute>()?.Apply(schema);
context.MemberInfo.GetCustomAttribute<SwaggerValidValuesAttribute>()?.Apply(schema);
foreach (CustomSwaggerAttribute customSwaggerAttribute in context.MemberInfo.GetCustomAttributes<CustomSwaggerAttribute>()) {
customSwaggerAttribute.Apply(schema);
}
}
}
}

View File

@@ -0,0 +1,31 @@
// _ _ _ ____ _ _____
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
// |
// Copyright 2015-2021 Ł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;
namespace ArchiSteamFarm.IPC.Integration {
[PublicAPI]
public abstract class CustomSwaggerAttribute : Attribute {
public abstract void Apply(OpenApiSchema schema);
}
}

View File

@@ -20,14 +20,13 @@
// limitations under the License.
using System;
using System.ComponentModel.DataAnnotations;
using JetBrains.Annotations;
using Microsoft.OpenApi.Models;
namespace ArchiSteamFarm.IPC.Integration {
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property)]
[PublicAPI]
public sealed class SwaggerItemsMinMaxAttribute : ValidationAttribute {
public sealed class SwaggerItemsMinMaxAttribute : CustomSwaggerAttribute {
public uint MaximumUint {
get => BackingMaximum.HasValue ? decimal.ToUInt32(BackingMaximum.Value) : default(uint);
set => BackingMaximum = value;
@@ -41,7 +40,7 @@ namespace ArchiSteamFarm.IPC.Integration {
private decimal? BackingMaximum;
private decimal? BackingMinimum;
public void Apply(OpenApiSchema schema) {
public override void Apply(OpenApiSchema schema) {
if (schema == null) {
throw new ArgumentNullException(nameof(schema));
}

View File

@@ -20,7 +20,6 @@
// limitations under the License.
using System;
using System.ComponentModel.DataAnnotations;
using JetBrains.Annotations;
using Microsoft.OpenApi.Models;
using SteamKit2;
@@ -28,13 +27,13 @@ using SteamKit2;
namespace ArchiSteamFarm.IPC.Integration {
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property)]
[PublicAPI]
public sealed class SwaggerSteamIdentifierAttribute : ValidationAttribute {
public sealed class SwaggerSteamIdentifierAttribute : CustomSwaggerAttribute {
public EAccountType AccountType { get; set; } = EAccountType.Individual;
public uint MaximumAccountID { get; set; } = uint.MaxValue;
public uint MinimumAccountID { get; set; } = 1;
public EUniverse Universe { get; set; } = EUniverse.Public;
public void Apply(OpenApiSchema schema) {
public override void Apply(OpenApiSchema schema) {
if (schema == null) {
throw new ArgumentNullException(nameof(schema));
}

View File

@@ -20,7 +20,6 @@
// limitations under the License.
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using JetBrains.Annotations;
using Microsoft.OpenApi.Any;
@@ -30,11 +29,11 @@ using Microsoft.OpenApi.Models;
namespace ArchiSteamFarm.IPC.Integration {
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property)]
[PublicAPI]
public sealed class SwaggerValidValuesAttribute : ValidationAttribute {
public sealed class SwaggerValidValuesAttribute : CustomSwaggerAttribute {
public int[]? ValidIntValues { get; set; }
public string[]? ValidStringValues { get; set; }
public void Apply(OpenApiSchema schema) {
public override void Apply(OpenApiSchema schema) {
if (schema == null) {
throw new ArgumentNullException(nameof(schema));
}