mirror of https://github.com/Squidex/squidex.git
34 changed files with 140 additions and 414 deletions
@ -1,44 +0,0 @@ |
|||||
// ==========================================================================
|
|
||||
// Swagger.cs
|
|
||||
// PinkParrot Headless CMS
|
|
||||
// ==========================================================================
|
|
||||
// Copyright (c) PinkParrot Group
|
|
||||
// All rights reserved.
|
|
||||
// ==========================================================================
|
|
||||
|
|
||||
using System.IO; |
|
||||
using Microsoft.AspNetCore.Builder; |
|
||||
using Microsoft.Extensions.DependencyInjection; |
|
||||
using Microsoft.Extensions.PlatformAbstractions; |
|
||||
using PinkParrot.Pipeline.Swagger; |
|
||||
using Swashbuckle.Swagger.Model; |
|
||||
|
|
||||
namespace PinkParrot.Configurations |
|
||||
{ |
|
||||
public static class Swagger |
|
||||
{ |
|
||||
public static void AddAppSwagger(this IServiceCollection services) |
|
||||
{ |
|
||||
services.AddSwaggerGen(options => |
|
||||
{ |
|
||||
options.SingleApiVersion(new Info { Title = "Pink Parrot", Version = "v1" }); |
|
||||
options.OperationFilter<HidePropertyFilter>(); |
|
||||
options.OperationFilter<CamelCaseParameterFilter>(); |
|
||||
options.SchemaFilter<HidePropertyFilter>(); |
|
||||
options.SchemaFilter<RemoveReadonlyFilter>(); |
|
||||
options.IncludeXmlComments(GetXmlCommentsPath(PlatformServices.Default.Application)); |
|
||||
}); |
|
||||
} |
|
||||
|
|
||||
public static void UseAppSwagger(this IApplicationBuilder app) |
|
||||
{ |
|
||||
app.UseSwagger(); |
|
||||
app.UseSwaggerUi(); |
|
||||
} |
|
||||
|
|
||||
private static string GetXmlCommentsPath(ApplicationEnvironment appEnvironment) |
|
||||
{ |
|
||||
return Path.Combine(appEnvironment.ApplicationBasePath, "PinkParrot.xml"); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,29 +0,0 @@ |
|||||
// ==========================================================================
|
|
||||
// CamelCaseParameterFilter.cs
|
|
||||
// PinkParrot Headless CMS
|
|
||||
// ==========================================================================
|
|
||||
// Copyright (c) PinkParrot Group
|
|
||||
// All rights reserved.
|
|
||||
// ==========================================================================
|
|
||||
|
|
||||
using Swashbuckle.Swagger.Model; |
|
||||
using Swashbuckle.SwaggerGen.Generator; |
|
||||
|
|
||||
namespace PinkParrot.Pipeline.Swagger |
|
||||
{ |
|
||||
public sealed class CamelCaseParameterFilter : IOperationFilter |
|
||||
{ |
|
||||
public void Apply(Operation operation, OperationFilterContext context) |
|
||||
{ |
|
||||
if (operation.Parameters == null) |
|
||||
{ |
|
||||
return; |
|
||||
} |
|
||||
|
|
||||
foreach (var parameter in operation.Parameters) |
|
||||
{ |
|
||||
parameter.Name = char.ToLowerInvariant(parameter.Name[0]) + parameter.Name.Substring(1); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,65 +0,0 @@ |
|||||
// ==========================================================================
|
|
||||
// HidePropertyFilter.cs
|
|
||||
// PinkParrot Headless CMS
|
|
||||
// ==========================================================================
|
|
||||
// Copyright (c) PinkParrot Group
|
|
||||
// All rights reserved.
|
|
||||
// ==========================================================================
|
|
||||
|
|
||||
using System.Linq; |
|
||||
using System.Reflection; |
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata; |
|
||||
using PinkParrot.Infrastructure; |
|
||||
using Swashbuckle.Swagger.Model; |
|
||||
using Swashbuckle.SwaggerGen.Generator; |
|
||||
|
|
||||
namespace PinkParrot.Pipeline.Swagger |
|
||||
{ |
|
||||
public class HidePropertyFilter : ISchemaFilter, IOperationFilter |
|
||||
{ |
|
||||
public void Apply(Schema model, SchemaFilterContext context) |
|
||||
{ |
|
||||
foreach (var property in context.JsonContract.UnderlyingType.GetProperties()) |
|
||||
{ |
|
||||
var attribute = property.GetCustomAttribute<HideAttribute>(); |
|
||||
|
|
||||
if (attribute != null) |
|
||||
{ |
|
||||
model.Properties.Remove(property.Name); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
public void Apply(Operation operation, OperationFilterContext context) |
|
||||
{ |
|
||||
if (context?.ApiDescription.ParameterDescriptions == null) |
|
||||
{ |
|
||||
return; |
|
||||
} |
|
||||
|
|
||||
if (operation.Parameters == null) |
|
||||
{ |
|
||||
return; |
|
||||
} |
|
||||
|
|
||||
foreach (var parameterDescription in context.ApiDescription.ParameterDescriptions) |
|
||||
{ |
|
||||
var metadata = parameterDescription.ModelMetadata as DefaultModelMetadata; |
|
||||
|
|
||||
var hasAttribute = metadata?.Attributes?.Attributes.OfType<HideAttribute>().Any(); |
|
||||
|
|
||||
if (hasAttribute != true) |
|
||||
{ |
|
||||
continue; |
|
||||
} |
|
||||
|
|
||||
var parameter = operation.Parameters.FirstOrDefault(p => p.Name == parameterDescription.Name); |
|
||||
|
|
||||
if (parameter != null) |
|
||||
{ |
|
||||
operation.Parameters.Remove(parameter); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,36 +0,0 @@ |
|||||
// ==========================================================================
|
|
||||
// RemoveReadonlyFilter.cs
|
|
||||
// PinkParrot Headless CMS
|
|
||||
// ==========================================================================
|
|
||||
// Copyright (c) PinkParrot Group
|
|
||||
// All rights reserved.
|
|
||||
// ==========================================================================
|
|
||||
|
|
||||
using Swashbuckle.Swagger.Model; |
|
||||
using Swashbuckle.SwaggerGen.Generator; |
|
||||
|
|
||||
namespace PinkParrot.Pipeline.Swagger |
|
||||
{ |
|
||||
public class RemoveReadonlyFilter : ISchemaFilter |
|
||||
{ |
|
||||
public void Apply(Schema model, SchemaFilterContext context) |
|
||||
{ |
|
||||
Apply(model); |
|
||||
} |
|
||||
|
|
||||
private static void Apply(Schema model) |
|
||||
{ |
|
||||
model.ReadOnly = null; |
|
||||
|
|
||||
if (model.Properties == null) |
|
||||
{ |
|
||||
return; |
|
||||
} |
|
||||
|
|
||||
foreach (var property in model.Properties) |
|
||||
{ |
|
||||
Apply(property.Value); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,24 +1,17 @@ |
|||||
// ==========================================================================
|
// ==========================================================================
|
||||
// FieldDto.cs
|
// IValidatable.cs
|
||||
// PinkParrot Headless CMS
|
// PinkParrot Headless CMS
|
||||
// ==========================================================================
|
// ==========================================================================
|
||||
// Copyright (c) PinkParrot Group
|
// Copyright (c) PinkParrot Group
|
||||
// All rights reserved.
|
// All rights reserved.
|
||||
// ==========================================================================
|
// ==========================================================================
|
||||
|
|
||||
namespace PinkParrot.Core.Schema.Json |
using System.Collections.Generic; |
||||
{ |
|
||||
public class FieldDto |
|
||||
{ |
|
||||
public long Id { get; } |
|
||||
|
|
||||
public ModelFieldProperties Properties { get; } |
|
||||
|
|
||||
public FieldDto(long id, ModelFieldProperties properties) |
namespace PinkParrot.Infrastructure |
||||
|
{ |
||||
|
public interface IValidatable |
||||
{ |
{ |
||||
Id = id; |
void Validate(IList<ValidationError> errors); |
||||
|
|
||||
Properties = properties; |
|
||||
} |
|
||||
} |
} |
||||
} |
} |
||||
@ -0,0 +1,29 @@ |
|||||
|
// ==========================================================================
|
||||
|
// ValidationExtensions.cs
|
||||
|
// PinkParrot Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) PinkParrot Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
|
||||
|
namespace PinkParrot.Infrastructure |
||||
|
{ |
||||
|
public static class ValidationExtensions |
||||
|
{ |
||||
|
public static void Validate(this IValidatable target, Func<string> message) |
||||
|
{ |
||||
|
var errors = new List<ValidationError>(); |
||||
|
|
||||
|
target.Validate(errors); |
||||
|
|
||||
|
if (errors.Any()) |
||||
|
{ |
||||
|
throw new ValidationException(message(), errors); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue