Skip to content
Permalink
Browse files

Merge pull request #12 from enbiso/dotnet3-support

Fix openAPI
  • Loading branch information...
farajfarook committed Oct 8, 2019
2 parents f3ceef5 + 11d8ae2 commit 92e4e1c4eda92548fff496401e02b0e67c19f263
@@ -1,6 +1,6 @@
env:
global:
- PACKAGE_VERSION="3.0.0-rc1"
- PACKAGE_VERSION="3.0.0-rc2"
- PACKAGE_ICON="https://www.enbiso.com/logo.svg"
- PACKAGE_PROJECT="https://nlib.enbiso.com"
- PACKAGE_REPO="https://github.com/enbiso/Enbiso.NLib"
@@ -1,30 +1,20 @@
using System;
using Microsoft.OpenApi.Any;

namespace Enbiso.NLib.OpenApi.Attributes.Actions
{
[AttributeUsage(AttributeTargets.Class)]
public class ExtActionAttribute : ExtAttribute
{
public ExtActionAttribute(string type) : base("x-action",
new Action {Link = type, Type = type, Display = type})
{
}

public ExtActionAttribute(string type, string display) : base("x-action",
new Action {Link = type, Type = type, Display = display})
{
}


public ExtActionAttribute(string type, string display, string link) : base("x-action",
new Action {Link = link, Type = type, Display = display})
{
}

public class Action
new OpenApiObject
{
["type"] = new OpenApiString(type),
["display"] = new OpenApiString(display),
["link"] = new OpenApiString(link),
})
{
public string Type { get; set; }
public string Display { get; set; }
public string Link { get; set; }
}
}
}
@@ -1,4 +1,5 @@
using System;
using Microsoft.OpenApi.Any;

namespace Enbiso.NLib.OpenApi.Attributes
{
@@ -7,9 +8,9 @@ namespace Enbiso.NLib.OpenApi.Attributes
public class ExtAttribute : Attribute
{
public readonly string Name;
public readonly object Value;
public readonly IOpenApiAny Value;

public ExtAttribute(string name, object value)
public ExtAttribute(string name, IOpenApiAny value)
{
Name = name.StartsWith("x-") ? name : "x-" + name;
Value = value;
@@ -1,11 +1,12 @@
using System;
using Microsoft.OpenApi.Any;

namespace Enbiso.NLib.OpenApi.Attributes
{
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class ExtHiddenAttribute : ExtAttribute
{
public ExtHiddenAttribute() : base("x-hidden", true)
public ExtHiddenAttribute() : base("x-hidden", new OpenApiBoolean(true))
{
}
}
@@ -1,11 +1,12 @@
using System;
using Microsoft.OpenApi.Any;

namespace Enbiso.NLib.OpenApi.Attributes
{
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class ExtKeyAttribute : ExtAttribute
{
public ExtKeyAttribute() : base("x-key", true)
public ExtKeyAttribute() : base("x-key", new OpenApiBoolean(true))
{
}
}
@@ -1,11 +1,12 @@
using System;
using Microsoft.OpenApi.Any;

namespace Enbiso.NLib.OpenApi.Attributes.Inputs
{
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class ExtInputAttribute : ExtAttribute
{
public ExtInputAttribute(string type) : base("x-input", type)
public ExtInputAttribute(string type) : base("x-input", new OpenApiString(type))
{
}
}
@@ -26,7 +26,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="3.0.*" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.*" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc4" />
</ItemGroup>

</Project>
@@ -1,18 +1,19 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Enbiso.NLib.OpenApi.Attributes;
using Enbiso.NLib.OpenApi.Attributes.Actions;
using Swashbuckle.AspNetCore.Swagger;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace Enbiso.NLib.OpenApi
{
public class SchemaExtensionFilter : ISchemaFilter
{
public void Apply(Schema schema, SchemaFilterContext context)
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
var type = context.SystemType.GetTypeInfo();
var type = context.ApiModel.Type;
var attributes = type.GetCustomAttributes(false).OfType<ExtAttribute>();
foreach (var attribute in attributes)
schema.Extensions.Add(attribute);
@@ -29,20 +30,19 @@ public void Apply(Schema schema, SchemaFilterContext context)
}
}
}

internal static class DictionaryExtensions
{
internal static void Add(this IDictionary<string, object> dictionary, ExtAttribute attribute)
internal static void Add(this IDictionary<string, IOpenApiExtension> dictionary, ExtAttribute attribute)
{
var value = attribute.Value;
if (attribute is ExtActionAttribute)
{
if (dictionary.ContainsKey(attribute.Name))
{
(dictionary[attribute.Name] as List<object>)?.Add(attribute.Value);
(dictionary[attribute.Name] as OpenApiArray)?.Add(attribute.Value);
return;
}
value = new List<object> {attribute.Value};
value = new OpenApiArray { attribute.Value };
}
dictionary.Add(attribute.Name, value);
}

This file was deleted.

@@ -5,7 +5,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Swashbuckle.AspNetCore.Swagger;
using Microsoft.OpenApi.Models;

namespace Enbiso.NLib.OpenApi
{
@@ -27,36 +27,39 @@ public static void AddOpenApi(this IServiceCollection services, IConfiguration c
/// <param name="services"></param>
/// <param name="optBuilder"></param>
public static void AddOpenApi(this IServiceCollection services, Action<OpenApiOptions> optBuilder)
{
{
var opts = new OpenApiOptions();
optBuilder?.Invoke(opts);
services.Configure(optBuilder);
services.AddSwaggerGen(options =>
services.Configure(optBuilder);
services.AddSwaggerGen(c =>
{
options.SchemaFilter<SchemaExtensionFilter>();
options.DocumentFilter<SecurityDocumentFilter>();
options.DescribeAllEnumsAsStrings();
options.SwaggerDoc("swagger", new Info
c.SchemaFilter<SchemaExtensionFilter>();
c.SwaggerDoc("swagger", new OpenApiInfo
{
Title = opts.Id?.ToUpper(),
Version = opts.Version,
Description = opts.Description
});

if (string.IsNullOrEmpty(opts.Authority)) return;

var scopes = opts.ExtraScopes?.ToDictionary(s => s, s => s.ToUpper()) ??
new Dictionary<string, string>();

if(opts.Id != null) scopes.Add(opts.Id, opts.Id.ToUpper());

options.AddSecurityDefinition("oauth2", new OAuth2Scheme {
Type = "oauth2",
Flow = "implicit",
AuthorizationUrl = $"{opts.Authority}/connect/authorize",
TokenUrl = $"{opts.Authority}/connect/token",
Scopes = scopes
});
new Dictionary<string, string>();

if (opts.Id != null) scopes.Add(opts.Id, opts.Description);

c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
{
Type = SecuritySchemeType.OAuth2,
Flows = new OpenApiOAuthFlows
{
Implicit = new OpenApiOAuthFlow
{
AuthorizationUrl = new Uri($"{opts.Authority}/connect/authorize"),
TokenUrl = new Uri($"{opts.Authority}/connect/token"),
Scopes = scopes
}
},
});
});
}

@@ -66,9 +69,18 @@ public static void UseOpenApi(this IApplicationBuilder app)
app.UseSwagger(c =>
{
c.RouteTemplate = "/{documentName}.json";
c.PreSerializeFilters.Add((swaggerDoc, httpReq) => { swaggerDoc.BasePath = settings.BasePath; });
c.PreSerializeFilters.Add(((document, request) =>
{
document.Servers = new List<OpenApiServer>
{
new OpenApiServer
{
Url = settings.BasePath
}
};
}));
});
app.UseSwaggerUI(c => { c.SwaggerEndpoint($"{settings.BasePath}swagger.json", settings.Id?.ToUpper()); });
app.UseSwaggerUI(c => { c.SwaggerEndpoint($"{settings.BasePath}swagger.json", settings.Id.ToUpper());});
}
}

0 comments on commit 92e4e1c

Please sign in to comment.
You can’t perform that action at this time.